forked from evadne/LPKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LPSwitch.j
304 lines (250 loc) · 10.4 KB
/
LPSwitch.j
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
297
298
299
300
301
302
303
304
/*
* LPSwitch.j
* LPKit
*
* Created by Ludwig Pettersson on November 7, 2009.
*
* The MIT License
*
* Copyright (c) 2009 Ludwig Pettersson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
@import <AppKit/CPControl.j>
@import <LPKit/LPViewAnimation.j>
@implementation LPSwitch : CPControl
{
BOOL on @accessors(readonly, getter=isOn);
CGPoint dragStartPoint;
LPSwitchKnob knob;
CGPoint knobDragStartPoint;
BOOL isDragging;
float animationDuration;
id animationCurve;
CPView offBackgroundView;
CPView onBackgroundView;
CPTextField offLabel;
CPTextField onLabel;
LPViewAnimation animation;
}
+ (CPString)themeClass
{
return @"lp-switch";
}
+ (id)themeAttributes
{
return [CPDictionary dictionaryWithObjects:[[CPNull null], [CPNull null], [CPNull null], CGSizeMake(30.0, 24.0), [CPNull null], [CPNull null], [CPNull null], [CPNull null], [CPNull null], [CPNull null], [CPNull null], [CPNull null], [CPNull null]]
forKeys:[@"off-background-color", @"on-background-color", @"knob-background-color", @"knob-size", @"label-offset",
@"off-label-font", @"off-label-text-color", @"off-label-text-shadow-color", @"off-label-text-shadow-offset",
@"on-label-font", @"on-label-text-color", @"on-label-text-shadow-color", @"on-label-text-shadow-offset"]];
}
- (id)initWithFrame:(CGRect)aFrame
{
if (self = [super initWithFrame:aFrame])
{
offBackgroundView = [[CPView alloc] initWithFrame:[self bounds]];
[offBackgroundView setHitTests:NO];
[self addSubview:offBackgroundView];
onBackgroundView = [[CPView alloc] initWithFrame:CGRectMake(0,0,0,CGRectGetHeight([self bounds]))];
[onBackgroundView setHitTests:NO];
[self addSubview:onBackgroundView];
knob = [[LPSwitchKnob alloc] initWithFrame:CGRectMakeZero()];
[self addSubview:knob];
offLabel = [CPTextField labelWithTitle:@"Off"];
[self addSubview:offLabel];
onLabel = [CPTextField labelWithTitle:@"On"];
[self addSubview:onLabel];
animationDuration = 0.2;
animationCurve = CPAnimationEaseOut;
// Need to call layoutSubviews directly to make sure
// all theme attributes are set.
// TODO: FIX THIS.
[self layoutSubviews];
[self setNeedsLayout];
}
return self;
}
- (void)setOn:(BOOL)shouldSetOn animated:(BOOL)shouldAnimate
{
[self setOn:shouldSetOn animated:shouldAnimate sendAction:YES];
}
- (void)setOn:(BOOL)shouldSetOn animated:(BOOL)shouldAnimate sendAction:(BOOL)shouldSendAction
{
// changed to stop the action firing if the user moved the switch to the inverse state, but then back to original state before releasing the mouse button
if (shouldSendAction && on !== shouldSetOn)
{
on = shouldSetOn;
[self sendAction:_action to:_target];
}
else
on = shouldSetOn;
var knobMinY = CGRectGetMinY([knob frame]),
knobEndFrame = CGRectMake((on) ? [knob maxX] : [knob minX], knobMinY, CGRectGetWidth([knob frame]), CGRectGetHeight([knob frame])),
onBackgroundEndFrame = CGRectMake(0,0, CGRectGetMinX(knobEndFrame) + CGRectGetMidX([knob bounds]), CGRectGetHeight([onBackgroundView bounds])),
labelOffset = [self labelOffset],
offLabelEndFrame = CGRectMake(CGRectGetMaxX(knobEndFrame) + labelOffset.width, labelOffset.height,
CGRectGetWidth([offLabel bounds]), CGRectGetHeight([offLabel bounds])),
onLabelEndFrame = CGRectMake(CGRectGetMinX(knobEndFrame) - labelOffset.width - CGRectGetWidth([onLabel bounds]), labelOffset.height,
CGRectGetWidth([onLabel bounds]), CGRectGetHeight([onLabel bounds]));
// added to counter a problem whereby changing the state more than once (i.e., ON -> OFF -> ON) before giving control to the run loop,
// caused the control to not update properly
if([animation isAnimating])
[animation stopAnimation];
if (shouldAnimate)
{
animation = [[LPViewAnimation alloc] initWithViewAnimations:[
{
@"target": knob,
@"animations": [
[LPOriginAnimationKey, [knob frame].origin, knobEndFrame.origin]
]
},
{
@"target": onBackgroundView,
@"animations": [
[LPFrameAnimationKey, [onBackgroundView frame], onBackgroundEndFrame]
]
},
{
@"target": offLabel,
@"animations": [
[LPOriginAnimationKey, [offLabel frame].origin, offLabelEndFrame.origin]
]
},
{
@"target": onLabel,
@"animations": [
[LPOriginAnimationKey, [onLabel frame].origin, onLabelEndFrame.origin]
]
}
]];
[animation setAnimationCurve:animationCurve];
[animation setDuration:animationDuration];
[animation setDelegate:self];
[animation startAnimation];
}
else
{
[knob setFrame:knobEndFrame];
[onBackgroundView setFrame:onBackgroundEndFrame];
[offLabel setFrame:offLabelEndFrame];
}
}
- (void)mouseDown:(CPEvent)anEvent
{
if (![self isEnabled])
return;
dragStartPoint = [self convertPoint:[anEvent locationInWindow] fromView:nil];
knobDragStartPoint = [knob frame].origin;
isDragging = NO;
// If the drag started on top of the knob, we highlight it
var startPointX = [knob convertPoint:dragStartPoint fromView:self].x;
if (startPointX > 0 && startPointX < CGRectGetWidth([knob bounds]))
{
[knob setHighlighted:YES];
[self setNeedsLayout];
}
}
- (void)mouseDragged:(CPEvent)anEvent
{
if (![self isEnabled])
return;
// We are dragging
isDragging = YES;
var point = [self convertPoint:[anEvent locationInWindow] fromView:nil],
knobX = knobDragStartPoint.x + (point.x - dragStartPoint.x),
knobMinX = [knob minX],
knobMaxX = [knob maxX],
height = CGRectGetHeight([self bounds]);
// Limit X
if (knobX < knobMinX)
knobX = knobMinX;
else if(knobX > knobMaxX)
knobX = knobMaxX;
// Resize background views
[onBackgroundView setFrameSize:CGSizeMake(knobX + CGRectGetMidX([knob bounds]), height)];
// Re-position knob
[knob setFrameOrigin:CGPointMake(knobX, CGRectGetMinY([knob frame]))];
[self setNeedsLayout];
}
- (void)mouseUp:(CPEvent)anEvent
{
if (![self isEnabled])
return;
[self setOn:isDragging ? CGRectGetMidX([self bounds]) < CGRectGetMidX([knob frame]) : !on animated:YES];
[knob setHighlighted:NO];
[self setNeedsLayout];
}
- (CGSize)labelOffset
{
// Provide a default size so that we can use switches even without themes.
var labelOffset = [self currentValueForThemeAttribute:@"label-offset"];
return (labelOffset) ? labelOffset : CGSizeMake(0,0);
}
- (void)layoutSubviews
{
[offBackgroundView setBackgroundColor:[self currentValueForThemeAttribute:@"off-background-color"]];
[onBackgroundView setBackgroundColor:[self currentValueForThemeAttribute:@"on-background-color"]];
[knob setBackgroundColor:[self valueForThemeAttribute:@"knob-background-color" inState:[knob themeState]]];
[knob setFrameSize:[self currentValueForThemeAttribute:@"knob-size"]];
var labelOffset = [self labelOffset];
[offLabel setFont:[self currentValueForThemeAttribute:@"off-label-font"]];
[offLabel setTextColor:[self currentValueForThemeAttribute:@"off-label-text-color"]];
[offLabel setTextShadowColor:[self currentValueForThemeAttribute:@"off-label-text-shadow-color"]];
[offLabel setTextShadowOffset:[self currentValueForThemeAttribute:@"off-label-text-shadow-offset"]];
[offLabel setFrameOrigin:CGPointMake(CGRectGetMaxX([knob frame]) + labelOffset.width, labelOffset.height)];
[offLabel sizeToFit];
[onLabel setFont:[self currentValueForThemeAttribute:@"on-label-font"]];
[onLabel setTextColor:[self currentValueForThemeAttribute:@"on-label-text-color"]];
[onLabel setTextShadowColor:[self currentValueForThemeAttribute:@"on-label-text-shadow-color"]];
[onLabel setTextShadowOffset:[self currentValueForThemeAttribute:@"on-label-text-shadow-offset"]];
[onLabel sizeToFit];
[onLabel setFrameOrigin:CGPointMake(CGRectGetMinX([knob frame]) - labelOffset.width - CGRectGetWidth([onLabel bounds]), CGRectGetMinY([offLabel frame]))];
}
@end
@implementation LPSwitchKnob : CPView
{
}
- (id)initWithFrame:(CGRect)aFrame
{
if (self = [super initWithFrame:aFrame])
{
[self setHitTests:NO];
//[self setBackgroundColor:[CPColor colorWithHexString:@"333"]];
}
return self;
}
- (void)setHighlighted:(BOOL)shouldBeHighlighted
{
isHighlighted = shouldBeHighlighted;
if (shouldBeHighlighted)
[self setThemeState:CPThemeStateHighlighted];
else
[self unsetThemeState:CPThemeStateHighlighted];
}
- (unsigned int)minX
{
return 0;
}
- (unsigned int)maxX
{
return CGRectGetWidth([[self superview] bounds]) - CGRectGetWidth([self bounds]);
}
@end