Browse Source

Added manual color selection with NSColorPanel

Thomas Buck 9 years ago
parent
commit
ba3ecbec87
3 changed files with 94 additions and 1 deletions
  1. 2
    0
      CaseLights/AppDelegate.h
  2. 91
    0
      CaseLights/AppDelegate.m
  3. 1
    1
      CaseLights/Info.plist

+ 2
- 0
CaseLights/AppDelegate.h View File

@@ -28,6 +28,8 @@
28 28
 @property (weak) IBOutlet NSMenuItem *brightnessLabel;
29 29
 @property (weak) IBOutlet NSMenuItem *buttonLights;
30 30
 
31
+@property (strong) NSMenuItem *menuItemColor;
32
+
31 33
 - (void)clearDisplayUI;
32 34
 - (void)updateDisplayUI:(NSArray *)displayIDs;
33 35
 

+ 91
- 0
CaseLights/AppDelegate.m View File

@@ -17,7 +17,9 @@
17 17
 #define PREF_LIGHTS_STATE @"LightState"
18 18
 #define PREF_LED_MODE @"LEDMode"
19 19
 #define PREF_BRIGHTNESS @"Brightness"
20
+#define PREF_COLOR @"ManualColor"
20 21
 
22
+#define TEXT_MANUAL @"Select..."
21 23
 #define TEXT_CPU_USAGE @"CPU Usage"
22 24
 #define TEXT_RAM_USAGE @"RAM Usage"
23 25
 #define TEXT_GPU_USAGE @"GPU Usage"
@@ -72,6 +74,7 @@
72 74
 @synthesize statusItem, statusImage;
73 75
 @synthesize staticColors, animation;
74 76
 @synthesize serial, lastLEDMode;
77
+@synthesize menuItemColor;
75 78
 
76 79
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
77 80
     srand((unsigned)time(NULL));
@@ -99,6 +102,11 @@
99 102
     BOOL turnOnLights = [store boolForKey:PREF_LIGHTS_STATE];
100 103
     NSString *lastMode = [store stringForKey:PREF_LED_MODE];
101 104
     float brightness = [store floatForKey:PREF_BRIGHTNESS];
105
+    NSData *lastColorData = [store dataForKey:PREF_COLOR];
106
+    NSColor *lastColor = nil;
107
+    if (lastColorData != nil) {
108
+        lastColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:lastColorData];
109
+    }
102 110
     
103 111
     // Prepare brightness menu
104 112
     brightnessItem.view = brightnessSlider;
@@ -153,6 +161,16 @@
153 161
         }
154 162
         [menuColors addItem:item];
155 163
     }
164
+    menuItemColor = [[NSMenuItem alloc] initWithTitle:TEXT_MANUAL action:@selector(setColorSelected:) keyEquivalent:@""];
165
+    if ([lastMode isEqualToString:TEXT_MANUAL]) {
166
+        if (lastColor != nil) {
167
+            // Restore previously set RGB color
168
+            [self setLightsColor:lastColor];
169
+        }
170
+        [menuItemColor setState:NSOnState];
171
+    }
172
+    [menuItemColor setTag:-1];
173
+    [menuColors addItem:menuItemColor];
156 174
     
157 175
     // Prepare animations menu
158 176
     NSArray *animationStrings = [NSArray arrayWithObjects:
@@ -308,6 +326,49 @@
308 326
     }
309 327
 }
310 328
 
329
+- (void)setLightsColor:(NSColor *)color {
330
+    CGFloat red, green, blue, alpha;
331
+    [color getRed:&red green:&green blue:&blue alpha:&alpha];
332
+    [self setLightsR:red * 255 G:green * 255 B:blue * 255];
333
+    
334
+    // Stop previous timer setting
335
+    if (animation != nil) {
336
+        [animation invalidate];
337
+        animation = nil;
338
+    }
339
+    
340
+    // Turn off all other LED menu items
341
+    if (menuColors != nil) {
342
+        for (int i = 0; i < [menuColors numberOfItems]; i++) {
343
+            [[menuColors itemAtIndex:i] setState:NSOffState];
344
+        }
345
+    }
346
+    if (menuAnimations != nil) {
347
+        for (int i = 0; i < [menuAnimations numberOfItems]; i++) {
348
+            [[menuAnimations itemAtIndex:i] setState:NSOffState];
349
+        }
350
+    }
351
+    if (menuVisualizations != nil) {
352
+        for (int i = 0; i < [menuVisualizations numberOfItems]; i++) {
353
+            [[menuVisualizations itemAtIndex:i] setState:NSOffState];
354
+        }
355
+    }
356
+    if (menuDisplays != nil) {
357
+        for (int i = 0; i < [menuDisplays numberOfItems]; i++) {
358
+            [[menuDisplays itemAtIndex:i] setState:NSOffState];
359
+        }
360
+    }
361
+    [buttonOff setState:NSOffState];
362
+    [menuItemColor setState:NSOnState];
363
+    
364
+    // Store new manually selected color
365
+    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
366
+    NSData *data = [NSArchiver archivedDataWithRootObject:color];
367
+    [store setObject:data forKey:PREF_COLOR];
368
+    [store setObject:TEXT_MANUAL forKey:PREF_LED_MODE];
369
+    [store synchronize];
370
+}
371
+
311 372
 - (void)setLightsR:(unsigned char)r G:(unsigned char)g B:(unsigned char)b {
312 373
     if ([serial isOpen]) {
313 374
         unsigned char red = r * ([brightnessSlider floatValue] / 100.0);
@@ -340,6 +401,30 @@
340 401
     }
341 402
 }
342 403
 
404
+- (void)setColorSelected:(NSMenuItem *)sender {
405
+    NSColorPanel *cp = [NSColorPanel sharedColorPanel];
406
+    [cp setTarget:self];
407
+    [cp setAction:@selector(colorSelected:)];
408
+    [cp setShowsAlpha:NO];
409
+    [cp setContinuous:NO];
410
+    [cp setMode:NSRGBModeColorPanel];
411
+    
412
+    // TODO Try to restore last manually selected color
413
+    NSUserDefaults *store = [NSUserDefaults standardUserDefaults];
414
+    NSData *lastColorData = [store dataForKey:PREF_COLOR];
415
+    NSColor *lastColor = nil;
416
+    if (lastColorData != nil) {
417
+        lastColor = (NSColor *)[NSUnarchiver unarchiveObjectWithData:lastColorData];
418
+        [cp setColor:lastColor];
419
+    }
420
+    
421
+    [[NSApplication sharedApplication] orderFrontColorPanel:cp];
422
+}
423
+
424
+- (void)colorSelected:(id)sender {
425
+    [self setLightsColor:[sender color]];
426
+}
427
+
343 428
 - (IBAction)brightnessMoved:(NSSlider *)sender {
344 429
     [brightnessLabel setTitle:[NSString stringWithFormat:@"Value: %.0f%%", [sender floatValue]]];
345 430
     
@@ -522,6 +607,12 @@
522 607
         [self displayVisualization:sender];
523 608
     }
524 609
     
610
+    // Check if it is the manual color select item
611
+    if ([sender.title isEqualToString:TEXT_MANUAL]) {
612
+        found = YES;
613
+        [self colorSelected:[NSColorPanel sharedColorPanel]];
614
+    }
615
+    
525 616
     // Check if a static color was selected
526 617
     if ((found == NO) && (staticColors != nil)) {
527 618
         for (NSString *key in [staticColors allKeys]) {

+ 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>153</string>
24
+	<string>170</string>
25 25
 	<key>LSApplicationCategoryType</key>
26 26
 	<string>public.app-category.utilities</string>
27 27
 	<key>LSMinimumSystemVersion</key>

Loading…
Cancel
Save