-
Notifications
You must be signed in to change notification settings - Fork 5
/
ISWindow.j
579 lines (439 loc) · 19.1 KB
/
ISWindow.j
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
/*
* ISWindow.j
* GithubIssues
*
* Created by Randy Luecke on April 22nd, 2011.
* Copyright 2011, RCLConcepts, LLC All rights reserved.
*/
var WindowBackground = nil,
DetachedWindowBackground = nil,
ContentBackgroundColor = nil,
GreenButtonColor = nil,
GreenButtonDownColor = nil,
RedButtonColor = nil,
RedButtonDownColor = nil,
GreenButtonDisabledColor = nil,
RedButtonDisabledColor = nil,
TextFieldBezelColor = nil;
/*!
used for the awesome translucent windows
*/
@implementation ISWindow : CPWindow
{
BOOL isDetached @accessors;
}
+ (void)initialize
{
WindowBackground = [CPColor colorWithPatternImage:[[CPNinePartImage alloc] initWithImageSlices:[
resourcesImage("windows/popoverwindow-0.png", 65, 53),
resourcesImage("windows/popoverwindow-1.png", 17, 53),
resourcesImage("windows/popoverwindow-2.png", 44, 53),
resourcesImage("windows/popoverwindow-3.png", 65, 48),
resourcesImage("windows/popoverwindow-4.png", 17, 48),
resourcesImage("windows/popoverwindow-5.png", 44, 48),
resourcesImage("windows/popoverwindow-6.png", 65, 62),
resourcesImage("windows/popoverwindow-7.png", 17, 62),
resourcesImage("windows/popoverwindow-8.png", 44, 62)
]]];
DetachedWindowBackground = [CPColor colorWithPatternImage:[[CPNinePartImage alloc] initWithImageSlices:[
resourcesImage("windows/popoverwindow-0-no-spike.png", 65, 53),
resourcesImage("windows/popoverwindow-1.png", 17, 53),
resourcesImage("windows/popoverwindow-2.png", 44, 53),
resourcesImage("windows/popoverwindow-3.png", 65, 48),
resourcesImage("windows/popoverwindow-4.png", 17, 48),
resourcesImage("windows/popoverwindow-5.png", 44, 48),
resourcesImage("windows/popoverwindow-6.png", 65, 62),
resourcesImage("windows/popoverwindow-7.png", 17, 62),
resourcesImage("windows/popoverwindow-8.png", 44, 62)
]]];
GreenButtonColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[
resourcesImage("green-button-0.png", 13, 23),
resourcesImage("green-button-1.png", 2, 23),
resourcesImage("green-button-2.png", 13, 23)
] isVertical:NO]];
RedButtonColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[
resourcesImage("red-button-0.png", 13, 23),
resourcesImage("red-button-1.png", 2, 23),
resourcesImage("red-button-2.png", 13, 23)
] isVertical:NO]];
GreenButtonDownColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[
resourcesImage("green-button-down-0.png", 13, 23),
resourcesImage("green-button-down-1.png", 2, 23),
resourcesImage("green-button-down-2.png", 13, 23)
] isVertical:NO]];
RedButtonDownColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[
resourcesImage("red-button-down-0.png", 13, 23),
resourcesImage("red-button-down-1.png", 2, 23),
resourcesImage("red-button-down-2.png", 13, 23)
] isVertical:NO]];
RedButtonDisabledColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[
resourcesImage("red-button-disabled-0.png", 13, 23),
resourcesImage("red-button-disabled-1.png", 2, 23),
resourcesImage("red-button-disabled-2.png", 13, 23)
] isVertical:NO]];
GreenButtonDisabledColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[
resourcesImage("green-button-disabled-0.png", 13, 23),
resourcesImage("green-button-disabled-1.png", 2, 23),
resourcesImage("green-button-disabled-2.png", 13, 23)
] isVertical:NO]];
BlueButtonColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[
resourcesImage("blue-button-0.png", 13, 23),
resourcesImage("blue-button-1.png", 2, 23),
resourcesImage("blue-button-2.png", 13, 23)
] isVertical:NO]];
BlueButtonDownColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[
resourcesImage("blue-button-down-0.png", 13, 23),
resourcesImage("blue-button-down-1.png", 2, 23),
resourcesImage("blue-button-down-2.png", 13, 23)
] isVertical:NO]];
TextFieldBezelColor = [CPColor colorWithPatternImage:[[CPThreePartImage alloc] initWithImageSlices:[
resourcesImage("textfield-0.png", 18, 43),
resourcesImage("textfield-1.png", 10, 43),
resourcesImage("textfield-2.png", 18, 43)
] isVertical:NO]];
}
- (void)awakeFromCib
{
[self setIsDetached:NO];
}
- (id)initWithContentRect:(CGRect)aRect
{
self = [super initWithContentRect:aRect styleMask:CPBorderlessWindowMask];
return self;
}
- (id)initWithContentRect:(CGRect)aRect styleMask:(unsigned)aMask
{
self = [super initWithContentRect:aRect styleMask:0];
[self setMovableByWindowBackground:YES];
[[self contentView] setBackgroundColor:WindowBackground];
return self;
}
- (void)styleTextField:(CPTextField)aField
{
[aField setValue:TextFieldBezelColor forThemeAttribute:"bezel-color"];
[aField setValue:CGInsetMake(0,0,0,0) forThemeAttribute:"bezel-inset"];
[aField setValue:CPCenterVerticalTextAlignment forThemeAttribute:"vertical-alignment"];
[aField setValue: CGInsetMake(2.0, 7.0, 5.0, 12.0) forThemeAttribute:"content-inset"];
[aField setValue: CGInsetMake(0.0, 7.0, 5.0, 12.0) forThemeAttribute:"content-inset" inState:CPThemeStateEditing|CPThemeStateBezeled];
}
- (void)styleButton:(CPButton)aButton withColor:(CPString)aColor
{
switch (aColor)
{
case "red":
var textColor = [CPColor whiteColor],
shadowColor = [CPColor blackColor],
bezel = RedButtonColor,
bezelDown = RedButtonDownColor,
bezelDisabled = RedButtonColor,
disabledText = textColor,
disabledShadow = shadowColor,
font = [CPFont boldSystemFontOfSize:11];
break;
case "green":
var textColor = [CPColor whiteColor],
shadowColor = [CPColor blackColor],
bezel = GreenButtonColor,
bezelDown = GreenButtonDownColor,
bezelDisabled = GreenButtonDisabledColor,
disabledText = [CPColor colorWithRed:59/255 green:89/255 blue:49/255 alpha:1],
disabledShadow = [CPColor colorWithRed:1 green:1 blue:1 alpha:.2],
font = [CPFont boldSystemFontOfSize:11];
break;
case "blue":
var textColor = [CPColor whiteColor],
shadowColor = [CPColor blackColor],
bezel = BlueButtonColor,
bezelDown = BlueButtonDownColor,
bezelDisabled = BlueButtonColor,
disabledText = [CPColor colorWithRed:59/255 green:89/255 blue:49/255 alpha:1],
disabledShadow = [CPColor colorWithRed:1 green:1 blue:1 alpha:.2],
font = [CPFont boldSystemFontOfSize:11];
break;
}
[aButton setValue:bezel forThemeAttribute:"bezel-color"];
[aButton setValue:bezelDown forThemeAttribute:"bezel-color" inState:CPThemeStateHighlighted];
[aButton setValue:bezelDisabled forThemeAttribute:"bezel-color" inState:CPThemeStateDisabled];
[aButton setValue:textColor forThemeAttribute:"text-color"];
[aButton setValue:shadowColor forThemeAttribute:"text-shadow-color"];
[aButton setValue:disabledText forThemeAttribute:"text-color" inState:CPThemeStateBordered|CPThemeStateDisabled];
[aButton setValue:disabledShadow forThemeAttribute:"text-shadow-color" inState:CPThemeStateDisabled];
[aButton setValue:font forThemeAttribute:"font"];
}
- (void)setIsDetached:(BOOL)aFlag
{
if (isDetached === aFlag)
return;
isDetached = !!aFlag;
if (isDetached)
[[self contentView] setBackgroundColor:DetachedWindowBackground];
else
[[self contentView] setBackgroundColor:WindowBackground];
console.log("blah");
}
- (@action)cancel:(id)sender
{
[self orderOutWithAnimation:sender];
window.setTimeout(function(){
[self _windowAnimatedOut];
},230);
}
@end
/*!
The window for adding a new repo
*/
var SharedNewRepoWindow = nil;
@implementation ISNewRepoWindow : ISWindow
{
@outlet CPTextField repoNameField;
@outlet CPButton submitButton;
@outlet CPButton cancelButton;
@outlet CPTextField errorLabel;
@outlet ISLoadingIndicator loadingIndicator;
@outlet ISRepositoriesController repoController;
CPScrollView scrollview;
CPTableView suggestTable;
CPArrayController suggestedReposController;
}
+ (id)sharedWindow
{
return SharedNewRepoWindow;
}
- (void)awakeFromCib
{
[self setDelegate:self];
[super awakeFromCib];
SharedNewRepoWindow = self;
[self styleTextField:repoNameField];
[self styleButton:submitButton withColor:"green"];
[self styleButton:cancelButton withColor:"red"];
[repoNameField setDelegate:self];
[errorLabel sizeToFit];
}
- (void)showWindow:(id)sender
{
var pt = [sender bounds],
pt = [sender convertRect:pt toView:nil],
origin = CGPointMake(CGRectGetMidX(pt), CGRectGetMidY(pt));
// offset for the spiky thing
origin.x -= 52;
[self setFrameOrigin:origin];
[self setAnimationLocation:"15% 0%"];
[self setAnimationLength:"170"];
[self orderFontWithAnimation:sender];
[self makeKeyWindow];
[submitButton setEnabled:NO];
[self setIsDetached:NO];
}
- (void)controlTextDidChange:(CPNotification)aNote
{
[submitButton setEnabled:[self _isValid]];
var currentFrame = [self frame];
if ([[repoNameField stringValue] length])
{
if (!CGSizeEqualToSize(currentFrame.size, CGSizeMake(381, 365)))
{
var visible = [CPPlatform isBrowser] ? [[self platformWindow] visibleFrame] : [[[CPScreen alloc] init] visibleFrame],
realY = (currentFrame.origin.y + 364 > visible.size.height) ? visible.size.height - 364 : currentFrame.origin.y,
y = MAX(realY,0),
offset = MAX(y - realY, 0); // This will prevent it from ever being too large... unless it's < 364
[self setFrame:CGRectMake(currentFrame.origin.x, y, 381, MIN(364, 364 + offset)) display:YES animate:[self isVisible]];
// FIX ME: If we do this while the window is animating it'll likely result in a terrible frame rate.
// set it up
if (!scrollview)
{
suggestedReposController = [[CPArrayController alloc] init];
[suggestedReposController setAvoidsEmptySelection:NO];
// FIX ME: use HUD style.
scrollview = [[CPScrollView alloc] initWithFrame:CGRectMake(11, 100, 356, 200)];
suggestTable = [[CPTableView alloc] initWithFrame:CGRectMakeZero()];
var col = [[CPTableColumn alloc] initWithIdentifier:"Suggestions"];
[suggestTable addTableColumn:col];
[col bind:CPValueBinding toObject:suggestedReposController withKeyPath:"arrangedObjects.identifier" options:nil];
var dv = [[CPTextField alloc] init];
[dv setValue:[CPColor colorWithHexString:"cccccc"] forThemeAttribute:"text-color"];
[dv setFont:[CPFont systemFontOfSize:11]];
[col setDataView:dv];
[suggestTable setHeaderView:nil];
[suggestTable setCornerView:nil];
[scrollview setDocumentView:suggestTable];
[suggestTable sizeLastColumnToFit];
[suggestTable setTarget:self];
[suggestTable setDoubleAction:@selector(_setRepoAndAdd:)];
[scrollview setHasHorizontalScroller:NO];
[scrollview setAutohidesScrollers:YES];
[suggestTable setAlternatingRowBackgroundColors:[[CPColor colorWithHexString:"242c31"], [CPColor colorWithHexString:"262e33"]]];
[suggestTable setUsesAlternatingRowBackgroundColors:YES];
[suggestTable setSelectionHighlightColor:[CPColor colorWithHexString:"364046"]];
[suggestTable setAllowsEmptySelection:YES];
}
// Wait for the window to finish animating
window.setTimeout(function(){
[[self contentView] addSubview:scrollview];
},200);
}
// Make calls to the Search API
[[ISGithubAPIController sharedController] repoSuggestWithSearchString:[repoNameField stringValue] callback:function(/*CPArray*/results){
[suggestedReposController setContent:results];
}];
}
else
{
[scrollview removeFromSuperview];
if (!CGSizeEqualToSize(currentFrame.size, CGSizeMake(381,161)))
[self setFrame:CGRectMake(currentFrame.origin.x, currentFrame.origin.y, 381,161) display:YES animate:YES];
}
}
- (void)windowDidMove:(CPNotification)aNote
{
[self setIsDetached:YES];
}
- (@action)addRepo:(id)sender
{
if (![self _isValid])
return;
var callback = function(aRepo, aRequest)
{
[loadingIndicator stopAnimating];
[loadingIndicator setHidden:YES];
[repoNameField setEnabled:YES];
if (!aRequest.success())
{
[errorLabel setHidden:NO];
return;
}
[repoController addRepository:aRepo select:YES];
[self orderOutWithAnimation:sender];
// wait for the window to disapear before removing the text.
window.setTimeout(function(){
[repoNameField setStringValue:""];
[self controlTextDidChange:nil];
},230);
}
[[ISGithubAPIController sharedController] loadRepositoryWithIdentifier:[repoNameField stringValue] callback:callback];
[repoNameField setEnabled:NO];
[errorLabel setHidden:YES];
[loadingIndicator setHidden:NO];
[loadingIndicator startAnimating];
}
- (BOOL)_isValid
{
var slashPosition = [repoNameField stringValue].indexOf("/");
enabled = ([[repoNameField stringValue] length] > 2 && slashPosition !== CPNotFound && slashPosition !== 0 && slashPosition !== [[repoNameField stringValue] length] -1);
return enabled;
}
- (void)sendEvent:(CPEvent)anEvent
{
if ([anEvent type] === CPKeyUp)
{
var excludedKeys = [CPUpArrowKeyCode, CPDownArrowKeyCode];
if ([anEvent keyCode] === CPEscapeKeyCode)
[self cancel:self];
else if ([anEvent keyCode] === CPReturnKeyCode)
{
var sender = [self firstResponder];
if (sender === suggestTable)
[self _setRepoAndAdd:sender];
else
[self addRepo:sender];
// prevent the call to super
return;
}
else if ([self firstResponder] !== repoNameField && ![excludedKeys containsObject:[anEvent keyCode]])
[self makeFirstResponder:repoNameField];
else if ([excludedKeys containsObject:[anEvent keyCode]])
[self makeFirstResponder:suggestTable];
}
// FIX ME: this is broked.
[submitButton setEnabled:([self _isValid] || [self firstResponder] === suggestTable)];
[super sendEvent:anEvent];
}
- (void)_setRepoAndAdd:(id)sender
{
var index = [[suggestTable selectedRowIndexes] firstIndex];
[repoNameField setStringValue:[[suggestedReposController contentArray][index] identifier]];
[self addRepo:sender];
}
- (void)_windowAnimatedOut
{
[repoNameField setStringValue:""];
[self controlTextDidChange:nil];
}
@end
/*!
The window for adding a new issue.
*/
@implementation ISNewIssueWindow : ISWindow
{
@outlet CPView containerView;
@outlet CPTextField titleField @accessors(readonly);
@outlet CPPopUpButton repoField @accessors(readonly);
@outlet LPMultiLineTextField bodyField @accessors(readonly);
@outlet CPButton saveButton @accessors(readonly);
@outlet CPButton cancelButton;
@outlet CPButton previewButton;
@outlet CPImageView titleIconView;
@outlet CPImageView repoIconView;
@outlet CPImageView bodyIconView;
@outlet CPImageView divImage;
@outlet CPButton newWindowButton;
}
+ (void)initialize
{
var nineprtimg = [[CPNinePartImage alloc] initWithImageSlices:[
resourcesImage("windows/newissuebg-0.png", 27, 83),
resourcesImage("windows/newissuebg-1.png", 19, 83),
resourcesImage("windows/newissuebg-2.png", 27, 83),
resourcesImage("windows/newissuebg-3.png", 27, 24),
resourcesImage("windows/newissuebg-4.png", 19, 24),
resourcesImage("windows/newissuebg-5.png", 27, 24),
resourcesImage("windows/newissuebg-6.png", 27, 28),
resourcesImage("windows/newissuebg-7.png", 19, 28),
resourcesImage("windows/newissuebg-8.png", 27, 28),
]];
ContentBackgroundColor = [CPColor colorWithPatternImage:nineprtimg];
}
- (void)awakeFromCib
{
[super awakeFromCib];
[self styleButton:saveButton withColor:"green"];
[self styleButton:cancelButton withColor:"red"];
[self styleButton:previewButton withColor:"blue"];
[self styleButton:newWindowButton withColor:"blue"];
[newWindowButton setImage:resourcesImage("newwindow-image.png", 18, 13)];
[containerView setBackgroundColor:ContentBackgroundColor];
[titleField setValue:[CPColor colorWithHexString:"a3b3bf"] forThemeAttribute:"text-color" inState:CPTextFieldStatePlaceholder];
[titleField setValue:[CPColor clearColor] forThemeAttribute:"text-shadow-color" inState:CPTextFieldStatePlaceholder];
[titleField setValue:[CPColor whiteColor] forThemeAttribute:"text-shadow-color"];
[titleField setValue:CGSizeMake(1,1) forThemeAttribute:"text-shadow-offset"];
[titleIconView setImage:resourcesImage("title-icon.png", 13, 13)];
[repoIconView setImage:resourcesImage("repo-icon.png", 13, 13)];
[bodyIconView setImage:resourcesImage("body-icon.png", 13, 13)];
[bodyIconView setImage:resourcesImage("body-icon.png", 13, 13)];
repoPattern = [[CPThreePartImage alloc] initWithImageSlices:[resourcesImage("blank.png", 1,1), resourcesImage("blank.png", 1,1), resourcesImage("popup-arrow-2.png", 22, 24)] isVertical:NO];
[repoField setValue:[CPColor colorWithPatternImage:repoPattern] forThemeAttribute:"bezel-color"];
[repoField setValue:[CPColor colorWithHexString:"4c5b66"] forThemeAttribute:"text-color"];
[bodyField setValue:[CPColor colorWithHexString:"4c5b66"] forThemeAttribute:"text-color"];
[bodyField setEditable:YES];
[bodyField setEnabled:YES];
[saveButton setEnabled:NO];
[self setIsDetached:NO];
}
- (void)setRepos:(CPArray)repos
{
var titles = [],
i = 0,
c = repos.length;
for (; i < c; i++)
titles.push([repos[i] identifier]);
[repoField addItemsWithTitles:titles];
}
- (void)windowDidMove:(CPNotification)aNote
{
[self setIsDetached:YES];
}
- (void)_windowAnimatedOut
{
[self setIsDetached:YES];
}
@end