Skip to content

Windows Separation Remastered: Preferences Window #618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions GitUp/Application/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@
#import <AppKit/AppKit.h>

@interface AppDelegate : NSObject <NSApplicationDelegate>
@property(nonatomic, strong) IBOutlet NSWindow* preferencesWindow;
@property(nonatomic, weak) IBOutlet NSToolbar* preferencesToolbar;
@property(nonatomic, weak) IBOutlet NSTabView* preferencesTabView;
@property(nonatomic, weak) IBOutlet NSPopUpButton* channelPopUpButton;
@property(nonatomic, weak) IBOutlet NSPopUpButton* themePopUpButton;
+ (instancetype)sharedDelegate;

- (void)handleDocumentCountChanged;
@end
143 changes: 28 additions & 115 deletions GitUp/Application/AppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,21 @@

#import "AboutWindowController.h"
#import "CloneWindowController.h"
#import "PreferencesWindowController.h"
#import "WelcomeWindowController.h"

#define __ENABLE_SUDDEN_TERMINATION__ 1

#define kNotificationUserInfoKey_Action @"action" // NSString

#define kPreferencePaneIdentifier_General @"general"

#define kInstallerName @"install.sh"
#define kToolName @"gitup"
#define kToolInstallPath @"/usr/local/bin/" kToolName

@interface AppDelegate () <NSUserNotificationCenterDelegate, SUUpdaterDelegate>
@property(nonatomic, strong) AboutWindowController *aboutWindowController;
@property(nonatomic, strong) CloneWindowController *cloneWindowController;
@property(nonatomic, strong) PreferencesWindowController *preferencesWindowController;
@property(nonatomic, strong) WelcomeWindowController *welcomeWindowController;
@end

Expand All @@ -58,6 +58,7 @@ @implementation AppDelegate {
}

#pragma mark - Properties

- (AboutWindowController *)aboutWindowController {
if (!_aboutWindowController) {
_aboutWindowController = [[AboutWindowController alloc] init];
Expand All @@ -72,6 +73,24 @@ - (CloneWindowController *)cloneWindowController {
return _cloneWindowController;
}

- (void)didChangeReleaseChannel:(BOOL)didChange {
if (didChange) {
_manualCheck = NO;
[_updater checkForUpdatesInBackground];
}
}

- (PreferencesWindowController *)preferencesWindowController {
if (!_preferencesWindowController) {
_preferencesWindowController = [[PreferencesWindowController alloc] init];
__weak typeof(self) weakSelf = self;
_preferencesWindowController.didChangeReleaseChannel = ^(BOOL didChange) {
[weakSelf didChangeReleaseChannel:didChange];
};
}
return _preferencesWindowController;
}

- (WelcomeWindowController *)welcomeWindowController {
if (!_welcomeWindowController) {
_welcomeWindowController = [[WelcomeWindowController alloc] init];
Expand All @@ -94,12 +113,12 @@ + (void)initialize {
GICommitMessageViewUserDefaultKey_ShowMargins : @(YES),
GICommitMessageViewUserDefaultKey_EnableSpellChecking : @(YES),
GIUserDefaultKey_FontSize : @(GIDefaultFontSize),
kUserDefaultsKey_ReleaseChannel : kReleaseChannel_Stable,
kUserDefaultsKey_ReleaseChannel : PreferencesWindowController_ReleaseChannel_Stable,
kUserDefaultsKey_CheckInterval : @(15 * 60),
kUserDefaultsKey_FirstLaunch : @(YES),
kUserDefaultsKey_DiffWhitespaceMode : @(kGCLiveRepositoryDiffWhitespaceMode_Normal),
kUserDefaultsKey_ShowWelcomeWindow : @(YES),
kUserDefaultsKey_Theme : kTheme_SystemPreference,
kUserDefaultsKey_Theme : PreferencesWindowController_Theme_SystemPreference,
};
[[NSUserDefaults standardUserDefaults] registerDefaults:defaults];
}
Expand Down Expand Up @@ -138,52 +157,10 @@ - (void)_openDocumentAtURL:(NSURL *)url {
[self _openRepositoryWithURL:url withCloneMode:kCloneMode_None windowModeID:NSNotFound];
}

- (void)awakeFromNib {

_preferencesToolbar.selectedItemIdentifier = kPreferencePaneIdentifier_General;
[self selectPreferencePane:nil];

[_channelPopUpButton.menu removeAllItems];
for (NSString* string in @[ kReleaseChannel_Stable, kReleaseChannel_Continuous ]) {
NSMenuItem* item = [[NSMenuItem alloc] initWithTitle:NSLocalizedString(string, nil) action:NULL keyEquivalent:@""];
item.representedObject = string;
[_channelPopUpButton.menu addItem:item];
}

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)handleDocumentCountChanged {
[self.welcomeWindowController handleDocumentCountChanged];
}

- (void)_updatePreferencePanel {
NSString* channel = [[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultsKey_ReleaseChannel];
for (NSMenuItem* item in _channelPopUpButton.menu.itemArray) {
if ([item.representedObject isEqualToString:channel]) {
[_channelPopUpButton selectItem:item];
break;
}
}
}

- (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 All @@ -199,17 +176,6 @@ - (void)_showNotificationWithTitle:(NSString*)title action:(SEL)action message:(
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
}

#pragma mark - Loading Xibs
- (id)_loadWindowFromBundleXibWithName:(NSString *)name expectedClass:(Class)class {
NSBundle *mainBundle = [NSBundle mainBundle];
NSArray *objects = @[];
[mainBundle loadNibNamed:name owner:self topLevelObjects:&objects];
NSArray *filteredObjects = [objects filteredArrayUsingPredicate:[NSPredicate predicateWithBlock:^BOOL(id _Nullable evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
return [evaluatedObject isKindOfClass:class];
}]];
return filteredObjects.firstObject;
}

#pragma mark - NSApplicationDelegate

- (void)applicationWillFinishLaunching:(NSNotification*)notification {
Expand Down Expand Up @@ -248,6 +214,9 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notification {
}
#endif

// Locate installed apps.
[GILaunchServicesLocator setup];

// Initialize user notification center
[[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];

Expand Down Expand Up @@ -303,8 +272,7 @@ - (void)applicationDidFinishLaunching:(NSNotification*)notification {
}

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

#if __ENABLE_SUDDEN_TERMINATION__
// Enable sudden termination
Expand Down Expand Up @@ -413,44 +381,6 @@ - (IBAction)openDocument:(id)sender {
[[NSDocumentController sharedDocumentController] openDocument:sender];
}

- (IBAction)changeReleaseChannel:(id)sender {
NSString* oldChannel = [[NSUserDefaults standardUserDefaults] stringForKey:kUserDefaultsKey_ReleaseChannel];
NSString* newChannel = _channelPopUpButton.selectedItem.representedObject;
if (![newChannel isEqualToString:oldChannel]) {
[[NSUserDefaults standardUserDefaults] setObject:newChannel forKey:kUserDefaultsKey_ReleaseChannel];

_manualCheck = NO;
[_updater checkForUpdatesInBackground];
}
}

- (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 All @@ -469,24 +399,7 @@ - (IBAction)showAboutPanel:(id)sender {
}

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

- (IBAction)selectPreferencePane:(id)sender {
[_preferencesTabView selectTabViewItemWithIdentifier:_preferencesToolbar.selectedItemIdentifier];
NSSize size = NSSizeFromString(_preferencesTabView.selectedTabViewItem.label);
NSRect rect = [_preferencesWindow contentRectForFrameRect:_preferencesWindow.frame];
if (sender) {
rect.origin.y += rect.size.height;
}
rect.size.width = size.width;
rect.size.height = size.height;
if (sender) {
rect.origin.y -= rect.size.height;
}
[_preferencesWindow setFrame:[_preferencesWindow frameRectForContentRect:rect] display:YES animate:(sender ? YES : NO)];
[self.preferencesWindowController showWindow:nil];
}

- (IBAction)resetPreferences:(id)sender {
Expand Down
Loading