forked from AlessandroSangiuliano/rik.theme
-
Notifications
You must be signed in to change notification settings - Fork 1
/
NSSearchFieldCell+Rik.m
175 lines (148 loc) · 5.52 KB
/
NSSearchFieldCell+Rik.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
/**
* Copyright (C) 2013 Alessandro Sangiuliano
* Author: Alessandro Sangiuliano <[email protected]>
* Date: 31 December 2013
*/
#import "Rik.h"
#import "NSSearchFieldCell+Rik.h"
#import "Rik+Button.h"
#define ICON_WIDTH 16
@implementation NSSearchFieldCell (RikTheme)
- (void) drawWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
{
NSRect frame = cellFrame;
[super drawWithFrame: [self searchTextRectForBounds: cellFrame ]
inView: controlView];
[_search_button_cell drawWithFrame: [self searchButtonRectForBounds: cellFrame] inView: controlView];
if ([[self stringValue] length] > 0)
[_cancel_button_cell drawWithFrame: [self cancelButtonRectForBounds: cellFrame]
inView: controlView];
}
/* This method put the "x" cell inside the Text cell */
- (NSRect) searchTextRectForBounds: (NSRect)rect
{
NSRect search, text, part;
if (_search_button_cell)
{
part = rect;
/*set the right point and size*/
part.origin.x +=0;
part.size.width -= 1;
}
else
{
NSDivideRect(rect, &search, &part, ICON_WIDTH, NSMinXEdge);
}
text = part;
return text;
}
- (void) _drawBorderAndBackgroundWithFrame: (NSRect)cellFrame
inView: (NSView*)controlView
{
NSColor* whiteColor = [NSColor colorWithCalibratedRed: 1
green: 1
blue: 1
alpha: 0.8];
NSColor* clearColor = [NSColor colorWithCalibratedRed: 1
green: 1
blue: 1
alpha: 0];
NSColor * strokeBaseColor = [Rik controlStrokeColor];
NSColor * strokeLightColor = [strokeBaseColor highlightWithLevel: 0.3];
NSGradient* lightGradient = [[NSGradient alloc] initWithColorsAndLocations:
clearColor, 0.0,
whiteColor, 0.97, nil];
NSGradient* bezelBorderGradient = [[NSGradient alloc] initWithColorsAndLocations:
strokeBaseColor, 1.0,
strokeLightColor, 0.5, nil];
NSGradient* fillGradient = [[NSGradient alloc] initWithColorsAndLocations:
[strokeBaseColor highlightWithLevel: 0.7], 0.0,
[NSColor whiteColor], 0.2, nil];
NSRect rect = cellFrame;
CGFloat radius = rect.size.height / 2.0;
NSBezierPath* lightPath = [NSBezierPath bezierPathWithRoundedRect: rect
xRadius: radius
yRadius: radius];
NSBezierPath* bezelPath = [NSBezierPath bezierPathWithRoundedRect: NSInsetRect(rect, 1, 1)
xRadius: radius-2
yRadius: radius-2];
NSBezierPath* fillPath = [NSBezierPath bezierPathWithRoundedRect: NSInsetRect(rect, 2, 2)
xRadius: radius-2
yRadius: radius-2];
[lightGradient drawInBezierPath: lightPath angle: 90];
[bezelBorderGradient drawInBezierPath: bezelPath angle: -90];
[fillGradient drawInBezierPath: fillPath angle: 90];
}
- (void) drawInteriorWithFrame: (NSRect)cellFrame inView: (NSView*)controlView
{
if (_cell.in_editing)
[self _drawEditorWithFrame: cellFrame inView: controlView];
else
{
NSRect titleRect;
/* Make sure we are a text cell; titleRect might return an incorrect
rectangle otherwise. Note that the type could be different if the
user has set an image on us, which we just ignore (OS X does so as
well). */
_cell.type = NSTextCellType;
titleRect = [self titleRectForBounds: cellFrame];
[[self _drawAttributedString] drawInRect: titleRect];
}
}
- (void) _drawEditorWithFrame: (NSRect)cellFrame
inView: (NSView *)controlView
{
if ([controlView isKindOfClass: [NSControl class]])
{
/* Adjust the text editor's frame to match cell's frame (w/o border) */
NSRect titleRect = [self titleRectForBounds: cellFrame];
NSText *textObject = [(NSControl*)controlView currentEditor];
NSView *clipView = [textObject superview];
if ([clipView isKindOfClass: [NSClipView class]])
{
[clipView setFrame: titleRect];
}
else
{
[textObject setFrame: titleRect];
}
}
}
- (NSRect) titleRectForBounds: (NSRect)theRect
{
if (_cell.type == NSTextCellType)
{
NSRect frame = [self drawingRectForBounds: theRect];
//Add spacing between border and inside
if (_cell.is_bordered || _cell.is_bezeled)
{
frame.origin.x += 16;
frame.size.width -= 30;
/*By Slex: If you modify this value, then the chars. will overlap when the text field is *full filled of charcters. You'll see part of characters like 'p' or 'g' or 'j' taking
*the next line of the editor text field, looking very bad
*/
frame.size.height += 0;
}
return frame;
}
else
{
return theRect;
}
}
- (NSRect) searchButtonRectForBounds: (NSRect)rect;
{
NSRect search, part;
NSDivideRect(rect, &search, &part, ICON_WIDTH, NSMinXEdge);
search.origin.x += 4;
search.origin.y += 0;
return search;
}
- (NSRect) cancelButtonRectForBounds: (NSRect)rect
{
NSRect part, clear;
NSDivideRect(rect, &clear, &part, ICON_WIDTH, NSMaxXEdge);
clear.origin.x -= 5; //This set the position inside the textsearch box
return clear;
}
@end