-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQSMouseTriggerManager.m
805 lines (615 loc) · 26.2 KB
/
QSMouseTriggerManager.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
//
// QSMouseTriggerManager.m
// Quicksilver
//
// Created by Nicholas Jitkoff on Sun Jun 13 2004.
// Copyright (c) 2004 __MyCompanyName__. All rights reserved.
//
#import "QSMouseTriggerManager.h"
#import "QSMouseTriggerView.h"
#define QSTriggerCenter NSClassFromString(@"QSTriggerCenter")
#define NSAllModifierKeysMask (NSShiftKeyMask|NSControlKeyMask|NSAlternateKeyMask|NSCommandKeyMask|NSFunctionKeyMask)
@implementation NSEvent (CarbonConversion)
- (NSEvent *)mouseEventWithCarbonClickEvent:(EventRef)theEvent{
return nil;
}
@end
@interface QSMouseTriggerTableCell : NSTextFieldCell
@end
@implementation QSMouseTriggerTableCell
- (void)drawWithFrame:(NSRect)cellFrame inView:(NSView *)controlView{
NSRect imageRect,textRect;
NSDivideRect(cellFrame,&imageRect,&textRect,NSHeight(cellFrame),NSMinXEdge);
[[NSColor lightGrayColor]set];
//NSRectFill(imageRect);
[NSGraphicsContext currentContext];
NSAffineTransform *flipTransform=[NSAffineTransform transform];
[flipTransform translateXBy:0 yBy:NSMinY(cellFrame)];
[flipTransform scaleXBy:1 yBy:-1];
[flipTransform translateXBy:0 yBy:-NSMaxY(cellFrame)];
[flipTransform concat];
NSUInteger anchors=[[[self representedObject] objectForKey:@"anchorMask"] unsignedIntegerValue];
NSBezierPath *path=[NSBezierPath bezierPath];
[path appendBezierPathWithRoundedRectangle:NSInsetRect(imageRect,4,4)
withRadius:2];
[path fill];
[[NSColor blueColor]set];
imageRect=NSInsetRect(imageRect,1,1);
if (anchors&QSMinXAnchorMask) //Left
NSRectFill(rectForAnchor(QSMinXAnchor, imageRect,2,4));
if (anchors&QSTopLeftAnchorMask)
NSRectFill(rectForAnchor(QSTopLeftAnchor, imageRect,2,4));
if (anchors&QSMaxYAnchorMask) //Top
NSRectFill(rectForAnchor(QSMaxYAnchor, imageRect,2,4));
if (anchors&QSTopRightAnchorMask)
NSRectFill(rectForAnchor(QSTopRightAnchor, imageRect,2,4));
if (anchors&QSMaxXAnchorMask) //Right
NSRectFill(rectForAnchor(QSMaxXAnchor, imageRect,2,4));
if (anchors&QSBottomRightAnchorMask)
NSRectFill(rectForAnchor(QSBottomRightAnchor, imageRect,2,4));
if (anchors&QSMinYAnchorMask) //Bottom
NSRectFill(rectForAnchor(QSMinYAnchor, imageRect,2,4));
if (anchors&QSBottomLeftAnchorMask)
NSRectFill(rectForAnchor(QSBottomLeftAnchor, imageRect,2,4));
[flipTransform invert];
[flipTransform concat];
[super drawWithFrame:textRect inView:controlView];
}
@end
OSStatus mouseActivated(EventHandlerCallRef nextHandler, EventRef theEvent, void *userData) {
EventMouseButton button;
GetEventParameter(theEvent, kEventParamMouseButton,typeMouseButton,0,
sizeof(button),0,&button);
// NSLog(@"------------------event %d",button);
return CallNextEventHandler(nextHandler, theEvent);
return eventNotHandledErr;
}
@implementation QSMouseTriggerManager
+(void)registerEventHandlers{
// if (VERBOSE) NSLog(@"Registering for Global Mouse Events");
static EventHandlerRef trackMouse;
EventTypeSpec eventType[2]={{kEventClassMouse, kEventMouseUp}, {kEventClassMouse, kEventMouseDown}};
EventHandlerUPP handlerFunction = NewEventHandlerUPP(mouseActivated);
InstallEventHandler(GetEventMonitorTarget(), handlerFunction, 2, eventType, NULL, &trackMouse);
}
+ (void)initialize
{
[self registerEventHandlers];
}
- (NSCell *)descriptionCellForTrigger:(QSTrigger *)trigger{
return [[[QSMouseTriggerTableCell alloc]init]autorelease];
}
-(NSString *)name{
return @"Mouse";
}
-(NSImage *)image{
return [NSImage imageNamed:@"MouseTrigger"];
}
- (void)initializeTrigger:(NSMutableDictionary *)trigger{
if (![trigger objectForKey:@"eventType"])
[trigger setObject:[NSNumber numberWithUnsignedInteger:NSLeftMouseDown] forKey:@"eventType"];
}
+ (id)sharedInstance{
static QSMouseTriggerManager *_sharedInstance = nil;
if (!_sharedInstance){
_sharedInstance = [[[self class] allocWithZone:[self zone]] init];
}
return _sharedInstance;
}
- (id) init{
if (self=[super init]){
anchorWindows=[[NSMutableDictionary alloc]initWithCapacity:0];
anchorArrays=[[NSMutableDictionary alloc]initWithCapacity:0];
anywhereArray=[[NSMutableArray alloc]init];
// int i;
// for (i=1;i<9;i++)
// [anchorArrays setObject:[NSMutableArray arrayWithCapacity:0] forKey:[NSString stringWithFormat:@"%d",i]];
//NSLog(@"init");
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationDidChangeScreenParameters:)
name:NSApplicationDidChangeScreenParametersNotification object:nil];
[self addObserver:self
forKeyPath:@"currentTrigger"
options:0
context:nil];
}
return self;
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
[self populateInfoFields];
}
-(NSMutableArray *)anchorArrayForScreen:(NSInteger)screen anchor:(NSUInteger)anchor{
NSString *key=[NSString stringWithFormat:@"%ld:%lu",(long)screen, (unsigned long)anchor];
NSMutableArray *array=[anchorArrays objectForKey:key];
if (!array){
array=[NSMutableArray array];
[anchorArrays setObject:array forKey:key];
//NSLog(@"Set %@",key);
}
return array;
}
-(void)addTriggerDictionary:(QSTrigger *)entry toAnchorArray:(NSUInteger)anchor forScreens:(NSArray *)screens {
for (NSScreen *screen in screens) {
NSMutableArray *matchedArray = [self anchorArrayForScreen:[screen screenNumber] anchor:anchor];
if (![matchedArray containsObject:entry]) {
[matchedArray addObject:entry];
}
}
}
// Gets the screen(s) (as an array) based on the tag of the popup menu in the trigger settings
-(NSArray *)screensForScreenTag:(NSInteger)screenTag {
NSArray *screens=[NSScreen screens];
NSUInteger screenCount=[screens count];
if (screenTag==0 || (screenTag==-1 && screenCount==1)) {
return [NSArray arrayWithObject:[screens objectAtIndex:0]];
} else if (screenTag==1) {
if (screenCount>1) {
return [NSArray arrayWithObject:[screens objectAtIndex:1]];
}
} else if (screenTag==-1 && screenCount>1) {
return screens;
} else {
return [NSArray arrayWithObject:[NSScreen screenWithNumber:screenTag]];
}
return nil;
}
-(BOOL)enableTrigger:(QSTrigger *)entry{
NSUInteger anchorMask=[[entry objectForKey:@"anchorMask"] unsignedIntegerValue];
BOOL anywhere=[[entry objectForKey:@"anywhere"]boolValue];
NSInteger screenTag = [[entry objectForKey:@"screen"] integerValue];
if (anywhere)
[anywhereArray addObject:entry];
NSUInteger i;
//NSArray *array;
for (i=1;i<9;i++){
if ((1 << i) & anchorMask){
[self addTriggerDictionary:entry toAnchorArray:i forScreens:[self screensForScreenTag:screenTag]];
}
}
[self updateTriggerWindows];
return YES;
}
-(BOOL)disableTrigger:(QSTrigger *)entry{
// NSString *theID=[entry objectForKey:kItemID];
[[anchorArrays allValues]makeObjectsPerformSelector:@selector(removeObject:) withObject:entry];
[anywhereArray removeObject:entry];
[self updateTriggerWindows];
return YES;
}
- (NSWindow *)triggerDescriptionWindowForAnchor:(NSUInteger)anchor onScreen:(NSScreen *)screen{
// NSArray *array=[anchorArrays objectForKey:[NSString stringWithFormat:@"%d",anchor]];
// NSDictionary *[currentTrigger info];
// NSEnumerator *triggerEnum=[[anchorArrays objectForKey:[NSString stringWithFormat:@"%d",anchor]]objectEnumerator];
// while([currentTrigger info]=[triggerEnum nextObject]){
// NSLog(@"\rTrigger: %@\rCommand: %@",[self descriptionForTrigger:[currentTrigger info]],[[QSTriggerCenter commandForTrigger:[currentTrigger info]]description]);
// }
return nil;
}
- (void)handleMouseTriggerEvent:(NSEvent *)theEvent type:(NSEventType)type forView:(QSMouseTriggerView *)view{
//NSLog(@"Mouse %@",theEvent);
NSUInteger anchor= view ? view->anchor : -1;
NSInteger screen= view ? view->screenNum : -1;
NSArray *enumerateArray=nil;
if (!view)
enumerateArray = anywhereArray;
else
enumerateArray = [anchorArrays objectForKey:[NSString stringWithFormat:@"%ld:%lu",screen,anchor]];
NSMutableArray *matchedTriggers=[NSMutableArray array];
BOOL checkForMoreClicks=NO;
//BOOL checkForDelay=NO;
//int inverseEvent=0;
NSEventMask inverseEventMask = 1 << (type+1);
NSDate *date=[NSDate date];
CGFloat thisDelay = 0.0;
CGFloat longestDelay = 0.0;
for (QSTrigger *thisTrigger in enumerateArray){
if (!type) {
type = [theEvent type];
}
if ([[[thisTrigger info] objectForKey:@"eventType"] unsignedIntegerValue]!=type) {
continue;
}
if (!theEvent) {
theEvent = [NSApp currentEvent];
}
if (type==NSOtherMouseDown && [[[thisTrigger info] objectForKey:@"buttonNumber"] integerValue]!=([theEvent buttonNumber]+1)) {
continue;
}
NSUInteger flags=[[[thisTrigger info] objectForKey:@"modifierFlags"] unsignedIntegerValue];
NSUInteger currentFlags=[theEvent modifierFlags];
if(flags!=(currentFlags&NSAllModifierKeysMask)) {
continue;
}
if (type==NSLeftMouseDown || type==NSRightMouseDown || type==NSOtherMouseDown){
#warning if only single click events are enabled, multi clicks should still resolve
NSInteger clickCount=[[[thisTrigger info] objectForKey:@"clickCount"] unsignedIntegerValue];
if (!clickCount) {
clickCount=1;
}
if (clickCount != [theEvent clickCount]) {
if (clickCount>[theEvent clickCount])
checkForMoreClicks=YES;
continue;
}
}
id delayValue=[[thisTrigger info] objectForKey:@"delay"];
thisDelay = delayValue ? [delayValue doubleValue]:0;
if (thisDelay<longestDelay) {
continue;
} else if (thisDelay>longestDelay) {
//NSLog(@"lookingForDelay %f",thisDelay,longestDelay);
NSEvent *anEvent=[NSApp nextEventMatchingMask:inverseEventMask untilDate:[date dateByAddingTimeInterval:thisDelay] inMode:NSDefaultRunLoopMode dequeue:NO];
if (anEvent){
continue;
}else {
//NSLog(@"accepting x, clearing previous %d",[matchedTriggers count]);
longestDelay = thisDelay;
[matchedTriggers removeAllObjects];
}
}
[matchedTriggers addObject:thisTrigger];
}
if (checkForMoreClicks){
NSEvent *anotherClick=[NSApp nextEventMatchingMask:1<<[theEvent type] untilDate:[NSDate dateWithTimeIntervalSinceNow:0.2] inMode:NSDefaultRunLoopMode dequeue:NO];
if (anotherClick) return;
}
for(id match in matchedTriggers){
[[QSTriggerCenter sharedInstance] executeTrigger:match];
}
}
- (void)handleMouseTriggerEvent:(NSEvent *)theEvent forView:(QSMouseTriggerView *)view{
[self handleMouseTriggerEvent:theEvent type:[theEvent type] forView:view];
}
//- (NSString *)descriptionForTrigger:(NSDictionary *)dict{return [[self class]descriptionForTrigger:dict];}
- (NSString *)descriptionForTrigger:(NSDictionary *)dict{
//return [self descriptionForMouseTrigger:dict];
NSUInteger anchors=[[dict objectForKey:@"anchorMask"] unsignedIntegerValue];
NSMutableString *desc=[NSMutableString stringWithCapacity:0];
NSMutableArray *anchorArray=[NSMutableArray array];
if (anchors&QSMinXAnchorMask) //Left
[anchorArray addObject:[NSString stringWithFormat:@"%C",0x2190]];
if (anchors&QSTopLeftAnchorMask)
[anchorArray addObject:[NSString stringWithFormat:@"%C",0x2196]];
if (anchors&QSMaxYAnchorMask) //Top
[anchorArray addObject:[NSString stringWithFormat:@"%C",0x2191]];
if (anchors&QSTopRightAnchorMask)
[anchorArray addObject:[NSString stringWithFormat:@"%C",0x2197]];
if (anchors&QSMaxXAnchorMask) //Right
[anchorArray addObject:[NSString stringWithFormat:@"%C",0x2192]];
if (anchors&QSBottomRightAnchorMask)
[anchorArray addObject:[NSString stringWithFormat:@"%C",0x2198]];
if (anchors&QSMinYAnchorMask) //Bottom
[anchorArray addObject:[NSString stringWithFormat:@"%C",0x2193]];
if (anchors&QSBottomLeftAnchorMask)
[anchorArray addObject:[NSString stringWithFormat:@"%C",0x2199]];
[desc appendString:[self descriptionForMouseTrigger:dict]];
if ([anchorArray count])
[desc appendFormat:@"(%@)",[anchorArray componentsJoinedByString:@","]];
return desc;
}
- (NSString *)descriptionForMouseTrigger:(NSDictionary *)dict{
NSMutableString *desc=[NSMutableString stringWithCapacity:0];
NSUInteger modifiers=[[dict objectForKey:@"modifierFlags"] unsignedIntegerValue];
if (modifiers&NSShiftKeyMask)[desc appendFormat:@"%C",0x21e7];
if (modifiers&NSControlKeyMask)[desc appendFormat:@"%C",0x2303];
if (modifiers&NSAlternateKeyMask)[desc appendFormat:@"%C",0x2325];
if (modifiers&NSCommandKeyMask)[desc appendFormat:@"%C",0x2318];
if (modifiers&NSFunctionKeyMask)[desc appendString:@"Fn"];
NSEventType type=[[dict objectForKey:@"eventType"] unsignedIntegerValue];
switch (type){
case NSLeftMouseDown:
[desc appendFormat:@"%C",0x25BD];
break;
case NSRightMouseDown:
[desc appendFormat:@"%C%C",0x25BD,0x1D3F];
break;
case NSOtherMouseDown:
switch ([[dict objectForKey:@"buttonNumber"] integerValue]){
case 3: [desc appendFormat:@"%C%C",0x25BD,0x00B3]; break;
case 4: [desc appendFormat:@"%C%C",0x25BD,0x2074]; break;
case 5: [desc appendFormat:@"%C%C",0x25BD,0x2075]; break;
default: [desc appendFormat:@"%C?",0x25BD]; break;
}
break;
case 101:
[desc appendFormat:@"Drag Enter"];
break;
case 102:
[desc appendFormat:@"Drag Exit"];
break;
case NSMouseEntered:
[desc appendFormat:@"Mouse Enter"];
break;
case NSMouseExited:
[desc appendFormat:@"Mouse Exit"];
break;
default:
break;
}
NSInteger clickCount=[[dict objectForKey:@"clickCount"] integerValue];
if (clickCount>1)
[desc appendFormat:@"%C%ld", 0x00D7, (long)clickCount];
// NSLog(@"clickdel %@",[dict objectForKey:@"clickDelay"]);
if ([dict objectForKey:@"delay"] && [[dict objectForKey:@"delay"]doubleValue]>0)
[desc appendFormat:@"...%.1fs",[[dict objectForKey:@"clickDelay"]doubleValue]];
return desc;
}
- (void)updateTriggerWindows{
NSEnumerator *keyEnum=[anchorArrays keyEnumerator];
NSString *key;
NSEnumerator *triggerEnum;
BOOL visible;
BOOL clickable;
BOOL draggable;
NSInteger mainScreenNum=[[NSScreen mainScreen]screenNumber];
// NSLog(@"asch%@",anchorArrays);
while (key=[keyEnum nextObject]){
triggerEnum=[[anchorArrays objectForKey:key]objectEnumerator];
visible=clickable=draggable=NO;
NSMutableArray *tips=[NSMutableArray arrayWithCapacity:0];
for(QSTrigger *thisTrigger in triggerEnum){
if (![[[thisTrigger info] objectForKey:@"enabled"] boolValue]) {
continue;
}
[tips addObject:[NSString stringWithFormat:@"%@:\t%@",[self descriptionForMouseTrigger:[thisTrigger info]],[thisTrigger name]]];
visible=YES;
NSEventType type=[[[thisTrigger info] objectForKey:@"eventType"] unsignedIntegerValue];
switch (type){
case NSLeftMouseDown:
case NSRightMouseDown:
case NSOtherMouseDown:
clickable=YES;
break;
case 100:
case 101:
case 102:
draggable=YES;
break;
case NSMouseEntered:
break;
default:
break;
}
}
if (visible){
NSWindow *window=[anchorWindows objectForKey:key];
if (!window){
NSArray *components=[key componentsSeparatedByString:@":"];
NSInteger tempAnchor;
[[NSScanner scannerWithString:[components objectAtIndex:1]] scanInteger:&tempAnchor] ;
NSUInteger anchor = (NSUInteger)tempAnchor;
NSInteger screenNum;
[[NSScanner scannerWithString:[components objectAtIndex:0]] scanInteger:&screenNum];
// NSLog(@"%d %x",anchor,screenNum);
if (anchor==QSMaxYAnchor && screenNum==mainScreenNum){
// NSLog(@"skipping menu edge");
}else{
window=[QSMouseTriggerView triggerWindowWithAnchor:anchor
onScreenNum:screenNum];
[anchorWindows setObject:window forKey:key];
//NSLog(@"Creating anchor window %@",key);
[[window contentView]setToolTip:nil];
}
}
[[window contentView] setToolTip:[tips componentsJoinedByString:@"\r"]];
[window orderFront:self];
[window setIgnoresMouseEvents:!(clickable || draggable)];
}else{
[anchorWindows removeObjectForKey:key];
}
}
}
-(void)enableCaptureMode{
NSUInteger i;
for(i=1;i<9;i++){
NSString *key=[NSString stringWithFormat:@"%lu",(unsigned long)i];
NSWindow *window = [anchorWindows objectForKey:key];
if (!window){
window= [QSMouseTriggerView triggerWindowWithAnchor:i onScreenNum:0];
[anchorWindows setObject:window forKey:key];
// if (VERBOSE)NSLog(@"Creating anchor window %d",i);
[[window contentView]setToolTip:nil];
}
[window setIgnoresMouseEvents:NO];
[window orderFront:self];
[[window contentView]setCaptureMode:YES];
}
}
-(void)disableCaptureMode{
NSUInteger i;
for(i=1;i<9;i++){
NSWindow *window=[anchorWindows objectForKey:[NSString stringWithFormat:@"%lu",(unsigned long)i]];
[[window contentView]setCaptureMode:NO];
}
[self updateTriggerWindows];
}
- (NSView *) settingsView{
if (!settingsView){
[NSBundle loadNibNamed:@"QSMouseTrigger" owner:self];
}
// NSLog(@"sview %@",settingsView);
return [[settingsView retain] autorelease];
}
- (void)populateInfoFields{
//NSLog(@"trigger %@",currentTrigger);
if([[currentTrigger type] isEqualToString:@"QSMouseTrigger"]){
NSEventType eventType=[[[currentTrigger info] objectForKey:@"eventType"] unsignedIntegerValue];
BOOL otherClick=eventType==NSOtherMouseDown;
NSUInteger modifiersMask=[[[currentTrigger info] objectForKey:@"modifierFlags"]unsignedIntegerValue];
BOOL anywhere=[[[currentTrigger info] objectForKey:@"anywhere"]boolValue] && (otherClick);
[mouseTriggerScreenPopUp removeAllItems];
NSMenuItem *item=[[mouseTriggerScreenPopUp menu] addItemWithTitle:@"All Displays" action:nil keyEquivalent:@""];
[item setTag:-1];
item=[NSMenuItem separatorItem];
[item setTag:NSNotFound];
[[mouseTriggerScreenPopUp menu]addItem:item];
item= [[mouseTriggerScreenPopUp menu] addItemWithTitle:@"Main Display" action:nil keyEquivalent:@""];
[item setTag:0];
item= [[mouseTriggerScreenPopUp menu] addItemWithTitle:@"Secondary Display" action:nil keyEquivalent:@""];
[item setTag:1];
item=[NSMenuItem separatorItem];
[item setTag:NSNotFound];
[[mouseTriggerScreenPopUp menu]addItem:item];
item=[[mouseTriggerScreenPopUp menu] addItemWithTitle:@"Specific Displays:" action:nil keyEquivalent:@""];
[item setTarget:nil];
NSEnumerator *e=[[NSScreen screens]objectEnumerator];
NSScreen *screen;
while(screen=[e nextObject]){
NSString *name=[screen deviceName];
name=[name stringByAppendingString:[[NSString stringWithFormat:@" (%lx)", (long)[screen screenNumber]] uppercaseString]];
[[mouseTriggerScreenPopUp menu] addItemWithTitle:name action:nil keyEquivalent:@""];
[item setTag:[screen screenNumber]];
}
NSUInteger screenNum = [[[currentTrigger info] objectForKey:@"screen"] integerValue];
if (anywhere)screenNum=-1;
[mouseTriggerScreenPopUp setEnabled:!anywhere];
item=[[mouseTriggerScreenPopUp menu]itemWithTag:screenNum];
if (!item){
item=[[mouseTriggerScreenPopUp menu] addItemWithTitle:[NSString stringWithFormat:@"Other (%lu)", (unsigned long)screenNum] action:nil keyEquivalent:@""];
[item setTag:screenNum];
}
[mouseTriggerScreenPopUp selectItem:item];
NSArray *screens=[NSScreen screens];
if (screenNum==1 && [screens count]>1)
screenNum= [[[NSScreen screens] objectAtIndex:1] screenNumber];
if (screenNum<=0)
screenNum=[[screens objectAtIndex:0]screenNumber];
[desktopImageView setScreenNumber:screenNum];
[menuBarAnchorButton setEnabled:screenNum];
BOOL clickEvent=(eventType==NSLeftMouseDown || eventType==NSRightMouseDown || eventType==NSOtherMouseDown);
if (eventType==NSOtherMouseDown)
eventType+= (NSUInteger)[[[currentTrigger info] objectForKey:@"buttonNumber"] integerValue]-3;
NSInteger index=[mouseTriggerTypePopUp indexOfItemWithTag:eventType];
//NSLog(@"type %@ %d %d",mouseTriggerTypePopUp,eventType,index);
[mouseTriggerTypePopUp selectItemAtIndex:index];
[anywhereButton setState:anywhere];
[anywhereButton setHidden:(!otherClick && !modifiersMask)];
NSUInteger anchorMask= [[[currentTrigger info] objectForKey:@"anchorMask"]unsignedIntegerValue];
NSInteger i;
for (i=1;i<9;i++){
[[anchorView viewWithTag:i] setState:(anchorMask&(1<<i))];
[[anchorView viewWithTag:i] setEnabled:!anywhere];
}
for (i=17;i<24;i++){
// NSLog(@"%@ %d %d",[modifiersView viewWithTag:i],modifiersMask,(modifiersMask&(1<<i)));
[[modifiersView viewWithTag:i]setState:(modifiersMask&(1<<i))];
}
NSInteger clickCount=MAX(1,[[[currentTrigger info] objectForKey:@"clickCount"]integerValue]);
[mouseTriggerClickCountField setIntegerValue:clickCount];
[mouseTriggerClickCountStepper setIntegerValue:clickCount];
[mouseTriggerClickCountField setHidden:!clickEvent];
[mouseTriggerClickCountStepper setHidden:!clickEvent];
BOOL delayableEvent=clickEvent || (eventType==NSMouseEntered);
NSNumber *delay=[[currentTrigger info] objectForKey:@"delay"];
[mouseTriggerDelaySwitch setEnabled:delayableEvent];
[mouseTriggerDelaySwitch setState:delay && delayableEvent];
[mouseTriggerDelayField setEnabled: delay && delayableEvent];
[mouseTriggerDelayField setObjectValue:delay];
// IBOutlet NSTextField *mouseTriggerDelayField;
// IBOutlet NSView *modifiersView;
}
}
- (IBAction)setMouseTriggerModifierFlag:(id)sender{
NSUInteger mask=[[[currentTrigger info] objectForKey:@"modifierFlags"] unsignedIntegerValue];
if ([(NSButton *)sender state])
mask |= 1<<[sender tag];
else
mask &= ~(1<<[sender tag]);
//NSLog(@" %d",mask);
[[currentTrigger info] setObject:[NSNumber numberWithInteger:mask] forKey:@"modifierFlags"];
[[QSTriggerCenter sharedInstance] triggerChanged:currentTrigger];
// [triggerTable reloadData];
[self populateInfoFields];
}
// Method that is bound to a button in the .nib on whether or not the top (menu bar) side of the screen should be enabled for triggers (doesn't work on the main screen with the menu bar)
-(BOOL)isMainDisplay {
NSArray *screens = [self screensForScreenTag:[[currentTrigger objectForKey:@"screen"] integerValue]];
if ([screens count] > 1) {
return NO;
}
return [[screens lastObject] isEqual:[NSScreen mainScreen]];
}
- (IBAction) setMouseTriggerValueForSender:(id)sender{
id nextResponder=nil;
if (sender==mouseTriggerClickCountStepper){
[[currentTrigger info] setObject:[sender objectValue] forKey:@"clickCount"];
}else if (sender==mouseTriggerDelaySwitch){
if ([(NSButton *)sender state]){
[[currentTrigger info] setObject:[NSNumber numberWithDouble:0.5] forKey:@"delay"];
nextResponder=mouseTriggerDelayField;
}else{
[[currentTrigger info] removeObjectForKey:@"delay"];
}
}else if (sender==mouseTriggerDelayField){
CGFloat delay=[sender doubleValue];
if (delay>0)
[[currentTrigger info] setObject:[NSNumber numberWithDouble:delay] forKey:@"delay"];
else
[[currentTrigger info] removeObjectForKey:@"delay"];
}else if (sender==mouseTriggerScreenPopUp){
[[currentTrigger info] setObject:[NSNumber numberWithInteger:[[sender selectedItem]tag]] forKey:@"screen"];
// NSLog(@"Screen set to: %x",[[sender selectedItem]tag]);
}else if (sender==anywhereButton){
[[currentTrigger info] setObject:[sender objectValue] forKey:@"anywhere"];
NSLog(@"%@",[sender objectValue]);
if ([sender objectValue]) {
// When the 'anywhere' button is pressed, make the text bold to show that it's activated
[anywhereButton setFont:[NSFont boldSystemFontOfSize:[[anywhereButton font] pointSize]]];
}
}
[[QSTriggerCenter sharedInstance] triggerChanged:currentTrigger];
[self populateInfoFields];
if (nextResponder)
[[sender window]makeFirstResponder:nextResponder];
}
- (IBAction) setMouseTriggerAnchorMask:(id)sender{
NSUInteger mask=[[[currentTrigger info] objectForKey:@"anchorMask"] unsignedIntegerValue];
if ([(NSButton *)sender state])
mask |= 1<<[sender tag];
else
mask &= ~(1<<[sender tag]);
[[currentTrigger info] setObject:[NSNumber numberWithUnsignedInteger:mask] forKey:@"anchorMask"];
[[QSTriggerCenter sharedInstance] triggerChanged:currentTrigger];
// [triggerTable reloadData];
}
- (IBAction) setMouseTriggerType:(id)sender{
NSInteger eventType=[[sender selectedItem]tag];
NSInteger buttonNumber=0;
switch (eventType){
case 25: buttonNumber=3; break;
case 26: eventType=25; buttonNumber=4; break;
case 27: eventType=25; buttonNumber=5; break;
default: break;
}
[[currentTrigger info] setObject:[NSNumber numberWithInteger:eventType] forKey:@"eventType"];
if (buttonNumber)
[[currentTrigger info] setObject:[NSNumber numberWithInteger:buttonNumber] forKey:@"buttonNumber"];
[[QSTriggerCenter sharedInstance] triggerChanged:currentTrigger];
// [triggerTable reloadData];
[self populateInfoFields];
}
- (void)applicationDidChangeScreenParameters:(NSNotification *)aNotification{
//NSLog(@"screen change!");
[anchorArrays removeAllObjects];
[anchorWindows removeAllObjects];
for (QSTrigger *trigger in [[QSTriggerCenter sharedInstance]triggers]){
if ([[trigger objectForKey:@"type"]isEqualToString:@"QSMouseTrigger"]){
[self disableTrigger:trigger];
[self enableTrigger:trigger];
}
}
}
-(id)resolveProxyObject:(id)proxy{
return [self mouseTriggerObject];
}
-(NSArray *)typesForProxyObject:(id)proxy{
return [[self mouseTriggerObject] types];
}
- (id)mouseTriggerObject {
return [[mouseTriggerObject retain] autorelease];
}
- (void)setMouseTriggerObject:(id)newMouseTriggerObject
{
[mouseTriggerObject autorelease];
mouseTriggerObject = [newMouseTriggerObject retain];
}
@end