Skip to content
This repository
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 179 lines (131 sloc) 6.322 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
#import "SpectaclePreferencesController.h"
#import "SpectacleHotKeyManager.h"
#import "SpectacleHotKeyValidator.h"
#import "SpectacleWindowPositionManager.h"
#import "SpectacleUtilities.h"
#import "SpectacleConstants.h"

@interface SpectaclePreferencesController ()

@property (nonatomic, weak) SpectacleHotKeyManager *hotKeyManager;
@property (nonatomic) NSDictionary *hotKeyRecorders;

@end

#pragma mark -

@implementation SpectaclePreferencesController

- (id)init {
    if ((self = [super initWithWindowNibName: SpectaclePreferencesWindowNibName])) {
        _hotKeyManager = SpectacleHotKeyManager.sharedManager;
    }
    
    return self;
}

#pragma mark -

- (void)windowDidLoad {
    NSInteger loginItemEnabledState = NSOffState;
    BOOL isStatusItemEnabled = [NSUserDefaults.standardUserDefaults boolForKey: SpectacleStatusItemEnabledPreference];
    
    _hotKeyRecorders = [[NSDictionary alloc] initWithObjectsAndKeys:
        _moveToCenterHotKeyRecorder, SpectacleWindowActionMoveToCenter,
        _moveToFullscreenHotKeyRecorder, SpectacleWindowActionMoveToFullscreen,
        _moveToLeftHotKeyRecorder, SpectacleWindowActionMoveToLeftHalf,
        _moveToRightHotKeyRecorder, SpectacleWindowActionMoveToRightHalf,
        _moveToTopHotKeyRecorder, SpectacleWindowActionMoveToTopHalf,
        _moveToBottomHotKeyRecorder, SpectacleWindowActionMoveToBottomHalf,
        _moveToUpperLeftHotKeyRecorder, SpectacleWindowActionMoveToUpperLeft,
        _moveToLowerLeftHotKeyRecorder, SpectacleWindowActionMoveToLowerLeft,
        _moveToUpperRightHotKeyRecorder, SpectacleWindowActionMoveToUpperRight,
        _moveToLowerRightHotKeyRecorder, SpectacleWindowActionMoveToLowerRight,
        _moveToNextDisplayHotKeyRecorder, SpectacleWindowActionMoveToNextDisplay,
        _moveToPreviousDisplayHotKeyRecorder, SpectacleWindowActionMoveToPreviousDisplay,
        _moveToNextThirdHotKeyRecorder, SpectacleWindowActionMoveToNextThird,
        _moveToPreviousThirdHotKeyRecorder, SpectacleWindowActionMoveToPreviousThird,
        _makeLargerHotKeyRecorder, SpectacleWindowActionMakeLarger,
        _makeSmallerHotKeyRecorder, SpectacleWindowActionMakeSmaller,
        _undoLastMoveHotKeyRecorder, SpectacleWindowActionUndoLastMove,
        _redoLastMoveHotKeyRecorder, SpectacleWindowActionRedoLastMove, nil];
    
    [self loadRegisteredHotKeys];
    
    if ([SpectacleUtilities isLoginItemEnabledForBundle: SpectacleUtilities.applicationBundle]) {
        loginItemEnabledState = NSOnState;
    }
    
    _loginItemEnabled.state = loginItemEnabledState;
    
    [_statusItemEnabled selectItemWithTag: isStatusItemEnabled ? 0 : 1];
}

#pragma mark -

- (void)toggleWindow: (id)sender {
    if (self.window.isKeyWindow) {
        [self hideWindow: sender];
    } else {
        [self showWindow: sender];
    }
}

#pragma mark -

- (void)hideWindow: (id)sender {
    [self close];
}

#pragma mark -

- (void)hotKeyRecorder: (ZKHotKeyRecorder *)hotKeyRecorder didReceiveNewHotKey: (ZKHotKey *)hotKey {
    SpectacleWindowPositionManager *windowPositionManager = SpectacleWindowPositionManager.sharedManager;
    
    [hotKey setHotKeyAction: ^(ZKHotKey *hotKey) {
        [windowPositionManager moveFrontMostWindowWithAction: [windowPositionManager windowActionForHotKey: hotKey]];
    }];
    
    [_hotKeyManager registerHotKey: hotKey];
}

- (void)hotKeyRecorder: (ZKHotKeyRecorder *)hotKeyRecorder didClearExistingHotKey: (ZKHotKey *)hotKey {
    [_hotKeyManager unregisterHotKeyForName: hotKey.hotKeyName];
}

#pragma mark -

- (IBAction)toggleLoginItem: (id)sender {
    NSBundle *applicationBundle = SpectacleUtilities.applicationBundle;
    
    if (_loginItemEnabled.state == NSOnState) {
        [SpectacleUtilities enableLoginItemForBundle: applicationBundle];
    } else{
        [SpectacleUtilities disableLoginItemForBundle: applicationBundle];
    }
}

- (IBAction)toggleStatusItem: (id)sender {
    NSString *notificationName = SpectacleStatusItemEnabledNotification;
    BOOL isStatusItemEnabled = YES;
    __block BOOL statusItemStateChanged = YES;
    NSUserDefaults *userDefaults = NSUserDefaults.standardUserDefaults;
    
    if ([userDefaults boolForKey: SpectacleStatusItemEnabledPreference] == ([[sender selectedItem] tag] == 0)) {
        return;
    }
    
    if ([sender selectedItem].tag != 0) {
        notificationName = SpectacleStatusItemDisabledNotification;
        isStatusItemEnabled = NO;
        
        if (![userDefaults boolForKey: SpectacleBackgroundAlertSuppressedPreference]) {
            [SpectacleUtilities displayRunningInBackgroundAlertWithCallback: ^(BOOL isConfirmed, BOOL isSuppressed) {
                if (!isConfirmed) {
                    statusItemStateChanged = NO;
                    
                    [sender selectItemWithTag: 0];
                }
                
                [userDefaults setBool: isSuppressed forKey: SpectacleBackgroundAlertSuppressedPreference];
            }];
        }
    }
    
    if (statusItemStateChanged) {
        [NSNotificationCenter.defaultCenter postNotificationName: notificationName object: self];
        
        [userDefaults setBool: isStatusItemEnabled forKey: SpectacleStatusItemEnabledPreference];
    }
}

#pragma mark -

- (void)loadRegisteredHotKeys {
    SpectacleHotKeyValidator *hotKeyValidator = [SpectacleHotKeyValidator new];
    
    for (NSString *hotKeyName in _hotKeyRecorders.allKeys) {
        ZKHotKeyRecorder *hotKeyRecorder = _hotKeyRecorders[hotKeyName];
        ZKHotKey *hotKey = [_hotKeyManager registeredHotKeyForName: hotKeyName];
        
        hotKeyRecorder.hotKeyName = hotKeyName;
        
        if (hotKey) {
            hotKeyRecorder.hotKey = hotKey;
        }
        
        hotKeyRecorder.delegate = self;
        
        hotKeyRecorder.additionalHotKeyValidators = @[hotKeyValidator];
    }
    
    
    [self enableHotKeyRecorders: YES];
}

#pragma mark -

- (void)enableHotKeyRecorders: (BOOL)enabled {
    for (ZKHotKeyRecorder *hotKeyRecorder in _hotKeyRecorders.allValues) {
        if (!enabled) {
            hotKeyRecorder.hotKey = nil;
        }
        
        hotKeyRecorder.enabled = enabled;
    }
}

@end
Something went wrong with that request. Please try again.