Skip to content

Commit

Permalink
Use notification center for clock ticks - seems to work, low overhead.
Browse files Browse the repository at this point in the history
  • Loading branch information
mszoek committed Jul 2, 2022
1 parent cddc1bf commit 6add883
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
16 changes: 9 additions & 7 deletions CoreServices/WindowServer/SystemUIServer/ClockView.m
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,21 @@ @implementation ClockView
self = [super initWithText:dateString
atPoint:NSMakePoint(frame.size.width - sz.width - menuBarHPad, menuBarVPad)
withMaxWidth:300];
[self setFont:font];

[NSThread detachNewThreadSelector:@selector(update:) toTarget:self
withObject:[self window]];
[NSThread detachNewThreadSelector:@selector(notifyTick:) toTarget:self withObject:nil];

return self;
}

- (void)update:(NSWindow*)window {
- (NSString *)currentDateValue {
return [dateFormatter stringForObjectValue:[NSDate date]];
}

- (void)notifyTick:(id)arg {
while(1) {
[self setAttributedStringValue:[[NSAttributedString alloc]
initWithString:[dateFormatter stringForObjectValue:[NSDate date]]
attributes:attributes]];
usleep(500000);
[[NSNotificationCenter defaultCenter] postNotificationName:@"ClockTick" object:nil userInfo:NULL];
usleep(400000);
}
}

Expand Down
9 changes: 9 additions & 0 deletions CoreServices/WindowServer/SystemUIServer/MenuBarWindow.m
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ @implementation MenuBarWindow

[self setMovableByWindowBackground:NO];

NSNotificationCenter *nctr = [NSNotificationCenter defaultCenter];
[nctr addObserver:self selector:@selector(notifyTick:) name:@"ClockTick" object:nil];

clockView = [[ClockView alloc] initWithFrame:frame];
menuView = [[MenuView alloc] initWithFrame:frame];
extrasView = [ExtrasView new];
Expand All @@ -53,6 +56,12 @@ @implementation MenuBarWindow
return self;
}

- (void)notifyTick:(id)arg {
NSString *value = [clockView currentDateValue];
[clockView setStringValue:value];
[clockView display];
}

- (mach_port_t)activePort {
return _menuPort;
}
Expand Down
4 changes: 3 additions & 1 deletion CoreServices/WindowServer/SystemUIServer/desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ extern const NSString *WLMenuDidUpdateNotification;
}

- (ClockView *)initWithFrame:(NSRect)frame;
- (void)update:(NSWindow *)window;
- (NSString *)currentDateValue;
- (void)notifyTick:(id)arg;
@end

// system and application menu titles view
Expand Down Expand Up @@ -94,6 +95,7 @@ extern const NSString *WLMenuDidUpdateNotification;
}

- (MenuBarWindow *)initWithFrame:(NSRect)frame forOutput:(NSScreen *)output;
- (void)notifyTick:(id)arg;
- (void)setPort:(mach_port_t)port forPID:(unsigned int)pid;
- (void)removePortForPID:(unsigned int)pid;
- (mach_port_t)portForPID:(unsigned int)pid;
Expand Down

0 comments on commit 6add883

Please sign in to comment.