Cog/Window/TimeField.m

65 lines
1.7 KiB
Mathematica
Raw Normal View History

//
// TimeField.m
// Cog
//
// Created by Vincent Spader on 2/22/09.
// Copyright 2009 __MyCompanyName__. All rights reserved.
//
#import "TimeField.h"
static NSString *kTimerModeKey = @"timerShowTimeRemaining";
2021-05-02 11:35:38 -04:00
NSString * formatTimer(NSTimeInterval minutes, NSTimeInterval seconds, char *prefix) {
return [NSString localizedStringWithFormat:@"%s%02u:%02u", prefix, (unsigned)minutes, (unsigned)seconds];
}
@implementation TimeField {
BOOL showTimeRemaining;
NSDictionary *fontAttributes;
}
- (void)awakeFromNib
{
showTimeRemaining = [[NSUserDefaults standardUserDefaults] boolForKey:kTimerModeKey];
if (@available(macOS 10.11, *)) {
fontAttributes =
@{NSFontAttributeName : [NSFont monospacedDigitSystemFontOfSize:13
weight:NSFontWeightRegular]};
[self update];
}
}
- (void)update
{
NSString *text;
if (showTimeRemaining == NO)
{
2021-05-02 11:35:38 -04:00
NSTimeInterval sec = self.currentTime;
text = formatTimer(sec / 60, fmod(sec, 60), ""); // No prefix.
}
else
{
2021-05-02 11:35:38 -04:00
NSTimeInterval sec = MAX(0.0, self.duration - self.currentTime);
text = formatTimer(sec / 60, fmod(sec, 60), "-"); // Hyphen-minus.
}
NSAttributedString *string = [[NSAttributedString alloc] initWithString:text
attributes:fontAttributes];
[self setAttributedStringValue: string];
}
- (void)mouseDown:(NSEvent *)theEvent
{
showTimeRemaining = !showTimeRemaining;
[[NSUserDefaults standardUserDefaults] setBool:showTimeRemaining forKey:kTimerModeKey];
[self update];
}
2021-05-02 11:35:38 -04:00
- (void)setCurrentTime:(NSTimeInterval)currentTime
{
_currentTime = currentTime;
[self update];
}
@end