-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolorpicker.mm
executable file
·101 lines (95 loc) · 3.46 KB
/
colorpicker.mm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2022-present Contributors to the colorpicker project.
// SPDX-License-Identifier: BSD-3-Clause
// https://github.com/mikaelsundell/colorpicker
#include "colorpicker.h"
#include "mac.h"
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
#include <QMutex>
void
Colorpicker::registerEvents()
{
static QMutex mutex;
static QPoint lastpos;
[[NSNotificationCenter defaultCenter] addObserverForName:NSApplicationDidBecomeActiveNotification
object:nil
queue:nil
usingBlock:^(NSNotification *notification) {
NSPoint point = [NSEvent mouseLocation];
QPoint cursor = mac::fromNativeCursor(point.x, point.y);
if (active()) {
if (cursor != lastpos && mutex.tryLock()) {
mac::IccProfile iccProfile = mac::grabIccProfile(cursor.x(), cursor.y());
pickEvent(
Colorpicker::PickEvent() = {
iccProfile.screenNumber,
iccProfile.displayProfileUrl,
cursor
}
);
lastpos = cursor;
mutex.unlock();
}
} else {
moveEvent(
Colorpicker::MoveEvent() = {
cursor
}
);
}
}];
[NSEvent addLocalMonitorForEventsMatchingMask:
(NSEventMaskMouseMoved | NSEventMaskLeftMouseDragged | NSEventMaskRightMouseDragged | NSEventMaskOtherMouseDragged)
handler:^(NSEvent * event) {
NSPoint point = [NSEvent mouseLocation];
QPoint cursor = mac::fromNativeCursor(point.x, point.y);
if (active()) {
if (cursor != lastpos && mutex.tryLock()) {
mac::IccProfile iccProfile = mac::grabIccProfile(cursor.x(), cursor.y());
pickEvent(
Colorpicker::PickEvent() = {
iccProfile.screenNumber,
iccProfile.displayProfileUrl,
cursor
}
);
lastpos = cursor;
mutex.unlock();
}
} else {
moveEvent(
Colorpicker::MoveEvent() = {
cursor
}
);
}
return event;
}];
// copies of events the system posts to other applications.
[NSEvent addGlobalMonitorForEventsMatchingMask:
(NSEventMaskMouseMoved | NSEventMaskLeftMouseDragged | NSEventMaskRightMouseDragged | NSEventMaskOtherMouseDragged)
handler:^(NSEvent * event) {
NSPoint point = [NSEvent mouseLocation];
QPoint cursor = mac::fromNativeCursor(point.x, point.y);
if (active()) {
if (cursor != lastpos && mutex.tryLock()) {
mac::IccProfile iccProfile = mac::grabIccProfile(cursor.x(), cursor.y());
pickEvent(
Colorpicker::PickEvent() = {
iccProfile.screenNumber,
iccProfile.displayProfileUrl,
cursor
}
);
lastpos = cursor;
mutex.unlock();
}
} else {
moveEvent(
Colorpicker::MoveEvent() = {
cursor
}
);
}
}];
}