You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

178 lines
4.7 KiB

9 years ago
#import "MenubarController.h"
#import "StatusItemView.h"
#import "CLTimezoneData.h"
#import "ApplicationDelegate.h"
#import "CLTimezoneDataOperations.h"
9 years ago
typedef void (^CompletionType)(void);
9 years ago
@implementation MenubarController
@synthesize statusItemView = _statusItemView;
#pragma mark -
- (instancetype)init
9 years ago
{
self = [super init];
if (self != nil)
{
// Install status item into the menu bar
NSData *dataObject = [[NSUserDefaults standardUserDefaults] objectForKey:@"favouriteTimezone"];
NSStatusItem *statusItem;
NSTextField *textField;
if (dataObject)
{
CLTimezoneData *timezoneObject = [CLTimezoneData getCustomObject:dataObject];
CLTimezoneDataOperations *operationObject = [[CLTimezoneDataOperations alloc] initWithTimezoneData:timezoneObject];
NSString *menuTitle = [operationObject getMenuTitle];
textField = [self setUpTextfieldForMenubar];
textField.stringValue = (menuTitle.length > 0) ? menuTitle : @"Icon";
[textField sizeToFit];
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:textField.frame.size.width+3];
[self setUpTimerForUpdatingMenubar];
}
else
{
statusItem = [[NSStatusBar systemStatusBar] statusItemWithLength:STATUS_ITEM_VIEW_WIDTH];
[self invalidateTimerForMenubar];
}
9 years ago
_statusItemView = [[StatusItemView alloc] initWithStatusItem:statusItem];
if (dataObject)
{
_statusItemView.image = [self convertTextfieldRepresentationToImage:textField];
}
else
{
if ([[[NSUserDefaults standardUserDefaults] stringForKey:@"AppleInterfaceStyle"] isEqualToString:@"Dark"])
{
_statusItemView.image = [NSImage imageNamed:@"DarkModeIcon"];
}
else
{
_statusItemView.image = [NSImage imageNamed:@"LightModeIcon"];
}
}
9 years ago
_statusItemView.alternateImage = [NSImage imageNamed:@"StatusHighlighted"];
_statusItemView.action = @selector(togglePanel:);
}
9 years ago
return self;
}
- (NSTextField *)setUpTextfieldForMenubar
{
NSTextField *textField= [[NSTextField alloc] initWithFrame:NSMakeRect(0, 0, STATUS_ITEM_VIEW_WIDTH, 22)];
textField.backgroundColor = [NSColor whiteColor];
textField.bordered = NO;
textField.textColor = [NSColor blackColor];
textField.alignment = NSTextAlignmentCenter;
return textField;
}
- (void)updateMenubar
{
[self.statusItemView setNeedsDisplay:YES];
}
- (void)setUpTimerForUpdatingMenubar
{
self.checkIfMenubarUpdatingWasCancelled = NO;
[self tryingSomethingHere];
}
- (void)tryingSomethingHere
{
//a block calling itself without the ARC retain warning
__block CompletionType completionBlock = nil;
__block __weak CompletionType weakCompletionBlock = nil;
completionBlock = ^(void) {
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
if(!self.checkIfMenubarUpdatingWasCancelled)
{
[self updateMenubar];
weakCompletionBlock = completionBlock;
weakCompletionBlock();
}
});
};
completionBlock();
}
- (void)stoppingTheMenubar
{
self.checkIfMenubarUpdatingWasCancelled = YES;
[self.statusItemView setNeedsDisplay:YES];
}
- (void)invalidateTimerForMenubar
{
[self stoppingTheMenubar];
}
- (NSImage *)convertTextfieldRepresentationToImage:(NSTextField *)textField
{
NSSize textfieldSize = textField.bounds.size;
NSSize imgSize = NSMakeSize(textfieldSize.width, textfieldSize.height);
NSBitmapImageRep *bitmapRepresentation = [textField bitmapImageRepForCachingDisplayInRect:textField.bounds];
bitmapRepresentation.size = imgSize;
[textField cacheDisplayInRect:textField.bounds toBitmapImageRep:bitmapRepresentation];
NSImage* image = [[NSImage alloc] initWithSize:imgSize];
[image addRepresentation:bitmapRepresentation];
return image;
}
9 years ago
- (void)dealloc
{
[[NSStatusBar systemStatusBar] removeStatusItem:self.statusItem];
}
#pragma mark -
#pragma mark Public accessors
- (NSStatusItem *)statusItem
{
return self.statusItemView.statusItem;
}
#pragma mark -
- (BOOL)hasActiveIcon
{
return self.statusItemView.isHighlighted;
}
- (void)setHasActiveIcon:(BOOL)flag
{
self.statusItemView.isHighlighted = flag;
}
@end