cog/Window/TimeField.m

74 lines
2.0 KiB
Matlab
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";
NSString * formatTimer(long minutes, long seconds, unichar prefix) {
return [NSString localizedStringWithFormat:@"%C%lu:%02lu", prefix, minutes, seconds];;
}
@implementation TimeField {
BOOL showTimeRemaining;
NSDictionary *fontAttributes;
}
- (void)awakeFromNib
{
showTimeRemaining = [[NSUserDefaults standardUserDefaults] boolForKey:kTimerModeKey];
NSFontDescriptor * fontDesc = [[NSFont systemFontOfSize:13] fontDescriptor];
NSDictionary *fontFeatureSettings = @{NSFontFeatureTypeIdentifierKey: @(kNumberSpacingType),
NSFontFeatureSelectorIdentifierKey: @(kMonospacedNumbersSelector)
};
NSDictionary *fontFeatureAttributes = @{NSFontFeatureSettingsAttribute: @[ fontFeatureSettings ] };
fontDesc = [fontDesc fontDescriptorByAddingAttributes:fontFeatureAttributes];
NSFont * font = [NSFont fontWithDescriptor:fontDesc size:13];
fontAttributes =
@{NSFontAttributeName : font};
}
- (void)update
{
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, 0x2212); // 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];
}
- (void)setCurrentTime:(NSInteger)currentTime
{
_currentTime = currentTime;
[self update];
}
@end