-
Notifications
You must be signed in to change notification settings - Fork 1
/
HUDWindow.m
202 lines (166 loc) · 6.86 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
//
// HUDWindow.m
// HUDWindow
//
// Created by Matt Gemmell on 12/02/2006.
// Copyright 2006 Magic Aubergine. All rights reserved.
//
#import "HUDWindow.h"
@implementation HUDWindow
- (id)initWithContentRect:(NSRect)contentRect
styleMask:(unsigned int)styleMask
backing:(NSBackingStoreType)bufferingType
defer:(BOOL)flag
{
if (self = [super initWithContentRect:contentRect
styleMask:NSBorderlessWindowMask
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]];
[self addCloseWidget];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowDidResize:)
name:NSWindowDidResizeNotification
object:self];
return self;
}
return nil;
}
- (void)awakeFromNib
{
[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:[NSImage imageNamed:@"hud_titlebar-close"]];
[closeButton setTitle:@""];
[closeButton setImagePosition:NSImageBelow];
[closeButton setTarget:self];
[closeButton setFocusRingType:NSFocusRingTypeNone];
[closeButton setAction:@selector(close)];
//[closeButton setAction:@selector(orderOut:)];
[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.75;
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:alpha];
[titlebarColor set];
[titlePath fill];
// Title
NSFont *titleFont = [NSFont systemFontOfSize:[NSFont systemFontSizeForControlSize:NSSmallControlSize]];
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];
BOOL tmp = forceDisplay;
forceDisplay = YES;
[self windowDidResize:nil];
forceDisplay = tmp;
}
- (BOOL)canBecomeKeyWindow
{
return NO;
}
@end