Skip to content

Commit

Permalink
Support change theme in preference panel (#549)
Browse files Browse the repository at this point in the history
* support change theme in preference panel

* fix theme preference options

* format code

* make theme popup button and text field baseline aligned
  • Loading branch information
wqz-leo authored and lucasderraugh committed Aug 8, 2019
1 parent 6e9c587 commit e1d6572
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 37 deletions.
1 change: 1 addition & 0 deletions GitUp/Application/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
@property(nonatomic, weak) IBOutlet NSToolbar* preferencesToolbar;
@property(nonatomic, weak) IBOutlet NSTabView* preferencesTabView;
@property(nonatomic, weak) IBOutlet NSPopUpButton* channelPopUpButton;
@property(nonatomic, weak) IBOutlet NSPopUpButton* themePopUpButton;

@property(nonatomic, strong) IBOutlet NSWindow* cloneWindow;
@property(nonatomic, weak) IBOutlet NSTextField* cloneURLTextField;
Expand Down
52 changes: 52 additions & 0 deletions GitUp/Application/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ + (void)initialize {
kUserDefaultsKey_FirstLaunch : @(YES),
kUserDefaultsKey_DiffWhitespaceMode : @(kGCLiveRepositoryDiffWhitespaceMode_Normal),
kUserDefaultsKey_ShowWelcomeWindow : @(YES),
kUserDefaultsKey_Theme : kTheme_SystemPreference,
};
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}
Expand Down Expand Up @@ -265,6 +266,15 @@ - (void)awakeFromNib {
}

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_willShowRecentPopUpMenu:) name:NSPopUpButtonWillPopUpNotification object:_recentPopUpButton];

NSString* theme = [[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultsKey_Theme];
[self _applyTheme:theme];
[_themePopUpButton.menu removeAllItems];
for (NSString* string in [self _themePreferences]) {
NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(string, nil) action:NULL keyEquivalent:@""];
item.representedObject = string;
[_themePopUpButton.menu addItem:item];
}
}

- (void)_updatePreferencePanel {
Expand All @@ -277,6 +287,16 @@ - (void)_updatePreferencePanel {
}
}

- (void)_updateThemePopUpButton {
NSString* theme = [[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultsKey_Theme];
for (NSMenuItem* item in _themePopUpButton.menu.itemArray) {
if ([item.representedObject isEqualToString:theme]) {
[_themePopUpButton selectItem:item];
break;
}
}
}

- (void)_showNotificationWithTitle:(NSString*)title action:(SEL)action message:(NSString*)format, ... NS_FORMAT_FUNCTION(3, 4) {
NSUserNotification* notification = [[NSUserNotification alloc] init];
if (action) {
Expand Down Expand Up @@ -397,6 +417,10 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notification {
XLOG_DEBUG_UNREACHABLE();
}

// Load theme preference
NSString* theme = [[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultsKey_Theme];
[self _applyTheme:theme];

#if __ENABLE_SUDDEN_TERMINATION__
// Enable sudden termination
[[NSProcessInfo processInfo] enableSuddenTermination];
Expand Down Expand Up @@ -515,6 +539,33 @@ - (IBAction)changeReleaseChannel:(id)sender {
}
}

- (NSArray*)_themePreferences {
return @[ kTheme_SystemPreference, kTheme_Dark, kTheme_Light ];
}

- (void)_applyTheme:(NSString*)theme {
if (@available(macOS 10.14, *)) {
NSInteger index = [[self _themePreferences] indexOfObject:theme];
switch (index) {
case 0:
NSApp.appearance = nil;
break;
case 1:
NSApp.appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
break;
case 2:
NSApp.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
break;
}
}
[[NSUserDefaults standardUserDefaults] setObject:theme forKey:kUserDefaultsKey_Theme];
}

- (IBAction)changeTheme:(id)sender {
NSString* theme = _themePopUpButton.selectedItem.representedObject;
[self _applyTheme:theme];
}

- (IBAction)viewWiki:(id)sender {
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:kURL_Wiki]];
}
Expand Down Expand Up @@ -543,6 +594,7 @@ - (IBAction)showAboutPanel:(id)sender {

- (IBAction)showPreferences:(id)sender {
[self _updatePreferencePanel];
[self _updateThemePopUpButton];
[_preferencesWindow makeKeyAndOrderFront:nil];
}

Expand Down
Loading

0 comments on commit e1d6572

Please sign in to comment.