-
Notifications
You must be signed in to change notification settings - Fork 1
/
HUDWindow.m
296 lines (244 loc) · 8.52 KB
/
HUDWindow.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
//
// HUDWindow.m
// HUDWindow
//
// Created by Matt Gemmell on 12/02/2006.
// Copyright 2006 Magic Aubergine. All rights reserved.
//
#import <SproutedInterface/HUDWindow.h>
@implementation HUDWindow
- (id)initWithContentRect:(NSRect)contentRect
styleMask:(NSUInteger)styleMask
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag
{
unsigned int replacementMask = NSBorderlessWindowMask;
if ( styleMask & NSClosableWindowMask ) replacementMask |= NSClosableWindowMask;
if (self = [super initWithContentRect:contentRect
styleMask:replacementMask
backing:bufferingType
defer:flag]) {
[self setBackgroundColor: [NSColor clearColor]];
[self setAlphaValue:1.0];
[self setOpaque:NO];
[self setHasShadow:YES];
[self setMovableByWindowBackground:YES];
forceDisplay = NO;
[self setBackgroundColor:[self sizedHUDBackground]];
if ( styleMask & NSClosableWindowMask )
[self addCloseWidget];
[self setReleasedWhenClosed:YES];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidResize:)
name:NSWindowDidResizeNotification
object:self];
closesOnEvent = NO;
closesOnEscape = NO;
return self;
}
return nil;
}
- (void)awakeFromNib
{
if ( [self styleMask] & NSClosableWindowMask )
[self addCloseWidget];
}
- (void)addCloseWidget
{
NSButton *closeButton = [[NSButton alloc] initWithFrame:NSMakeRect(3.0, [self frame].size.height - 16.0, 13.0, 13.0)];
[[self contentView] addSubview:closeButton];
[closeButton setBezelStyle:NSRoundedBezelStyle];
[closeButton setButtonType:NSMomentaryChangeButton];
[closeButton setBordered:NO];
[closeButton setImage:BundledImageWithName(@"hud_titlebar-close",@"com.sprouted.interface")];
[closeButton setTitle:@""];
[closeButton setImagePosition:NSImageBelow];
[closeButton setTarget:self];
[closeButton setFocusRingType:NSFocusRingTypeNone];
if ( [[self delegate] respondsToSelector:@selector(runClose:)] )
{
[closeButton setTarget:[self delegate]];
[closeButton setAction:@selector(runClose:)];
}
else
{
[closeButton setTarget:self];
[closeButton setAction:@selector(close)];
}
[closeButton release];
}
- (void)dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSWindowDidResizeNotification object:self];
[super dealloc];
}
- (void)windowDidResize:(NSNotification *)aNotification
{
[self setBackgroundColor:[self sizedHUDBackground]];
if (forceDisplay) {
[self display];
}
}
- (void)setFrame:(NSRect)frameRect display:(BOOL)displayFlag animate:(BOOL)animationFlag
{
forceDisplay = YES;
[super setFrame:frameRect display:displayFlag animate:animationFlag];
forceDisplay = NO;
}
- (NSColor *)sizedHUDBackground
{
float alpha = 0.85;
float titlebarHeight = 19.0;
NSImage *bg = [[NSImage alloc] initWithSize:[self frame].size];
[bg lockFocus];
// Make background path
NSRect bgRect = NSMakeRect(0, 0, [bg size].width, [bg size].height - titlebarHeight);
int minX = NSMinX(bgRect);
int midX = NSMidX(bgRect);
int maxX = NSMaxX(bgRect);
int minY = NSMinY(bgRect);
int midY = NSMidY(bgRect);
int maxY = NSMaxY(bgRect);
float radius = 6.0;
NSBezierPath *bgPath = [NSBezierPath bezierPath];
// Bottom edge and bottom-right curve
[bgPath moveToPoint:NSMakePoint(midX, minY)];
[bgPath appendBezierPathWithArcFromPoint:NSMakePoint(maxX, minY)
toPoint:NSMakePoint(maxX, midY)
radius:radius];
[bgPath lineToPoint:NSMakePoint(maxX, maxY)];
[bgPath lineToPoint:NSMakePoint(minX, maxY)];
// Top edge and top-left curve
[bgPath appendBezierPathWithArcFromPoint:NSMakePoint(minX, maxY)
toPoint:NSMakePoint(minX, midY)
radius:radius];
// Left edge and bottom-left curve
[bgPath appendBezierPathWithArcFromPoint:bgRect.origin
toPoint:NSMakePoint(midX, minY)
radius:radius];
[bgPath closePath];
// Composite background color into bg
[[NSColor colorWithCalibratedWhite:0.1 alpha:alpha] set];
[bgPath fill];
// Make titlebar path
NSRect titlebarRect = NSMakeRect(0, [bg size].height - titlebarHeight, [bg size].width, titlebarHeight);
minX = NSMinX(titlebarRect);
midX = NSMidX(titlebarRect);
maxX = NSMaxX(titlebarRect);
minY = NSMinY(titlebarRect);
midY = NSMidY(titlebarRect);
maxY = NSMaxY(titlebarRect);
NSBezierPath *titlePath = [NSBezierPath bezierPath];
// Bottom edge and bottom-right curve
[titlePath moveToPoint:NSMakePoint(minX, minY)];
[titlePath lineToPoint:NSMakePoint(maxX, minY)];
// Right edge and top-right curve
[titlePath appendBezierPathWithArcFromPoint:NSMakePoint(maxX, maxY)
toPoint:NSMakePoint(midX, maxY)
radius:radius];
// Top edge and top-left curve
[titlePath appendBezierPathWithArcFromPoint:NSMakePoint(minX, maxY)
toPoint:NSMakePoint(minX, minY)
radius:radius];
[titlePath closePath];
// Titlebar
NSColor *titlebarColor = [NSColor colorWithCalibratedWhite:0.25 alpha:1.0];
[titlebarColor set];
[titlePath fill];
// Title
NSFont *titleFont = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSRegularControlSize]];
NSMutableParagraphStyle *paraStyle = [[NSMutableParagraphStyle alloc] init];
[paraStyle setParagraphStyle:[NSParagraphStyle defaultParagraphStyle]];
[paraStyle setAlignment:NSCenterTextAlignment];
[paraStyle setLineBreakMode:NSLineBreakByTruncatingTail];
NSMutableDictionary *titleAttrs = [NSMutableDictionary dictionaryWithObjectsAndKeys:
titleFont, NSFontAttributeName,
[NSColor whiteColor], NSForegroundColorAttributeName,
[[paraStyle copy] autorelease], NSParagraphStyleAttributeName,
nil];
NSSize titleSize = [[self title] sizeWithAttributes:titleAttrs];
// We vertically centre the title in the titlbar area, and we also horizontally
// inset the title by 19px, to allow for the 3px space from window's edge to close-widget,
// plus 13px for the close widget itself, plus another 3px space on the other side of
// the widget.
NSRect titleRect = NSInsetRect(titlebarRect, 19.0, (titlebarRect.size.height - titleSize.height) / 2.0);
[[self title] drawInRect:titleRect withAttributes:titleAttrs];
[bg unlockFocus];
return [NSColor colorWithPatternImage:[bg autorelease]];
}
- (void)setTitle:(NSString *)value {
[super setTitle:value];
[self windowDidResize:nil];
}
- (BOOL)canBecomeKeyWindow
{
return YES;
}
#pragma mark -
- (BOOL) closesOnEvent
{
return closesOnEvent;
}
- (void) setClosesOnEvent:(BOOL)closes
{
closesOnEvent = closes;
}
- (BOOL) closesOnEscape
{
return closesOnEscape;
}
- (void) setClosesOnEscape:(BOOL)closes
{
closesOnEscape = closes;
}
#pragma mark -
- (void)keyDown:(NSEvent *)theEvent
{
#ifdef __DEBUG__
NSLog(@"%@ %s",[self className],_cmd);
#endif
if ( closesOnEvent || ( closesOnEscape && [theEvent keyCode] == 53 ) )
if ( [[self delegate] respondsToSelector:@selector(runClose:)] )
[[self delegate] runClose:self];
else
[self close];
else
[super keyDown:theEvent];
}
- (void) mouseDown:(NSEvent*)theEvent
{
#ifdef __DEBUG__
NSLog(@"%@ %s",[self className],_cmd);
#endif
if ( closesOnEvent )
if ( [[self delegate] respondsToSelector:@selector(runClose:)] )
[[self delegate] runClose:self];
else
[self close];
else
[super mouseDown:theEvent];
}
#pragma mark -
- (void)resignKeyWindow
{
#ifdef __DEBUG__
NSLog(@"%@ %s",[self className],_cmd);
#endif
if ( closesOnEvent )
[[self delegate] runClose:self];
else
[super resignKeyWindow];
//[self close];
}
- (void)resignMainWindow
{
#ifdef __DEBUG__
NSLog(@"%@ %s",[self className],_cmd);
#endif
if ( closesOnEvent )
[[self delegate] runClose:self];
else
[super resignMainWindow];
//[self close];
}
@end