Skip to content

Commit e1d6572

Browse files
wqz-leolucasderraugh
authored andcommitted
Support change theme in preference panel (#549)
* support change theme in preference panel * fix theme preference options * format code * make theme popup button and text field baseline aligned
1 parent 6e9c587 commit e1d6572

File tree

5 files changed

+126
-37
lines changed

5 files changed

+126
-37
lines changed

GitUp/Application/AppDelegate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
@property(nonatomic, weak) IBOutlet NSToolbar* preferencesToolbar;
2121
@property(nonatomic, weak) IBOutlet NSTabView* preferencesTabView;
2222
@property(nonatomic, weak) IBOutlet NSPopUpButton* channelPopUpButton;
23+
@property(nonatomic, weak) IBOutlet NSPopUpButton* themePopUpButton;
2324

2425
@property(nonatomic, strong) IBOutlet NSWindow* cloneWindow;
2526
@property(nonatomic, weak) IBOutlet NSTextField* cloneURLTextField;

GitUp/Application/AppDelegate.m

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ + (void)initialize {
9393
kUserDefaultsKey_FirstLaunch : @(YES),
9494
kUserDefaultsKey_DiffWhitespaceMode : @(kGCLiveRepositoryDiffWhitespaceMode_Normal),
9595
kUserDefaultsKey_ShowWelcomeWindow : @(YES),
96+
kUserDefaultsKey_Theme : kTheme_SystemPreference,
9697
};
9798
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
9899
}
@@ -265,6 +266,15 @@ - (void)awakeFromNib {
265266
}
266267

267268
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_willShowRecentPopUpMenu:) name:NSPopUpButtonWillPopUpNotification object:_recentPopUpButton];
269+
270+
NSString* theme = [[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultsKey_Theme];
271+
[self _applyTheme:theme];
272+
[_themePopUpButton.menu removeAllItems];
273+
for (NSString* string in [self _themePreferences]) {
274+
NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(string, nil) action:NULL keyEquivalent:@""];
275+
item.representedObject = string;
276+
[_themePopUpButton.menu addItem:item];
277+
}
268278
}
269279

270280
- (void)_updatePreferencePanel {
@@ -277,6 +287,16 @@ - (void)_updatePreferencePanel {
277287
}
278288
}
279289

290+
- (void)_updateThemePopUpButton {
291+
NSString* theme = [[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultsKey_Theme];
292+
for (NSMenuItem* item in _themePopUpButton.menu.itemArray) {
293+
if ([item.representedObject isEqualToString:theme]) {
294+
[_themePopUpButton selectItem:item];
295+
break;
296+
}
297+
}
298+
}
299+
280300
- (void)_showNotificationWithTitle:(NSString*)title action:(SEL)action message:(NSString*)format, ... NS_FORMAT_FUNCTION(3, 4) {
281301
NSUserNotification* notification = [[NSUserNotification alloc] init];
282302
if (action) {
@@ -397,6 +417,10 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notification {
397417
XLOG_DEBUG_UNREACHABLE();
398418
}
399419

420+
// Load theme preference
421+
NSString* theme = [[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultsKey_Theme];
422+
[self _applyTheme:theme];
423+
400424
#if __ENABLE_SUDDEN_TERMINATION__
401425
// Enable sudden termination
402426
[[NSProcessInfo processInfo] enableSuddenTermination];
@@ -515,6 +539,33 @@ - (IBAction)changeReleaseChannel:(id)sender {
515539
}
516540
}
517541

542+
- (NSArray*)_themePreferences {
543+
return @[ kTheme_SystemPreference, kTheme_Dark, kTheme_Light ];
544+
}
545+
546+
- (void)_applyTheme:(NSString*)theme {
547+
if (@available(macOS 10.14, *)) {
548+
NSInteger index = [[self _themePreferences] indexOfObject:theme];
549+
switch (index) {
550+
case 0:
551+
NSApp.appearance = nil;
552+
break;
553+
case 1:
554+
NSApp.appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
555+
break;
556+
case 2:
557+
NSApp.appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
558+
break;
559+
}
560+
}
561+
[[NSUserDefaults standardUserDefaults] setObject:theme forKey:kUserDefaultsKey_Theme];
562+
}
563+
564+
- (IBAction)changeTheme:(id)sender {
565+
NSString* theme = _themePopUpButton.selectedItem.representedObject;
566+
[self _applyTheme:theme];
567+
}
568+
518569
- (IBAction)viewWiki:(id)sender {
519570
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:kURL_Wiki]];
520571
}
@@ -543,6 +594,7 @@ - (IBAction)showAboutPanel:(id)sender {
543594

544595
- (IBAction)showPreferences:(id)sender {
545596
[self _updatePreferencePanel];
597+
[self _updateThemePopUpButton];
546598
[_preferencesWindow makeKeyAndOrderFront:nil];
547599
}
548600

0 commit comments

Comments
 (0)