Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Toggling extra based on checkbox’s state in prefpane

  • Loading branch information...
commit dce061ca553f2dc565e7f69cbfbfda610738bc77 1 parent 0ff0c99
authored
59 Preference Pane/English.lproj/SolcounterPref.xib
@@ -12,7 +12,7 @@
12 12 </object>
13 13 <object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
14 14 <bool key="EncodedWithXMLCoder">YES</bool>
15   - <integer value="12"/>
  15 + <integer value="6"/>
16 16 </object>
17 17 <object class="NSArray" key="IBDocument.PluginDependencies">
18 18 <bool key="EncodedWithXMLCoder">YES</bool>
@@ -55,13 +55,13 @@
55 55 <int key="NSvFlags">256</int>
56 56 <object class="NSMutableArray" key="NSSubviews">
57 57 <bool key="EncodedWithXMLCoder">YES</bool>
58   - <object class="NSButton" id="742961861">
  58 + <object class="NSButton" id="938817885">
59 59 <reference key="NSNextResponder" ref="1037298196"/>
60 60 <int key="NSvFlags">268</int>
61 61 <string key="NSFrame">{{18, 18}, {207, 18}}</string>
62 62 <reference key="NSSuperview" ref="1037298196"/>
63 63 <bool key="NSEnabled">YES</bool>
64   - <object class="NSButtonCell" key="NSCell" id="860083014">
  64 + <object class="NSButtonCell" key="NSCell" id="362444410">
65 65 <int key="NSCellFlags">67239424</int>
66 66 <int key="NSCellFlags2">0</int>
67 67 <string key="NSContents">Show Solcounter in menu bar</string>
@@ -70,7 +70,7 @@
70 70 <double key="NSSize">13</double>
71 71 <int key="NSfFlags">1044</int>
72 72 </object>
73   - <reference key="NSControlView" ref="742961861"/>
  73 + <reference key="NSControlView" ref="938817885"/>
74 74 <int key="NSButtonFlags">1211912703</int>
75 75 <int key="NSButtonFlags2">2</int>
76 76 <object class="NSCustomResource" key="NSNormalImage">
@@ -108,11 +108,19 @@
108 108 </object>
109 109 <object class="IBConnectionRecord">
110 110 <object class="IBActionConnection" key="connection">
111   - <string key="label">toggleActivation:</string>
  111 + <string key="label">toggleExtra:</string>
112 112 <reference key="source" ref="294453543"/>
113   - <reference key="destination" ref="742961861"/>
  113 + <reference key="destination" ref="938817885"/>
114 114 </object>
115   - <int key="connectionID">101</int>
  115 + <int key="connectionID">103</int>
  116 + </object>
  117 + <object class="IBConnectionRecord">
  118 + <object class="IBOutletConnection" key="connection">
  119 + <string key="label">extraEnabled</string>
  120 + <reference key="source" ref="294453543"/>
  121 + <reference key="destination" ref="938817885"/>
  122 + </object>
  123 + <int key="connectionID">104</int>
116 124 </object>
117 125 </object>
118 126 <object class="IBMutableOrderedSet" key="objectRecords">
@@ -151,7 +159,7 @@
151 159 <reference key="object" ref="1037298196"/>
152 160 <object class="NSMutableArray" key="children">
153 161 <bool key="EncodedWithXMLCoder">YES</bool>
154   - <reference ref="742961861"/>
  162 + <reference ref="938817885"/>
155 163 </object>
156 164 <reference key="parent" ref="660800786"/>
157 165 </object>
@@ -163,17 +171,17 @@
163 171 </object>
164 172 <object class="IBObjectRecord">
165 173 <int key="objectID">99</int>
166   - <reference key="object" ref="742961861"/>
  174 + <reference key="object" ref="938817885"/>
167 175 <object class="NSMutableArray" key="children">
168 176 <bool key="EncodedWithXMLCoder">YES</bool>
169   - <reference ref="860083014"/>
  177 + <reference ref="362444410"/>
170 178 </object>
171 179 <reference key="parent" ref="1037298196"/>
172 180 </object>
173 181 <object class="IBObjectRecord">
174 182 <int key="objectID">100</int>
175   - <reference key="object" ref="860083014"/>
176   - <reference key="parent" ref="742961861"/>
  183 + <reference key="object" ref="362444410"/>
  184 + <reference key="parent" ref="938817885"/>
177 185 </object>
178 186 </object>
179 187 </object>
@@ -197,9 +205,9 @@
197 205 <bool key="EncodedWithXMLCoder">YES</bool>
198 206 <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
199 207 <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
200   - <string>{{351, 423}, {668, 54}}</string>
  208 + <string>{{321, 421}, {668, 54}}</string>
201 209 <string>com.apple.InterfaceBuilder.CocoaPlugin</string>
202   - <string>{{351, 423}, {668, 54}}</string>
  210 + <string>{{321, 421}, {668, 54}}</string>
203 211 <integer value="1"/>
204 212 <integer value="1"/>
205 213 <string>{224.664, 10}</string>
@@ -224,7 +232,7 @@
224 232 </object>
225 233 </object>
226 234 <nil key="sourceID"/>
227   - <int key="maxID">101</int>
  235 + <int key="maxID">104</int>
228 236 </object>
229 237 <object class="IBClassDescriber" key="IBDocument.Classes">
230 238 <object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -249,16 +257,27 @@
249 257 <string key="className">YRSolcounterPref</string>
250 258 <string key="superclassName">NSPreferencePane</string>
251 259 <object class="NSMutableDictionary" key="actions">
252   - <string key="NS.key.0">toggleActivation:</string>
  260 + <string key="NS.key.0">toggleExtra:</string>
253 261 <string key="NS.object.0">id</string>
254 262 </object>
255 263 <object class="NSMutableDictionary" key="actionInfosByName">
256   - <string key="NS.key.0">toggleActivation:</string>
  264 + <string key="NS.key.0">toggleExtra:</string>
257 265 <object class="IBActionInfo" key="NS.object.0">
258   - <string key="name">toggleActivation:</string>
  266 + <string key="name">toggleExtra:</string>
259 267 <string key="candidateClassName">id</string>
260 268 </object>
261 269 </object>
  270 + <object class="NSMutableDictionary" key="outlets">
  271 + <string key="NS.key.0">extraEnabled</string>
  272 + <string key="NS.object.0">NSButton</string>
  273 + </object>
  274 + <object class="NSMutableDictionary" key="toOneOutletInfosByName">
  275 + <string key="NS.key.0">extraEnabled</string>
  276 + <object class="IBToOneOutletInfo" key="NS.object.0">
  277 + <string key="name">extraEnabled</string>
  278 + <string key="candidateClassName">NSButton</string>
  279 + </object>
  280 + </object>
262 281 <object class="IBClassDescriptionSource" key="sourceIdentifier">
263 282 <string key="majorKey">IBProjectSource</string>
264 283 <string key="minorKey">YRSolcounterPref.h</string>
@@ -345,7 +364,7 @@
345 364 <object class="IBPartialClassDescription">
346 365 <string key="className">NSControl</string>
347 366 <string key="superclassName">NSView</string>
348   - <object class="IBClassDescriptionSource" key="sourceIdentifier" id="533327388">
  367 + <object class="IBClassDescriptionSource" key="sourceIdentifier" id="13902790">
349 368 <string key="majorKey">IBFrameworkSource</string>
350 369 <string key="minorKey">AppKit.framework/Headers/NSControl.h</string>
351 370 </object>
@@ -387,7 +406,7 @@
387 406 </object>
388 407 <object class="IBPartialClassDescription">
389 408 <string key="className">NSObject</string>
390   - <reference key="sourceIdentifier" ref="533327388"/>
  409 + <reference key="sourceIdentifier" ref="13902790"/>
391 410 </object>
392 411 <object class="IBPartialClassDescription">
393 412 <string key="className">NSObject</string>
8 Preference Pane/YRSolcounterPref.h
@@ -11,10 +11,14 @@
11 11
12 12 @interface YRSolcounterPref : NSPreferencePane
13 13 {
14   -
  14 + IBOutlet NSButton *extraEnabled;
15 15 }
16 16
17 17 - (void) mainViewDidLoad;
18   -- (IBAction) toggleActivation:(id)sender;
  18 +- (IBAction) toggleExtra:(id)sender;
  19 +
  20 +- (void)loadExtra:(NSString *)extraPath;
  21 +- (void)removeExtra:(NSString *)extraPath;
  22 +- (BOOL)isExtraLoaded:(NSString *)extraPath;
19 23
20 24 @end
79 Preference Pane/YRSolcounterPref.m
@@ -8,17 +8,90 @@
8 8
9 9 #import "YRSolcounterPref.h"
10 10
  11 +// Some declarations for private CoreMenuExtra functions:
  12 +typedef struct _OpaqueMenuExtra OpaqueMenuExtra;
  13 +OSStatus CoreMenuExtraGetMenuExtra(CFStringRef identifier, OpaqueMenuExtra **menuExtraOut);
  14 +OSStatus CoreMenuExtraAddMenuExtra(CFURLRef path, SInt32 position, void *_0, void *_1, void *_2, void *_3);
  15 +OSStatus CoreMenuExtraRemoveMenuExtra(OpaqueMenuExtra *menuExtraIn, void *_0);
  16 +#define CMEGetExtra(identifier, menuExtraOut) \
  17 + CoreMenuExtraGetMenuExtra(identifier, menuExtraOut)//;
  18 +#define CMEAddExtra(path, position) \
  19 + CoreMenuExtraAddMenuExtra(path, position, NULL, NULL, NULL, NULL)//;
  20 +#define CMERemoveExtra(menuExtraIn) \
  21 + CoreMenuExtraRemoveMenuExtra(menuExtraIn, NULL)//;
  22 +// (From http://www.cocoadev.com/index.pl?CoreMenuExtra)
  23 +
11 24
12 25 @implementation YRSolcounterPref
13 26
14 27 - (void) mainViewDidLoad
15 28 {
16   - NSLog(@"- mainViewDidLoad");
  29 + NSString *extraPath = [[self bundle] pathForResource: @"Solcounter"
  30 + ofType: @"menu"
  31 + inDirectory: @""];
  32 +
  33 + if ([self isExtraLoaded:extraPath])
  34 + [extraEnabled setState:NSOnState];
17 35 }
18 36
19   -- (IBAction) toggleActivation:(id)sender
  37 +- (IBAction)toggleExtra:(id)sender
20 38 {
21   - NSLog(@"- toggleActivation: %@", sender);
  39 + NSString *extraPath = [[self bundle] pathForResource: @"Solcounter"
  40 + ofType: @"menu"
  41 + inDirectory: @""];
  42 +
  43 + if (([sender state] == NSOnState) && ![self isExtraLoaded:extraPath]) {
  44 + [self loadExtra:extraPath];
  45 + if (![self isExtraLoaded:extraPath])
  46 + [sender setState:NSOffState];
  47 + } else //»
  48 + if (([sender state] == NSOffState) && [self isExtraLoaded:extraPath]) {
  49 + [self removeExtra:extraPath];
  50 + if ([self isExtraLoaded:extraPath])
  51 + [sender setState:NSOnState];
  52 + }
  53 +}
  54 +
  55 +- (void)loadExtra:(NSString *)extraPath
  56 +{ int sleepCount;
  57 + NSURL *extraURL = [NSURL fileURLWithPath: extraPath];
  58 + CMEAddExtra((CFURLRef)extraURL, 0);
  59 +
  60 + sleepCount = 0;
  61 + while (sleepCount < 1000000) {
  62 + if ([self isExtraLoaded: extraPath]) return;
  63 + usleep(sleepCount += 250000); }
  64 +
  65 + NSString *crackerPath = [[self bundle] pathForResource: @"MenuCracker"
  66 + ofType: @"menu"
  67 + inDirectory: @""];
  68 +
  69 + if (extraPath != crackerPath && ![self isExtraLoaded: extraPath])
  70 + [self loadExtra:crackerPath];
  71 +
  72 + CMEAddExtra((CFURLRef)extraURL, 0);
  73 +
  74 + sleepCount = 0;
  75 + while (sleepCount < 1000000) {
  76 + if ([self isExtraLoaded: extraPath]) return;
  77 + usleep(sleepCount += 250000); }
  78 +}
  79 +
  80 +- (void)removeExtra:(NSString *)extraPath
  81 +{ OpaqueMenuExtra *extra;
  82 + NSString *extraID = [[NSBundle bundleWithPath: extraPath]
  83 + objectForInfoDictionaryKey: @"CFBundleIdentifier"];
  84 +
  85 + if ((CMEGetExtra((CFStringRef)extraID, &extra) == 0) && extra)
  86 + CMERemoveExtra(extra);
  87 +}
  88 +
  89 +- (BOOL)isExtraLoaded:(NSString *)extraPath
  90 +{ OpaqueMenuExtra *extra;
  91 + NSString *extraID = [[NSBundle bundleWithPath: extraPath]
  92 + objectForInfoDictionaryKey: @"CFBundleIdentifier"];
  93 +
  94 + return CMEGetExtra((CFStringRef) extraID, &extra) == 0 && extra;
22 95 }
23 96
24 97 @end

0 comments on commit dce061c

Please sign in to comment.
Something went wrong with that request. Please try again.