-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
ETTransparentScroller.m
238 lines (201 loc) · 7.47 KB
/
ETTransparentScroller.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
//
// ETTransparentScroller.h
//
// Created by Brandon Walkin (www.brandonwalkin.com)
// All code is provided under the New BSD license.
//
// Modified by elasticthreads 10/19/2010
//
#import "ETTransparentScroller.h"
//@interface NSScroller (NVTSPrivate)
//- (NSRect)_drawingRectForPart:(NSScrollerPart)aPart;
//@end
@implementation ETTransparentScroller
//+ (void)initialize
//{
//}
+ (BOOL)isCompatibleWithResponsiveScrolling{
return NO;
}
- (id)initWithFrame:(NSRect)frameRect{
if ((self=[super initWithFrame:frameRect])) {
fillBackground=NO;
NSBundle *bundle = [NSBundle mainBundle];
knobTop = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentScrollerKnobTop.tif"]];
knobVerticalFill = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentScrollerKnobVerticalFill.tif"]];
knobBottom = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentScrollerKnobBottom.tif"]];
slotTop = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentScrollerSlotTop.tif"]];
slotVerticalFill = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentScrollerSlotVerticalFill.tif"]];
slotBottom = [[NSImage alloc] initWithContentsOfFile:[bundle pathForImageResource:@"TransparentScrollerSlotBottom.tif"]];
verticalPaddingLeft = 4.0f;
verticalPaddingRight = 3.0f;
verticalPaddingTop =3.75f;
verticalPaddingBottom = 4.25f;
minKnobHeight = knobTop.size.height + knobVerticalFill.size.height + knobBottom.size.height + 20.0;
slotAlpha=0.45f;
knobAlpha=0.45f;
[self setArrowsPosition:NSScrollerArrowsNone];
// isOverlay=NO;
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
if (IsLionOrLater) {
// isOverlay=[[self class]isCompatibleWithOverlayScrollers];
}
#endif
}
return self;
}
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
+ (BOOL)isCompatibleWithOverlayScrollers {
return NO;
}
+ (NSScrollerStyle)preferredScrollerStyle{
// if ([[NSScroller class]preferredScrollerStyle]==NSScrollerStyleOverlay) {
//
// }
return NSScrollerStyleLegacy;
}
#endif
- (void)dealloc{
[knobTop release];
[knobVerticalFill release];
[knobBottom release];
[slotTop release];
[slotBottom release];
[slotVerticalFill release];
[super dealloc];
}
#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_7
+ (CGFloat)scrollerWidthForControlSize:(NSControlSize)controlSize scrollerStyle:(NSScrollerStyle)scrollerStyle{
return 15.0;
}
#else
+ (CGFloat)scrollerWidth
{
return 15.0;//slotVerticalFill.size.width + verticalPaddingLeft + verticalPaddingRight;
}
+ (CGFloat)scrollerWidthForControlSize:(NSControlSize)controlSize
{
return 15.0;//slotVerticalFill.size.width + verticalPaddingLeft + verticalPaddingRight;
}
#endif
- (void)setFillBackground:(BOOL)fillIt{
fillBackground=fillIt;
}
- (void)drawRect:(NSRect)aRect;
{
// Only draw if the slot is larger than the knob
if (IsLionOrLater) {
[super drawRect:aRect];
}else{
if (([self bounds].size.height - verticalPaddingTop - verticalPaddingBottom + 1) > minKnobHeight){
if (fillBackground) {
[[[[self window] contentView] backgroundColor] setFill];
NSRectFill([self bounds]);
}
[self drawKnobSlotInRect:[self rectForPart:NSScrollerKnobSlot] highlight:NO];
if ([self knobProportion] > 0.0)
[self drawKnob];
}
}
}
- (void)drawKnobSlotInRect:(NSRect)slotRect highlight:(BOOL)flag{
NSDrawThreePartImage(slotRect, slotTop, slotVerticalFill, slotBottom, YES, NSCompositeSourceOver, slotAlpha, NO);
}
- (void)drawKnob;
{
NSRect knobRect = [self rectForPart:NSScrollerKnob];
NSDrawThreePartImage(knobRect, knobTop, knobVerticalFill, knobBottom, YES, NSCompositeSourceOver, knobAlpha, NO);
}
//
//- (NSRect)_drawingRectForPart:(NSScrollerPart)aPart;
//{
// // Call super even though we're not using its value (has some side effects we need)
// [super _drawingRectForPart:aPart];
//
// // Return our own rects rather than use the default behavior
// return [self rectForPart:aPart];
//}
//- (void)trackKnob:(NSEvent *)theEvent{
// NSPoint aPoint=[theEvent locationInWindow];
// NSScrollerPart aPart=[super testPart:aPoint];
// [super trackKnob:theEvent];
//}
- (NSRect)rectForPart:(NSScrollerPart)aPart{
switch (aPart)
{
case NSScrollerNoPart:
{
return [self bounds];
break;
}
case NSScrollerKnob:
{
NSRect knobRect=[super rectForPart:NSScrollerKnob];
// NSRect slotRect = [self rectForPart:NSScrollerKnobSlot];
// float knobHeight = roundf(slotRect.size.height * [self knobProportion]);
// if (knobHeight < minKnobHeight){
// if (minKnobHeight>slotRect.size.height) {
// knobHeight=knobRect.size.height;
// }else{
// knobHeight = minKnobHeight;
// }
// }
CGFloat slotY=roundf(verticalPaddingTop);
CGFloat knobY=knobRect.origin.y;
CGFloat slotHt=roundf([self bounds].size.height-(verticalPaddingTop+verticalPaddingBottom));
if (knobY<slotY) {
knobY=slotY;
}
else if ((knobY+knobRect.size.height)>(slotHt+slotY)){
knobY=slotHt+slotY-knobRect.size.height;
}
// knobRect = NSMakeRect(verticalPaddingLeft, knobY, slotRect.size.width, knobHeight);
knobRect.origin.x=roundf(verticalPaddingLeft);
knobRect.origin.y=roundf(knobY);
knobRect.size.width=roundf([self bounds].size.width - verticalPaddingLeft - verticalPaddingRight);
return knobRect;
}
break;
case NSScrollerKnobSlot:
{
NSRect slotRect=[self bounds];
slotRect.origin.x=roundf(verticalPaddingLeft);
slotRect.size.width=roundf(slotRect.size.width - verticalPaddingLeft - verticalPaddingRight);
slotRect.origin.y=roundf(verticalPaddingTop);
slotRect.size.height=roundf(slotRect.size.height-(verticalPaddingTop+verticalPaddingBottom));
return slotRect;
}
break;
case NSScrollerIncrementLine:
return NSZeroRect;
break;
case NSScrollerDecrementLine:
return NSZeroRect;
break;
case NSScrollerIncrementPage:
{
NSRect incrementPageRect;
NSRect knobRect = [self rectForPart:NSScrollerKnob];
// NSRect slotRect = [self rectForPart:NSScrollerKnobSlot];
// NSRect decPageRect = [self rectForPart:NSScrollerDecrementPage];
CGFloat slotHt=roundf([self bounds].size.height-(verticalPaddingTop+verticalPaddingBottom));
CGFloat knobY = roundf(knobRect.origin.y + knobRect.size.height);
CGFloat knobHt=roundf(slotHt - knobRect.size.height - knobRect.origin.y - verticalPaddingTop);
incrementPageRect = NSMakeRect(roundf(verticalPaddingLeft), knobY, knobRect.size.width, knobHt);
return incrementPageRect;
}
break;
case NSScrollerDecrementPage:
{
NSRect decrementPageRect;
NSRect knobRect = [self rectForPart:NSScrollerKnob];
decrementPageRect = NSMakeRect(roundf(verticalPaddingLeft), roundf(verticalPaddingTop), knobRect.size.width, roundf(knobRect.origin.y - verticalPaddingTop));
return decrementPageRect;
}
break;
default:
break;
}
return NSZeroRect;
}
@end