Skip to content

Commit 482ccdc

Browse files
committed
Support choosing light, dark, or the system appearance (issue #714)
Some users want GitX to stay light or dark whatever the rest of the Mac is doing, which until now only System Settings could decide. - Store the choice as PBAppearance, defaulting to following the system - Apply it to NSApp.appearance before the first window opens, so no window shows the wrong appearance at launch - Offer it as a popup in Settings > General and as a View > Appearance submenu, both driving the same default - Leave every existing consumer of the appearance alone: overriding NSApp.appearance updates effectiveAppearance, which already drives them
1 parent 4ece962 commit 482ccdc

6 files changed

Lines changed: 122 additions & 1 deletion

File tree

‎Classes/Controllers/ApplicationController.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
}
2121

2222
- (IBAction)openPreferencesWindow:(id)sender;
23+
- (IBAction)changeAppearance:(id)sender;
2324
- (IBAction)showAboutPanel:(id)sender;
2425

2526
- (IBAction)installCliTool:(id)sender;

‎Classes/Controllers/ApplicationController.m‎

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,56 @@ - (BOOL)applicationOpenUntitledFile:(NSApplication *)theApplication
115115
}
116116
}
117117

118+
- (void)applicationWillFinishLaunching:(NSNotification *)notification
119+
{
120+
[self applyAppearancePreference];
121+
122+
[[NSNotificationCenter defaultCenter] addObserver:self
123+
selector:@selector(userDefaultsDidChange:)
124+
name:NSUserDefaultsDidChangeNotification
125+
object:nil];
126+
}
127+
128+
- (void)userDefaultsDidChange:(NSNotification *)notification
129+
{
130+
[self applyAppearancePreference];
131+
}
132+
133+
- (void)applyAppearancePreference
134+
{
135+
NSAppearance *appearance = nil;
136+
137+
switch ([PBGitDefaults appearance]) {
138+
case PBAppearanceLight:
139+
appearance = [NSAppearance appearanceNamed:NSAppearanceNameAqua];
140+
break;
141+
case PBAppearanceDark:
142+
appearance = [NSAppearance appearanceNamed:NSAppearanceNameDarkAqua];
143+
break;
144+
case PBAppearanceSystem:
145+
break;
146+
}
147+
148+
if (NSApp.appearance == appearance || [NSApp.appearance.name isEqualToString:appearance.name])
149+
return;
150+
151+
NSLog(@"Applying appearance preference: %@", appearance.name ?: @"system");
152+
NSApp.appearance = appearance;
153+
}
154+
155+
- (IBAction)changeAppearance:(id)sender
156+
{
157+
[PBGitDefaults setAppearance:[sender tag]];
158+
}
159+
160+
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem
161+
{
162+
if (menuItem.action == @selector(changeAppearance:))
163+
menuItem.state = (menuItem.tag == [PBGitDefaults appearance]) ? NSControlStateValueOn : NSControlStateValueOff;
164+
165+
return YES;
166+
}
167+
118168
- (void)applicationDidFinishLaunching:(NSNotification *)notification
119169
{
120170
#if !DEBUG

‎Classes/git/PBGitDefaults.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ typedef NS_ENUM(NSInteger, PBPruneOnFetchSetting) {
1414
PBPruneOnFetchNever = 2,
1515
};
1616

17+
typedef NS_ENUM(NSInteger, PBAppearanceSetting) {
18+
PBAppearanceSystem = 0,
19+
PBAppearanceLight = 1,
20+
PBAppearanceDark = 2,
21+
};
22+
1723
typedef NS_ENUM(NSInteger, PBCommitDateFormatSetting) {
1824
PBCommitDateFormatShort = 0,
1925
PBCommitDateFormatMedium = 1,
@@ -46,6 +52,8 @@ typedef NS_ENUM(NSInteger, PBCommitDateFormatSetting) {
4652
+ (PBPruneOnFetchSetting)pruneOnFetch;
4753
+ (NSString *)terminalHandler;
4854
+ (void)setTerminalHandler:(NSString *)bundleIdentifier;
55+
+ (PBAppearanceSetting)appearance;
56+
+ (void)setAppearance:(PBAppearanceSetting)appearance;
4957
+ (PBCommitDateFormatSetting)commitDateFormat;
5058
+ (NSString *)commitDateCustomFormat;
5159

‎Classes/git/PBGitDefaults.m‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#define kUseRepositoryWatcher @"PBUseRepositoryWatcher"
3333
#define kPruneOnFetch @"PBPruneOnFetch"
3434
#define kTerminalHandler @"PBTerminalHandler"
35+
#define kAppearance @"PBAppearance"
3536
#define kCommitDateFormat @"PBCommitDateFormat"
3637
#define kCommitDateCustomFormat @"PBCommitDateCustomFormat"
3738
#define kDefaultCommitDateCustomFormat @"yyyy-MM-dd HH:mm"
@@ -73,6 +74,8 @@ + (void)initialize
7374
forKey:kPruneOnFetch];
7475
[defaultValues setObject:@"com.apple.Terminal"
7576
forKey:kTerminalHandler];
77+
[defaultValues setObject:[NSNumber numberWithInteger:PBAppearanceSystem]
78+
forKey:kAppearance];
7679
[defaultValues setObject:[NSNumber numberWithInteger:PBCommitDateFormatLong]
7780
forKey:kCommitDateFormat];
7881
[defaultValues setObject:kDefaultCommitDateCustomFormat
@@ -253,6 +256,16 @@ + (void)setTerminalHandler:(NSString *)bundleIdentifier
253256
[[NSUserDefaults standardUserDefaults] setObject:bundleIdentifier forKey:kTerminalHandler];
254257
}
255258

259+
+ (PBAppearanceSetting)appearance
260+
{
261+
return [[NSUserDefaults standardUserDefaults] integerForKey:kAppearance];
262+
}
263+
264+
+ (void)setAppearance:(PBAppearanceSetting)appearance
265+
{
266+
[[NSUserDefaults standardUserDefaults] setInteger:appearance forKey:kAppearance];
267+
}
268+
256269
+ (PBCommitDateFormatSetting)commitDateFormat
257270
{
258271
return [[NSUserDefaults standardUserDefaults] integerForKey:kCommitDateFormat];

‎Resources/en.lproj/MainMenu.xib‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,28 @@ DQ
574574
<menuItem isSeparatorItem="YES" id="847">
575575
<modifierMask key="keyEquivalentModifierMask" command="YES"/>
576576
</menuItem>
577+
<menuItem title="Appearance" id="Apr-mn-itm">
578+
<menu key="submenu" title="Appearance" id="Apr-mn-sub">
579+
<items>
580+
<menuItem title="System" tag="0" id="Apr-sy-itm">
581+
<connections>
582+
<action selector="changeAppearance:" target="-1" id="Apr-sy-act"/>
583+
</connections>
584+
</menuItem>
585+
<menuItem title="Light" tag="1" id="Apr-lt-itm">
586+
<connections>
587+
<action selector="changeAppearance:" target="-1" id="Apr-lt-act"/>
588+
</connections>
589+
</menuItem>
590+
<menuItem title="Dark" tag="2" id="Apr-dk-itm">
591+
<connections>
592+
<action selector="changeAppearance:" target="-1" id="Apr-dk-act"/>
593+
</connections>
594+
</menuItem>
595+
</items>
596+
</menu>
597+
</menuItem>
598+
<menuItem isSeparatorItem="YES" id="Apr-sp-itm"/>
577599
<menuItem title="History" keyEquivalent="1" id="927">
578600
<connections>
579601
<action selector="showHistoryView:" target="-1" id="930"/>

‎Resources/en.lproj/Preferences.xib‎

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,36 @@
2222
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
2323
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
2424
<customView id="1" userLabel="General">
25-
<rect key="frame" x="0.0" y="0.0" width="500" height="334"/>
25+
<rect key="frame" x="0.0" y="0.0" width="500" height="364"/>
2626
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
2727
<subviews>
28+
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Apr-lb-fld">
29+
<rect key="frame" x="91" y="323" width="110" height="17"/>
30+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
31+
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Appearance:" id="Apr-lb-cel">
32+
<font key="font" metaFont="system"/>
33+
<color key="textColor" name="controlTextColor" catalog="System" colorSpace="catalog"/>
34+
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
35+
</textFieldCell>
36+
</textField>
37+
<popUpButton verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="Apr-pu-btn">
38+
<rect key="frame" x="205" y="319" width="215" height="25"/>
39+
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>
40+
<popUpButtonCell key="cell" type="push" title="System" bezelStyle="rounded" alignment="left" lineBreakMode="truncatingTail" state="on" borderStyle="borderAndBezel" imageScaling="proportionallyDown" inset="2" selectedItem="Apr-pu-sys" id="Apr-pu-cel">
41+
<behavior key="behavior" lightByBackground="YES" lightByGray="YES"/>
42+
<font key="font" metaFont="menu"/>
43+
<menu key="menu" title="OtherViews" id="Apr-pu-mnu">
44+
<items>
45+
<menuItem title="System" state="on" tag="0" id="Apr-pu-sys"/>
46+
<menuItem title="Light" tag="1" id="Apr-pu-lgt"/>
47+
<menuItem title="Dark" tag="2" id="Apr-pu-drk"/>
48+
</items>
49+
</menu>
50+
</popUpButtonCell>
51+
<connections>
52+
<binding destination="28" name="selectedTag" keyPath="values.PBAppearance" id="Apr-pu-bnd"/>
53+
</connections>
54+
</popUpButton>
2855
<textField verticalHuggingPriority="750" fixedFrame="YES" translatesAutoresizingMaskIntoConstraints="NO" id="164">
2956
<rect key="frame" x="91" y="154" width="110" height="17"/>
3057
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMinY="YES"/>

0 commit comments

Comments
 (0)