Cog/Window/TimeField.m
Christopher Snowhill 763f4c8cd5 Shrink the time field on macOS 10.13 and older
Since 10.13 doesn't want to observe the auto sizing attribute of the
field properly, gotta use the tiny font size that the field is already
configured to use.

Should fix #243

Signed-off-by: Christopher Snowhill <kode54@gmail.com>
2022-02-21 16:51:46 -08:00

90 lines
2.8 KiB
Objective-C

//
// 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, 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];
}
@implementation TimeField {
BOOL showTimeRemaining;
NSDictionary *fontAttributes;
}
- (void)awakeFromNib {
CGFloat fontSize = 11.0;
if(@available(macOS 10.14, *)) {
fontSize = 13.0;
}
showTimeRemaining = [[NSUserDefaults standardUserDefaults] boolForKey:kTimerModeKey];
NSFontDescriptor *fontDesc = [[NSFont systemFontOfSize:fontSize] fontDescriptor];
NSDictionary *fontFeatureSettings = @{ NSFontFeatureTypeIdentifierKey: @(kNumberSpacingType),
NSFontFeatureSelectorIdentifierKey: @(kMonospacedNumbersSelector) };
NSDictionary *fontFeatureAttributes = @{ NSFontFeatureSettingsAttribute: @[fontFeatureSettings] };
fontDesc = [fontDesc fontDescriptorByAddingAttributes:fontFeatureAttributes];
NSFont *font = [NSFont fontWithDescriptor:fontDesc size:fontSize];
fontAttributes = @{ NSFontAttributeName: font };
[self update];
}
static int _log10(long minutes) {
int ret = 1;
while(minutes >= 10) {
minutes /= 10;
ret++;
}
return ret;
}
- (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];
}
- (void)mouseDown:(NSEvent *)theEvent {
showTimeRemaining = !showTimeRemaining;
[[NSUserDefaults standardUserDefaults] setBool:showTimeRemaining forKey:kTimerModeKey];
[self update];
}
- (void)setCurrentTime:(NSInteger)currentTime {
_currentTime = currentTime;
[self update];
}
@end