Repository navigation
Expand file tree
/
Copy pathMLSwitcher2Controller.m
More file actions
182 lines (148 loc) · 4.88 KB
/
Copy pathMLSwitcher2Controller.m
File metadata and controls
182 lines (148 loc) · 4.88 KB
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
//
// MLSwitcher2Controller.m
// MLSwitcher2
//
// Created by Oleksandr Tymoshenko on 10-07-31.
// Copyright 2010 Bluezbox Software. All rights reserved.
//
#import "MLSwitcher2Controller.h"
#import "LayoutManager.h"
#import "Layout.h"
int modifiersMasks[4] = { NSControlKeyMask, NSAlternateKeyMask,
NSCommandKeyMask, NSShiftKeyMask };
@implementation MLSwitcher2Controller
- (void)awakeFromNib
{
sourcePlaceholders = 1;
currentLayouts = nil;
modifiersSettings = [[NSMutableDictionary alloc] init];
allSubviews = [[NSMutableArray alloc] init];
[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(escapeKeysPressed)
name:@"notifyEscapeKeyPressedFromControllerWindow"
object:nil];
[comboesButton setTarget:self];
[comboesButton setAction:@selector(setCombo:)];
}
- (BOOL)shortcutRecorder:(SRRecorderControl *)aRecorder
isKeyCode:(NSInteger)keyCode
andFlagsTaken:(NSUInteger)flags
reason:(NSString **)aReason
{
return NO;
}
- (void)shortcutRecorder:(SRRecorderControl *)aRecorder
keyComboDidChange:(KeyCombo)newKeyCombo
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
for (NSString *sourceId in [modifiersSettings allKeys]) {
SRRecorderControl *recorder = [modifiersSettings objectForKey:sourceId];
if (recorder != aRecorder)
continue;
NSInteger combo = newKeyCombo.flags | newKeyCombo.code;
[defaults setInteger:combo forKey:sourceId];
}
[defaults synchronize];
[[LayoutManager sharedInstance] reloadLayouts];
}
- (void) refreshSources
{
NSArray *layouts = [[LayoutManager sharedInstance] layouts];
BOOL changed = NO;
// check if layouts has changed
if ([layouts count] == [modifiersSettings count]) {
for (NSString *i in [modifiersSettings allKeys]) {
BOOL haveIt = NO;
for (Layout *l in layouts) {
if ([l.sourceId isEqualToString:i]) {
haveIt = YES;
break;
}
}
if (!haveIt) {
changed = YES;
break;
}
}
}
else {
changed = YES;
}
if (!changed)
return;
[modifiersSettings removeAllObjects];
for (NSView *v in allSubviews) {
[v removeFromSuperview];
}
[allSubviews removeAllObjects];
float originX;
float originY;
int height = 24;
int layoutsTotal = [layouts count];
float delta = (layoutsTotal - sourcePlaceholders) * height;
sourcePlaceholders = layoutsTotal;
if (delta) {
NSRect frame = [window frame];
frame.origin.y -= delta;
frame.size.height += delta;
[window setFrame:frame
display:YES
animate:NO];
}
originX = 0;
originY = 5;
float width = sourcesView.frame.size.width;
int i = 0;
for (Layout *l in [layouts reverseObjectEnumerator]) {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int comboInt = [defaults integerForKey:l.sourceId];
KeyCombo combo;
int y = originY + i*height;
NSImageView *view =
[[NSImageView alloc] initWithFrame:NSMakeRect( originX, y, 16, 16)];
[view setImage:l.image];
NSTextField *text = [[NSTextField alloc]
initWithFrame:NSMakeRect( originX + 20, y - 4, 180, 20)];
[text setEditable:NO];
[text setStringValue:l.name];
[text setBordered:NO];
[text setDrawsBackground:NO];
SRRecorderControl *recorder = [[SRRecorderControl alloc] initWithFrame:NSMakeRect(originX + width - 100 , y - 2, 100, 20)];
[recorder setDelegate:self];
if (comboInt) {
combo.flags = comboInt & 0xffff0000;
combo.code = comboInt & 0x0000ffff;
[recorder setKeyCombo:combo];
}
[sourcesView addSubview:view];
[sourcesView addSubview:text];
[sourcesView addSubview:recorder];
[view release];
[text release];
[recorder release];
[allSubviews addObject:view];
[allSubviews addObject:text];
[allSubviews addObject:recorder];
[modifiersSettings setObject:recorder forKey:l.sourceId];
i++;
}
}
- (void) preferences
{
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
if (![window isVisible]) {
[[LayoutManager sharedInstance] reloadLayouts];
[self refreshSources];
[window makeKeyAndOrderFront:self];
}
}
- (void) escapeKeysPressed
{
[window close];
}
- (void)setCombo:(id)sender
{
[[LayoutManager sharedInstance] setMaskIndex:[comboesButton indexOfSelectedItem]];
}
@end