forked from nongio-zz/rik.theme
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Rik+WindowDecoration.m
218 lines (172 loc) · 6.75 KB
/
Rik+WindowDecoration.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
#import "Rik.h"
@interface Rik(RikWindowDecoration)
@end
#define TITLE_HEIGHT 24.0
#define RESIZE_HEIGHT 9.0
#define WINDOW_CORNER_RADIUS 6
@implementation Rik(RikWindowDecoration)
static NSDictionary *titleTextAttributes[3] = {nil, nil, nil};
- (float) resizebarHeight {
return RESIZE_HEIGHT;
}
- (float) titlebarHeight {
return TITLE_HEIGHT;
}
- (void) drawWindowBackground: (NSRect) frame view: (NSView*) view
{
NSWindow * w = [view window];
NSColor* backgroundColor = [[view window] backgroundColor];
NSColor *borderColor = [Rik controlStrokeColor];
//i want to draw over the resize bar
frame.origin.x -= 1;
frame.size.width += 1;
NSBezierPath* backgroundPath = [NSBezierPath bezierPath];
NSRect backgroundRect;
if([w styleMask] & NSResizableWindowMask)
{
frame.size.height += RESIZE_HEIGHT;
frame.origin.y -= RESIZE_HEIGHT;
backgroundRect = NSOffsetRect(frame, 0.5, 0.5);
NSRect backgroundInnerRect = NSInsetRect( backgroundRect,
WINDOW_CORNER_RADIUS,
WINDOW_CORNER_RADIUS);
[backgroundPath setLineWidth: 1];
[backgroundPath appendBezierPathWithArcWithCenter: NSMakePoint(NSMinX(backgroundInnerRect), NSMinY(backgroundInnerRect))
radius: WINDOW_CORNER_RADIUS startAngle: 180 endAngle: 270];
[backgroundPath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(backgroundInnerRect), NSMinY(backgroundInnerRect))
radius: WINDOW_CORNER_RADIUS startAngle: 270 endAngle: 360];
[backgroundPath lineToPoint: NSMakePoint(NSMaxX(backgroundRect), NSMaxY(backgroundRect))];
[backgroundPath lineToPoint: NSMakePoint(NSMinX(backgroundRect), NSMaxY(backgroundRect))];
[backgroundPath closePath];
}
else
{
backgroundRect = NSOffsetRect(frame, 0.5, 0.5);
[backgroundPath appendBezierPathWithRect: backgroundRect];
}
[backgroundColor setFill];
[borderColor setStroke];
[backgroundPath fill];
[backgroundPath stroke];
}
- (void) drawWindowBorder: (NSRect)rect
withFrame: (NSRect)frame
forStyleMask: (unsigned int)styleMask
state: (int)inputState
andTitle: (NSString*)title
{
if (styleMask & (NSTitledWindowMask | NSClosableWindowMask
| NSMiniaturizableWindowMask))
{
NSRect titleRect;
titleRect = NSMakeRect(0.0, frame.size.height - TITLE_HEIGHT,
frame.size.width, TITLE_HEIGHT);
if (NSIntersectsRect(rect, titleRect))
[self drawtitleRect: titleRect
forStyleMask: styleMask
state: inputState
andTitle: title];
}
}
- (void) drawtitleRect: (NSRect)titleRect
forStyleMask: (unsigned int)styleMask
state: (int)inputState
andTitle: (NSString*)title
{
if (!titleTextAttributes[0])
{
[self prepareTitleTextAttributes];
}
NSRect workRect;
workRect = titleRect;
workRect.origin.x -= 0.5;
workRect.origin.y -= 0.5;
[self drawTitleBarBackground:workRect];
// Draw the title.
if (styleMask & NSTitledWindowMask)
{
NSSize titleSize;
if (styleMask & NSMiniaturizableWindowMask)
{
workRect.origin.x += 17;
workRect.size.width -= 17;
}
if (styleMask & NSClosableWindowMask)
{
workRect.size.width -= 17;
}
titleSize = [title sizeWithAttributes: titleTextAttributes[inputState]];
if (titleSize.width <= workRect.size.width)
workRect.origin.x = NSMidX(workRect) - titleSize.width / 2;
workRect.origin.y = NSMidY(workRect) - titleSize.height / 2;
workRect.size.height = titleSize.height;
[title drawInRect: workRect
withAttributes: titleTextAttributes[inputState]];
}
}
- (void) drawTitleBarBackground: (NSRect)rect {
NSColor* borderColor = [Rik controlStrokeColor];
NSGradient* gradient = [self _windowTitlebarGradient];
CGFloat titleBarCornerRadius = WINDOW_CORNER_RADIUS;
NSRect titleRect = rect;
titleRect.origin.x += 1;
titleRect.size.width -= 1;
NSRectFillUsingOperation(titleRect, NSCompositeClear);
NSRect titleinner = NSInsetRect(titleRect, titleBarCornerRadius, titleBarCornerRadius);
NSBezierPath* titleBarPath = [NSBezierPath bezierPath];
[titleBarPath moveToPoint: NSMakePoint(NSMinX(titleRect), NSMinY(titleRect))];
[titleBarPath lineToPoint: NSMakePoint(NSMaxX(titleRect), NSMinY(titleRect))];
[titleBarPath appendBezierPathWithArcWithCenter: NSMakePoint(NSMaxX(titleinner), NSMaxY(titleinner))
radius: titleBarCornerRadius
startAngle: 0
endAngle: 90];
[titleBarPath appendBezierPathWithArcWithCenter: NSMakePoint(NSMinX(titleinner), NSMaxY(titleinner))
radius: titleBarCornerRadius
startAngle: 90
endAngle: 180];
[titleBarPath closePath];
NSBezierPath* linePath = [NSBezierPath bezierPath];
[linePath moveToPoint: NSMakePoint(NSMinX(titleRect), NSMinY(titleRect)+1)];
[linePath lineToPoint: NSMakePoint(NSMaxX(titleRect), NSMinY(titleRect)+1)];
[borderColor setStroke];
[gradient drawInBezierPath: titleBarPath angle: -90];
[titleBarPath setLineWidth: 1];
[titleBarPath stroke];
[linePath setLineWidth: 1];
[linePath stroke];
}
- (void) drawResizeBarRect: (NSRect)resizeBarRect
{
//I don't want to draw the resize bar
//TODO change the mouse cursor on hover
}
- (void)prepareTitleTextAttributes
{
NSMutableParagraphStyle *p;
NSColor *keyColor, *normalColor, *mainColor;
p = [[NSParagraphStyle defaultParagraphStyle] mutableCopy];
[p setLineBreakMode: NSLineBreakByClipping];
normalColor = [NSColor colorWithCalibratedRed: 0.1 green: 0.1 blue: 0.1 alpha: 1];
mainColor = normalColor;
keyColor = normalColor;
titleTextAttributes[0] = [[NSMutableDictionary alloc]
initWithObjectsAndKeys:
[NSFont titleBarFontOfSize: 0], NSFontAttributeName,
keyColor, NSForegroundColorAttributeName,
p, NSParagraphStyleAttributeName,
nil];
titleTextAttributes[1] = [[NSMutableDictionary alloc]
initWithObjectsAndKeys:
[NSFont titleBarFontOfSize: 0], NSFontAttributeName,
normalColor, NSForegroundColorAttributeName,
p, NSParagraphStyleAttributeName,
nil];
titleTextAttributes[2] = [[NSMutableDictionary alloc]
initWithObjectsAndKeys:
[NSFont titleBarFontOfSize: 0], NSFontAttributeName,
mainColor, NSForegroundColorAttributeName,
p, NSParagraphStyleAttributeName,
nil];
RELEASE(p);
}
@end