-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppController.m
executable file
·223 lines (183 loc) · 5.96 KB
/
AppController.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
//
// AppController.m
// App Pie
//
// Created by Giacomo Trezzi on 17/04/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "AppController.h"
#import <Carbon/Carbon.h>
@implementation AppController
@synthesize prefWindow, theItem, theMenu, rWin, isWindowOn, app, bundleController, menuDown, menuLeft, menuRight, leftMouseDown,rightMouseDown;
/*
Quando l'os mi manda il sengale che un bottone del mouse è premuto agisco!
*/
OSStatus globalMouseDown(EventHandlerCallRef nextHandler, EventRef theEvent, AppController *self)
{
NSEvent *anEvent = [NSEvent eventWithEventRef:theEvent];
NSEventType type = [anEvent type];
//is it a key up event?
if(type == NSLeftMouseDown){
self.leftMouseDown = YES;
}
if(type == NSRightMouseDown){
self.rightMouseDown = YES;
}
if(type == NSOtherMouseDown){
[self loadTransparentWindow];
}
if(self.leftMouseDown == YES && self.rightMouseDown == YES){
[self loadTransparentWindow];
self.leftMouseDown = NO;
self.rightMouseDown = NO;
}
return CallNextEventHandler(nextHandler, theEvent);
}
/*
Quando l'os mi manda il sengale che un bottone del mouse è stato rilasciato setto i trigger to false
*/
OSStatus globalMouseUp(EventHandlerCallRef nextHandler, EventRef theEvent, AppController *self)
{
NSEvent *anEvent = [NSEvent eventWithEventRef:theEvent];
NSEventType type = [anEvent type];
//is it a key up event?
if(type == NSLeftMouseUp){
self.leftMouseDown = NO;
}
if(type == NSRightMouseUp){
self.rightMouseDown = NO;
}
return CallNextEventHandler(nextHandler, theEvent);
}
/*
Quando l'os mi manda il sengale che la scorciatoia è stata premuta chiamo il methodo loadRoundWindow:
*/
OSStatus myHotKeyHandler(EventHandlerCallRef nextHandler,EventRef theEvent,AppController *self)
{
[self loadTransparentWindow];
return noErr;
}
/*
Carico l'icona nella status bar e ci attacco il menu
*/
- (void)activateStatusMenu{
NSStatusBar *bar = [NSStatusBar systemStatusBar];
theItem = [bar statusItemWithLength:NSVariableStatusItemLength];
[theItem retain];
NSImage *logo;
logo = [[NSImage alloc] initWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"MenuBarIcon" ofType:@"png"]];
[theItem setImage:logo];
//[theItem setTitle:@"AP"];
[theItem setHighlightMode:YES];
[theItem setMenu:theMenu];
SEL loadTransp = @selector(loadTransparentWindow);
[theItem setAction:loadTransp];
}
/*
Porto la mia applicazione di fronte alle altre
carico RoundWindow:
- se è visibile allora la chiudo e ripulisco i menu
- se non è visibile carico i menu in basso (se l'applicazione attiva ha il suo bundle) a sinistra e a destra
*/
-(void)loadTransparentWindow{
if([rWin isVisible]){
[rWin orderOut:self];
if(![menuDown isHidden]){
[menuDown releaseMenu];
}
[menuLeft releaseMenu];
[menuRight releaseMenu];
}
else {
//menuDown.bundleController = nil;
NSString * activeAppName= [[[NSWorkspace sharedWorkspace] activeApplication] objectForKey:@"NSApplicationName"];
[menuDown configureMenuWithTitle:activeAppName andActive:YES];
PieBundle * bundle = [bundleController loadBundle:activeAppName];
if(bundle){
if([menuDown isHidden]){
menuDown.bundleController = [bundleController loadBundle:activeAppName];
[menuDown setHidden:NO];
}
else {
menuDown.bundleController = [bundleController loadBundle:activeAppName];
}
}
else {
[menuDown setHidden:YES];
}
[menuLeft configureMenuWithTitle:@"Common" andActive:YES];
menuLeft.bundleController = [bundleController loadBundle:@"Common"];
[menuRight configureMenuWithTitle:@"Social" andActive:YES];
menuRight.bundleController = [bundleController loadBundle:@"Social"];
if([menuDown.thisFloatMenu alphaValue]<1.0){
[menuDown.thisFloatMenu setAlphaValue:1.0];
}
if([menuLeft.thisFloatMenu alphaValue]<1.0){
[menuLeft.thisFloatMenu setAlphaValue:1.0];
}
if([menuRight.thisFloatMenu alphaValue]<1.0){
[menuRight.thisFloatMenu setAlphaValue:1.0];
}
[rWin loadWindow];
[rWin makeKeyWindow];
}
}
/*
Porto la mia applicazione di fronte alle altre
carico la finestra delle preferenze
*/
-(IBAction)loadPrefWindow:(id)sender{
[prefWindow makeKeyAndOrderFront:self];
[app activateIgnoringOtherApps:YES];
[prefWindow setAcceptsMouseMovedEvents:YES];
}
-(void)awakeFromNib{
[self activateStatusMenu];
isWindowOn = NO;
[self attachEventHandlers];
bundleController = [[BundleLoader alloc] init];
}
/*
Con un po' di carbon è possibile ricevere eventi globali dal sistema. dai tasti premuti sul mouse ai bottoni della tastiera
*/
- (void)attachEventHandlers
{
/*
Creo un evento per quando una combinazione di tasti viene premuta
in questo caso cmd+<
*/
EventHotKeyRef myHotKeyRef;
EventHotKeyID myHotKeyID;
EventTypeSpec eventType;
eventType.eventClass=kEventClassKeyboard;
eventType.eventKind=kEventHotKeyPressed;
InstallApplicationEventHandler(&myHotKeyHandler,1,&eventType,self,NULL);
myHotKeyID.signature ='mhk1';
myHotKeyID.id=1;
RegisterEventHotKey(50, cmdKey, myHotKeyID, GetApplicationEventTarget(), 0, &myHotKeyRef);
/*
Creo un evento per quando il mouse viene pigiato
*/
EventTypeSpec eventTypeMD;
eventTypeMD.eventClass = kEventClassMouse;
eventTypeMD.eventKind = kEventMouseDown;
EventHandlerUPP handlerFunctionMD = NewEventHandlerUPP(globalMouseDown);
OSStatus errMD = InstallEventHandler(GetEventMonitorTarget(), handlerFunctionMD, 1, &eventTypeMD, self, NULL);
if( errMD )
{
NSLog(@"Error registering mouse handler...%d", errMD);
}
/*
e quando viene rilasciato
*/
EventTypeSpec eventTypeMU;
eventTypeMU.eventClass = kEventClassMouse;
eventTypeMU.eventKind = kEventMouseUp;
EventHandlerUPP handlerFunctionMU = NewEventHandlerUPP(globalMouseUp);
OSStatus errMU = InstallEventHandler(GetEventMonitorTarget(), handlerFunctionMU, 1, &eventTypeMU, self, NULL);
if( errMU )
{
NSLog(@"Error registering mouse handler...%d", errMU);
}
}
@end