-
Notifications
You must be signed in to change notification settings - Fork 41
/
AMSwitchButton.m
126 lines (103 loc) · 2.89 KB
/
AMSwitchButton.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
//
// AMSwitchButton.m
// SSHTunnel
//
// Created by Antoine Mercadal on 18/08/08.
// Copyright 2008 Capgemini. All rights reserved.
//
#import "AMSwitchButton.h"
@implementation AMSwitchButton
@synthesize status;
- (id) init
{
self = [super init];
if (self)
{
[self setStatus:NO];
return self;
}
return nil;
}
- (void) observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqual:@"status"])
{
if ([self status] == YES)
{
[self pushOn];
}
else
{
[self pushOff];
}
}
}
- (void) awakeFromNib
{
[self addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
[self bind:@"status" toObject:[sessionController sessionsTreeController] withKeyPath:@"selection.connected" options:nil];
}
- (void)drawRect:(NSRect)rect
{
if ([self status] == NO)
{
startingColor = [NSColor colorWithCalibratedRed:0.23 green:0.26 blue:0.29 alpha:1.0];
endingColor = [NSColor colorWithCalibratedRed:0.53 green:0.56 blue:0.59 alpha:1.0];
}
else
{
startingColor = [NSColor colorWithCalibratedRed:0.31 green:0.75 blue:0.40 alpha:1.0];
endingColor = [NSColor colorWithCalibratedRed:0.21 green:0.65 blue:0.30 alpha:1.0];
}
if (endingColor == nil || [startingColor isEqual:endingColor]) {
[startingColor set];
NSRectFill(rect);
}
else {
NSGradient* aGradient = [[NSGradient alloc]
initWithStartingColor:startingColor
endingColor:endingColor];
[aGradient drawInRect:[self bounds] angle:270];
}
}
- (void) pushOn
{
NSRect newFrame = [switchView frame];
newFrame.origin.x += [switchView frame].size.width;
if (newFrame.origin.x > [self frame].size.width / 2 )
newFrame.origin.x = [self frame].size.width / 2;
[[switchView animator] setFrame:newFrame];
newFrame = [offLabel frame];
newFrame.origin.x += ([offLabel frame].size.width) + 5;
[[offLabel animator] setFrame:newFrame];
newFrame = [onLabel frame];
newFrame.origin.x = 7;
[[onLabel animator] setFrame:newFrame];
[appController openSession:nil];
//[NSTimer scheduledTimerWithTimeInterval:0.35 target:self selector:@selector(display) userInfo:nil repeats:NO];
}
- (void) pushOff
{
NSRect newFrame = [switchView frame];
newFrame.origin.x -= [switchView frame].size.width;
if (newFrame.origin.x < 0 )
newFrame.origin.x = 0;
[[switchView animator] setFrame:newFrame];
newFrame = [offLabel frame];
newFrame.origin.x = 35;
[[offLabel animator] setFrame:newFrame];
newFrame = [onLabel frame];
newFrame.origin.x -= [onLabel frame].size.width + 7;
[[onLabel animator] setFrame:newFrame];
[appController closeSession:nil];
//[NSTimer scheduledTimerWithTimeInterval:0.35 target:self selector:@selector(display) userInfo:nil repeats:NO];
}
- (IBAction) switchStatus:(id)sender
{
[self setStatus:![self status]];
if ([self status] == YES)
[self pushOn];
else
[self pushOff];
}
@end