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
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
NSString *formatTimer(long minutes, long seconds, unichar prefix, int padding) {
|
|
|
|
NSString *paddingChar = [NSString stringWithFormat:@"%C", (unichar)0x2007]; // Digit-width space
|
|
|
|
NSString *paddingString = [@"" stringByPaddingToLength:padding withString:paddingChar startingAtIndex:0];
|
|
|
|
return [NSString localizedStringWithFormat:@"%@%C%lu:%02lu", paddingString, prefix, minutes, seconds];
|
2021-05-08 22:13:49 +00:00
|
|
|
}
|
2021-01-25 20:17:57 +00:00
|
|
|
|
|
|
|
@implementation TimeField {
|
2022-02-07 05:49:27 +00:00
|
|
|
BOOL showTimeRemaining;
|
|
|
|
NSDictionary *fontAttributes;
|
2021-01-25 20:17:57 +00:00
|
|
|
}
|
2009-02-22 22:28:09 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (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 };
|
2013-10-11 12:53:37 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
static int _log10(long minutes) {
|
|
|
|
int ret = 1;
|
|
|
|
while(minutes >= 10) {
|
|
|
|
minutes /= 10;
|
|
|
|
ret++;
|
|
|
|
}
|
|
|
|
return ret;
|
2022-01-21 06:30:31 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)update {
|
|
|
|
NSString *text;
|
|
|
|
if(showTimeRemaining == NO) {
|
|
|
|
long sec = self.currentTime;
|
|
|
|
long sectotal = self.duration;
|
|
|
|
int minutedigits = _log10(sec / 60);
|
|
|
|
int otherminutedigits = _log10(sectotal / 60) + 1; // Plus hyphen
|
|
|
|
int padding = MAX(0, otherminutedigits - minutedigits);
|
|
|
|
text = formatTimer(sec / 60, sec % 60, 0x200B, padding); // Zero-width space
|
|
|
|
} else {
|
|
|
|
long sec = MAX(0, self.duration - self.currentTime);
|
|
|
|
long sectotal = self.duration;
|
|
|
|
int minutedigits = _log10(sec / 60) + 1; // Plus hyphen
|
|
|
|
int otherminutedigits = _log10(sectotal / 60) + 1; // Also plus hyphen
|
|
|
|
int padding = MAX(0, otherminutedigits - minutedigits);
|
|
|
|
text = formatTimer(sec / 60, sec % 60, 0x2212, padding); // Minus
|
|
|
|
}
|
|
|
|
NSAttributedString *string = [[NSAttributedString alloc] initWithString:text
|
|
|
|
attributes:fontAttributes];
|
|
|
|
[self setAttributedStringValue:string];
|
2009-02-22 22:28:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)mouseDown:(NSEvent *)theEvent {
|
|
|
|
showTimeRemaining = !showTimeRemaining;
|
|
|
|
[[NSUserDefaults standardUserDefaults] setBool:showTimeRemaining forKey:kTimerModeKey];
|
|
|
|
[self update];
|
2009-02-22 22:28:09 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setCurrentTime:(NSInteger)currentTime {
|
|
|
|
_currentTime = currentTime;
|
|
|
|
[self update];
|
2009-02-22 22:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|