Browse Source

Implemented customizable toggleable drop shadow

Thomas Buck 8 years ago
parent
commit
32b5b20f07
6 changed files with 99 additions and 7 deletions
  1. 4
    0
      OtaClock/Base.lproj/MainMenu.xib
  2. 1
    1
      OtaClock/Info.plist
  3. 1
    0
      OtaClock/MainWindow.h
  4. 53
    2
      OtaClock/MainWindow.m
  5. 9
    0
      OtaClock/Render.h
  6. 31
    4
      OtaClock/Render.m

+ 4
- 0
OtaClock/Base.lproj/MainMenu.xib View File

@@ -96,6 +96,7 @@
96 96
                 <outlet property="changeSize7" destination="7sp-5h-kYL" id="ore-Fs-2W9"/>
97 97
                 <outlet property="changeSize8" destination="WnO-2m-uXI" id="jKZ-CW-zyj"/>
98 98
                 <outlet property="changeSize9" destination="NhP-qX-OsO" id="yL9-ON-q1Y"/>
99
+                <outlet property="dropShadowItem" destination="4eS-hA-ECa" id="i60-fH-hLA"/>
99 100
                 <outlet property="keepOnTopItem" destination="LVI-ch-nme" id="lL1-Yi-zMN"/>
100 101
                 <outlet property="lockPositionItem" destination="ShL-8K-SbX" id="ppY-l8-Bda"/>
101 102
                 <outlet property="mainView" destination="EiT-Mj-1SZ" id="sQZ-BE-YrX"/>
@@ -143,6 +144,9 @@
143 144
                             </menuItem>
144 145
                             <menuItem title="Drop Shadow" id="4eS-hA-ECa">
145 146
                                 <modifierMask key="keyEquivalentModifierMask"/>
147
+                                <connections>
148
+                                    <action selector="toggleDropShadow:" target="QvC-M9-y7g" id="JNQ-x1-O17"/>
149
+                                </connections>
146 150
                             </menuItem>
147 151
                         </items>
148 152
                     </menu>

+ 1
- 1
OtaClock/Info.plist View File

@@ -19,7 +19,7 @@
19 19
 	<key>CFBundleSignature</key>
20 20
 	<string>????</string>
21 21
 	<key>CFBundleVersion</key>
22
-	<string>401</string>
22
+	<string>456</string>
23 23
 	<key>LSApplicationCategoryType</key>
24 24
 	<string>public.app-category.utilities</string>
25 25
 	<key>LSMinimumSystemVersion</key>

+ 1
- 0
OtaClock/MainWindow.h View File

@@ -14,5 +14,6 @@
14 14
 
15 15
 @property (assign) NSPoint dragStart;
16 16
 @property (assign) BOOL keepPosition;
17
+@property (assign) NSInteger startScale;
17 18
 
18 19
 @end

+ 53
- 2
OtaClock/MainWindow.m View File

@@ -20,6 +20,7 @@
20 20
 #define CONFIG_SHOW_DATE @"show_date"
21 21
 #define CONFIG_ALARM_TIME @"alarm_time"
22 22
 #define CONFIG_ALARM_MODE @"alarm_mode"
23
+#define CONFIG_DROP_SHADOW @"drop_shadow"
23 24
 
24 25
 #define MOUSE_CENTER_X 67
25 26
 #define MOUSE_CENTER_Y 47
@@ -42,7 +43,6 @@
42 43
 @interface MainWindow ()
43 44
 
44 45
 @property (assign) NSSize defaultSize;
45
-@property (assign) NSInteger startScale;
46 46
 @property (assign) NSInteger lastEyeState;
47 47
 @property (assign) BOOL currentlyBlinking, showDate;
48 48
 
@@ -56,6 +56,7 @@
56 56
 @property (weak) IBOutlet NSMenuItem *alarmModeItem;
57 57
 @property (weak) IBOutlet NSMenuItem *alarmTextItem;
58 58
 @property (weak) IBOutlet NSMenuItem *militaryTimeItem;
59
+@property (weak) IBOutlet NSMenuItem *dropShadowItem;
59 60
 
60 61
 @property (weak) IBOutlet NSMenuItem *changeSize1;
61 62
 @property (weak) IBOutlet NSMenuItem *changeSize2;
@@ -125,7 +126,6 @@
125 126
     defaultSize = size;
126 127
     NSRect frame = [self frame];
127 128
     frame.size = defaultSize;
128
-    
129 129
     frame.size.width *= startScale;
130 130
     frame.size.height *= startScale;
131 131
     
@@ -194,11 +194,58 @@
194 194
         }
195 195
     }
196 196
     
197
+    // load drop shadow state
198
+    if ([defaults objectForKey:CONFIG_DROP_SHADOW] != nil) {
199
+        if ([defaults boolForKey:CONFIG_DROP_SHADOW]) {
200
+            [[self.mainView render] drawDropShadow:YES];
201
+            [self.dropShadowItem setState:NSOnState];
202
+            
203
+            // increase window size
204
+            frame.size.width += DROP_SHADOW_OFFSET * startScale;
205
+            frame.size.height += DROP_SHADOW_OFFSET * startScale;
206
+            frame.origin.y -= DROP_SHADOW_OFFSET * startScale;
207
+        }
208
+    }
209
+    
197 210
     [[self.mainView render] drawDate:showDate];
198 211
     
199 212
     [self setFrame:frame display:YES];
200 213
 }
201 214
 
215
+- (IBAction)toggleDropShadow:(id)sender {
216
+    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
217
+    
218
+    if (self.dropShadowItem.state == NSOnState) {
219
+        // Turn off drop shadow
220
+        [self.dropShadowItem setState:NSOffState];
221
+        [[self.mainView render] drawDropShadow:NO];
222
+        [defaults setBool:NO forKey:CONFIG_DROP_SHADOW];
223
+        
224
+        // decrease window size
225
+        NSRect rect = [self frame];
226
+        rect.size = defaultSize;
227
+        rect.size.width *= startScale;
228
+        rect.size.height *= startScale;
229
+        rect.origin.y += DROP_SHADOW_OFFSET * startScale;
230
+        [self setFrame:rect display:YES];
231
+    } else {
232
+        // Turn on drop shadow
233
+        [self.dropShadowItem setState:NSOnState];
234
+        [[self.mainView render] drawDropShadow:YES];
235
+        [defaults setBool:YES forKey:CONFIG_DROP_SHADOW];
236
+        
237
+        // increase window size
238
+        NSRect rect = [self frame];
239
+        rect.size.width += DROP_SHADOW_OFFSET * startScale;
240
+        rect.size.height += DROP_SHADOW_OFFSET * startScale;
241
+        rect.origin.y -= DROP_SHADOW_OFFSET * startScale;
242
+        [self setFrame:rect display:YES];
243
+    }
244
+    
245
+    [defaults synchronize];
246
+    self.mainView.needsDisplay = YES;
247
+}
248
+
202 249
 - (IBAction)toggleMilitaryTime:(id)sender {
203 250
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
204 251
     
@@ -350,6 +397,10 @@
350 397
         if ([[sender title] isEqualToString:title]) {
351 398
             [sender setState:NSOnState];
352 399
             NSSize newSize = defaultSize;
400
+            if (self.dropShadowItem.state == NSOnState) {
401
+                newSize.width += DROP_SHADOW_OFFSET;
402
+                newSize.height += DROP_SHADOW_OFFSET;
403
+            }
353 404
             newSize.height *= i;
354 405
             newSize.width *= i;
355 406
             frame.size = newSize;

+ 9
- 0
OtaClock/Render.h View File

@@ -8,6 +8,14 @@
8 8
 
9 9
 #import <Cocoa/Cocoa.h>
10 10
 
11
+#define DROP_SHADOW_OFFSET 2.0
12
+#define DROP_SHADOW_OFFSET_VIS (DROP_SHADOW_OFFSET / 2.0)
13
+#define DROP_SHADOR_BLUR 0.0
14
+#define DROP_SHADOW_RED 0.38
15
+#define DROP_SHADOW_GREEN 0.36
16
+#define DROP_SHADOW_BLUE 0.35
17
+#define DROP_SHADOW_ALPHA 1.0
18
+
11 19
 @class MainView;
12 20
 
13 21
 @interface Render : NSObject
@@ -16,6 +24,7 @@
16 24
 - (NSSize)baseSize;
17 25
 
18 26
 - (void)blinkDots;
27
+- (void)drawDropShadow:(BOOL)shadow;
19 28
 - (void)drawMilitaryTime:(BOOL)mil;
20 29
 - (void)drawAnimation:(NSInteger)state;
21 30
 - (void)drawAlarmDate:(NSDate *)alarm;

+ 31
- 4
OtaClock/Render.m View File

@@ -7,6 +7,7 @@
7 7
 //
8 8
 
9 9
 #import "MainView.h"
10
+#import "MainWindow.h"
10 11
 #import "Render.h"
11 12
 
12 13
 #define FULL_IMAGE_WIDTH 86
@@ -83,7 +84,7 @@
83 84
 @property (assign) NSInteger dateDigit0, dateDigit1, dateDigit2, dateDigit3;
84 85
 @property (assign) NSInteger alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
85 86
 @property (assign) NSInteger timeDigit0, timeDigit1, timeDigit2, timeDigit3, timeDigit4, timeDigit5;
86
-@property (assign) BOOL alarmSign, alarmDots, timeDots, drawDate, militaryTime, isAfternoon;
87
+@property (assign) BOOL alarmSign, alarmDots, timeDots, drawDate, militaryTime, isAfternoon, dropShadow;
87 88
 
88 89
 @property (assign) NSSize fullSize;
89 90
 @property (assign) CGContextRef drawContext;
@@ -105,7 +106,7 @@
105 106
 @synthesize dateDigit0, dateDigit1, dateDigit2, dateDigit3;
106 107
 @synthesize alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
107 108
 @synthesize timeDigit0, timeDigit1, timeDigit2, timeDigit3, timeDigit4, timeDigit5;
108
-@synthesize alarmSign, alarmDots, timeDots, drawDate, militaryTime, isAfternoon;
109
+@synthesize alarmSign, alarmDots, timeDots, drawDate, militaryTime, isAfternoon, dropShadow;
109 110
 
110 111
 @synthesize fullSize;
111 112
 @synthesize drawContext;
@@ -319,6 +320,8 @@
319 320
     alarmSign = NO;
320 321
     alarmDots = NO;
321 322
     
323
+    dropShadow = NO;
324
+    
322 325
     return self;
323 326
 }
324 327
 
@@ -355,6 +358,11 @@
355 358
     }
356 359
 }
357 360
 
361
+- (void)drawDropShadow:(BOOL)shadow {
362
+    dropShadow = shadow;
363
+    [self drawWithDate:[NSDate date]];
364
+}
365
+
358 366
 - (void)drawMilitaryTime:(BOOL)mil {
359 367
     militaryTime = mil;
360 368
     [self drawWithDate:[NSDate date]];
@@ -602,10 +610,29 @@
602 610
         
603 611
     }
604 612
     
605
-    // Render resulting canvas into bitmap and scale this into our window
613
+    // Render resulting canvas into bitmap
606 614
     CGImageRef drawnImage = CGBitmapContextCreateImage(drawContext);
615
+    
616
+    // Add Drop Shadow if required
617
+    if (dropShadow == YES) {
618
+        NSShadow *shadow = [[NSShadow alloc] init];
619
+        [shadow setShadowOffset:NSMakeSize(DROP_SHADOW_OFFSET_VIS * [(MainWindow *)[view window] startScale], -DROP_SHADOW_OFFSET_VIS * [(MainWindow *)[view window] startScale])];
620
+        [shadow setShadowBlurRadius:DROP_SHADOR_BLUR];
621
+        [shadow setShadowColor:[NSColor colorWithCalibratedRed:DROP_SHADOW_RED green:DROP_SHADOW_GREEN blue:DROP_SHADOW_BLUE alpha:DROP_SHADOW_ALPHA]];
622
+        [shadow set];
623
+    }
624
+    
625
+    // Leave some space for the drop shadow
626
+    NSRect bound = [view bounds];
627
+    if (dropShadow == YES) {
628
+        bound.size.width -= DROP_SHADOW_OFFSET * [(MainWindow *)[view window] startScale];
629
+        bound.size.height -= DROP_SHADOW_OFFSET * [(MainWindow *)[view window] startScale];
630
+        bound.origin.y += DROP_SHADOW_OFFSET * [(MainWindow *)[view window] startScale];
631
+    }
632
+    
633
+    // Scale bitmap into the window
607 634
     NSImage *result = [[NSImage alloc] initWithCGImage:drawnImage size:fullSize];
608
-    [result drawInRect:[view bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:NO hints:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:NSImageInterpolationNone] forKey:NSImageHintInterpolation]];
635
+    [result drawInRect:bound fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0 respectFlipped:NO hints:[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:NSImageInterpolationNone] forKey:NSImageHintInterpolation]];
609 636
     CGImageRelease(drawnImage);
610 637
 }
611 638
 

Loading…
Cancel
Save