forked from Sophiestication/CoverSutra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDesktopWindowController.m
375 lines (294 loc) · 10.6 KB
/
DesktopWindowController.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
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
//
// MIT License
//
// Copyright (c) 2006-2023 Sophiestication Software, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
#import "DesktopWindowController.h"
#import "CoverSutra.h"
#import "CoverSutra+Menu.h"
#import "CoverSutra+Shortcuts.h"
#import "ApplicationWindowController.h"
#import "PlayerController.h"
#import "PlaybackController.h"
#import "CoverView.h"
#import "SkinController.h"
#import "Skin.h"
#import "NSArray+Additions.h"
#import "NSString+Additions.h"
#import "NSShadow+Additions.h"
#import "TextField.h"
#import "Utilities.h"
@implementation DesktopWindowController
+ (DesktopWindowController*)desktopWindowController {
return [[self alloc] initWithWindowNibName:@"DesktopWindow"];
}
+ (BOOL)desktopWindowShown {
BOOL showDesktopWindow = [[[NSUserDefaultsController sharedUserDefaultsController]
valueForKeyPath:@"values.desktopWindowShown"] boolValue];
return showDesktopWindow;
}
+ (NSInteger)desktopWindowSize {
NSInteger size = [[[NSUserDefaultsController sharedUserDefaultsController]
valueForKeyPath:@"values.desktopWindowSize"] integerValue];
return size;
}
+ (NSInteger)desktopWindowLevel {
NSString* desktopWindowLevel = [[NSUserDefaultsController sharedUserDefaultsController]
valueForKeyPath:@"values.desktopWindowLevel"];
CGWindowLevelKey windowLevelKey = kCGDesktopIconWindowLevelKey;
if(desktopWindowLevel != nil) {
if(EqualStrings(desktopWindowLevel, @"kCGDesktopWindowLevelKey")) {
// windowLevelKey = kCGDesktopWindowLevelKey;
} else if(EqualStrings(desktopWindowLevel, @"kCGDesktopIconWindowLevelKey")) {
windowLevelKey = kCGDesktopIconWindowLevelKey;
} else if(EqualStrings(desktopWindowLevel, @"kCGNormalWindowLevelKey")) {
windowLevelKey = kCGNormalWindowLevelKey;
} else if(EqualStrings(desktopWindowLevel, @"kCGStatusWindowLevelKey")) {
windowLevelKey = kCGStatusWindowLevelKey;
}
}
return CGWindowLevelForKey(windowLevelKey);
}
- (void)windowDidLoad {
[super windowDidLoad];
NSWindow* window = [self window];
[window setOpaque:NO];
[window setAlphaValue:0.0];
// [window setHasShadow:NO];
[window setBackgroundColor:[NSColor clearColor]];
[window setMovableByWindowBackground:YES];
[window setAllowsConcurrentViewDrawing:YES];
[window setCollectionBehavior:NSWindowCollectionBehaviorCanJoinAllSpaces];
// [window setCollectionBehavior:NSWindowCollectionBehaviorStationary];
// [window setLevel:
// [[self class] desktopWindowLevel]];
if([NSPopover class]) {
[window setLevel:kCGDesktopIconWindowLevel + 1];
} else {
[window setLevel:kCGDesktopWindowLevel];
}
[self bind:@"songDetailsShown"
toObject:[NSUserDefaultsController sharedUserDefaultsController]
withKeyPath:@"values.songDetailsShown"
options:nil];
// [coverView setMenu:[[CoverSutra self] valueForKey:@"actionMenu"]];
[self updateCaseSizeAnimated:NO];
// Setup all textfields
NSShadow* textShadow = [NSShadow desktopTextShadow];
for(TextField* subview in [[window contentView] subviews]) {
if([subview isKindOfClass:[TextField class]]) {
[subview setAlignment:NSCenterTextAlignment];
[subview setDrawsBackground:NO];
[subview setBackgroundColor:[NSColor clearColor]];
[subview setTextColor:[NSColor whiteColor]];
[[subview cell] setTextShadow:textShadow];
[subview setEditable:NO];
[subview setSelectable:NO];
[subview setBordered:NO];
[[subview cell] setUsesSingleLineMode:YES];
[[subview cell] setLineBreakMode:NSLineBreakByTruncatingTail];
}
}
// ...
[textLabel setFont:[NSFont boldSystemFontOfSize:12.0]];
[textLabel bind:NSValueBinding
toObject:[[CoverSutra self] nowPlayingController]
withKeyPath:@"track.albumCoverTitle"
options:0];
[textLabel bind:NSToolTipBinding
toObject:[[CoverSutra self] nowPlayingController]
withKeyPath:@"track.albumCoverTitle"
options:0];
[detailTextLabel setFont:[NSFont boldSystemFontOfSize:11.0]];
[detailTextLabel bind:NSValueBinding
toObject:[[CoverSutra self] nowPlayingController]
withKeyPath:@"track.albumCoverSecondaryTitle"
options:0];
[detailTextLabel bind:NSToolTipBinding
toObject:[[CoverSutra self] nowPlayingController]
withKeyPath:@"track.albumCoverSecondaryTitle"
options:0];
[[CoverSutra self] addObserver:self
forKeyPath:@"skinController.selection.self"
options:0
context:NULL];
[[NSUserDefaultsController sharedUserDefaultsController]
addObserver:self
forKeyPath:@"values.desktopWindowSize"
options:0
context:NULL];
[self setWindowFrameAutosaveName:@"desktopWindow"];
NSRect windowFrame = [window frame];
windowFrame = [window constrainFrameRect:windowFrame toScreen:[window screen]];
[window setFrame:windowFrame display:NO];
[self updateFromUserDefaults];
}
- (void)observeValueForKeyPath:(NSString*)keyPath ofObject:(id)object change:(NSDictionary*)change context:(void*)context {
if([keyPath isEqualToString:@"values.desktopWindowSize"]) {
[self updateCaseSizeAnimated:YES];
}
[self tile];
}
- (void)windowDidMove:(NSNotification*)notification {
[self tile];
}
- (void)windowDidChangeScreen:(NSNotification*)notification {
[self tile];
}
- (IBAction)orderFront:(id)sender {
[self orderFront:sender animate:YES];
[[NSUserDefaultsController sharedUserDefaultsController]
setValue:[NSNumber numberWithBool:YES]
forKeyPath:@"values.desktopWindowShown"];
}
- (IBAction)orderFront:(id)sender animate:(BOOL)animate {
[super orderFront:sender animate:animate];
[self updateCaseSizeAnimated:NO];
[self tile];
}
- (IBAction)orderOut:(id)sender {
[self orderOut:sender animate:YES];
[[NSUserDefaultsController sharedUserDefaultsController]
setValue:[NSNumber numberWithBool:NO]
forKeyPath:@"values.desktopWindowShown"];
[coverView unbind:@"image"];
}
- (void)tile:(id)sender {
[self tile];
}
- (void)tile {
NSWindow* window = [self window];
// [window setBackgroundColor:[NSColor orangeColor]];
// Layout the window
NSInteger desktopWindowSize = [[self class] desktopWindowSize];
NSSize contentSize = desktopWindowSize == 0 ?
NSMakeSize(320.0, 290.0) :
NSMakeSize(300.0, 180.0);
[window setContentSize:contentSize];
// Layout the cover view
Skin* skin = [[[[CoverSutra self] skinController] selectedObjects] firstObject];
NSString* skinSize = desktopWindowSize == 0 ?
LargeSkinSize :
MediumSkinSize;
NSSize caseSize = [[[skin caseDescriptionOfSkinSize:skinSize]
objectForKey:SkinCaseSizeKey]
sizeValue];
NSRect alignmentRect = [[[skin caseDescriptionOfSkinSize:skinSize]
objectForKey:SkinCaseAlignmentRectKey]
rectValue];
NSRect contentRect = [(NSView*)[[self window] contentView] bounds];
NSRect coverViewRect = NSMakeRect(
floorf(NSMidX(contentRect) - caseSize.width * 0.5),
floorf(NSMaxY(contentRect) - caseSize.height),
caseSize.width,
caseSize.height);
coverViewRect.origin.x += alignmentRect.origin.x;
coverViewRect.origin.y += alignmentRect.origin.y;
coverView.frame = coverViewRect;
// Layout the text label
NSSize textLabelSize = [[textLabel cell]
cellSizeForBounds:contentRect];
textLabelSize.height += 2.0; // To prevent text shadow clipping
NSRect textLabelRect = NSMakeRect(
NSMinX(contentRect),
NSMinY(coverViewRect) - textLabelSize.height,
NSWidth(contentRect),
textLabelSize.height);
// Tweak for the large skin
if(desktopWindowSize == 0) {
textLabelRect = NSOffsetRect(textLabelRect, 0.0, 5.0);
}
textLabel.frame = textLabelRect;
// Layout the text label
NSSize detailTextLabelSize = [[detailTextLabel cell]
cellSizeForBounds:contentRect];
detailTextLabelSize.height += 2.0; // To prevent text shadow clipping
NSRect detailTextLabelRect = NSMakeRect(
NSMinX(contentRect),
NSMinY(textLabelRect) - detailTextLabelSize.height,
NSWidth(contentRect),
detailTextLabelSize.height);
detailTextLabel.frame = detailTextLabelRect;
// Show/Hide the text labels if needed
NSRect visibleFrame = [[window screen] frame];
for(id subview in [[window contentView] subviews]) {
if([subview isKindOfClass:[TextField class]]) {
NSRect frame = [(NSView*)subview frame];
frame.origin = [window convertBaseToScreen:frame.origin];
BOOL visible =
(NSMinY(frame) > NSMinY(visibleFrame)) &&
[self songDetailsShown];
[subview setHidden:!visible];
}
}
}
- (BOOL)songDetailsShown {
return _songDetailsShown;
}
- (void)setSongDetailsShown:(BOOL)songDetailsShown {
if(songDetailsShown != _songDetailsShown) {
_songDetailsShown = songDetailsShown;
[self tile];
}
}
- (void)updateFromUserDefaults {
BOOL showDesktopWindow = [[self class] desktopWindowShown];
if(showDesktopWindow) {
[self orderFront:nil animate:YES];
} else {
[[self window] orderOut:nil];
}
}
- (void)updateCaseSizeAnimated:(BOOL)animated {
NSString* keyPath = [[self class] desktopWindowSize] == 0 ?
@"nowPlayingController.largeAlbumCaseImage" :
@"nowPlayingController.mediumAlbumCaseImage";
[coverView
bind:@"image"
toObject:[CoverSutra self]
withKeyPath:keyPath
options:nil];
if([[self class] desktopWindowSize] == 0) {
[textLabel setFont:[NSFont boldSystemFontOfSize:12.0]];
[detailTextLabel setFont:[NSFont boldSystemFontOfSize:11.0]];
} else {
[textLabel setFont:[NSFont boldSystemFontOfSize:11.0]];
[detailTextLabel setFont:[NSFont boldSystemFontOfSize:10.0]];
}
NSWindow* window = [self window];
NSRect windowFrame = [window
constrainFrameRect:[window frame]
toScreen:[window screen]];
[window setFrame:windowFrame display:YES animate:animated];
}
- (void)mouseUp:(NSEvent*)theEvent {
if([theEvent clickCount] == 2) {
unsigned int modifiers = [theEvent modifierFlags];
if(modifiers & NSAlternateKeyMask) {
[[CoverSutra self] showCurrentSong:theEvent];
} else {
[[CoverSutra self] toggleApplicationWindowShown:theEvent];
}
return;
}
[super mouseUp:theEvent];
}
@end