Sfoglia il codice sorgente

Can switch from military time to am/pm display

Thomas Buck 9 anni fa
parent
commit
252c641c8d

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

@@ -99,6 +99,7 @@
99 99
                 <outlet property="keepOnTopItem" destination="LVI-ch-nme" id="lL1-Yi-zMN"/>
100 100
                 <outlet property="lockPositionItem" destination="ShL-8K-SbX" id="ppY-l8-Bda"/>
101 101
                 <outlet property="mainView" destination="EiT-Mj-1SZ" id="sQZ-BE-YrX"/>
102
+                <outlet property="militaryTimeItem" destination="Cgd-DK-xB0" id="7Sz-zg-2dg"/>
102 103
                 <outlet property="setAlarmItem" destination="IRB-i7-bez" id="V2f-2Y-Eyh"/>
103 104
                 <outlet property="showDateItem" destination="0Cy-4I-9bF" id="T7y-ik-8nT"/>
104 105
             </connections>
@@ -112,6 +113,9 @@
112 113
                         <items>
113 114
                             <menuItem title="Military Time" id="Cgd-DK-xB0">
114 115
                                 <modifierMask key="keyEquivalentModifierMask"/>
116
+                                <connections>
117
+                                    <action selector="toggleMilitaryTime:" target="QvC-M9-y7g" id="hoC-N1-2pe"/>
118
+                                </connections>
115 119
                             </menuItem>
116 120
                             <menuItem title="Alarm Mode" id="S8W-ak-RY9">
117 121
                                 <modifierMask key="keyEquivalentModifierMask"/>

+ 21
- 0
OtaClock/Images.xcassets/font_time.imageset/Contents.json Vedi File

@@ -0,0 +1,21 @@
1
+{
2
+  "images" : [
3
+    {
4
+      "idiom" : "universal",
5
+      "scale" : "1x",
6
+      "filename" : "font_time.png"
7
+    },
8
+    {
9
+      "idiom" : "universal",
10
+      "scale" : "2x"
11
+    },
12
+    {
13
+      "idiom" : "universal",
14
+      "scale" : "3x"
15
+    }
16
+  ],
17
+  "info" : {
18
+    "version" : 1,
19
+    "author" : "xcode"
20
+  }
21
+}

BIN
OtaClock/Images.xcassets/font_time.imageset/font_time.png Vedi File


+ 1
- 1
OtaClock/Info.plist Vedi File

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

+ 35
- 2
OtaClock/MainWindow.m Vedi File

@@ -55,6 +55,7 @@
55 55
 @property (weak) IBOutlet NSMenuItem *showDateItem;
56 56
 @property (weak) IBOutlet NSMenuItem *alarmModeItem;
57 57
 @property (weak) IBOutlet NSMenuItem *alarmTextItem;
58
+@property (weak) IBOutlet NSMenuItem *militaryTimeItem;
58 59
 
59 60
 @property (weak) IBOutlet NSMenuItem *changeSize1;
60 61
 @property (weak) IBOutlet NSMenuItem *changeSize2;
@@ -172,6 +173,7 @@
172 173
         self.showDateItem.state = NSOffState;
173 174
     }
174 175
     
176
+    // load alarm mode state
175 177
     if ([defaults objectForKey:CONFIG_ALARM_MODE] != nil) {
176 178
         if ([defaults boolForKey:CONFIG_ALARM_MODE] == YES) {
177 179
             [self.alarmModeItem setState:NSOnState];
@@ -179,11 +181,43 @@
179 181
         }
180 182
     }
181 183
     
184
+    // load military time state
185
+    if ([defaults objectForKey:CONFIG_MILITARY_TIME] == nil) {
186
+        [self.militaryTimeItem setState:NSOnState];
187
+        [[self.mainView render] drawMilitaryTime:YES];
188
+    } else {
189
+        if ([defaults boolForKey:CONFIG_MILITARY_TIME]) {
190
+            [self.militaryTimeItem setState:NSOnState];
191
+            [[self.mainView render] drawMilitaryTime:YES];
192
+        } else {
193
+            [[self.mainView render] drawMilitaryTime:NO];
194
+        }
195
+    }
196
+    
182 197
     [[self.mainView render] drawDate:showDate];
183 198
     
184 199
     [self setFrame:frame display:YES];
185 200
 }
186 201
 
202
+- (IBAction)toggleMilitaryTime:(id)sender {
203
+    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
204
+    
205
+    if (self.militaryTimeItem.state == NSOnState) {
206
+        // Turn off military time
207
+        [self.militaryTimeItem setState:NSOffState];
208
+        [[self.mainView render] drawMilitaryTime:NO];
209
+        [defaults setBool:NO forKey:CONFIG_MILITARY_TIME];
210
+    } else {
211
+        // Turn on military time
212
+        [self.militaryTimeItem setState:NSOnState];
213
+        [[self.mainView render] drawMilitaryTime:YES];
214
+        [defaults setBool:YES forKey:CONFIG_MILITARY_TIME];
215
+    }
216
+    
217
+    [defaults synchronize];
218
+    self.mainView.needsDisplay = YES;
219
+}
220
+
187 221
 - (IBAction)toggleAlarm:(id)sender {
188 222
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
189 223
     
@@ -238,8 +272,7 @@
238 272
     if (self.alarmModeItem.state == NSOnState) {
239 273
         NSDateComponents *alarmComponents = [[NSCalendar currentCalendar] components:(NSHourCalendarUnit | NSMinuteCalendarUnit) fromDate:self.alarmDatePicker.dateValue];
240 274
         if (([components minute] == [alarmComponents minute]) && ([components hour] == [alarmComponents hour]) && ([components second] == 0)) {
241
-            // We have reached the alarm time
242
-            NSLog(@"Alarm time reached!");
275
+            NSLog(@"Alarm time reached! (%02ld:%02ld)", (long)[alarmComponents hour], (long)[alarmComponents minute]);
243 276
             [self drawAnimation:[NSNumber numberWithInteger:1]];
244 277
         }
245 278
     }

+ 1
- 0
OtaClock/Render.h Vedi File

@@ -16,6 +16,7 @@
16 16
 - (NSSize)baseSize;
17 17
 
18 18
 - (void)blinkDots;
19
+- (void)drawMilitaryTime:(BOOL)mil;
19 20
 - (void)drawAnimation:(NSInteger)state;
20 21
 - (void)drawAlarmDate:(NSDate *)alarm;
21 22
 - (void)drawDate:(BOOL)draw;

+ 69
- 14
OtaClock/Render.m Vedi File

@@ -53,6 +53,10 @@
53 53
 #define FONT_SMALL_ALARM_X2_OFFSET 41
54 54
 #define FONT_SMALL_ALARM_X3_OFFSET 46
55 55
 
56
+#define FONT_TIME_WIDTH 11
57
+#define FONT_TIME_HEIGHT 3
58
+#define FONT_TIME_PADDING 1
59
+
56 60
 #define ALARM_X_OFFSET 5
57 61
 #define ALARM_Y_OFFSET 57
58 62
 
@@ -71,7 +75,7 @@
71 75
 @property (assign) CGImageRef otaconGraphic, bubbleGraphic, alarmGraphic, blankGraphic;
72 76
 @property (assign) CGImageRef otacon1Graphic, otacon2Graphic, otacon3Graphic;
73 77
 @property (assign) CGImageRef eye0, eye1, eye2, eye3, eye4, dotSmallGraphic, dotLargeGraphic;
74
-@property (assign) CGImageRef fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday;
78
+@property (assign) CGImageRef fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday, fontAM, fontPM;
75 79
 @property (assign) CGImageRef fontSmall1, fontSmall2, fontSmall3, fontSmall4, fontSmall5, fontSmall6, fontSmall7, fontSmall8, fontSmall9, fontSmall0;
76 80
 @property (assign) CGImageRef fontLarge1, fontLarge2, fontLarge3, fontLarge4, fontLarge5, fontLarge6, fontLarge7, fontLarge8, fontLarge9, fontLarge0;
77 81
 
@@ -79,7 +83,7 @@
79 83
 @property (assign) NSInteger dateDigit0, dateDigit1, dateDigit2, dateDigit3;
80 84
 @property (assign) NSInteger alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
81 85
 @property (assign) NSInteger timeDigit0, timeDigit1, timeDigit2, timeDigit3, timeDigit4, timeDigit5;
82
-@property (assign) BOOL alarmSign, alarmDots, timeDots, drawDate;
86
+@property (assign) BOOL alarmSign, alarmDots, timeDots, drawDate, militaryTime, isAfternoon;
83 87
 
84 88
 @property (assign) NSSize fullSize;
85 89
 @property (assign) CGContextRef drawContext;
@@ -93,7 +97,7 @@
93 97
 @synthesize otaconGraphic, bubbleGraphic, alarmGraphic, blankGraphic;
94 98
 @synthesize otacon1Graphic, otacon2Graphic, otacon3Graphic;
95 99
 @synthesize eye0, eye1, eye2, eye3, eye4, dotSmallGraphic, dotLargeGraphic;
96
-@synthesize fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday;
100
+@synthesize fontMonday, fontTuesday, fontWednesday, fontThursday, fontFriday, fontSaturday, fontSunday, fontAM, fontPM;
97 101
 @synthesize fontSmall1, fontSmall2, fontSmall3, fontSmall4, fontSmall5, fontSmall6, fontSmall7, fontSmall8, fontSmall9, fontSmall0;
98 102
 @synthesize fontLarge1, fontLarge2, fontLarge3, fontLarge4, fontLarge5, fontLarge6, fontLarge7, fontLarge8, fontLarge9, fontLarge0;
99 103
 
@@ -101,7 +105,7 @@
101 105
 @synthesize dateDigit0, dateDigit1, dateDigit2, dateDigit3;
102 106
 @synthesize alarmDigit0, alarmDigit1, alarmDigit2, alarmDigit3;
103 107
 @synthesize timeDigit0, timeDigit1, timeDigit2, timeDigit3, timeDigit4, timeDigit5;
104
-@synthesize alarmSign, alarmDots, timeDots, drawDate;
108
+@synthesize alarmSign, alarmDots, timeDots, drawDate, militaryTime, isAfternoon;
105 109
 
106 110
 @synthesize fullSize;
107 111
 @synthesize drawContext;
@@ -180,6 +184,7 @@
180 184
     NSImage *fontDaysImage = [NSImage imageNamed:@"font_days"];
181 185
     NSImage *fontSmallImage = [NSImage imageNamed:@"font_small"];
182 186
     NSImage *fontLargeImage = [NSImage imageNamed:@"font_large"];
187
+    NSImage *fontTimeImage = [NSImage imageNamed:@"font_time"];
183 188
     NSImage *alarmImage = [NSImage imageNamed:@"alarm"];
184 189
     NSImage *dotsSmallImage = [NSImage imageNamed:@"dots_small"];
185 190
     NSImage *dotsLargeImage = [NSImage imageNamed:@"dots_large"];
@@ -276,6 +281,15 @@
276 281
     rect.origin.x += FONT_LARGE_WIDTH + FONT_LARGE_PADDING;
277 282
     fontLarge9 = CGImageCreateWithImageInRect(fontLarge, rect);
278 283
     
284
+    CGImageRef fontTime = [fontTimeImage CGImageForProposedRect:nil context:context hints:nil];
285
+    rect.size.width = FONT_TIME_WIDTH;
286
+    rect.size.height = FONT_TIME_HEIGHT;
287
+    rect.origin.x = 0;
288
+    rect.origin.y = 0;
289
+    fontAM = CGImageCreateWithImageInRect(fontTime, rect);
290
+    rect.origin.y += FONT_TIME_HEIGHT + FONT_TIME_PADDING;
291
+    fontPM = CGImageCreateWithImageInRect(fontTime, rect);
292
+    
279 293
     fullSize.width = FULL_IMAGE_WIDTH;
280 294
     fullSize.height = FULL_IMAGE_HEIGHT;
281 295
     
@@ -295,6 +309,8 @@
295 309
     timeDigit4 = 8;
296 310
     timeDigit5 = 8;
297 311
     timeDots = YES;
312
+    militaryTime = NO;
313
+    isAfternoon = NO;
298 314
     
299 315
     alarmDigit0 = -1;
300 316
     alarmDigit1 = -1;
@@ -339,6 +355,11 @@
339 355
     }
340 356
 }
341 357
 
358
+- (void)drawMilitaryTime:(BOOL)mil {
359
+    militaryTime = mil;
360
+    [self drawWithDate:[NSDate date]];
361
+}
362
+
342 363
 - (void)drawAnimation:(NSInteger)state {
343 364
     if (animState != state) {
344 365
         [[self.parent window] invalidateShadow];
@@ -374,11 +395,34 @@
374 395
     NSDateComponents *components = [[NSCalendar currentCalendar] components:comps fromDate:date];
375 396
     dayOfWeek = [components weekday] - 2; // map sun=1 to sun=-1
376 397
     
398
+    // Convert from military time to am/pm format if neccessary
399
+    NSInteger hour = [components hour];
400
+    if (militaryTime == NO) {
401
+        if (hour == 0) {
402
+            hour = 12;
403
+            isAfternoon = false;
404
+        } else if (hour == 12) {
405
+            isAfternoon = true;
406
+        } else if (hour > 12) {
407
+            isAfternoon = true;
408
+            hour -= 12;
409
+        } else if (hour < 12) {
410
+            isAfternoon = false;
411
+        }
412
+    }
413
+    
377 414
     CONVERT_DECIMAL([components month], dateDigit0, dateDigit1);
378 415
     CONVERT_DECIMAL([components day], dateDigit2, dateDigit3);
379
-    CONVERT_DECIMAL([components hour], timeDigit0, timeDigit1);
416
+    CONVERT_DECIMAL(hour, timeDigit0, timeDigit1);
380 417
     CONVERT_DECIMAL([components minute], timeDigit2, timeDigit3);
381 418
     CONVERT_DECIMAL([components second], timeDigit4, timeDigit5);
419
+    
420
+    // Remove leading hour zero when drawing in am/pm mode
421
+    if (militaryTime == NO) {
422
+        if (timeDigit0 == 0) {
423
+            timeDigit0 = -1;
424
+        }
425
+    }
382 426
 }
383 427
 
384 428
 - (void)drawWithEye:(NSInteger)eyeIndex {
@@ -456,13 +500,22 @@
456 500
         size.origin.x = FONT_LARGE_X3_OFFSET;
457 501
         CGContextDrawImage(drawContext, size, [self largeHelper:timeDigit3]);
458 502
     }
459
-    if ((timeDigit4 >= 0) && (timeDigit4 <= 9)) {
460
-        size.origin.x = FONT_LARGE_X4_OFFSET;
461
-        CGContextDrawImage(drawContext, size, [self largeHelper:timeDigit4]);
462
-    }
463
-    if ((timeDigit5 >= 0) && (timeDigit5 <= 9)) {
464
-        size.origin.x = FONT_LARGE_X5_OFFSET;
465
-        CGContextDrawImage(drawContext, size, [self largeHelper:timeDigit5]);
503
+    if (militaryTime == YES) {
504
+        if ((timeDigit4 >= 0) && (timeDigit4 <= 9)) {
505
+            size.origin.x = FONT_LARGE_X4_OFFSET;
506
+            CGContextDrawImage(drawContext, size, [self largeHelper:timeDigit4]);
507
+        }
508
+        if ((timeDigit5 >= 0) && (timeDigit5 <= 9)) {
509
+            size.origin.x = FONT_LARGE_X5_OFFSET;
510
+            CGContextDrawImage(drawContext, size, [self largeHelper:timeDigit5]);
511
+        }
512
+    } else {
513
+        // Draw AM/PM marker
514
+        size.size.width = FONT_TIME_WIDTH;
515
+        size.size.height = FONT_TIME_HEIGHT;
516
+        size.origin.x = 36;
517
+        size.origin.y = 63;
518
+        CGContextDrawImage(drawContext, size, isAfternoon ? fontPM : fontAM);
466 519
     }
467 520
     
468 521
     // Draw dots between hours, minutes and seconds
@@ -472,8 +525,10 @@
472 525
         size.origin.y = DOTS_TIME_Y_OFFSET;
473 526
         size.origin.x = DOTS_TIME_X0_OFFSET;
474 527
         CGContextDrawImage(drawContext, size, dotLargeGraphic);
475
-        size.origin.x = DOTS_TIME_X1_OFFSET;
476
-        CGContextDrawImage(drawContext, size, dotLargeGraphic);
528
+        if (militaryTime == YES) {
529
+            size.origin.x = DOTS_TIME_X1_OFFSET;
530
+            CGContextDrawImage(drawContext, size, dotLargeGraphic);
531
+        }
477 532
     }
478 533
     
479 534
     // Draw Alarm Graphic

Loading…
Annulla
Salva