|
1 | 1 | #import "SSYToolbarButton.h"
|
| 2 | +#import "NSImage+Transform.h" |
2 | 3 |
|
3 | 4 | static NSString* const constKeyValue = @"value" ;
|
4 | 5 | static NSString* const constKeyToolTip = @"toolTip" ;
|
5 | 6 |
|
| 7 | + |
6 | 8 | @interface SSYToolbarButton ()
|
7 | 9 |
|
8 | 10 | // To avoid retain cycles, it is conventional to not retain
|
9 | 11 | // targets. (In this case, I tested [super setTarget:aTarg]
|
10 | 12 | // and it does not increase the retain count of aTarg.)
|
11 | 13 | @property (assign) id externalTarget ;
|
12 | 14 | @property (assign) SEL externalAction ;
|
| 15 | +@property (retain) NSImage* originalImage ; |
| 16 | +@property (assign) NSTimer* flashTimer ; // NSTimer retains itself, so 'assign' |
13 | 17 |
|
14 | 18 | @end
|
15 | 19 |
|
@@ -37,14 +41,16 @@ - (id)initWithItemIdentifier:(NSString*)identifier {
|
37 | 41 | @synthesize onImage = m_onImage ;
|
38 | 42 | @synthesize offImage = m_offImage ;
|
39 | 43 | @synthesize disImage = m_disImage ;
|
| 44 | +@synthesize originalImage = m_originalImage ; |
40 | 45 | @synthesize onLabel = m_onLabel ;
|
41 | 46 | @synthesize offLabel = m_offLabel ;
|
42 | 47 | @synthesize disLabel = m_disLabel ;
|
43 | 48 | @synthesize onToolTip = m_onToolTip ;
|
44 | 49 | @synthesize offToolTip = m_offToolTip ;
|
45 | 50 | @synthesize disToolTip = m_disToolTip ;
|
46 | 51 | @synthesize externalTarget = m_externalTarget ;
|
47 |
| -@synthesize externalAction = m_externalAction ; |
| 52 | +@synthesize externalAction = m_externalAction ; |
| 53 | +@synthesize flashDuration = m_flashDuration ; |
48 | 54 |
|
49 | 55 | - (void)awakeFromNib {
|
50 | 56 | // The following is to support some other object binding to the
|
@@ -108,18 +114,44 @@ - (void)setValue:(NSInteger)value {
|
108 | 114 | if (toolTip) {
|
109 | 115 | [self setToolTip:toolTip] ;
|
110 | 116 | }
|
111 |
| -} |
| 117 | +} |
| 118 | + |
| 119 | +- (void)restoreOriginalImage:(NSTimer*)timer { |
| 120 | + [self setImage:[self originalImage]] ; |
| 121 | + [self setOriginalImage:nil] ; |
| 122 | +} |
112 | 123 |
|
113 | 124 | - (IBAction)doDaClick:(id)sender {
|
| 125 | + if ([self flashDuration] > 0.0) { |
| 126 | + NSImage* image = [self image] ; |
| 127 | + [self setOriginalImage:image] ; |
| 128 | + NSImage* darkerImage = [image copy] ; |
| 129 | + [darkerImage darken] ; |
| 130 | + [self setImage:darkerImage] ; |
| 131 | + [darkerImage release] ; |
| 132 | + NSTimer* timer = [NSTimer scheduledTimerWithTimeInterval:[self flashDuration] |
| 133 | + target:self |
| 134 | + selector:@selector(restoreOriginalImage:) |
| 135 | + userInfo:nil |
| 136 | + repeats:NO] ; |
| 137 | + // Stash the timer so that we can invalidate it during -dealloc, in case |
| 138 | + // we are deallocced (window closes, for example) before the timer |
| 139 | + // fires. Otherwise there would be a crash. |
| 140 | + [self setFlashTimer:timer] ; |
| 141 | + } |
| 142 | + |
114 | 143 | //[self setValue:([self value] == NSOnState) ? NSOffState : NSOnState] ;
|
115 | 144 | [[self externalTarget] performSelector:[self externalAction]
|
116 | 145 | withObject:self] ;
|
117 | 146 | }
|
118 | 147 |
|
119 | 148 | - (void)dealloc {
|
| 149 | + [m_flashTimer invalidate] ; |
| 150 | + |
120 | 151 | [m_onImage release] ;
|
121 | 152 | [m_offImage release] ;
|
122 | 153 | [m_disImage release] ;
|
| 154 | + [m_originalImage release] ; |
123 | 155 | [m_onLabel release] ;
|
124 | 156 | [m_offLabel release] ;
|
125 | 157 | [m_disLabel release] ;
|
|
0 commit comments