Browse Source

run-time option for icon colorization

Thomas Buck 9 years ago
parent
commit
fe0dc0986c
3 changed files with 53 additions and 24 deletions
  1. 45
    23
      CaseLights/AppDelegate.m
  2. 7
    0
      CaseLights/Base.lproj/MainMenu.xib
  3. 1
    1
      CaseLights/Info.plist

+ 45
- 23
CaseLights/AppDelegate.m View File

20
 #define PREF_BRIGHTNESS @"Brightness"
20
 #define PREF_BRIGHTNESS @"Brightness"
21
 #define PREF_COLOR @"ManualColor"
21
 #define PREF_COLOR @"ManualColor"
22
 #define PREF_SENSITIVITY @"Sensitivity"
22
 #define PREF_SENSITIVITY @"Sensitivity"
23
+#define PREF_COLORED_ICON @"ColorizeIcon"
23
 
24
 
24
 #define TEXT_MANUAL @"Select..."
25
 #define TEXT_MANUAL @"Select..."
25
 #define TEXT_CPU_USAGE @"CPU Usage"
26
 #define TEXT_CPU_USAGE @"CPU Usage"
62
 #define MENU_ITEM_TAG_NOTHING -1
63
 #define MENU_ITEM_TAG_NOTHING -1
63
 #define MENU_ITEM_TAG_AUDIO -2
64
 #define MENU_ITEM_TAG_AUDIO -2
64
 
65
 
65
-// Define this to color the status bar icon according to the current RGB LED value
66
-#define COLORED_MENU_BAR_ICON
67
-
68
 @interface AppDelegate ()
66
 @interface AppDelegate ()
69
 
67
 
70
 @property (weak) IBOutlet NSMenu *statusMenu;
68
 @property (weak) IBOutlet NSMenu *statusMenu;
86
 @property (weak) IBOutlet NSSlider *sensitivitySlider;
84
 @property (weak) IBOutlet NSSlider *sensitivitySlider;
87
 @property (weak) IBOutlet NSMenuItem *sensitivityLabel;
85
 @property (weak) IBOutlet NSMenuItem *sensitivityLabel;
88
 @property (weak) IBOutlet NSMenuItem *sensitivityMenu;
86
 @property (weak) IBOutlet NSMenuItem *sensitivityMenu;
87
+@property (weak) IBOutlet NSMenuItem *colorizeIconItem;
89
 
88
 
90
 @property (strong) NSMenuItem *menuItemColor;
89
 @property (strong) NSMenuItem *menuItemColor;
91
 
90
 
110
 @synthesize statusItem, statusImage;
109
 @synthesize statusItem, statusImage;
111
 @synthesize staticColors, animation;
110
 @synthesize staticColors, animation;
112
 @synthesize serial, lastLEDMode, microphone;
111
 @synthesize serial, lastLEDMode, microphone;
113
-@synthesize menuItemColor;
112
+@synthesize menuItemColor, colorizeIconItem;
114
 
113
 
115
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
114
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
116
     srand((unsigned)time(NULL));
115
     srand((unsigned)time(NULL));
121
     animation = nil;
120
     animation = nil;
122
     microphone = nil;
121
     microphone = nil;
123
     
122
     
124
-    // Prepare status bar menu
125
-    statusImage = [NSImage imageNamed:@"MenuIcon"];
126
-    statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
127
-#ifdef COLORED_MENU_BAR_ICON
128
-    [statusImage setTemplate:NO];
129
-    [statusItem setImage:[AppDelegate tintedImage:statusImage WithColor:[NSColor colorWithCalibratedRed:0.0f green:0.0f blue:0.0f alpha:1.0f]]];
130
     [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(darkModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil];
123
     [[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(darkModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil];
131
-#else
132
-    [statusImage setTemplate:YES];
133
-    [statusItem setImage:statusImage];
134
-#endif
135
-    [statusItem setMenu:statusMenu];
136
     
124
     
137
     // Set default configuration values, load existing ones
125
     // Set default configuration values, load existing ones
138
     NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
126
     NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
141
     [appDefaults setObject:@"" forKey:PREF_LED_MODE];
129
     [appDefaults setObject:@"" forKey:PREF_LED_MODE];
142
     [appDefaults setObject:[NSNumber numberWithFloat:50.0] forKey:PREF_BRIGHTNESS];
130
     [appDefaults setObject:[NSNumber numberWithFloat:50.0] forKey:PREF_BRIGHTNESS];
143
     [appDefaults setObject:[NSNumber numberWithFloat:100.0] forKey:PREF_SENSITIVITY];
131
     [appDefaults setObject:[NSNumber numberWithFloat:100.0] forKey:PREF_SENSITIVITY];
132
+    [appDefaults setObject:[NSNumber numberWithBool:YES] forKey:PREF_COLORED_ICON];
144
     [store registerDefaults:appDefaults];
133
     [store registerDefaults:appDefaults];
145
     [store synchronize];
134
     [store synchronize];
146
     NSString *savedPort = [store stringForKey:PREF_SERIAL_PORT];
135
     NSString *savedPort = [store stringForKey:PREF_SERIAL_PORT];
153
     if (lastColorData != nil) {
142
     if (lastColorData != nil) {
154
         lastColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:lastColorData];
143
         lastColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:lastColorData];
155
     }
144
     }
145
+    BOOL coloredIcon = [store boolForKey:PREF_COLORED_ICON];
146
+    
147
+    // Prepare status bar menu
148
+    statusImage = [NSImage imageNamed:@"MenuIcon"];
149
+    statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
150
+    if (coloredIcon == YES) {
151
+        [statusImage setTemplate:NO];
152
+        [statusItem setImage:[AppDelegate tintedImage:statusImage WithColor:[NSColor colorWithCalibratedRed:0.0f green:0.0f blue:0.0f alpha:1.0f]]];
153
+        [colorizeIconItem setState:NSOnState];
154
+    } else {
155
+        [statusImage setTemplate:YES];
156
+        [statusItem setImage:statusImage];
157
+        [colorizeIconItem setState:NSOffState];
158
+    }
159
+    [statusItem setMenu:statusMenu];
156
     
160
     
157
     // Prepare brightness menu
161
     // Prepare brightness menu
158
     brightnessItem.view = brightnessSlider;
162
     brightnessItem.view = brightnessSlider;
471
 #endif
475
 #endif
472
     }
476
     }
473
     
477
     
474
-#ifdef COLORED_MENU_BAR_ICON
475
-    [statusItem setImage:[AppDelegate tintedImage:statusImage WithColor:[NSColor colorWithCalibratedRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:1.0f]]];
476
-#endif
478
+    if (colorizeIconItem.state == NSOnState) {
479
+        [statusItem setImage:[AppDelegate tintedImage:statusImage WithColor:[NSColor colorWithCalibratedRed:r / 255.0f green:g / 255.0f blue:b / 255.0f alpha:1.0f]]];
480
+    }
481
+}
482
+
483
+- (IBAction)colorizeIconSelected:(NSMenuItem *)sender {
484
+    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
485
+    if ([sender state] == NSOffState) {
486
+        // Colorize icon
487
+        [statusImage setTemplate:NO];
488
+        [statusItem setImage:[AppDelegate tintedImage:statusImage WithColor:[NSColor colorWithCalibratedRed:0.0f green:0.0f blue:0.0f alpha:1.0f]]];
489
+        [colorizeIconItem setState:NSOnState];
490
+        [store setObject:[NSNumber numberWithBool:YES] forKey:PREF_COLORED_ICON];
491
+    } else {
492
+        // Don't colorize icon
493
+        [statusImage setTemplate:YES];
494
+        [statusItem setImage:statusImage];
495
+        [colorizeIconItem setState:NSOffState];
496
+        [store setObject:[NSNumber numberWithBool:NO] forKey:PREF_COLORED_ICON];
497
+    }
498
+    [store synchronize];
477
 }
499
 }
478
 
500
 
479
 - (IBAction)relistSerialPorts:(id)sender {
501
 - (IBAction)relistSerialPorts:(id)sender {
934
     [application orderFrontStandardAboutPanel:self];
956
     [application orderFrontStandardAboutPanel:self];
935
 }
957
 }
936
 
958
 
937
-#ifdef COLORED_MENU_BAR_ICON
938
 - (void)darkModeChanged:(NSNotification *)notification {
959
 - (void)darkModeChanged:(NSNotification *)notification {
939
-    [statusItem setImage:[AppDelegate tintedImage:statusImage WithColor:nil]];
960
+    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
961
+    BOOL colorize = [store boolForKey:PREF_COLORED_ICON];
962
+    if (colorize == YES) {
963
+        [statusItem setImage:[AppDelegate tintedImage:statusImage WithColor:nil]];
964
+    }
940
 }
965
 }
941
-#endif
942
 
966
 
943
 // ------------------------------------------------------
967
 // ------------------------------------------------------
944
 // ----------------- Microphone Delegate ----------------
968
 // ----------------- Microphone Delegate ----------------
1165
 // --------------------- Utilities ---------------------
1189
 // --------------------- Utilities ---------------------
1166
 // -----------------------------------------------------
1190
 // -----------------------------------------------------
1167
 
1191
 
1168
-#ifdef COLORED_MENU_BAR_ICON
1169
 + (NSImage *)tintedImage:(NSImage *)image WithColor:(NSColor *)tint {
1192
 + (NSImage *)tintedImage:(NSImage *)image WithColor:(NSColor *)tint {
1170
     NSSize size = [image size];
1193
     NSSize size = [image size];
1171
     NSRect imageBounds = NSMakeRect(0, 0, size.width, size.height);
1194
     NSRect imageBounds = NSMakeRect(0, 0, size.width, size.height);
1217
     
1240
     
1218
     return copiedImage;
1241
     return copiedImage;
1219
 }
1242
 }
1220
-#endif
1221
 
1243
 
1222
 + (double)map:(double)val FromMin:(double)fmin FromMax:(double)fmax ToMin:(double)tmin ToMax:(double)tmax {
1244
 + (double)map:(double)val FromMin:(double)fmin FromMax:(double)fmax ToMin:(double)tmin ToMax:(double)tmax {
1223
     double norm = (val - fmin) / (fmax - fmin);
1245
     double norm = (val - fmin) / (fmax - fmin);

+ 7
- 0
CaseLights/Base.lproj/MainMenu.xib View File

20
                 <outlet property="brightnessSlider" destination="axk-la-AqI" id="J4R-Mw-KN7"/>
20
                 <outlet property="brightnessSlider" destination="axk-la-AqI" id="J4R-Mw-KN7"/>
21
                 <outlet property="buttonLights" destination="tLQ-BA-kGu" id="FZv-Ju-l7n"/>
21
                 <outlet property="buttonLights" destination="tLQ-BA-kGu" id="FZv-Ju-l7n"/>
22
                 <outlet property="buttonOff" destination="gT4-tm-Hvn" id="EEv-lf-Qo7"/>
22
                 <outlet property="buttonOff" destination="gT4-tm-Hvn" id="EEv-lf-Qo7"/>
23
+                <outlet property="colorizeIconItem" destination="F4i-Jg-lxm" id="TIi-7F-Pxz"/>
23
                 <outlet property="menuAnimations" destination="FJm-ND-abA" id="iGI-Hg-tms"/>
24
                 <outlet property="menuAnimations" destination="FJm-ND-abA" id="iGI-Hg-tms"/>
24
                 <outlet property="menuAudio" destination="KUj-jy-iwd" id="ct3-CS-KXG"/>
25
                 <outlet property="menuAudio" destination="KUj-jy-iwd" id="ct3-CS-KXG"/>
25
                 <outlet property="menuColors" destination="ZKz-ek-t6c" id="B2S-fy-Ca6"/>
26
                 <outlet property="menuColors" destination="ZKz-ek-t6c" id="B2S-fy-Ca6"/>
761
                         <action selector="relistSerialPorts:" target="Voe-Tx-rLC" id="iFU-PL-xR8"/>
762
                         <action selector="relistSerialPorts:" target="Voe-Tx-rLC" id="iFU-PL-xR8"/>
762
                     </connections>
763
                     </connections>
763
                 </menuItem>
764
                 </menuItem>
765
+                <menuItem title="Colorize Icon" id="F4i-Jg-lxm">
766
+                    <modifierMask key="keyEquivalentModifierMask"/>
767
+                    <connections>
768
+                        <action selector="colorizeIconSelected:" target="Voe-Tx-rLC" id="CC5-K4-QIW"/>
769
+                    </connections>
770
+                </menuItem>
764
                 <menuItem title="About CaseLights" id="GeK-ey-XEn">
771
                 <menuItem title="About CaseLights" id="GeK-ey-XEn">
765
                     <modifierMask key="keyEquivalentModifierMask"/>
772
                     <modifierMask key="keyEquivalentModifierMask"/>
766
                     <connections>
773
                     <connections>

+ 1
- 1
CaseLights/Info.plist View File

21
 	<key>CFBundleSignature</key>
21
 	<key>CFBundleSignature</key>
22
 	<string>????</string>
22
 	<string>????</string>
23
 	<key>CFBundleVersion</key>
23
 	<key>CFBundleVersion</key>
24
-	<string>409</string>
24
+	<string>410</string>
25
 	<key>LSApplicationCategoryType</key>
25
 	<key>LSApplicationCategoryType</key>
26
 	<string>public.app-category.utilities</string>
26
 	<string>public.app-category.utilities</string>
27
 	<key>LSMinimumSystemVersion</key>
27
 	<key>LSMinimumSystemVersion</key>

Loading…
Cancel
Save