Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Toggling extra based on checkbox’s state in prefpane

  • Loading branch information...
commit dce061ca553f2dc565e7f69cbfbfda610738bc77 1 parent 0ff0c99
@ELLIOTTCABLE authored
View
59 Preference Pane/English.lproj/SolcounterPref.xib
@@ -12,7 +12,7 @@
</object>
<object class="NSMutableArray" key="IBDocument.EditedObjectIDs">
<bool key="EncodedWithXMLCoder">YES</bool>
- <integer value="12"/>
+ <integer value="6"/>
</object>
<object class="NSArray" key="IBDocument.PluginDependencies">
<bool key="EncodedWithXMLCoder">YES</bool>
@@ -55,13 +55,13 @@
<int key="NSvFlags">256</int>
<object class="NSMutableArray" key="NSSubviews">
<bool key="EncodedWithXMLCoder">YES</bool>
- <object class="NSButton" id="742961861">
+ <object class="NSButton" id="938817885">
<reference key="NSNextResponder" ref="1037298196"/>
<int key="NSvFlags">268</int>
<string key="NSFrame">{{18, 18}, {207, 18}}</string>
<reference key="NSSuperview" ref="1037298196"/>
<bool key="NSEnabled">YES</bool>
- <object class="NSButtonCell" key="NSCell" id="860083014">
+ <object class="NSButtonCell" key="NSCell" id="362444410">
<int key="NSCellFlags">67239424</int>
<int key="NSCellFlags2">0</int>
<string key="NSContents">Show Solcounter in menu bar</string>
@@ -70,7 +70,7 @@
<double key="NSSize">13</double>
<int key="NSfFlags">1044</int>
</object>
- <reference key="NSControlView" ref="742961861"/>
+ <reference key="NSControlView" ref="938817885"/>
<int key="NSButtonFlags">1211912703</int>
<int key="NSButtonFlags2">2</int>
<object class="NSCustomResource" key="NSNormalImage">
@@ -108,11 +108,19 @@
</object>
<object class="IBConnectionRecord">
<object class="IBActionConnection" key="connection">
- <string key="label">toggleActivation:</string>
+ <string key="label">toggleExtra:</string>
<reference key="source" ref="294453543"/>
- <reference key="destination" ref="742961861"/>
+ <reference key="destination" ref="938817885"/>
</object>
- <int key="connectionID">101</int>
+ <int key="connectionID">103</int>
+ </object>
+ <object class="IBConnectionRecord">
+ <object class="IBOutletConnection" key="connection">
+ <string key="label">extraEnabled</string>
+ <reference key="source" ref="294453543"/>
+ <reference key="destination" ref="938817885"/>
+ </object>
+ <int key="connectionID">104</int>
</object>
</object>
<object class="IBMutableOrderedSet" key="objectRecords">
@@ -151,7 +159,7 @@
<reference key="object" ref="1037298196"/>
<object class="NSMutableArray" key="children">
<bool key="EncodedWithXMLCoder">YES</bool>
- <reference ref="742961861"/>
+ <reference ref="938817885"/>
</object>
<reference key="parent" ref="660800786"/>
</object>
@@ -163,17 +171,17 @@
</object>
<object class="IBObjectRecord">
<int key="objectID">99</int>
- <reference key="object" ref="742961861"/>
+ <reference key="object" ref="938817885"/>
<object class="NSMutableArray" key="children">
<bool key="EncodedWithXMLCoder">YES</bool>
- <reference ref="860083014"/>
+ <reference ref="362444410"/>
</object>
<reference key="parent" ref="1037298196"/>
</object>
<object class="IBObjectRecord">
<int key="objectID">100</int>
- <reference key="object" ref="860083014"/>
- <reference key="parent" ref="742961861"/>
+ <reference key="object" ref="362444410"/>
+ <reference key="parent" ref="938817885"/>
</object>
</object>
</object>
@@ -197,9 +205,9 @@
<bool key="EncodedWithXMLCoder">YES</bool>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
- <string>{{351, 423}, {668, 54}}</string>
+ <string>{{321, 421}, {668, 54}}</string>
<string>com.apple.InterfaceBuilder.CocoaPlugin</string>
- <string>{{351, 423}, {668, 54}}</string>
+ <string>{{321, 421}, {668, 54}}</string>
<integer value="1"/>
<integer value="1"/>
<string>{224.664, 10}</string>
@@ -224,7 +232,7 @@
</object>
</object>
<nil key="sourceID"/>
- <int key="maxID">101</int>
+ <int key="maxID">104</int>
</object>
<object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
@@ -249,16 +257,27 @@
<string key="className">YRSolcounterPref</string>
<string key="superclassName">NSPreferencePane</string>
<object class="NSMutableDictionary" key="actions">
- <string key="NS.key.0">toggleActivation:</string>
+ <string key="NS.key.0">toggleExtra:</string>
<string key="NS.object.0">id</string>
</object>
<object class="NSMutableDictionary" key="actionInfosByName">
- <string key="NS.key.0">toggleActivation:</string>
+ <string key="NS.key.0">toggleExtra:</string>
<object class="IBActionInfo" key="NS.object.0">
- <string key="name">toggleActivation:</string>
+ <string key="name">toggleExtra:</string>
<string key="candidateClassName">id</string>
</object>
</object>
+ <object class="NSMutableDictionary" key="outlets">
+ <string key="NS.key.0">extraEnabled</string>
+ <string key="NS.object.0">NSButton</string>
+ </object>
+ <object class="NSMutableDictionary" key="toOneOutletInfosByName">
+ <string key="NS.key.0">extraEnabled</string>
+ <object class="IBToOneOutletInfo" key="NS.object.0">
+ <string key="name">extraEnabled</string>
+ <string key="candidateClassName">NSButton</string>
+ </object>
+ </object>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">YRSolcounterPref.h</string>
@@ -345,7 +364,7 @@
<object class="IBPartialClassDescription">
<string key="className">NSControl</string>
<string key="superclassName">NSView</string>
- <object class="IBClassDescriptionSource" key="sourceIdentifier" id="533327388">
+ <object class="IBClassDescriptionSource" key="sourceIdentifier" id="13902790">
<string key="majorKey">IBFrameworkSource</string>
<string key="minorKey">AppKit.framework/Headers/NSControl.h</string>
</object>
@@ -387,7 +406,7 @@
</object>
<object class="IBPartialClassDescription">
<string key="className">NSObject</string>
- <reference key="sourceIdentifier" ref="533327388"/>
+ <reference key="sourceIdentifier" ref="13902790"/>
</object>
<object class="IBPartialClassDescription">
<string key="className">NSObject</string>
View
8 Preference Pane/YRSolcounterPref.h
@@ -11,10 +11,14 @@
@interface YRSolcounterPref : NSPreferencePane
{
-
+ IBOutlet NSButton *extraEnabled;
}
- (void) mainViewDidLoad;
-- (IBAction) toggleActivation:(id)sender;
+- (IBAction) toggleExtra:(id)sender;
+
+- (void)loadExtra:(NSString *)extraPath;
+- (void)removeExtra:(NSString *)extraPath;
+- (BOOL)isExtraLoaded:(NSString *)extraPath;
@end
View
79 Preference Pane/YRSolcounterPref.m
@@ -8,17 +8,90 @@
#import "YRSolcounterPref.h"
+// Some declarations for private CoreMenuExtra functions:
+typedef struct _OpaqueMenuExtra OpaqueMenuExtra;
+OSStatus CoreMenuExtraGetMenuExtra(CFStringRef identifier, OpaqueMenuExtra **menuExtraOut);
+OSStatus CoreMenuExtraAddMenuExtra(CFURLRef path, SInt32 position, void *_0, void *_1, void *_2, void *_3);
+OSStatus CoreMenuExtraRemoveMenuExtra(OpaqueMenuExtra *menuExtraIn, void *_0);
+#define CMEGetExtra(identifier, menuExtraOut) \
+ CoreMenuExtraGetMenuExtra(identifier, menuExtraOut)//;
+#define CMEAddExtra(path, position) \
+ CoreMenuExtraAddMenuExtra(path, position, NULL, NULL, NULL, NULL)//;
+#define CMERemoveExtra(menuExtraIn) \
+ CoreMenuExtraRemoveMenuExtra(menuExtraIn, NULL)//;
+// (From http://www.cocoadev.com/index.pl?CoreMenuExtra)
+
@implementation YRSolcounterPref
- (void) mainViewDidLoad
{
- NSLog(@"- mainViewDidLoad");
+ NSString *extraPath = [[self bundle] pathForResource: @"Solcounter"
+ ofType: @"menu"
+ inDirectory: @""];
+
+ if ([self isExtraLoaded:extraPath])
+ [extraEnabled setState:NSOnState];
}
-- (IBAction) toggleActivation:(id)sender
+- (IBAction)toggleExtra:(id)sender
{
- NSLog(@"- toggleActivation: %@", sender);
+ NSString *extraPath = [[self bundle] pathForResource: @"Solcounter"
+ ofType: @"menu"
+ inDirectory: @""];
+
+ if (([sender state] == NSOnState) && ![self isExtraLoaded:extraPath]) {
+ [self loadExtra:extraPath];
+ if (![self isExtraLoaded:extraPath])
+ [sender setState:NSOffState];
+ } else //»
+ if (([sender state] == NSOffState) && [self isExtraLoaded:extraPath]) {
+ [self removeExtra:extraPath];
+ if ([self isExtraLoaded:extraPath])
+ [sender setState:NSOnState];
+ }
+}
+
+- (void)loadExtra:(NSString *)extraPath
+{ int sleepCount;
+ NSURL *extraURL = [NSURL fileURLWithPath: extraPath];
+ CMEAddExtra((CFURLRef)extraURL, 0);
+
+ sleepCount = 0;
+ while (sleepCount < 1000000) {
+ if ([self isExtraLoaded: extraPath]) return;
+ usleep(sleepCount += 250000); }
+
+ NSString *crackerPath = [[self bundle] pathForResource: @"MenuCracker"
+ ofType: @"menu"
+ inDirectory: @""];
+
+ if (extraPath != crackerPath && ![self isExtraLoaded: extraPath])
+ [self loadExtra:crackerPath];
+
+ CMEAddExtra((CFURLRef)extraURL, 0);
+
+ sleepCount = 0;
+ while (sleepCount < 1000000) {
+ if ([self isExtraLoaded: extraPath]) return;
+ usleep(sleepCount += 250000); }
+}
+
+- (void)removeExtra:(NSString *)extraPath
+{ OpaqueMenuExtra *extra;
+ NSString *extraID = [[NSBundle bundleWithPath: extraPath]
+ objectForInfoDictionaryKey: @"CFBundleIdentifier"];
+
+ if ((CMEGetExtra((CFStringRef)extraID, &extra) == 0) && extra)
+ CMERemoveExtra(extra);
+}
+
+- (BOOL)isExtraLoaded:(NSString *)extraPath
+{ OpaqueMenuExtra *extra;
+ NSString *extraID = [[NSBundle bundleWithPath: extraPath]
+ objectForInfoDictionaryKey: @"CFBundleIdentifier"];
+
+ return CMEGetExtra((CFStringRef) extraID, &extra) == 0 && extra;
}
@end
Please sign in to comment.
Something went wrong with that request. Please try again.