|
| 1 | +#import "AppDelegate.h" |
| 2 | +#import "TouchBar.h" |
| 3 | +#import <ServiceManagement/ServiceManagement.h> |
| 4 | +#import "TouchButton.h" |
| 5 | +#import "TouchDelegate.h" |
| 6 | +#import <Cocoa/Cocoa.h> |
| 7 | + |
| 8 | +static const NSTouchBarItemIdentifier muteIdentifier = @"ns.clock"; |
| 9 | +static NSString *const MASCustomShortcutKey = @"customShortcut"; |
| 10 | + |
| 11 | +@interface AppDelegate () <TouchDelegate> |
| 12 | + |
| 13 | +@end |
| 14 | + |
| 15 | +@implementation AppDelegate |
| 16 | + |
| 17 | +NSButton *touchBarButton; |
| 18 | + |
| 19 | +@synthesize statusBar; |
| 20 | + |
| 21 | +TouchButton *button; |
| 22 | + |
| 23 | +NSString *STATUS_ICON_BLACK = @"clock-64"; |
| 24 | + |
| 25 | +NSDateFormatter *timeformatter; |
| 26 | +NSString *format = @"hh:mm"; |
| 27 | +NSMutableAttributedString *colorTitle; |
| 28 | + |
| 29 | + |
| 30 | +- (void) awakeFromNib { |
| 31 | + |
| 32 | + BOOL hideStatusBar = NO; |
| 33 | + BOOL statusBarButtonToggle = NO; |
| 34 | + BOOL useAlternateStatusBarIcons = NO; |
| 35 | + |
| 36 | + if ([[NSUserDefaults standardUserDefaults] objectForKey:@"hide_status_bar"] != nil) { |
| 37 | + hideStatusBar = [[NSUserDefaults standardUserDefaults] boolForKey:@"hide_status_bar"]; |
| 38 | + } |
| 39 | + |
| 40 | + if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_button_toggle"] != nil) { |
| 41 | + statusBarButtonToggle = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_button_toggle"]; |
| 42 | + } |
| 43 | + |
| 44 | + if ([[NSUserDefaults standardUserDefaults] objectForKey:@"status_bar_alternate_icons"] != nil) { |
| 45 | + useAlternateStatusBarIcons = [[NSUserDefaults standardUserDefaults] boolForKey:@"status_bar_alternate_icons"]; |
| 46 | + } |
| 47 | + |
| 48 | + [[NSUserDefaults standardUserDefaults] setBool:hideStatusBar forKey:@"hide_status_bar"]; |
| 49 | + [[NSUserDefaults standardUserDefaults] setBool:statusBarButtonToggle forKey:@"status_bar_button_toggle"]; |
| 50 | + [[NSUserDefaults standardUserDefaults] setBool:useAlternateStatusBarIcons forKey:@"status_bar_alternate_icons"]; |
| 51 | + |
| 52 | + if (!hideStatusBar) { |
| 53 | + [self setupStatusBarItem]; |
| 54 | + } |
| 55 | + |
| 56 | +} |
| 57 | + |
| 58 | +- (void) setupStatusBarItem { |
| 59 | + |
| 60 | + self.statusBar = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength]; |
| 61 | + self.statusBar.menu = self.statusMenu; |
| 62 | + |
| 63 | + NSImage* statusImage = [self getStatusBarImage]; |
| 64 | + |
| 65 | + statusImage.size = NSMakeSize(18, 18); |
| 66 | + |
| 67 | + [statusImage setTemplate:YES]; |
| 68 | + |
| 69 | + self.statusBar.image = statusImage; |
| 70 | + self.statusBar.highlightMode = YES; |
| 71 | + self.statusBar.enabled = YES; |
| 72 | +} |
| 73 | + |
| 74 | + |
| 75 | +- (void) hideMenuBar: (BOOL) enableState { |
| 76 | + |
| 77 | + if (!enableState) { |
| 78 | + [self setupStatusBarItem]; |
| 79 | + } else { |
| 80 | + self.statusBar = nil; |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | + |
| 85 | +-(void)changeColor:(id)sender |
| 86 | +{ |
| 87 | + |
| 88 | + [colorTitle addAttribute:NSForegroundColorAttributeName value:sender range:NSMakeRange(0, button.title.length)]; |
| 89 | + [button setAttributedTitle:colorTitle]; |
| 90 | +} |
| 91 | + |
| 92 | +-(void)UpdateTime:(id)sender |
| 93 | +{ |
| 94 | + NSString *time = [timeformatter stringFromDate:[NSDate date]]; |
| 95 | + [colorTitle.mutableString setString:time]; |
| 96 | + [button setAttributedTitle:colorTitle]; |
| 97 | +} |
| 98 | + |
| 99 | +- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { |
| 100 | + [[[[NSApplication sharedApplication] windows] lastObject] close]; |
| 101 | + |
| 102 | + DFRSystemModalShowsCloseBoxWhenFrontMost(YES); |
| 103 | + |
| 104 | + timeformatter = [[NSDateFormatter alloc] init]; |
| 105 | + [timeformatter setTimeStyle: NSDateFormatterShortStyle]; |
| 106 | + [timeformatter setDateFormat:format]; |
| 107 | + |
| 108 | + NSDate *now = [NSDate date]; |
| 109 | + NSString *newDateString = [timeformatter stringFromDate:now]; |
| 110 | + |
| 111 | + |
| 112 | + button = [TouchButton buttonWithTitle:newDateString target:nil action:nil]; |
| 113 | + [button setDelegate: self]; |
| 114 | + |
| 115 | + NSFont *systemFont = [NSFont systemFontOfSize:14.0f]; |
| 116 | + NSDictionary * fontAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:systemFont, NSFontAttributeName, nil]; |
| 117 | + |
| 118 | + colorTitle = [[NSMutableAttributedString alloc] initWithString:[button title] attributes:fontAttributes]; |
| 119 | + |
| 120 | + NSString *colorString = [[NSUserDefaults standardUserDefaults] objectForKey:@"clock_color"]; |
| 121 | + NSColor *color = nil; |
| 122 | + if (colorString == nil){ |
| 123 | + color = [NSColor whiteColor]; |
| 124 | + } else{ |
| 125 | + color = [self getColorForString:colorString]; |
| 126 | + } |
| 127 | + |
| 128 | + [colorTitle addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0, button.title.length)]; |
| 129 | + [button setAttributedTitle:colorTitle]; |
| 130 | + |
| 131 | + |
| 132 | + NSCustomTouchBarItem *time = [[NSCustomTouchBarItem alloc] initWithIdentifier:muteIdentifier]; |
| 133 | + time.view = button; |
| 134 | + [NSTouchBarItem addSystemTrayItem:time]; |
| 135 | + [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(UpdateTime:) userInfo:nil repeats:YES]; |
| 136 | + |
| 137 | + touchBarButton = button; |
| 138 | + |
| 139 | + [NSTouchBarItem addSystemTrayItem:time]; |
| 140 | + DFRElementSetControlStripPresenceForIdentifier(muteIdentifier, YES); |
| 141 | + |
| 142 | + [self enableLoginAutostart]; |
| 143 | + |
| 144 | +} |
| 145 | + |
| 146 | +-(NSColor*)getColorForString:(id)sender{ |
| 147 | + return [self colorWithHexColorString:sender]; |
| 148 | +} |
| 149 | + |
| 150 | + |
| 151 | +- (NSImage*) getStatusBarImage { |
| 152 | + |
| 153 | + return [NSImage imageNamed:STATUS_ICON_BLACK]; |
| 154 | +} |
| 155 | + |
| 156 | + |
| 157 | +-(void) enableLoginAutostart { |
| 158 | + if ([[NSUserDefaults standardUserDefaults] objectForKey:@"auto_login"] == nil) { |
| 159 | + return; |
| 160 | + } |
| 161 | + |
| 162 | + BOOL state = [[NSUserDefaults standardUserDefaults] boolForKey:@"auto_login"]; |
| 163 | + if(!SMLoginItemSetEnabled((__bridge CFStringRef)@"Nihalsharma.Clock-Launcher", !state)) { |
| 164 | + NSLog(@"The login was not succesfull"); |
| 165 | + } |
| 166 | +} |
| 167 | + |
| 168 | +- (void)applicationWillTerminate:(NSNotification *)aNotification { |
| 169 | +} |
| 170 | + |
| 171 | +-(double) changeState { |
| 172 | + return 0; |
| 173 | +} |
| 174 | + |
| 175 | +-(double) changeStateFixed { |
| 176 | + return 0; |
| 177 | +} |
| 178 | + |
| 179 | +-(NSColor *)colorState:(double)volume { |
| 180 | + |
| 181 | + if(!volume) { |
| 182 | + return NSColor.redColor; |
| 183 | + } else { |
| 184 | + return NSColor.clearColor; |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +- (void)onPressed:(TouchButton*)sender |
| 189 | +{ |
| 190 | + NSLog(@"On Press clicked"); |
| 191 | + if ([format isEqual:@"hh:mm"]){ |
| 192 | + format = @"HH:mm"; |
| 193 | + } else { |
| 194 | + format = @"hh:mm"; |
| 195 | + } |
| 196 | + [timeformatter setDateFormat:format]; |
| 197 | +} |
| 198 | + |
| 199 | +- (void)onLongPressed:(TouchButton*)sender |
| 200 | +{ |
| 201 | + [[[[NSApplication sharedApplication] windows] lastObject] makeKeyAndOrderFront:nil]; |
| 202 | + [[NSApplication sharedApplication] activateIgnoringOtherApps:true]; |
| 203 | +} |
| 204 | + |
| 205 | +- (IBAction)prefsMenuItemAction:(id)sender { |
| 206 | + |
| 207 | + [self onLongPressed:sender]; |
| 208 | +} |
| 209 | + |
| 210 | +- (IBAction)quitMenuItemAction:(id)sender { |
| 211 | + [NSApp terminate:nil]; |
| 212 | +} |
| 213 | + |
| 214 | +- (NSColor*)colorWithHexColorString:(NSString*)inColorString |
| 215 | +{ |
| 216 | + NSColor* result = nil; |
| 217 | + unsigned colorCode = 0; |
| 218 | + unsigned char redByte, greenByte, blueByte; |
| 219 | + |
| 220 | + if (nil != inColorString) |
| 221 | + { |
| 222 | + NSScanner* scanner = [NSScanner scannerWithString:inColorString]; |
| 223 | + (void) [scanner scanHexInt:&colorCode]; // ignore error |
| 224 | + } |
| 225 | + redByte = (unsigned char)(colorCode >> 16); |
| 226 | + greenByte = (unsigned char)(colorCode >> 8); |
| 227 | + blueByte = (unsigned char)(colorCode); // masks off high bits |
| 228 | + |
| 229 | + result = [NSColor |
| 230 | + colorWithCalibratedRed:(CGFloat)redByte / 0xff |
| 231 | + green:(CGFloat)greenByte / 0xff |
| 232 | + blue:(CGFloat)blueByte / 0xff |
| 233 | + alpha:1.0]; |
| 234 | + return result; |
| 235 | +} |
| 236 | + |
| 237 | + |
| 238 | + |
| 239 | +@end |
0 commit comments