2009-02-22 22:28:09 +00:00
|
|
|
//
|
|
|
|
// TimeField.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 2/22/09.
|
|
|
|
// Copyright 2009 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "TimeField.h"
|
|
|
|
|
2021-01-25 20:17:57 +00:00
|
|
|
static NSString *kTimerModeKey = @"timerShowTimeRemaining";
|
2009-02-22 22:28:09 +00:00
|
|
|
|
2021-01-25 20:17:57 +00:00
|
|
|
NSString * formatTimer(long minutes, long seconds, unichar prefix) {
|
|
|
|
return [NSString localizedStringWithFormat:@"%C%lu:%02lu", prefix, minutes, seconds];;
|
|
|
|
}
|
|
|
|
|
|
|
|
@implementation TimeField {
|
|
|
|
BOOL showTimeRemaining;
|
|
|
|
NSDictionary *fontAttributes;
|
|
|
|
}
|
2009-02-22 22:28:09 +00:00
|
|
|
|
2013-10-11 12:53:37 +00:00
|
|
|
- (void)awakeFromNib
|
|
|
|
{
|
2021-01-25 20:17:57 +00:00
|
|
|
showTimeRemaining = [[NSUserDefaults standardUserDefaults] boolForKey:kTimerModeKey];
|
|
|
|
if (@available(macOS 10.15, *)) {
|
|
|
|
fontAttributes =
|
|
|
|
@{NSFontAttributeName : [NSFont monospacedSystemFontOfSize:13
|
|
|
|
weight:NSFontWeightMedium]};
|
|
|
|
[self update];
|
|
|
|
}
|
2013-10-11 12:53:37 +00:00
|
|
|
}
|
|
|
|
|
2009-02-22 22:28:09 +00:00
|
|
|
- (void)update
|
|
|
|
{
|
2021-01-25 20:17:57 +00:00
|
|
|
NSString *text;
|
|
|
|
if (showTimeRemaining == NO)
|
|
|
|
{
|
|
|
|
long sec = self.currentTime;
|
|
|
|
text = formatTimer(sec / 60, sec % 60, 0x2007); // Digit-width space
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
long sec = MAX(0, self.duration - self.currentTime);
|
|
|
|
text = formatTimer(sec / 60, sec % 60, 0x2012); // Hyphen
|
|
|
|
}
|
|
|
|
NSAttributedString *string = [[NSAttributedString alloc] initWithString:text
|
|
|
|
attributes:fontAttributes];
|
|
|
|
[self setAttributedStringValue: string];
|
2009-02-22 22:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDown:(NSEvent *)theEvent
|
|
|
|
{
|
2021-01-25 20:17:57 +00:00
|
|
|
showTimeRemaining = !showTimeRemaining;
|
|
|
|
[[NSUserDefaults standardUserDefaults] setBool:showTimeRemaining forKey:kTimerModeKey];
|
|
|
|
[self update];
|
2009-02-22 22:28:09 +00:00
|
|
|
}
|
|
|
|
|
2021-01-25 20:17:57 +00:00
|
|
|
- (void)setCurrentTime:(NSInteger)currentTime
|
2009-02-22 22:28:09 +00:00
|
|
|
{
|
2021-01-25 20:17:57 +00:00
|
|
|
_currentTime = currentTime;
|
|
|
|
[self update];
|
2009-02-22 22:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|