2009-02-22 19:28:09 -03:00
|
|
|
//
|
|
|
|
// TimeField.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 2/22/09.
|
|
|
|
// Copyright 2009 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "TimeField.h"
|
|
|
|
|
2021-05-06 22:50:26 -04:00
|
|
|
#import "SecondsFormatter.h"
|
|
|
|
|
|
|
|
|
2021-01-25 17:17:57 -03:00
|
|
|
static NSString *kTimerModeKey = @"timerShowTimeRemaining";
|
2009-02-22 19:28:09 -03:00
|
|
|
|
2021-01-25 17:17:57 -03:00
|
|
|
|
|
|
|
@implementation TimeField {
|
|
|
|
BOOL showTimeRemaining;
|
|
|
|
NSDictionary *fontAttributes;
|
2021-05-06 22:50:26 -04:00
|
|
|
SecondsFormatter *secondsFormatter;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)initWithCoder:(NSCoder *)aDecoder
|
|
|
|
{
|
|
|
|
if (self = [super initWithCoder:aDecoder])
|
|
|
|
{
|
|
|
|
[self commonInit];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
2021-01-25 17:17:57 -03:00
|
|
|
}
|
2009-02-22 19:28:09 -03:00
|
|
|
|
2021-05-06 22:50:26 -04:00
|
|
|
- (instancetype)initWithFrame:(CGRect)frame
|
|
|
|
{
|
|
|
|
if (self = [super initWithFrame:frame])
|
|
|
|
{
|
|
|
|
[self commonInit];
|
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)commonInit
|
|
|
|
{
|
|
|
|
secondsFormatter = [[SecondsFormatter alloc] init];
|
|
|
|
}
|
|
|
|
|
2013-10-11 09:53:37 -03:00
|
|
|
- (void)awakeFromNib
|
|
|
|
{
|
2021-01-25 17:17:57 -03:00
|
|
|
showTimeRemaining = [[NSUserDefaults standardUserDefaults] boolForKey:kTimerModeKey];
|
2013-10-11 09:53:37 -03:00
|
|
|
}
|
|
|
|
|
2009-02-22 19:28:09 -03:00
|
|
|
- (void)update
|
|
|
|
{
|
2021-01-25 17:17:57 -03:00
|
|
|
NSString *text;
|
|
|
|
if (showTimeRemaining == NO)
|
|
|
|
{
|
2021-05-06 22:50:26 -04:00
|
|
|
NSTimeInterval sec = self.currentTime;
|
|
|
|
text = [secondsFormatter stringForTimeInterval:sec];
|
2021-01-25 17:17:57 -03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-05-06 22:50:26 -04:00
|
|
|
NSTimeInterval sec = self.currentTime - self.duration;
|
|
|
|
// NOTE: The floating point standard has support for negative zero.
|
|
|
|
// We use that to enforce the sign prefix.
|
|
|
|
if (sec == 0.0) { sec = -0.0; }
|
|
|
|
text = [secondsFormatter stringForTimeInterval:sec];
|
2021-01-25 17:17:57 -03:00
|
|
|
}
|
|
|
|
NSAttributedString *string = [[NSAttributedString alloc] initWithString:text
|
|
|
|
attributes:fontAttributes];
|
|
|
|
[self setAttributedStringValue: string];
|
2009-02-22 19:28:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)mouseDown:(NSEvent *)theEvent
|
|
|
|
{
|
2021-01-25 17:17:57 -03:00
|
|
|
showTimeRemaining = !showTimeRemaining;
|
|
|
|
[[NSUserDefaults standardUserDefaults] setBool:showTimeRemaining forKey:kTimerModeKey];
|
|
|
|
[self update];
|
2009-02-22 19:28:09 -03:00
|
|
|
}
|
|
|
|
|
2021-05-06 22:50:26 -04:00
|
|
|
- (void)setCurrentTime:(NSTimeInterval)currentTime
|
2009-02-22 19:28:09 -03:00
|
|
|
{
|
2021-01-25 17:17:57 -03:00
|
|
|
_currentTime = currentTime;
|
|
|
|
[self update];
|
2009-02-22 19:28:09 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|