/
AddCompletionController.m
201 lines (165 loc) · 7.11 KB
/
AddCompletionController.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
#import "AddCompletionController.h"
#import "KeystoneController.h"
#import "NSWindow+EndEditingGracefully.h"
#import "ActionView.h"
@interface ComBelkadanKeystone_AddCompletionController ()
- (void)setUpWarning;
@end
@implementation ComBelkadanKeystone_AddCompletionController
- (id)initWithName:(NSString *)name URL:(NSString *)completionURL {
NSString *keyword;
NSString *host = [[NSURL URLWithString:[completionURL stringByReplacingOccurrencesOfString:ComBelkadanKeystone_kSubstitutionMarker withString:@""]] host];
if (host) {
// use domain name
NSRange searchRange = NSMakeRange(0, [host length]-1); // omit trailing . for absolute addresses
NSRange finalDotRange = [host rangeOfString:@"." options:NSBackwardsSearch|NSLiteralSearch range:searchRange];
if (finalDotRange.location != NSNotFound) {
searchRange.length = finalDotRange.location;
NSRange penultimateDotRange = [host rangeOfString:@"." options:NSBackwardsSearch|NSLiteralSearch range:searchRange];
if (penultimateDotRange.location != NSNotFound) {
keyword = [host substringWithRange:NSMakeRange(penultimateDotRange.location+1, finalDotRange.location-penultimateDotRange.location-1)];
} else {
keyword = [host substringToIndex:finalDotRange.location];
}
} else if ([host characterAtIndex:searchRange.length] == '.') {
keyword = [host substringToIndex:searchRange.length];
} else {
keyword = host;
}
} else {
// use page title
NSRange spaceRange = [name rangeOfString:@" "];
if (spaceRange.location != NSNotFound) {
keyword = [name substringToIndex:spaceRange.location];
} else {
keyword = name;
}
}
return [self initWithName:name keyword:[keyword lowercaseString] URL:completionURL];
}
- (id)initWithName:(NSString *)name keyword:(NSString *)keyword URL:(NSString *)completionURL {
self = [super initWithWindowNibName:@"AddCompletion"];
if (self) {
newCompletion = [[ComBelkadanKeystone_QueryCompletionItem alloc] initWithName:name keyword:keyword shortcutURL:completionURL];
}
return self;
}
- (void)dealloc {
[newCompletion release];
[tooltipFloatWindow release];
[super dealloc];
}
#pragma mark -
- (id)icon {
NSImage *icon = [[NSImage imageNamed:@"NSApplicationIcon"] copy]; // FIXME: why doesn't Safari like my icon?
[icon setSize:NSMakeSize(16, 16)];
return [icon autorelease];
}
- (id)label {
return [[self window] title];
}
- (void)displaySheetInWindow:(NSWindow *)window {
if (window == nil) window = [NSApp mainWindow];
[NSApp beginSheet:[self window] modalForWindow:window modalDelegate:[ComBelkadanKeystone_Controller sharedInstance] didEndSelector:@selector(newCompletionSheetDidEnd:returnCode:controller:) contextInfo:(void *)self];
}
- (void)displaySheet {
[self displaySheetInWindow:nil];
}
#pragma mark -
- (NSArray *)existingCompletions {
if (delegate) {
return [delegate completionsForKeyword:newCompletion.keyword ?: @""];
} else {
return [NSArray array];
}
}
+ (NSSet *)keyPathsForValuesAffectingExistingCompletions {
return [NSSet setWithObject:@"newCompletion.keyword"];
}
- (NSString *)existingCompletionWarning {
NSBundle *bundle = [NSBundle bundleForClass:[self class]];
NSArray *existingCompletions = [self existingCompletions];
switch ([existingCompletions count]) {
case 0:
return nil;
case 1: {
ComBelkadanKeystone_QueryCompletionItem *completion = [existingCompletions objectAtIndex:0];
NSString *warning = newCompletion.keyword ? @"'%@' is already using this keyword."
: @"There is already a default search shortcut: '%@'";
return [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(warning, @"Localizable", bundle, @"Existing completion warnings"), completion.name];
}
case 2: {
ComBelkadanKeystone_QueryCompletionItem *first = [existingCompletions objectAtIndex:0];
ComBelkadanKeystone_QueryCompletionItem *second = [existingCompletions objectAtIndex:1];
NSString *warning = newCompletion.keyword ? @"This keyword is already in use by '%@' and '%@'."
: @"There are already default search shortcuts: '%@' and '%@'.";
return [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(warning, @"Localizable", bundle, @"Existing completion warnings"), first.name, second.name];
}
default: {
NSString *warning = newCompletion.keyword ? @"This keyword is already in use. ('%@')"
: @"There are already default search shortcuts: '%@'";
return [NSString stringWithFormat:NSLocalizedStringFromTableInBundle(warning, @"Localizable", bundle, @"Existing completion warnings"), [[existingCompletions valueForKey:@"name"] componentsJoinedByString:@"', '"]];
}
}
}
+ (NSSet *)keyPathsForValuesAffectingExistingCompletionWarning {
return [NSSet setWithObject:@"existingCompletions"];
}
- (void)controlTextDidChange:(NSNotification *)obj {
[tooltipFloatWindow orderOut:nil];
}
- (IBAction)showWarning:(NSView *)sender
{
static const NSSize kMinSize = { .width = 220, .height = 18 };
if (!tooltipFloatWindow) {
[self setUpWarning];
NSAssert(tooltipFloatWindow, @"The fake tooltip panel should exist now.");
}
NSPoint topLeftCorner = NSMakePoint(0, ([sender isFlipped] ? 0 : NSMaxY([sender bounds])));
topLeftCorner = [[sender window] convertBaseToScreen:[sender convertPointToBase:topLeftCorner]];
[warningView setString:@""];
[warningView setFrameSize:kMinSize];
[warningView setString:[self existingCompletionWarning]];
[warningView sizeToFit];
NSRect frame;
frame.size = [[warningView superview] convertSizeToBase:[warningView frame].size];
frame.origin.x = topLeftCorner.x;
frame.origin.y = topLeftCorner.y - frame.size.height;
[tooltipFloatWindow setFrame:frame display:YES];
[tooltipFloatWindow makeKeyAndOrderFront:sender];
}
- (void)setUpWarning
{
NSScrollView *scrollView = [warningView enclosingScrollView];
NSRect initialFrame = {
.origin = NSZeroPoint,
.size = [scrollView bounds].size
};
tooltipFloatWindow = [[NSPanel alloc] initWithContentRect:initialFrame styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:YES];
[tooltipFloatWindow setFloatingPanel:YES];
[tooltipFloatWindow setLevel:NSFloatingWindowLevel+5];
[tooltipFloatWindow setHasShadow:YES];
[[tooltipFloatWindow contentView] addSubview:scrollView];
ActionView *actionView = [[ActionView alloc] initWithFrame:initialFrame];
[actionView setTarget:tooltipFloatWindow];
[actionView setAction:@selector(orderOut:)];
[actionView setAutoresizingMask:(NSViewWidthSizable|NSViewHeightSizable)];
[[tooltipFloatWindow contentView] addSubview:actionView positioned:NSWindowAbove relativeTo:nil];
[actionView release];
[warningView setTextContainerInset:NSMakeSize(0, 2)];
}
#pragma mark -
- (IBAction)close:(id)sender {
if (!newCompletion.keyword) newCompletion.keyword = @"";
if (!newCompletion.name) newCompletion.name = @"";
[tooltipFloatWindow orderOut:self];
NSWindow *window = [self window];
[window endEditingGracefully];
if ([window isSheet]) {
[NSApp endSheet:window returnCode:[sender tag]];
}
[window orderOut:self];
}
@synthesize newCompletion, delegate;
@dynamic window; // just to stop warnings! we have one inherited from NSWindowController
@end