-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathSSYToolbarButton.m
228 lines (189 loc) · 6.62 KB
/
SSYToolbarButton.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
#import "SSYToolbarButton.h"
#import "NSImage+Transform.h"
#import "NSImage+SSYDarkMode.h"
static NSString* const constKeyValue = @"value" ;
static NSString* const constKeyToolTip = @"toolTip" ;
@interface SSYToolbarButton ()
// To avoid retain cycles, it is conventional to not retain
// targets. (In this case, I tested [super setTarget:aTarg]
// and it does not increase the retain count of aTarg.)
@property (assign) id externalTarget ;
@property (assign) SEL externalAction ;
@property (retain) NSImage* originalImage ;
@property (assign) NSTimer* flashTimer ; // NSTimer retains itself, so 'assign'
@end
@implementation SSYToolbarButton
+ (void)initialize {
if (self == [SSYToolbarButton class] ) {
[self exposeBinding:constKeyValue] ;
[self exposeBinding:constKeyToolTip] ;
}
}
- (id)initWithItemIdentifier:(NSString*)identifier {
self = [super initWithItemIdentifier:identifier] ;
if (self) {
// Make sure that setValue: does not take its early return the
// first time it is invoked…
[self setValue:NSNotFound] ;
// The following is to support some other object binding to the
// 'value' binding of a SSYToolbarButton. We splice ourself in
// to observe the action.
[super setTarget:self] ;
[super setAction:@selector(doDaClick:)] ;
// In BookMacster, we use the conventional target/action and not
// the binding.
}
return self ;
}
@synthesize onImage = m_onImage ;
@synthesize offImage = m_offImage ;
@synthesize disImage = m_disImage ;
@synthesize backgroundImage = m_backgroundImage;
@synthesize originalImage = m_originalImage ;
@synthesize onLabel = m_onLabel ;
@synthesize offLabel = m_offLabel ;
@synthesize disLabel = m_disLabel ;
@synthesize onToolTip = m_onToolTip ;
@synthesize offToolTip = m_offToolTip ;
@synthesize disToolTip = m_disToolTip ;
@synthesize externalTarget = m_externalTarget ;
@synthesize externalAction = m_externalAction ;
@synthesize flashDuration = m_flashDuration ;
- (void)setTarget:(id)target {
[self setExternalTarget:target] ;
}
- (void)setAction:(SEL)action {
[self setExternalAction:action] ;
}
- (NSInteger)value {
return m_value ;
}
- (void)setValue:(NSInteger)value {
if (value == m_value) {
return ;
}
m_value = value ;
NSImage* image = nil ;
NSString* label = nil ;
NSString* toolTip = nil ;
switch (value) {
case NSControlStateValueOn:
image = [self onImage] ;
label = [self onLabel] ;
toolTip = [self onToolTip] ;
break ;
case NSControlStateValueOff:
image = [self offImage] ;
label = [self offLabel] ;
toolTip = [self offToolTip] ;
break ;
case NSControlStateValueMixed:
image = [self disImage] ;
label = [self disLabel] ;
toolTip = [self disToolTip] ;
break ;
}
if (self.view) {
/* Change the image for a view-based button */
self.view.needsDisplay=YES;
} else {
/* Change the image for a image-based button */
[self setImage:image] ;
}
if (label) {
[self setLabel:label] ;
}
if (toolTip) {
[self setToolTip:toolTip] ;
}
}
- (void)restoreOriginalImage:(NSTimer*)timer {
[self setImage:[self originalImage]] ;
[self setOriginalImage:nil] ;
}
- (IBAction)doDaClick:(id)sender {
if ([self flashDuration] > 0.0) {
NSImage* image = [self image] ;
[self setOriginalImage:image] ;
NSImage* darkerImage = [image darkenedImage] ;
[self setImage:darkerImage] ;
NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:[self flashDuration]
target:self
selector:@selector(restoreOriginalImage:)
userInfo:nil
repeats:NO] ;
// Stash the timer so that we can invalidate it during -dealloc, in case
// we are deallocced (window closes, for example) before the timer
// fires. Otherwise there would be a crash.
[self setFlashTimer:timer] ;
}
//[self setValue:([self value] == NSControlStateValueOn) ? NSControlStateValueOff : NSControlStateValueOn] ;
[[self externalTarget] performSelector:[self externalAction]
withObject:self] ;
}
- (void)dealloc {
[m_flashTimer invalidate] ;
[m_onImage release] ;
[m_offImage release] ;
[m_disImage release] ;
[m_backgroundImage release];
[m_originalImage release] ;
[m_onLabel release] ;
[m_offLabel release] ;
[m_disLabel release] ;
[m_onToolTip release] ;
[m_offToolTip release] ;
[m_disToolTip release] ;
[super dealloc] ;
}
@end
@implementation SSYToolbarButtonView
/* The following implementation is only to silence runtime warnings when
the toolbar displays in a product built with Xcode 12. The numbers (32)
do not matter. */
- (NSSize)intrinsicContentSize {
return self.ssyIntrinsicContentSize;
}
- (void)drawRect:(NSRect)dirtyRect {
[super drawRect:dirtyRect];
NSImage* image = nil;
NSString* toolTip = nil;
switch (self.toolbarItem.value) {
case NSControlStateValueOn:
image = [self.toolbarItem onImage];
toolTip = [self.toolbarItem onToolTip];
break ;
case NSControlStateValueOff:
image = [self.toolbarItem offImage];
toolTip = [self.toolbarItem offToolTip];
break ;
case NSControlStateValueMixed:
image = self.toolbarItem.disImage;
toolTip = self.toolbarItem.disToolTip;
break ;
}
[[NSGraphicsContext currentContext] setImageInterpolation:NSImageInterpolationHigh];
[image drawInRect:NSMakeRect(
0.0,
0.0,
[image size].width,
[image size].height)
fromRect:NSZeroRect
operation:NSCompositingOperationSourceOver
fraction:1.0];
[self.toolbarItem.backgroundImage drawInRect:NSMakeRect(
0.0,
0.0,
self.toolbarItem.backgroundImage.size.width,
self.toolbarItem.backgroundImage.size.height)
fromRect:NSZeroRect
operation:NSCompositingOperationSourceOver
fraction:1.0];
if (toolTip) {
[self setToolTip:toolTip] ;
}
}
- (void)mouseDown:(NSEvent*)event {
[self.toolbarItem doDaClick:self];
}
@end