-
Notifications
You must be signed in to change notification settings - Fork 0
/
NSCursor_SKExtensions.m
294 lines (252 loc) · 11.1 KB
/
NSCursor_SKExtensions.m
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//
// NSCursor_SKExtensions.m
// Skim
//
// Created by Christiaan Hofman on 2/16/07.
/*
This software is Copyright (c) 2007
Christiaan Hofman. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
- Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
- Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
- Neither the name of Christiaan Hofman nor the names of any
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#import "NSCursor_SKExtensions.h"
#import "NSImage_SKExtensions.h"
#import "SKRuntime.h"
#import "SKAnimatedBorderlessWindow.h"
#import "NSGeometry_SKExtensions.h"
static inline void hideLaserPointer(void);
@interface SKLaserPointerCursor : NSCursor
@end
#pragma mark -
@implementation NSCursor (SKExtensions)
static void (*original_set)(id, SEL) = NULL;
static void (*original_hide)(id, SEL) = NULL;
- (void)replacement_set {
original_set(self, _cmd);
hideLaserPointer();
}
+ (void)replacement_hide {
original_hide(self, _cmd);
hideLaserPointer();
}
+ (void)load {
original_set = (void(*)(id, SEL))SKReplaceInstanceMethodImplementationFromSelector(self, @selector(set), @selector(replacement_set));
original_hide = (void(*)(id, SEL))SKReplaceClassMethodImplementationFromSelector(self, @selector(hide), @selector(replacement_hide));
}
+ (NSCursor *)zoomInCursor {
static NSCursor *zoomInCursor = nil;
if (nil == zoomInCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameZoomInCursor] copy];
zoomInCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:NSMakePoint(7.0, 6.0)];
}
return zoomInCursor;
}
+ (NSCursor *)zoomOutCursor {
static NSCursor *zoomOutCursor = nil;
if (nil == zoomOutCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameZoomOutCursor] copy];
zoomOutCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:NSMakePoint(7.0, 6.0)];
}
return zoomOutCursor;
}
+ (NSCursor *)resizeDiagonal45Cursor {
static NSCursor *resizeDiagonal45Cursor = nil;
if (nil == resizeDiagonal45Cursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameResizeDiagonal45Cursor] copy];
resizeDiagonal45Cursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:NSMakePoint(8.0, 8.0)];
}
return resizeDiagonal45Cursor;
}
+ (NSCursor *)resizeDiagonal135Cursor {
static NSCursor *resizeDiagonal135Cursor = nil;
if (nil == resizeDiagonal135Cursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameResizeDiagonal135Cursor] copy];
resizeDiagonal135Cursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:NSMakePoint(8.0, 8.0)];
}
return resizeDiagonal135Cursor;
}
+ (NSCursor *)cameraCursor {
static NSCursor *cameraCursor = nil;
if (nil == cameraCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameCameraCursor] copy];
cameraCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:NSMakePoint(9.0, 8.0)];
}
return cameraCursor;
}
+ (NSCursor *)openHandBarCursor {
static NSCursor *openHandBarCursor = nil;
if (nil == openHandBarCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameOpenHandBarCursor] copy];
openHandBarCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[self openHandCursor] hotSpot]];
}
return openHandBarCursor;
}
+ (NSCursor *)closedHandBarCursor {
static NSCursor *closedHandBarCursor = nil;
if (nil == closedHandBarCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameClosedHandBarCursor] copy];
closedHandBarCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[self closedHandCursor] hotSpot]];
}
return closedHandBarCursor;
}
+ (NSCursor *)textNoteCursor {
static NSCursor *textNoteCursor = nil;
if (nil == textNoteCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameTextNoteCursor] copy];
textNoteCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[self arrowCursor] hotSpot]];
}
return textNoteCursor;
}
+ (NSCursor *)anchoredNoteCursor {
static NSCursor *anchoredNoteCursor = nil;
if (nil == anchoredNoteCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameAnchoredNoteCursor] copy];
anchoredNoteCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[self arrowCursor] hotSpot]];
}
return anchoredNoteCursor;
}
+ (NSCursor *)circleNoteCursor {
static NSCursor *circleNoteCursor = nil;
if (nil == circleNoteCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameCircleNoteCursor] copy];
circleNoteCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[self arrowCursor] hotSpot]];
}
return circleNoteCursor;
}
+ (NSCursor *)squareNoteCursor {
static NSCursor *squareNoteCursor = nil;
if (nil == squareNoteCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameSquareNoteCursor] copy];
squareNoteCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[self arrowCursor] hotSpot]];
}
return squareNoteCursor;
}
+ (NSCursor *)highlightNoteCursor {
static NSCursor *highlightNoteCursor = nil;
if (nil == highlightNoteCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameHighlightNoteCursor] copy];
highlightNoteCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[self arrowCursor] hotSpot]];
}
return highlightNoteCursor;
}
+ (NSCursor *)underlineNoteCursor {
static NSCursor *underlineNoteCursor = nil;
if (nil == underlineNoteCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameUnderlineNoteCursor] copy];
underlineNoteCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[self arrowCursor] hotSpot]];
}
return underlineNoteCursor;
}
+ (NSCursor *)strikeOutNoteCursor {
static NSCursor *strikeOutNoteCursor = nil;
if (nil == strikeOutNoteCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameStrikeOutNoteCursor] copy];
strikeOutNoteCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[self arrowCursor] hotSpot]];
}
return strikeOutNoteCursor;
}
+ (NSCursor *)lineNoteCursor {
static NSCursor *lineNoteCursor = nil;
if (nil == lineNoteCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameLineNoteCursor] copy];
lineNoteCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[self arrowCursor] hotSpot]];
}
return lineNoteCursor;
}
+ (NSCursor *)inkNoteCursor {
static NSCursor *inkNoteCursor = nil;
if (nil == inkNoteCursor) {
NSImage *cursorImage = [[NSImage imageNamed:SKImageNameInkNoteCursor] copy];
inkNoteCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:[[self arrowCursor] hotSpot]];
}
return inkNoteCursor;
}
+ (NSCursor *)emptyCursor {
static NSCursor *emptyCursor = nil;
if (nil == emptyCursor) {
NSImage *cursorImage = [[NSImage alloc] initWithSize:NSMakeSize(16.0, 16.0)];
emptyCursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:NSMakePoint(8.0, 8.0)];
}
return emptyCursor;
}
+ (NSCursor *)laserPointerCursorWithColor:(NSInteger)color {
static NSPointerArray *laserPointerCursors = nil;
if (laserPointerCursors == nil) {
laserPointerCursors = [NSPointerArray strongObjectsPointerArray];
[laserPointerCursors setCount:7];
}
NSCursor *cursor = (__bridge id)[laserPointerCursors pointerAtIndex:color % 7];
if (nil == cursor) {
NSImage *cursorImage = [NSImage laserPointerImageWithColor:color % 7];
cursor = [[NSCursor alloc] initWithImage:cursorImage hotSpot:NSMakePoint(0.5 * [cursorImage size].width, 0.5 * [cursorImage size].height)];
[laserPointerCursors replacePointerAtIndex:color % 7 withPointer:(__bridge void *)cursor];
}
return cursor;
}
+ (NSCursor *)safeLaserPointerCursorWithColor:(NSInteger)color {
static NSPointerArray *laserPointerCursors = nil;
if (laserPointerCursors == nil) {
laserPointerCursors = [NSPointerArray strongObjectsPointerArray];
[laserPointerCursors setCount:7];
}
NSCursor *cursor = (__bridge id)[laserPointerCursors pointerAtIndex:color % 7];
if (nil == cursor) {
NSImage *cursorImage = [NSImage laserPointerImageWithColor:color % 7];
cursor = [[SKLaserPointerCursor alloc] initWithImage:cursorImage hotSpot:NSMakePoint(0.5 * [cursorImage size].width, 0.5 * [cursorImage size].height)];
[laserPointerCursors replacePointerAtIndex:color % 7 withPointer:(__bridge void *)cursor];
}
return cursor;
}
@end
#pragma mark -
static NSWindow *laserPointerWindow = nil;
static inline void hideLaserPointer(void) {
if (laserPointerWindow) {
[laserPointerWindow close];
laserPointerWindow = nil;
}
}
@implementation SKLaserPointerCursor
- (void)set {
if (original_set)
original_set([NSCursor emptyCursor], _cmd);
else
[[NSCursor emptyCursor] set];
NSPoint p = [NSEvent mouseLocation];
p = NSMakePoint(round(p.x), round(p.y));
if (laserPointerWindow) {
[(SKAnimatedBorderlessWindow *)laserPointerWindow setBackgroundImage:[self image]];
[laserPointerWindow setFrame:SKRectFromCenterAndSize(p, [laserPointerWindow frame].size) display:YES];
} else {
NSImage *image = [self image];
NSNumber *size = [[[NSUserDefaults standardUserDefaults] persistentDomainForName:@"com.apple.universalaccess"] objectForKey:@"mouseDriverCursorSize"];
CGFloat s = 2.0 * round(0.5 * (size ? [size doubleValue] : 1.0) * [image size].width);
laserPointerWindow = [[SKAnimatedBorderlessWindow alloc] initWithContentRect:SKRectFromCenterAndSquareSize(p, s)];
[laserPointerWindow setLevel:(NSWindowLevel)kCGCursorWindowLevel];
[(SKAnimatedBorderlessWindow *)laserPointerWindow setBackgroundImage:image];
[laserPointerWindow setHidesOnDeactivate:YES];
[laserPointerWindow orderFrontRegardless];
}
}
@end