forked from johndbritton/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TPHotBorder.m
400 lines (313 loc) · 9.49 KB
/
TPHotBorder.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
//
// TPHotBorder.m
// Teleport
//
// Created by JuL on Sun Dec 07 2003.
// Copyright (c) 2003-2005 abyssoft. All rights reserved.
//
#import "TPHotBorder.h"
#import "TPHotBorderView.h"
#import "TPPreferencesManager.h"
static NSString * TPHotBorderLocationKey = @"TPHotBorderLocation";
static NSString * TPHotBorderDraggingInfoKey = @"TPHotBorderDraggingInfo";
@interface TPHotBorder ()
{
BOOL _ignoringEvents;
}
- (void)_discardFireAttempt;
@end
@implementation TPHotBorder
+ (TPHotBorder*)hotBorderRepresentingRect:(NSRect)representedRect inRect:(NSRect)parentRect
{
TPHotBorder * hotBorder = [[TPHotBorder alloc] initWithRepresentingRect:representedRect inRect:parentRect];
return hotBorder;
}
+ (NSRect)hotRectWithRepresentingRect:(NSRect)representedRect inRect:(NSRect)parentRect
{
NSRect hotRect;
NSRect enlargedRect;
#if 1
enlargedRect = NSInsetRect(representedRect, -BORDER_SIZE, -BORDER_SIZE);
hotRect = NSIntersectionRect(parentRect, enlargedRect);
#else
enlargedRect = representedRect;
enlargedRect.origin.x -= BORDER_SIZE;
enlargedRect.size.width += 2*BORDER_SIZE;
hotRect = NSIntersectionRect(parentRect, enlargedRect);
// DebugLog(@"1representedRect=%@, parentRect=%@, BORDER_SIZE=%d, offSetRect=%@, hotRect=%@", NSStringFromRect(representedRect), NSStringFromRect(parentRect), BORDER_SIZE, NSStringFromRect(NSOffsetRect(representedRect,BORDER_SIZE,BORDER_SIZE)), NSStringFromRect(hotRect));
if(!NSEqualRects(hotRect, NSZeroRect))
return hotRect;
enlargedRect = representedRect;
enlargedRect.origin.y -= BORDER_SIZE;
enlargedRect.size.height += 2*BORDER_SIZE;
hotRect = NSIntersectionRect(parentRect, enlargedRect);
#endif
// DebugLog(@"2representedRect=%@, parentRect=%@, BORDER_SIZE=%d, offSetRect=%@, hotRect=%@", NSStringFromRect(representedRect), NSStringFromRect(parentRect), BORDER_SIZE, NSStringFromRect(NSOffsetRect(representedRect,BORDER_SIZE,BORDER_SIZE)), NSStringFromRect(hotRect));
return hotRect;
}
- (instancetype) initWithRepresentingRect:(NSRect)representedRect inRect:(NSRect)parentRect
{
self = [super initWithContentRect:representedRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO];
[self setAlphaValue:1.0];
[self setOpaque:NO];
[self setHasShadow:NO];
[self setLevel:kCGDraggingWindowLevel-1];
[self setReleasedWhenClosed:NO];
[self setDelegate:self];
/* Setup view */
TPHotBorderView * hotView = [[TPHotBorderView alloc] initWithFrame:NSZeroRect];
[self setContentView:hotView];
_state = TPHotBorderInactiveState;
_hotDelegate = nil;
identifier = nil;
_fireTimer = nil;
_trackingRectTag = -1;
[self updateWithRepresentingRect:representedRect inRect:parentRect];
[self orderFront:self];
if([self respondsToSelector:@selector(setCollectionBehavior:)]) {
[self setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces|NSWindowCollectionBehaviorStationary];
}
return self;
}
- (void)dealloc
{
[_tapTimer invalidate];
_tapTimer = nil;
[self _discardFireAttempt];
}
- (void)updateWithRepresentingRect:(NSRect)representedRect inRect:(NSRect)parentRect
{
NSRect hotRect = [TPHotBorder hotRectWithRepresentingRect:representedRect inRect:parentRect];
TPGluedRect(NULL, &_side, parentRect, representedRect, TPUndefSide);
[self setFrame:hotRect display:YES];
NSView * hotView = [self contentView];
if(_trackingRectTag != -1) {
[hotView removeTrackingRect:_trackingRectTag];
}
_trackingRectTag = [hotView addTrackingRect:[hotView bounds] owner:self userData:NULL assumeInside:NO];
}
- (void)activate
{
if(NSPointInRect([self mouseLocationOutsideOfEventStream], [[self contentView] bounds])) {
[self performSelector:@selector(activate) withObject:nil afterDelay:0.1]; // post-pone activation until mouse is not already in the hot border
}
else {
_state = TPHotBorderActiveState;
}
}
- (void)deactivate
{
_state = TPHotBorderInactiveState;
}
- (TPHotBorderState)state
{
return _state;
}
- (void)setDoubleTap:(BOOL)doubleTap
{
_doubleTap = doubleTap;
}
- (BOOL)doubleTap
{
return _doubleTap;
}
- (void)setAcceptDrags:(BOOL)acceptDrags
{
_acceptDrags = acceptDrags;
[self setIgnoresMouseEvents:!acceptDrags];
if(acceptDrags) {
[self setLevel:kCGDraggingWindowLevel-1];
[[self contentView] registerForDraggedTypes:@[NSFilenamesPboardType]];
}
else {
[self setLevel:kCGMaximumWindowLevel];
[[self contentView] unregisterDraggedTypes];
}
}
- (BOOL)acceptDrags
{
return _acceptDrags;
}
- (void)setDelegate:(id)delegate
{
_hotDelegate = delegate;
}
- (void)setIdentifier:(NSString*)inIdentifier
{
identifier = [inIdentifier copy];
}
- (NSString*)identifier
{
return identifier;
}
- (void)setOpaqueToMouseEvents:(BOOL)opaque
{
TPHotBorderView * hotView = (TPHotBorderView*)[self contentView];
if(opaque) {
_ignoringEvents = YES;
[self setIgnoresMouseEvents:NO];
[hotView setColor:[NSColor colorWithCalibratedWhite:0.5 alpha:0.2]];
[hotView display];
}
else {
_ignoringEvents = NO;
[self setIgnoresMouseEvents:!_acceptDrags];
[hotView setColor:[hotView normalColor]];
[hotView display];
}
}
- (NSRect)hotRect
{
return [self frame];
}
- (TPSide)side
{
return _side;
}
- (void)delayedActivate
{
float inhibitionPeriod = [[TPPreferencesManager sharedPreferencesManager] floatForPref:INHIBITION_PERIOD];
if(inhibitionPeriod > 0.0) {
_state = TPHotBorderActivatingState;
[NSTimer scheduledTimerWithTimeInterval:inhibitionPeriod target:self selector:@selector(activate) userInfo:nil repeats:NO];
}
else {
[self activate];
}
}
- (NSPoint)screenPointFromLocalPoint:(NSPoint)localPoint flipped:(BOOL)flipped
{
NSPoint point = [self convertBaseToScreen:localPoint];
if(flipped) {
point.y = NSMaxY([[NSScreen screens][0] frame]) - point.y;
}
return point;
}
- (void)mouseDown:(NSEvent*)event
{
DebugLog(@"mouseDown");
[super mouseDown:event];
}
- (void)mouseUp:(NSEvent*)event
{
DebugLog(@"mouseUp");
[super mouseUp:event];
}
#pragma mark -
#pragma mark Fire methods
- (void)_fireAttemptWithEvent:(NSEvent*)event atLocation:(NSPoint)location withDraggingInfo:(id <NSDraggingInfo>)draggingInfo
{
if(_state != TPHotBorderActiveState) {
DebugLog(@"not active");
return;
}
if(_hotDelegate == nil) {
DebugLog(@"nil _hotDelegate!");
return;
}
if (_ignoringEvents) {
DebugLog(@"ignoring events");
return;
}
if([_hotDelegate respondsToSelector:@selector(hotBorder:canFireWithEvent:)]) {
if(![_hotDelegate hotBorder:self canFireWithEvent:event]) {
DebugLog(@"can't activate!");
return;
}
}
if(_doubleTap) {
if(_tapTimer == nil) {
float tapInterval = [[TPPreferencesManager sharedPreferencesManager] floatForPref:DOUBLE_TAP_INTERVAL];
_tapTimer = [NSTimer scheduledTimerWithTimeInterval:tapInterval target:self selector:@selector(tapIntervalExpired:) userInfo:nil repeats:NO];
return;
}
else {
[_tapTimer invalidate];
_tapTimer = nil;
}
}
float delay = [_delegate hotBorderSwitchDelay:self];
if(delay > 0.0) {
NSMutableDictionary * fireDict = [NSMutableDictionary dictionary];
fireDict[TPHotBorderLocationKey] = [NSValue valueWithPoint:location];
if (draggingInfo != nil) {
fireDict[TPHotBorderDraggingInfoKey] = draggingInfo;
}
_fireTimer = [NSTimer scheduledTimerWithTimeInterval:delay target:self selector:@selector(_fireWithTimer:) userInfo:fireDict repeats:NO];
}
else {
[self fireAtLocation:location withDraggingInfo:draggingInfo];
}
}
- (void)tapIntervalExpired:(NSTimer*)tapTimer
{
_tapTimer = nil;
}
- (void)_discardFireAttempt
{
[_fireTimer invalidate];
_fireTimer = nil;
}
- (void)mouseEntered:(NSEvent *)event
{
DebugLog(@"mouseEntered %@", [[NSPasteboard pasteboardWithName:NSDragPboard] types]);
[self _discardFireAttempt];
NSUInteger buttonState = [NSEvent pressedMouseButtons];
if(buttonState == 0) { // no mouse button down
[self _fireAttemptWithEvent:event atLocation:[event locationInWindow] withDraggingInfo:nil];
}
}
- (void)mouseExited:(NSEvent *)event
{
DebugLog(@"mouseExited");
[self _discardFireAttempt];
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
DebugLog(@"drag entered\ntypes=%@", [[sender draggingPasteboard] types]);
[self _discardFireAttempt];
NSPoint point = [sender draggingLocation];
[self _fireAttemptWithEvent:[NSApp currentEvent] atLocation:point withDraggingInfo:sender];
return NSDragOperationCopy;
}
- (void)draggingExited:(id <NSDraggingInfo>)sender
{
DebugLog(@"drag exited");
[self _discardFireAttempt];
}
- (BOOL)prepareForDragOperation:(id <NSDraggingInfo>)sender
{
DebugLog(@"prepareForDragOperation");
return YES;
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
DebugLog(@"performDragOperation");
return YES;
}
- (void)concludeDragOperation:(id <NSDraggingInfo>)sender
{
DebugLog(@"concludeDragOperation");
}
- (void)_fireWithTimer:(NSTimer*)timer
{
NSDictionary * fireDict = [timer userInfo];
NSPoint location = [fireDict[TPHotBorderLocationKey] pointValue];
[self fireAtLocation:location withDraggingInfo:fireDict[TPHotBorderDraggingInfoKey]];
}
- (void)fireAtLocation:(NSPoint)location withDraggingInfo:(id <NSDraggingInfo>)draggingInfo
{
[self _discardFireAttempt];
if(![_hotDelegate respondsToSelector:@selector(hotBorder:firedAtLocation:withDraggingInfo:)]) {
DebugLog(@"delegate does not respond to selector!");
return;
}
DebugLog(@"Fire %@ at position=%@", self, NSStringFromPoint(location));
[self deactivate];
BOOL doFireAnimation = [_hotDelegate hotBorder:self firedAtLocation:location withDraggingInfo:draggingInfo];
if(doFireAnimation)
[(TPHotBorderView*)[self contentView] fireAtLocation:location];
else
;
}
@end