|
7 | 7 | // |
8 | 8 |
|
9 | 9 | #import "iTermDelayedTitleSetter.h" |
| 10 | +#import "DebugLogging.h" |
10 | 11 |
|
11 | 12 | NSString *const kDelayedTitleSetterSetTitle = @"kDelayedTitleSetterSetTitle"; |
12 | 13 | NSString *const kDelayedTitleSetterTitleKey = @"title"; |
13 | 14 |
|
| 15 | +static NSString * const kDelayedTitleSetterCustomDelayKey = @"DelayedTitleCustomDelayTimeInterval"; |
| 16 | + |
14 | 17 | static const NSTimeInterval kDelay = 0.1; |
15 | 18 |
|
| 19 | +static const char * kDelayedTitleSetterNSTimeIntervalObjCType = @encode(NSTimeInterval); |
| 20 | + |
| 21 | +static const char * kDelayedTitleSetterFloatObjCType = @encode(float); |
| 22 | + |
16 | 23 | @interface iTermDelayedTitleSetter() |
17 | 24 | @property(nonatomic, assign) NSTimer *timer; |
18 | 25 | @property(nonatomic, copy) NSString *pendingTitle; |
19 | 26 | @end |
20 | 27 |
|
21 | 28 | @implementation iTermDelayedTitleSetter |
22 | 29 |
|
| 30 | ++ (void) initialize { |
| 31 | + [super initialize]; |
| 32 | + [[NSUserDefaults standardUserDefaults] registerDefaults:@{ kDelayedTitleSetterCustomDelayKey : @(kDelay) }]; |
| 33 | +} |
| 34 | + |
23 | 35 | - (void)dealloc { |
24 | 36 | [_pendingTitle release]; |
25 | 37 | [super dealloc]; |
26 | 38 | } |
27 | 39 |
|
| 40 | +- (NSTimeInterval) titleDelayDuration { |
| 41 | + NSNumber *titleDelayDurationNumber = [[NSUserDefaults standardUserDefaults] objectForKey:kDelayedTitleSetterCustomDelayKey]; |
| 42 | + |
| 43 | + if (![titleDelayDurationNumber isKindOfClass:[NSNumber class]]) { |
| 44 | + // Validate the duration, if it's not a number remove it. |
| 45 | + |
| 46 | + DLog(@"duration isn't a number! (%@) %@", NSStringFromClass([titleDelayDurationNumber class]), titleDelayDurationNumber); |
| 47 | + |
| 48 | + [[NSUserDefaults standardUserDefaults] removeObjectForKey:kDelayedTitleSetterCustomDelayKey]; |
| 49 | + |
| 50 | + [[NSUserDefaults standardUserDefaults] synchronize]; |
| 51 | + |
| 52 | + titleDelayDurationNumber = @(kDelay); |
| 53 | + } else { |
| 54 | + const char *titleDelayDurationObjCType = [titleDelayDurationNumber objCType]; |
| 55 | + |
| 56 | + if (strcmp(titleDelayDurationObjCType, kDelayedTitleSetterNSTimeIntervalObjCType) != 0) { |
| 57 | + if (strcmp(titleDelayDurationObjCType, kDelayedTitleSetterFloatObjCType) != 0) { |
| 58 | + DLog(@"types not equal: ori: %s saved: %s", kDelayedTitleSetterNSTimeIntervalObjCType, titleDelayDurationObjCType); |
| 59 | + |
| 60 | + titleDelayDurationNumber = @(kDelay); |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + NSTimeInterval titleDelayDuration = [titleDelayDurationNumber doubleValue]; |
| 66 | + |
| 67 | + if (titleDelayDuration < 0) { |
| 68 | + titleDelayDuration = kDelay; |
| 69 | + |
| 70 | + [[NSUserDefaults standardUserDefaults] removeObjectForKey:kDelayedTitleSetterCustomDelayKey]; |
| 71 | + |
| 72 | + [[NSUserDefaults standardUserDefaults] synchronize]; |
| 73 | + } |
| 74 | + |
| 75 | + return titleDelayDuration; |
| 76 | +} |
| 77 | + |
28 | 78 | - (void)setTitle:(NSString *)title { |
29 | 79 | self.pendingTitle = title; |
30 | | - if (!self.timer) { |
31 | | - self.timer = [NSTimer scheduledTimerWithTimeInterval:kDelay |
32 | | - target:self |
33 | | - selector:@selector(timerDidFire:) |
34 | | - userInfo:nil |
35 | | - repeats:NO]; |
| 80 | + |
| 81 | + NSTimeInterval timerDelay = [self titleDelayDuration]; |
| 82 | + |
| 83 | + if (timerDelay > 0) { |
| 84 | + if (!self.timer) { |
| 85 | + self.timer = [NSTimer scheduledTimerWithTimeInterval:[self titleDelayDuration] |
| 86 | + target:self |
| 87 | + selector:@selector(timerDidFire:) |
| 88 | + userInfo:nil |
| 89 | + repeats:NO]; |
| 90 | + } |
| 91 | + } else { |
| 92 | + [self timerDidFire:nil]; |
36 | 93 | } |
37 | 94 | } |
38 | 95 |
|
|
0 commit comments