Browse Source

Fix status bar image color when the interface style changes

Thomas Buck 8 years ago
parent
commit
a88d3999a9
2 changed files with 62 additions and 32 deletions
  1. 61
    31
      CaseLights/AppDelegate.m
  2. 1
    1
      CaseLights/Info.plist

+ 61
- 31
CaseLights/AppDelegate.m View File

@@ -112,37 +112,6 @@
112 112
 @synthesize serial, lastLEDMode, microphone;
113 113
 @synthesize menuItemColor;
114 114
 
115
-+ (NSImage *)tintedImage:(NSImage *)image WithColor:(NSColor *)tint {
116
-    NSSize size = [image size];
117
-    NSRect imageBounds = NSMakeRect(0, 0, size.width, size.height);
118
-    
119
-    NSImage *copiedImage = [image copy];
120
-    NSColor *copiedTint = [tint copy];
121
-    
122
-    // Fix colors for different interface styles
123
-    NSString *osxMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
124
-    CGFloat r, g, b, a;
125
-    [copiedTint getRed:&r green:&g blue:&b alpha:&a];
126
-    if ((osxMode != nil) && ([osxMode isEqualToString:@"Dark"])) {
127
-        // Dark mode
128
-        if ((r < 0.001f) && (g < 0.001f) && (b < 0.001f)) {
129
-            copiedTint = [NSColor whiteColor];
130
-        }
131
-    } else {
132
-        // Normal mode
133
-        if ((r > 0.999f) && (g > 0.999f) && (b > 0.999f)) {
134
-            copiedTint = [NSColor blackColor];
135
-        }
136
-    }
137
-    
138
-    [copiedImage lockFocus];
139
-    [copiedTint set];
140
-    NSRectFillUsingOperation(imageBounds, NSCompositeSourceAtop);
141
-    [copiedImage unlockFocus];
142
-    
143
-    return copiedImage;
144
-}
145
-
146 115
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
147 116
     srand((unsigned)time(NULL));
148 117
     [AudioVisualizer setDelegate:self];
@@ -158,6 +127,7 @@
158 127
 #ifdef COLORED_MENU_BAR_ICON
159 128
     [statusImage setTemplate:NO];
160 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];
161 131
 #else
162 132
     [statusImage setTemplate:YES];
163 133
     [statusItem setImage:statusImage];
@@ -964,6 +934,12 @@
964 934
     [application orderFrontStandardAboutPanel:self];
965 935
 }
966 936
 
937
+#ifdef COLORED_MENU_BAR_ICON
938
+- (void)darkModeChanged:(NSNotification *)notification {
939
+    [statusItem setImage:[AppDelegate tintedImage:statusImage WithColor:nil]];
940
+}
941
+#endif
942
+
967 943
 // ------------------------------------------------------
968 944
 // ----------------- Microphone Delegate ----------------
969 945
 // ------------------------------------------------------
@@ -1189,6 +1165,60 @@
1189 1165
 // --------------------- Utilities ---------------------
1190 1166
 // -----------------------------------------------------
1191 1167
 
1168
+#ifdef COLORED_MENU_BAR_ICON
1169
++ (NSImage *)tintedImage:(NSImage *)image WithColor:(NSColor *)tint {
1170
+    NSSize size = [image size];
1171
+    NSRect imageBounds = NSMakeRect(0, 0, size.width, size.height);
1172
+    
1173
+    NSString *osxMode = [[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"];
1174
+    BOOL darkMode = ((osxMode != nil) && ([osxMode isEqualToString:@"Dark"])) ? YES : NO;
1175
+    
1176
+    NSImage *copiedImage = [image copy];
1177
+    static NSColor *lastTint = nil;
1178
+    NSColor *copiedTint = nil;
1179
+    if (tint != nil) {
1180
+        // Just use the provided color
1181
+        copiedTint = [tint copy];
1182
+        lastTint = copiedTint;
1183
+    } else {
1184
+        // Try to use the same color as the last time
1185
+        if (lastTint != nil) {
1186
+            copiedTint = lastTint;
1187
+        } else {
1188
+#ifdef DEBUG
1189
+            NSLog(@"Trying to set last status bar icon color, but was not set!\n");
1190
+#endif
1191
+            
1192
+            if (darkMode == YES) {
1193
+                copiedTint = [NSColor whiteColor];
1194
+            } else {
1195
+                copiedTint = [NSColor blackColor];
1196
+            }
1197
+        }
1198
+    }
1199
+    
1200
+    // Fix colors for different interface styles
1201
+    CGFloat r, g, b, a;
1202
+    [copiedTint getRed:&r green:&g blue:&b alpha:&a];
1203
+    if (darkMode == YES) {
1204
+        if ((r < 0.001f) && (g < 0.001f) && (b < 0.001f)) {
1205
+            copiedTint = [NSColor whiteColor];
1206
+        }
1207
+    } else {
1208
+        if ((r > 0.999f) && (g > 0.999f) && (b > 0.999f)) {
1209
+            copiedTint = [NSColor blackColor];
1210
+        }
1211
+    }
1212
+    
1213
+    [copiedImage lockFocus];
1214
+    [copiedTint set];
1215
+    NSRectFillUsingOperation(imageBounds, NSCompositeSourceAtop);
1216
+    [copiedImage unlockFocus];
1217
+    
1218
+    return copiedImage;
1219
+}
1220
+#endif
1221
+
1192 1222
 + (double)map:(double)val FromMin:(double)fmin FromMax:(double)fmax ToMin:(double)tmin ToMax:(double)tmax {
1193 1223
     double norm = (val - fmin) / (fmax - fmin);
1194 1224
     return (norm * (tmax - tmin)) + tmin;

+ 1
- 1
CaseLights/Info.plist View File

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

Loading…
Cancel
Save