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

+ 1
- 1
OtaClock/Info.plist View File

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

+ 1
- 0
OtaClock/MainWindow.h View File

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

+ 53
- 2
OtaClock/MainWindow.m View File

20
 #define CONFIG_SHOW_DATE @"show_date"
20
 #define CONFIG_SHOW_DATE @"show_date"
21
 #define CONFIG_ALARM_TIME @"alarm_time"
21
 #define CONFIG_ALARM_TIME @"alarm_time"
22
 #define CONFIG_ALARM_MODE @"alarm_mode"
22
 #define CONFIG_ALARM_MODE @"alarm_mode"
23
+#define CONFIG_DROP_SHADOW @"drop_shadow"
23
 
24
 
24
 #define MOUSE_CENTER_X 67
25
 #define MOUSE_CENTER_X 67
25
 #define MOUSE_CENTER_Y 47
26
 #define MOUSE_CENTER_Y 47
42
 @interface MainWindow ()
43
 @interface MainWindow ()
43
 
44
 
44
 @property (assign) NSSize defaultSize;
45
 @property (assign) NSSize defaultSize;
45
-@property (assign) NSInteger startScale;
46
 @property (assign) NSInteger lastEyeState;
46
 @property (assign) NSInteger lastEyeState;
47
 @property (assign) BOOL currentlyBlinking, showDate;
47
 @property (assign) BOOL currentlyBlinking, showDate;
48
 
48
 
56
 @property (weak) IBOutlet NSMenuItem *alarmModeItem;
56
 @property (weak) IBOutlet NSMenuItem *alarmModeItem;
57
 @property (weak) IBOutlet NSMenuItem *alarmTextItem;
57
 @property (weak) IBOutlet NSMenuItem *alarmTextItem;
58
 @property (weak) IBOutlet NSMenuItem *militaryTimeItem;
58
 @property (weak) IBOutlet NSMenuItem *militaryTimeItem;
59
+@property (weak) IBOutlet NSMenuItem *dropShadowItem;
59
 
60
 
60
 @property (weak) IBOutlet NSMenuItem *changeSize1;
61
 @property (weak) IBOutlet NSMenuItem *changeSize1;
61
 @property (weak) IBOutlet NSMenuItem *changeSize2;
62
 @property (weak) IBOutlet NSMenuItem *changeSize2;
125
     defaultSize = size;
126
     defaultSize = size;
126
     NSRect frame = [self frame];
127
     NSRect frame = [self frame];
127
     frame.size = defaultSize;
128
     frame.size = defaultSize;
128
-    
129
     frame.size.width *= startScale;
129
     frame.size.width *= startScale;
130
     frame.size.height *= startScale;
130
     frame.size.height *= startScale;
131
     
131
     
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
     [[self.mainView render] drawDate:showDate];
210
     [[self.mainView render] drawDate:showDate];
198
     
211
     
199
     [self setFrame:frame display:YES];
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
 - (IBAction)toggleMilitaryTime:(id)sender {
249
 - (IBAction)toggleMilitaryTime:(id)sender {
203
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
250
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
204
     
251
     
350
         if ([[sender title] isEqualToString:title]) {
397
         if ([[sender title] isEqualToString:title]) {
351
             [sender setState:NSOnState];
398
             [sender setState:NSOnState];
352
             NSSize newSize = defaultSize;
399
             NSSize newSize = defaultSize;
400
+            if (self.dropShadowItem.state == NSOnState) {
401
+                newSize.width += DROP_SHADOW_OFFSET;
402
+                newSize.height += DROP_SHADOW_OFFSET;
403
+            }
353
             newSize.height *= i;
404
             newSize.height *= i;
354
             newSize.width *= i;
405
             newSize.width *= i;
355
             frame.size = newSize;
406
             frame.size = newSize;

+ 9
- 0
OtaClock/Render.h View File

8
 
8
 
9
 #import <Cocoa/Cocoa.h>
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
 @class MainView;
19
 @class MainView;
12
 
20
 
13
 @interface Render : NSObject
21
 @interface Render : NSObject
16
 - (NSSize)baseSize;
24
 - (NSSize)baseSize;
17
 
25
 
18
 - (void)blinkDots;
26
 - (void)blinkDots;
27
+- (void)drawDropShadow:(BOOL)shadow;
19
 - (void)drawMilitaryTime:(BOOL)mil;
28
 - (void)drawMilitaryTime:(BOOL)mil;
20
 - (void)drawAnimation:(NSInteger)state;
29
 - (void)drawAnimation:(NSInteger)state;
21
 - (void)drawAlarmDate:(NSDate *)alarm;
30
 - (void)drawAlarmDate:(NSDate *)alarm;

+ 31
- 4
OtaClock/Render.m View File

7
 //
7
 //
8
 
8
 
9
 #import "MainView.h"
9
 #import "MainView.h"
10
+#import "MainWindow.h"
10
 #import "Render.h"
11
 #import "Render.h"
11
 
12
 
12
 #define FULL_IMAGE_WIDTH 86
13
 #define FULL_IMAGE_WIDTH 86
83
 @property (assign) NSInteger dateDigit0, dateDigit1, dateDigit2, dateDigit3;
84
 @property (assign) NSInteger dateDigit0, dateDigit1, dateDigit2, dateDigit3;
84
 @property (assign) NSInteger alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
85
 @property (assign) NSInteger alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
85
 @property (assign) NSInteger timeDigit0, timeDigit1, timeDigit2, timeDigit3, timeDigit4, timeDigit5;
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
 @property (assign) NSSize fullSize;
89
 @property (assign) NSSize fullSize;
89
 @property (assign) CGContextRef drawContext;
90
 @property (assign) CGContextRef drawContext;
105
 @synthesize dateDigit0, dateDigit1, dateDigit2, dateDigit3;
106
 @synthesize dateDigit0, dateDigit1, dateDigit2, dateDigit3;
106
 @synthesize alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
107
 @synthesize alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
107
 @synthesize timeDigit0, timeDigit1, timeDigit2, timeDigit3, timeDigit4, timeDigit5;
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
 @synthesize fullSize;
111
 @synthesize fullSize;
111
 @synthesize drawContext;
112
 @synthesize drawContext;
319
     alarmSign = NO;
320
     alarmSign = NO;
320
     alarmDots = NO;
321
     alarmDots = NO;
321
     
322
     
323
+    dropShadow = NO;
324
+    
322
     return self;
325
     return self;
323
 }
326
 }
324
 
327
 
355
     }
358
     }
356
 }
359
 }
357
 
360
 
361
+- (void)drawDropShadow:(BOOL)shadow {
362
+    dropShadow = shadow;
363
+    [self drawWithDate:[NSDate date]];
364
+}
365
+
358
 - (void)drawMilitaryTime:(BOOL)mil {
366
 - (void)drawMilitaryTime:(BOOL)mil {
359
     militaryTime = mil;
367
     militaryTime = mil;
360
     [self drawWithDate:[NSDate date]];
368
     [self drawWithDate:[NSDate date]];
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
     CGImageRef drawnImage = CGBitmapContextCreateImage(drawContext);
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
     NSImage *result = [[NSImage alloc] initWithCGImage:drawnImage size:fullSize];
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
     CGImageRelease(drawnImage);
636
     CGImageRelease(drawnImage);
610
 }
637
 }
611
 
638
 

Loading…
Cancel
Save