9
9
#import < Appkit/NSImageView.h>
10
10
#import < Appkit/NSProgressIndicator.h>
11
11
#import < Appkit/NSWindow.h>
12
+ #import < objc/runtime.h>
12
13
13
14
@interface NWProgressBar : NSProgressIndicator
14
15
@end
@@ -84,17 +85,22 @@ - (void)drawRect:(NSRect)dirtyRect {
84
85
EXTENSION_FUNCTION_VALIDATE (args_);
85
86
double progress;
86
87
EXTENSION_FUNCTION_VALIDATE (args_->GetDouble (0 , &progress));
87
- NSDockTile *dockTile = [NSApp dockTile ];
88
- NWProgressBar *progressIndicator = NULL ;
89
88
90
- if ((dockTile.contentView == NULL || [dockTile.contentView.subviews count ] == 0 )&& progress >= 0 ) {
91
-
92
- // create image view to draw application icon
93
- NSImageView *iv = [[NSImageView alloc ] init ];
94
- [iv setImage: [NSApp applicationIconImage ]];
95
-
96
- // set dockTile content view to app icon
97
- [dockTile setContentView: iv];
89
+ NSDockTile *dockTile = [NSApp dockTile ];
90
+ static const char kProgressIndicator = ' \0 ' ;
91
+ NWProgressBar *progressIndicator = objc_getAssociatedObject (dockTile, &kProgressIndicator );
92
+ static const char kImageView = ' \0 ' ;
93
+
94
+ if (progressIndicator == NULL && progress >= 0 ) {
95
+ // contentView might not be NULL, i.e when download progress is running
96
+ if (dockTile.contentView == NULL ) {
97
+ // create image view to draw application icon
98
+ NSImageView * iv = [[NSImageView alloc ] init ];
99
+ [iv setImage: [NSApp applicationIconImage ]];
100
+ // set dockTile content view to app icon
101
+ [dockTile setContentView: iv];
102
+ objc_setAssociatedObject (dockTile, &kImageView , iv, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
103
+ }
98
104
99
105
progressIndicator = [[NWProgressBar alloc ]
100
106
initWithFrame: NSMakeRect (0 .0f , 0 .0f , dockTile.size.width, 15 .)];
@@ -108,11 +114,10 @@ - (void)drawRect:(NSRect)dirtyRect {
108
114
[progressIndicator setUsesThreadedAnimation: false ];
109
115
110
116
// add progress indicator to image view
111
- [iv addSubview: progressIndicator];
117
+ [dockTile.contentView addSubview: progressIndicator];
118
+ objc_setAssociatedObject (dockTile, &kProgressIndicator , progressIndicator, OBJC_ASSOCIATION_RETAIN_NONATOMIC );
112
119
}
113
-
114
- progressIndicator = (NWProgressBar*)[dockTile.contentView.subviews objectAtIndex: 0 ];
115
-
120
+
116
121
if (progress >= 0 ) {
117
122
[progressIndicator setIndeterminate: progress > 1 ];
118
123
if (progress > 1 ) {
@@ -125,11 +130,21 @@ - (void)drawRect:(NSRect)dirtyRect {
125
130
[progressIndicator setDoubleValue: progress];
126
131
}
127
132
}
128
- else {
133
+ else if (progressIndicator) {
129
134
// progress indicator < 0, destroy it
130
- [[dockTile.contentView.subviews objectAtIndex: 0 ]release];
131
- [dockTile.contentView release ];
132
- dockTile.contentView = NULL ;
135
+ objc_setAssociatedObject (dockTile, &kProgressIndicator , NULL , OBJC_ASSOCIATION_RETAIN_NONATOMIC );
136
+ [progressIndicator removeFromSuperview ];
137
+ [progressIndicator release ];
138
+ progressIndicator = NULL ;
139
+
140
+ // if we the one created the ImageView, we are the one who clean it
141
+ NSImageView * iv = objc_getAssociatedObject (dockTile,&kImageView );
142
+ if (iv) {
143
+ objc_setAssociatedObject (dockTile, &kImageView , NULL , OBJC_ASSOCIATION_RETAIN_NONATOMIC );
144
+ dockTile.contentView = NULL ;
145
+ [iv release ];
146
+ iv = NULL ;
147
+ }
133
148
}
134
149
135
150
[dockTile display ];
0 commit comments