-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQSMouseTriggerView.m
255 lines (195 loc) · 7.55 KB
/
QSMouseTriggerView.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
#import "QSMouseTriggerManager.h"
#import "QSMouseTriggerView.h"
#import "QSMouseTriggerDisplayView.h"
#define ESIZE 1
NSRect rectForAnchor(NSUInteger anchor, NSRect rect,CGFloat size, CGFloat inset){
switch(anchor){
case QSMaxXAnchor:
return NSMakeRect(NSMaxX(rect)-size,rect.origin.y+inset,size,NSHeight(rect)-inset*2);
case QSMinXAnchor:
return NSMakeRect(rect.origin.x,rect.origin.y+inset,size,NSHeight(rect)-inset*2);
case QSMaxYAnchor:
return NSMakeRect(rect.origin.x+inset,NSMaxY(rect)-size,NSWidth(rect)-inset*2,size);
case QSMinYAnchor:
return NSMakeRect(rect.origin.x+inset,rect.origin.y,NSWidth(rect)-inset*2,size);
default:
{
NSRect cornerRect=NSMakeRect(0,0,size,size);
cornerRect=alignRectInRect(cornerRect,rect,oppositeQuadrant(anchor));
return cornerRect;
}
}
return NSMakeRect(0,0,100,100);
}
@implementation QSMouseTriggerWindow
- (BOOL)canBecomeKeyWindow{return NO;}
- (BOOL)canBecomeMainWindow{return NO;}
@end
@implementation QSMouseTriggerView
+ (id)triggerWindowWithAnchor:(NSUInteger)thisAnchor onScreenNum:(NSInteger)thisScreen{
NSWindow* window = [[[QSMouseTriggerWindow alloc]initWithContentRect:NSZeroRect styleMask:NSBorderlessWindowMask|NSNonactivatingPanelMask backing:NSBackingStoreBuffered defer:YES]autorelease];
[window setBackgroundColor: [NSColor clearColor]];
[window setOpaque:NO];
[window setCanHide:NO];
[window setAllowsToolTipsWhenApplicationIsInactive:YES];
[window setIgnoresMouseEvents:YES];
[window setHasShadow:NO];
[window setLevel:kCGPopUpMenuWindowLevel-1];
[window setSticky:YES];
[window setContentView:[[[self alloc]initWithFrame:NSZeroRect anchor:thisAnchor onScreenNum:thisScreen]autorelease]];
//[window setDelegate:[window contentView]]];
return window;
}
- (id)initWithFrame:(NSRect)frame anchor:(NSUInteger)thisAnchor onScreenNum:(NSInteger)thisScreen{
self = [super initWithFrame:frame];
if (self) {
anchor=thisAnchor;
screenNum=thisScreen;
screen=nil;
// NSLog(@"init with screen %d",thisScreen);
}
return self;
}
- (void)viewDidMoveToWindow{
[self updateFrame];
[self registerForDraggedTypes:standardPasteboardTypes];
}
- (void)updateFrame{
NSRect rect=rectForAnchor(anchor,[[self screen]frame],anchor<5?2:1,64);
// NSLog(@"View %d:%d",screen, anchor);
//logRect(rect);
[[self window]setFrame:rect display:YES];
if (trackingRect)[self removeTrackingRect:trackingRect];
trackingRect=[self addTrackingRect:[self frame] owner:self userData:nil assumeInside:NO];
[self updateDisplayFrame];
}
- (void)updateDisplayFrame{
if (displayWindow){
NSRect rect;
if (anchor<5)
rect=rectForAnchor(anchor,[[self screen]frame],64,NO);
else
rect=rectForAnchor(anchor,[[self screen]frame],8,NO);
[displayWindow setFrame:rect display:YES];
// NSLog(@"display");
// logRect(rect);
}
}
- (void)drawRect:(NSRect)rect {
if (/* DISABLES CODE */ (1) ||active){
// ***warning * this should be enabled only if dragging destination
[[NSColor colorWithDeviceWhite:0.0 alpha:0.05]set];
}else{
[[NSColor clearColor]set];
}
NSRectFill(rect);
}
- (void)mouseEntered:(NSEvent *)theEvent{
NSEvent *mouseExited = [NSApp nextEventMatchingMask:NSMouseExitedMask untilDate:[NSDate dateWithTimeIntervalSinceNow:0.1] inMode:NSEventTrackingRunLoopMode dequeue:YES];
if (mouseExited) {
return;
}
if (dragging) {
return;
}
[self showTriggerList];
// NSLog(@"entered %x",[[self window]windowNumber]);
active=YES;
[self setNeedsDisplay:YES];
if (!captureMode) {
[[self displayWindow] orderFront:self];
}
[[QSMouseTriggerManager sharedInstance] handleMouseTriggerEvent:theEvent forView:self];
}
-(void)dealloc{
//NSLog(@"dealloc %d:%d",screen,anchor);
[screen release];
screen=nil;
[super dealloc];
}
- (NSScreen *)screen{
if (!screen)
screen=[[NSScreen screenWithNumber:screenNum]retain];
return screen;
}
- (void)mouseExited:(NSEvent *)theEvent{
if (dragging) {
return;
}
active=NO;
[self setNeedsDisplay:YES];
if (!captureMode) {
[[self displayWindow]orderOut:self];
}
[[QSMouseTriggerManager sharedInstance] handleMouseTriggerEvent:theEvent forView:self];
}
- (void)showTriggerList{
// NSWindow *descWindow=[[QSMouseTriggerManager sharedInstance] triggerDescriptionWindowForAnchor:anchor onScreen:[[self window]screen]];
// NSLog(@"triggers, %@",descWindow);
}
- (void)rightMouseDown:(NSEvent *)theEvent{
[super rightMouseDown:theEvent];
[[QSMouseTriggerManager sharedInstance] handleMouseTriggerEvent:theEvent forView:self];
}
- (void)mouseDown:(NSEvent *)theEvent{
[super mouseDown:theEvent];
[[QSMouseTriggerManager sharedInstance] handleMouseTriggerEvent:theEvent forView:self];
// [[NSDistributedNotificationCenter defaultCenter]postNotificationName:@"com.apple.HIToolbox.endMenuTrackingNotification" object:nil];
// [QSTriggerController handleMouseTriggerEvent:theEvent for]
}
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender{
// NSLog(@"drag enter");
dragging=YES;
[[self displayWindow] orderFront:self];
[[QSMouseTriggerManager sharedInstance] handleMouseTriggerEvent:nil type:101 forView:self];
return NSDragOperationEvery;
}
- (void)draggingExited:(id <NSDraggingInfo>)sender{
//NSLog(@"drag exit");
dragging=NO;
[[self displayWindow]orderOut:self];
[[QSMouseTriggerManager sharedInstance] handleMouseTriggerEvent:nil type:102 forView:self];
}
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender{
dragging = NO;
[[QSMouseTriggerManager sharedInstance] setMouseTriggerObject:
[QSObject objectWithPasteboard:[sender draggingPasteboard]]];
[[self displayWindow] orderOut:self];
[[QSMouseTriggerManager sharedInstance] handleMouseTriggerEvent:nil type:100 forView:self];
return YES;
}
- (NSWindow *)displayWindow{
if (!displayWindow){
displayWindow= [[QSWindow alloc] initWithContentRect:NSZeroRect styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
[displayWindow setShowEffect:[NSDictionary dictionaryWithObjectsAndKeys:@"show",@"type",[NSNumber numberWithDouble:0.2],@"duration",nil]];
[displayWindow setHideEffect:[NSDictionary dictionaryWithObjectsAndKeys:@"hide",@"type",[NSNumber numberWithDouble:0.2],@"duration",nil]];
[displayWindow setHidesOnDeactivate:NO];
[displayWindow setBackgroundColor: [NSColor clearColor]];
[displayWindow setOpaque:NO];
[displayWindow setCanHide:NO];
[displayWindow setHasShadow:NO];
[displayWindow setLevel:kCGStatusWindowLevel];
[displayWindow setIgnoresMouseEvents:YES];
[displayWindow setSticky:YES];
[displayWindow setContentView:[[[QSMouseTriggerDisplayView alloc]initWithFrame:NSZeroRect anchor:anchor]autorelease]];
[self updateDisplayFrame];
}
return displayWindow;
}
- (void)applicationDidChangeScreenParameters:(NSNotification *)aNotification{
[self updateFrame];
[self updateDisplayFrame];
[displayWindow release];
displayWindow=nil;
}
- (bool)captureMode {
return captureMode;
}
- (void)setCaptureMode:(BOOL)flag {
if (flag)
[[self displayWindow]orderFront:self];
else
[[self displayWindow]orderOut:self];
captureMode = flag;
}
@end