-
-
Notifications
You must be signed in to change notification settings - Fork 198
Expand file tree
/
Copy pathArgsController.m
More file actions
334 lines (267 loc) · 11.4 KB
/
Copy pathArgsController.m
File metadata and controls
334 lines (267 loc) · 11.4 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
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
/*
Copyright (c) 2003-2025, Sveinbjorn Thordarson <sveinbjorn@sveinbjorn.org>
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may
be used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#import "ArgsController.h"
#import "Common.h"
#define DEFAULT_ARG_VALUE @"-arg"
@interface ArgsController()
{
// Main window outlets
IBOutlet NSButton *argsButton;
IBOutlet NSTextField *interpreterTextField;
// Args window outlets
IBOutlet NSTextField *commandTextField;
IBOutlet NSButton *interpreterArgsRemoveButton;
IBOutlet NSResponderNotifyingTableView *interpreterArgsTableView;
IBOutlet NSButton *scriptArgsRemoveButton;
IBOutlet NSResponderNotifyingTableView *scriptArgsTableView;
IBOutlet NSButton *isDroppableCheckbox;
IBOutlet NSMenu *scriptArgsContextualMenu;
IBOutlet NSMenu *interpreterArgsContextualMenu;
NSMutableArray <NSString *> *interpreterArgs;
NSMutableArray <NSString *> *scriptArgs;
}
@end
@implementation ArgsController
- (instancetype)init {
if (self = [super init]) {
interpreterArgs = [[NSMutableArray alloc] init];
scriptArgs = [[NSMutableArray alloc] init];
}
return self;
}
#pragma mark - Getters/setters
- (void)setInterpreterArgs:(NSArray *)array {
[interpreterArgs removeAllObjects];
[interpreterArgs addObjectsFromArray:array];
[interpreterArgsTableView reloadData];
[self updateGUIStatus];
}
- (void)setScriptArgs:(NSArray *)array {
[scriptArgs removeAllObjects];
[scriptArgs addObjectsFromArray:array];
[scriptArgsTableView reloadData];
[self updateGUIStatus];
}
- (NSArray *)interpreterArgs {
return interpreterArgs;
}
- (NSArray *)scriptArgs {
return scriptArgs;
}
#pragma mark -
- (IBAction)apply:(id)sender {
[[self window] makeFirstResponder:commandTextField];
[NSApp stopModal];
}
- (IBAction)setToDefaults:(id)sender {
[self clearScriptArgs:self];
[self clearInterpreterArgs:self];
}
- (IBAction)show:(id)sender {
NSWindow *parentWindow = [argsButton window];
[self constructCommandString];
[[self window] makeFirstResponder:interpreterArgsTableView];
//open window
[NSApp beginSheet:[self window]
modalForWindow:parentWindow
modalDelegate:nil
didEndSelector:nil
contextInfo:nil];
[NSApp runModalForWindow:[self window]];
[NSApp endSheet:[self window]];
[[self window] orderOut:self];
}
- (void)constructCommandString {
// Interpreter
NSDictionary *defaultAttrs = @{ NSForegroundColorAttributeName: [NSColor blackColor],
NSBackgroundColorAttributeName: [NSColor whiteColor] };
BOOL darkMode = NO;
if (@available(macOS 10.14, *)) {
darkMode = ([[[NSAppearance currentAppearance] name] isEqualToString:NSAppearanceNameDarkAqua]);
}
if (darkMode) {
defaultAttrs = @{ NSForegroundColorAttributeName: [NSColor whiteColor],
NSBackgroundColorAttributeName: [NSColor blackColor] };
[commandTextField setBackgroundColor:[NSColor blackColor]];
}
NSMutableAttributedString *cmdString = [[NSMutableAttributedString alloc] initWithString:[interpreterTextField stringValue] attributes:defaultAttrs];
// Interpreter args
for (NSInteger i = 0; i < [interpreterArgs count]; i++)
{
NSString *a = [NSString stringWithFormat:@" %@", interpreterArgs[i]];
NSMutableDictionary *attrs = [defaultAttrs mutableCopy];
NSColor *highlightColor = [NSColor lightGrayColor];
BOOL dark = NO;
if (@available(macOS 10.14, *)) {
dark = ([[[NSAppearance currentAppearance] name] isEqualToString:NSAppearanceNameDarkAqua]);
}
if (dark) {
highlightColor = [NSColor darkGrayColor];
}
if ([interpreterArgsTableView selectedRow] == i && interpreterArgsTableView == [[self window] firstResponder]) {
attrs[NSBackgroundColorAttributeName] = highlightColor;
}
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:a attributes:attrs];
if (interpreterArgsTableView == [[self window] firstResponder]) {
[attrStr beginEditing];
[attrStr addAttribute:NSFontAttributeName
value:[NSFont boldSystemFontOfSize:11]
range:NSMakeRange(0, [attrStr length])];
[attrStr endEditing];
}
[cmdString appendAttributedString:attrStr];
}
// yourScript
NSAttributedString *scriptString = [[NSAttributedString alloc] initWithString:@" yourScript " attributes:defaultAttrs];
[cmdString appendAttributedString:scriptString];
// Script args
for (NSInteger i = 0; i < [scriptArgs count]; i++)
{
NSString *a = [NSString stringWithFormat:@"%@ ", scriptArgs[i]];
NSMutableDictionary *attrs = [defaultAttrs mutableCopy];
if ([scriptArgsTableView selectedRow] == i && scriptArgsTableView == [[self window] firstResponder]) {
attrs[NSBackgroundColorAttributeName] = [NSColor lightGrayColor];
}
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:a attributes:attrs];
if (scriptArgsTableView == [[self window] firstResponder]) {
[attrStr beginEditing];
[attrStr addAttribute:NSFontAttributeName
value:[NSFont boldSystemFontOfSize:11]
range:NSMakeRange(0, [attrStr length])];
[attrStr endEditing];
}
[cmdString appendAttributedString:attrStr];
}
// File args
if ([isDroppableCheckbox state] == NSControlStateValueOn) {
NSAttributedString *attrStr = [[NSAttributedString alloc] initWithString:@" [files ...]" attributes:defaultAttrs];
[cmdString appendAttributedString:attrStr];
}
[commandTextField setAttributedStringValue:cmdString];
}
- (void)updateGUIStatus {
[interpreterArgsRemoveButton setEnabled:
([interpreterArgsTableView selectedRow] != -1) &&
[[self window] firstResponder] == interpreterArgsTableView
];
[scriptArgsRemoveButton setEnabled:
([scriptArgsTableView selectedRow] != -1) &&
[[self window] firstResponder] == scriptArgsTableView
];
[self updateArgsButtonTitle];
[self constructCommandString];
}
- (void)updateArgsButtonTitle {
NSInteger numArgs = [interpreterArgs count] + [scriptArgs count];
if (numArgs) {
[argsButton setTitle:[NSString stringWithFormat:@"Args (%ld)", (long)numArgs]];
} else {
[argsButton setTitle:@"Args"];
}
}
#pragma mark - Manipulating list contents
- (IBAction)addInterpreterArg:(id)sender {
[interpreterArgs addObject:DEFAULT_ARG_VALUE];
[interpreterArgsTableView reloadData];
[interpreterArgsTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[interpreterArgs count] - 1] byExtendingSelection:NO];
[[self window] makeFirstResponder:interpreterArgsTableView];
[self updateGUIStatus];
}
- (IBAction)addScriptArg:(id)sender {
[scriptArgs addObject:DEFAULT_ARG_VALUE];
[scriptArgsTableView reloadData];
[scriptArgsTableView selectRowIndexes:[NSIndexSet indexSetWithIndex:[scriptArgs count] - 1] byExtendingSelection:NO];
[[self window] makeFirstResponder:scriptArgsTableView];
[self updateGUIStatus];
}
- (IBAction)clearInterpreterArgs:(id)sender {
[interpreterArgs removeAllObjects];
[interpreterArgsTableView reloadData];
[self updateGUIStatus];
}
- (IBAction)clearScriptArgs:(id)sender {
[scriptArgs removeAllObjects];
[scriptArgsTableView reloadData];
[self updateGUIStatus];
}
- (IBAction)removeListItem:(id)sender {
NSMutableArray <NSString *> *args;
id firstResponder = [[self window] firstResponder];
if (firstResponder == scriptArgsTableView) {
args = scriptArgs;
} else if (firstResponder == interpreterArgsTableView) {
args = interpreterArgs;
} else {
return;
}
NSTableView *tableView = firstResponder;
NSInteger selectedRow = [tableView selectedRow];
if (selectedRow == -1 || [args count] == 0) {
return;
}
[args removeObjectAtIndex:[tableView selectedRow]];
if ([tableView numberOfRows] == 0) {
return;
}
NSInteger rowToSelect = selectedRow - 1;
[tableView reloadData];
[tableView selectRowIndexes:[NSIndexSet indexSetWithIndex:rowToSelect] byExtendingSelection:NO];
[[self window] makeFirstResponder:tableView];
[self updateGUIStatus];
}
#pragma mark - NSTableViewDelegate
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
NSMutableArray *args = (aTableView == interpreterArgsTableView) ? interpreterArgs : scriptArgs;
return [args count];
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
NSMutableArray *args = (aTableView == interpreterArgsTableView) ? interpreterArgs : scriptArgs;
return args[rowIndex];
}
- (void)tableView:(NSTableView *)aTableView setObjectValue:anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
NSMutableArray *args = (aTableView == interpreterArgsTableView) ? interpreterArgs : scriptArgs;
args[rowIndex] = anObject;
[self constructCommandString];
}
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification {
[self updateGUIStatus];
}
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
return 20;
}
- (void)tableViewDidBecomeFirstResponder:(id)sender {
[self updateGUIStatus];
}
#pragma mark -
- (BOOL)validateMenuItem:(NSMenuItem *)anItem {
if ([anItem menu] == scriptArgsContextualMenu && [[anItem title] isEqualToString:@"Remove Entry"] && [scriptArgsTableView selectedRow] == -1) {
return NO;
}
if ([anItem menu] == interpreterArgsContextualMenu && [[anItem title] isEqualToString:@"Remove Entry"] && [interpreterArgsTableView selectedRow] == -1) {
return NO;
}
return YES;
}
@end