-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathABKeyAttachmentCell.m
229 lines (180 loc) · 5.78 KB
/
ABKeyAttachmentCell.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
//
// ABKeyAttachmentCell.m
// ABKeyManager
//
// Created by Robert Goldsmith on 17/02/2005.
// Copyright 2005 Far-Blue. All rights reserved.
//
#import "ABKeyAttachmentCell.h"
#import "ABKeyGlobalAnimationTimer.h"
#import "ABKeyManagerPluginBundleResourceAccess.h"
@implementation ABKeyAttachmentCell
-(void)constructBezierPath
{
NSBezierPath *halfArrow=[NSBezierPath bezierPath];
NSAffineTransform *rotate180=[NSAffineTransform transform];
[rotate180 rotateByDegrees:180];
[drawingPath setLineWidth:(drawSize.width-3)/10];
[halfArrow appendBezierPathWithArcWithCenter:NSMakePoint(0,0) radius:(drawSize.width-6)/2 startAngle:50 endAngle:180];
[halfArrow relativeLineToPoint:NSMakePoint(-1,1)];
[halfArrow relativeLineToPoint:NSMakePoint(2,0)];
[halfArrow relativeLineToPoint:NSMakePoint(-1,-1)];
[drawingPath appendBezierPath:halfArrow];
[halfArrow transformUsingAffineTransform:rotate180];
[drawingPath appendBezierPath:halfArrow];
}
-(id)initWithIdentifier:(NSString *)newIdentifier
{
if([super init])
{
identifier=[newIdentifier retain];
animationRotationAngle=0;
trackingRectTag=-1;
toolTipTag=-1;
drawInverted=NO;
stopAfterFullRotation=NO;
drawSize.width=drawSize.height=15+[[NSUserDefaults standardUserDefaults] integerForKey:@"ABTextSizeIncrement"];
hasRegisteredWithAnimationTimer=NO;
drawingPath=[[NSBezierPath alloc] init];
[self constructBezierPath];
[[GPGMEController sharedController] add:self selector:@selector(asyncStateChange:) forAsyncGPGOperationsConcerningIdentifier:identifier];
}
return self;
}
//basic text-attachment methods
-(NSPoint)cellBaselineOffset
{return NSMakePoint(0,-3);}
-(NSSize)cellSize
{return drawSize;}
-(void)mouseClicked
{
if(hasRegisteredWithAnimationTimer)
[[GPGMEController sharedController] interruptAsyncOperationForIdentifier:identifier];
else
[[GPGMEController sharedController] refreshGPGKeyWithFingerprint:identifier];
}
-(void)registerWithAnimationTimer
{
if(hasRegisteredWithAnimationTimer)
return;
[ABKeyGlobalAnimationTimer addToGlobalAnimationTimer:self selector:@selector(animationStep:)];
hasRegisteredWithAnimationTimer=YES;
}
-(void)unregisterWithAnimationTimer
{
if(!hasRegisteredWithAnimationTimer)
return;
[ABKeyGlobalAnimationTimer removeObserver:self];
hasRegisteredWithAnimationTimer=NO;
}
//key refresh status changes
-(void)asyncStateChange:(NSNotification *)notification
{
//NSLog(@"Status changed: %@", [[notification object] description]);
if([[notification object] isEqualToString:@"asyncOperationStarted"])
[self registerWithAnimationTimer];
else
stopAfterFullRotation=YES;
}
-(void)animationStep:(NSNotification *)notification
{
//NSLog(@"Animation step");
animationRotationAngle+=18;
if(animationRotationAngle>180)
{
if(stopAfterFullRotation)
{
[self unregisterWithAnimationTimer];
stopAfterFullRotation=NO;
animationRotationAngle=0;
}
else
animationRotationAngle-=180;
}
[myView displayRect:myFrame];
}
-(void)updateTrackingRect:(NSNotification *)notification
{
if(trackingRectTag!=-1)
[myView removeTrackingRect:trackingRectTag];
trackingRectTag=[myView addTrackingRect:myFrame owner:self userData:nil assumeInside:NO];
if(toolTipTag!=-1)
[[myView superview] removeToolTip:toolTipTag];
toolTipTag=[[myView superview] addToolTipRect:myFrame owner:self userData:nil];
}
-(void)drawWithFrame:(NSRect)newFrame inView:(NSView *)aView
{
if(myView==nil || ![myView isEqualTo:aView])
{
if(myView!=nil)
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSViewFrameDidChangeNotification object:myView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateTrackingRect:) name:NSViewFrameDidChangeNotification object:aView];
myView=aView;
}
if(!NSEqualRects(newFrame,myFrame))
{
myFrame=newFrame;
[self updateTrackingRect:nil];
}
NSAffineTransform *transform=[NSAffineTransform transform];
[transform translateXBy:myFrame.origin.x+(myFrame.size.width/2) yBy:myFrame.origin.y+(myFrame.size.height/2)];
if(drawInverted)
{
[[NSColor lightGrayColor] setFill];
[[NSColor whiteColor] setStroke];
}
else
{
[[NSColor lightGrayColor] setStroke];
[[NSColor whiteColor] setFill];
}
if([myView lockFocusIfCanDraw])
{
[[NSBezierPath bezierPathWithOvalInRect:myFrame] fill];
if(!drawInverted || !hasRegisteredWithAnimationTimer)
{
[transform rotateByDegrees:animationRotationAngle];
[[transform transformBezierPath:drawingPath] stroke];
}
else
{
int length=(drawSize.width-7);
NSBezierPath *cancelIcon=[NSBezierPath bezierPath];
[cancelIcon setLineWidth:(drawSize.width-3)/10];
[cancelIcon moveToPoint:NSMakePoint(length/2,length/2)];
[cancelIcon relativeLineToPoint:NSMakePoint(-length,-length)];
[cancelIcon moveToPoint:NSMakePoint(-(length/2),length/2)];
[cancelIcon relativeLineToPoint:NSMakePoint(length,-length)];
[[transform transformBezierPath:cancelIcon] stroke];
}
[myView unlockFocus];
}
}
-(void)mouseEntered:(NSEvent *)theEvent
{
drawInverted=YES;
[[NSCursor arrowCursor] push];
[myView displayRect:myFrame];
}
-(void)mouseExited:(NSEvent *)theEvent
{
drawInverted=NO;
[NSCursor pop];
[myView displayRect:myFrame];
}
-(NSString *)view:(NSView *)view stringForToolTip:(NSToolTipTag)tag point:(NSPoint)point userData:(void *)userData
{
return KeyManagerLocalizedString(@"refresh tooltip",@"key refresh tooltip");
}
-(void)dealloc
{
[myView removeTrackingRect:trackingRectTag];
[[myView superview] removeToolTip:toolTipTag];
[[NSNotificationCenter defaultCenter] removeObserver:self name:NSViewFrameDidChangeNotification object:myView];
[identifier release];
[drawingPath release];
[[GPGMEController sharedController] removeObserver:self];
[self unregisterWithAnimationTimer];
[super dealloc];
}
@end