Implement configurable shortcuts for toggle running and switch proxy mode.

- By MASShortcut.
- A new shortcuts preferences window.
This commit is contained in:
Charlie Qiu
2017-03-11 00:14:56 +08:00
parent 52709213e4
commit 003727a0fe
9 changed files with 251 additions and 90 deletions

View File

@ -18,6 +18,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
var proxyPreferencesWinCtrl: ProxyPreferencesController!
var editUserRulesWinCtrl: UserRulesController!
var httpPreferencesWinCtrl : HTTPPreferencesWindowController!
var shortcutsPreferencesWinCtrl: ShortcutsPreferencesWindowController!
let keyCodeP = kVK_ANSI_P
let keyCodeS = kVK_ANSI_S
@ -145,6 +146,45 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
self.updateCopyHttpProxyExportMenu()
}
)
notifyCenter.addObserver(forName: NSNotification.Name(rawValue: "NOTIFY_TOGGLE_RUNNING"), object: nil, queue: nil
, using: {
(note) in
var isOn = UserDefaults.standard.bool(forKey: "ShadowsocksOn")
isOn = !isOn
if isOn {
self.isNameTextField.stringValue = "Shadowsocks: On".localized
}
else {
self.isNameTextField.stringValue = "Shadowsocks: Off".localized
}
UserDefaults.standard.set(isOn, forKey: "ShadowsocksOn")
self.updateMainMenu()
self.applyConfig()
self.fadeInHud()
}
)
notifyCenter.addObserver(forName: NSNotification.Name(rawValue: "NOTIFY_SWITCH_PROXY_MODE"), object: nil, queue: nil
, using: {
(note) in
switch Globals.proxyType {
case .pac:
Globals.proxyType = .global
UserDefaults.standard.setValue("global", forKey: "ShadowsocksRunningMode")
self.isNameTextField.stringValue = "Global Mode".localized
case .global:
Globals.proxyType = .pac
UserDefaults.standard.setValue("auto", forKey: "ShadowsocksRunningMode")
self.isNameTextField.stringValue = "Auto Mode By PAC".localized
}
self.updateRunningModeMenu()
self.applyConfig()
self.fadeInHud()
}
)
notifyCenter.addObserver(forName: NSNotification.Name(rawValue: "NOTIFY_FOUND_SS_URL"), object: nil, queue: nil) {
(note: Notification) in
@ -209,7 +249,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
applyConfig()
// Register global hotkey
registerHotkey()
ShortcutsController.bindShortcuts()
}
func applicationWillTerminate(_ aNotification: Notification) {
@ -245,92 +285,6 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
}
}
// MARK: - Hotkey Methods
func registerHotkey() -> Void {
registerEventHotKey(keyCode: UInt32(keyCodeP)) // to toggle PAC and Global Mode
// registerEventHotKey(keyCode: UInt32(keyCodeS)) // to toggle SS on or off
registerEventHandler()
}
func registerEventHotKey(keyCode: UInt32) {
var gMyHotKeyID = EventHotKeyID()
gMyHotKeyID.signature = OSType(fourCharCodeFrom(string: "sxng"))
gMyHotKeyID.id = keyCode
// Register hotkey.
RegisterEventHotKey(UInt32(keyCode),
UInt32(modifierKeys),
gMyHotKeyID,
GetApplicationEventTarget(),
0,
&hotKeyRef)
}
func registerEventHandler() {
var eventType = EventTypeSpec()
eventType.eventClass = OSType(kEventClassKeyboard)
eventType.eventKind = OSType(kEventHotKeyPressed)
// Void pointer to `self`:
let context = Unmanaged.passUnretained(self).toOpaque()
// Install handler.
InstallEventHandler(GetApplicationEventTarget(), {(nextHanlder, theEvent, userContext) -> OSStatus in
// Extract pointer to `self` from void pointer:
let mySelf = Unmanaged<AppDelegate>.fromOpaque(userContext!).takeUnretainedValue()
var hotKeyId = EventHotKeyID()
GetEventParameter(theEvent, EventParamName(kEventParamDirectObject), EventParamType(typeEventHotKeyID), nil, MemoryLayout<EventHotKeyID>.size, nil, &hotKeyId)
if hotKeyId.id == UInt32(mySelf.keyCodeP) {
// P key pressed
switch Globals.proxyType {
case .pac:
Globals.proxyType = .global
UserDefaults.standard.setValue("global", forKey: "ShadowsocksRunningMode")
mySelf.isNameTextField.stringValue = "Global Mode"
mySelf.updateRunningModeMenu()
mySelf.applyConfig()
case .global:
Globals.proxyType = .pac
UserDefaults.standard.setValue("auto", forKey: "ShadowsocksRunningMode")
mySelf.isNameTextField.stringValue = "Auto Mode"
mySelf.updateRunningModeMenu()
mySelf.applyConfig()
}
}
else if hotKeyId.id == UInt32(mySelf.keyCodeS) {
// S key pressed
var isOn = UserDefaults.standard.bool(forKey: "ShadowsocksOn")
isOn = !isOn
if isOn {
mySelf.isNameTextField.stringValue = "Shadowsocks: On".localized
}
else {
mySelf.isNameTextField.stringValue = "Shadowsocks: Off".localized
}
UserDefaults.standard.set(isOn, forKey: "ShadowsocksOn")
mySelf.updateMainMenu()
mySelf.applyConfig()
}
mySelf.fadeInHud()
return noErr
}, 1, &eventType, context, nil)
}
func fourCharCodeFrom(string: String) -> FourCharCode {
assert(string.characters.count == 4, "String length must be 4")
var result: FourCharCode = 0
for char in string.utf16 {
result = (result << 8) + FourCharCode(char)
}
return result
}
// MARK: - UI Methods
@IBAction func toggleRunning(_ sender: NSMenuItem) {
let defaults = UserDefaults.standard
@ -477,6 +431,19 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
proxyPreferencesWinCtrl.window?.makeKeyAndOrderFront(self)
}
@IBAction func editShortcutsPreferences(_ sender: NSMenuItem) {
if shortcutsPreferencesWinCtrl != nil {
shortcutsPreferencesWinCtrl.close()
}
shortcutsPreferencesWinCtrl = ShortcutsPreferencesWindowController(
windowNibName: "ShortcutsPreferencesWindowController")
shortcutsPreferencesWinCtrl.showWindow(self)
NSApp.activate(ignoringOtherApps: true)
shortcutsPreferencesWinCtrl.window?.makeKeyAndOrderFront(self)
}
@IBAction func selectServer(_ sender: NSMenuItem) {
let index = sender.tag - kProfileMenuItemIndexBase
let spMgr = ServerProfileManager.instance

View File

@ -152,12 +152,18 @@
<action selector="editHTTPPreferences:" target="Voe-Tx-rLC" id="tkC-e3-PH9"/>
</connections>
</menuItem>
<menuItem title="Advanced PAC Proxy Preferences..." id="sbx-yz-3lO">
<menuItem title="Advanced PAC Proxy Preferences ..." id="sbx-yz-3lO">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="editProxyPreferences:" target="Voe-Tx-rLC" id="Jji-Ea-Sy8"/>
</connections>
</menuItem>
<menuItem title="Shortcuts Preferences ..." id="waZ-9G-2Xw">
<modifierMask key="keyEquivalentModifierMask"/>
<connections>
<action selector="editShortcutsPreferences:" target="Voe-Tx-rLC" id="dMl-t6-50N"/>
</connections>
</menuItem>
<menuItem isSeparatorItem="YES" id="ZrT-7B-OmN"/>
<menuItem title="Launch At Login" id="eUq-p7-ICK">
<modifierMask key="keyEquivalentModifierMask"/>
@ -219,7 +225,7 @@
<window title="Window" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" hidesOnDeactivate="YES" oneShot="NO" showsToolbarButton="NO" visibleAtLaunch="NO" animationBehavior="default" id="QWV-F6-ac1" customClass="NSPanel">
<windowStyleMask key="styleMask" closable="YES" miniaturizable="YES" resizable="YES" utility="YES"/>
<rect key="contentRect" x="139" y="81" width="200" height="100"/>
<rect key="screenRect" x="0.0" y="0.0" width="1920" height="1057"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="877"/>
<value key="minSize" type="size" width="200" height="100"/>
<value key="maxSize" type="size" width="200" height="100"/>
<view key="contentView" id="rai-SH-9tZ" userLabel="Panel View">

View File

@ -6,6 +6,8 @@
#import "LaunchAtLoginController.h"
#import "SWBQRCodeWindowController.h"
#import "ShortcutsPreferencesWindowController.h"
#import "ShortcutsController.h"
#import "Utils.h"
#import "ProxyConfHelper.h"
#import "ProxyConfTool.h"
#import "ProxyConfTool.h"

View File

@ -0,0 +1,15 @@
//
// ShortcutsController.h
// ShadowsocksX-NG
//
// Created by 邱宇舟 on 2017/3/10.
// Copyright © 2017年 qiuyuzhou. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface ShortcutsController : NSObject
+ (void)bindShortcuts;
@end

View File

@ -0,0 +1,32 @@
//
// ShortcutsController.m
// ShadowsocksX-NG
//
// Created by on 2017/3/10.
// Copyright © 2017 qiuyuzhou. All rights reserved.
//
#import "ShortcutsController.h"
#import <MASShortcut/Shortcut.h>
#import "ShortcutsPreferencesWindowController.h"
@implementation ShortcutsController
+ (void)bindShortcuts {
MASShortcutBinder* binder = [MASShortcutBinder sharedBinder];
[binder
bindShortcutWithDefaultsKey:kGlobalShortcutToggleRunning
toAction:^{
[[NSNotificationCenter defaultCenter] postNotificationName: @"NOTIFY_TOGGLE_RUNNING" object: nil];
}];
[binder
bindShortcutWithDefaultsKey:kGlobalShortcutSwitchProxyMode
toAction:^{
[[NSNotificationCenter defaultCenter] postNotificationName: @"NOTIFY_SWITCH_PROXY_MODE" object: nil];
}];
}
@end

View File

@ -0,0 +1,22 @@
//
// ShortcutsPreferencesWindowController.h
// ShadowsocksX-NG
//
// Created by 邱宇舟 on 2017/3/10.
// Copyright © 2017年 qiuyuzhou. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import <MASShortcut/Shortcut.h>
static NSString *const kGlobalShortcutToggleRunning = @"ToggleRunning";
static NSString *const kGlobalShortcutSwitchProxyMode= @"SwitchProxyMode";
@interface ShortcutsPreferencesWindowController : NSWindowController
@property(nonatomic, weak) IBOutlet MASShortcutView* toggleRunningShortcutCtrl;
@property(nonatomic, weak) IBOutlet MASShortcutView* switchModeShortcutCtrl;
@end

View File

@ -0,0 +1,26 @@
//
// ShortcutsPreferencesWindowController.m
// ShadowsocksX-NG
//
// Created by on 2017/3/10.
// Copyright © 2017 qiuyuzhou. All rights reserved.
//
#import "ShortcutsPreferencesWindowController.h"
@interface ShortcutsPreferencesWindowController ()
@end
@implementation ShortcutsPreferencesWindowController
- (void)windowDidLoad {
[super windowDidLoad];
// Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
self.toggleRunningShortcutCtrl.associatedUserDefaultsKey = kGlobalShortcutToggleRunning;
self.switchModeShortcutCtrl.associatedUserDefaultsKey = kGlobalShortcutSwitchProxyMode;
}
@end

View File

@ -0,0 +1,75 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.XIB" version="3.0" toolsVersion="11762" systemVersion="16D32" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" customObjectInstantitationMethod="direct">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="11762"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<objects>
<customObject id="-2" userLabel="File's Owner" customClass="ShortcutsPreferencesWindowController" customModule="ShadowsocksX_NG" customModuleProvider="target">
<connections>
<outlet property="switchModeShortcutCtrl" destination="nZn-Ee-h8z" id="2iw-P1-htd"/>
<outlet property="toggleRunningShortcutCtrl" destination="zcO-rg-gXN" id="5dF-Ls-4DH"/>
<outlet property="window" destination="F0z-JX-Cv5" id="gIp-Ho-8D9"/>
</connections>
</customObject>
<customObject id="-1" userLabel="First Responder" customClass="FirstResponder"/>
<customObject id="-3" userLabel="Application" customClass="NSObject"/>
<window title="Shortcuts Preferences" allowsToolTipsWhenApplicationIsInactive="NO" autorecalculatesKeyViewLoop="NO" oneShot="NO" releasedWhenClosed="NO" animationBehavior="default" id="F0z-JX-Cv5">
<windowStyleMask key="styleMask" titled="YES" closable="YES"/>
<windowPositionMask key="initialPositionMask" topStrut="YES" bottomStrut="YES"/>
<rect key="contentRect" x="526" y="581" width="365" height="86"/>
<rect key="screenRect" x="0.0" y="0.0" width="1440" height="877"/>
<view key="contentView" wantsLayer="YES" id="se5-gp-TjO">
<rect key="frame" x="0.0" y="0.0" width="365" height="86"/>
<autoresizingMask key="autoresizingMask"/>
<subviews>
<customView translatesAutoresizingMaskIntoConstraints="NO" id="nZn-Ee-h8z" customClass="MASShortcutView">
<rect key="frame" x="182" y="20" width="163" height="19"/>
</customView>
<customView translatesAutoresizingMaskIntoConstraints="NO" id="zcO-rg-gXN" customClass="MASShortcutView">
<rect key="frame" x="182" y="47" width="163" height="19"/>
</customView>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="Bme-MP-1H3">
<rect key="frame" x="18" y="21" width="158" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Switch mode:" id="MJQ-KA-SGa">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="b8A-hd-VRY">
<rect key="frame" x="18" y="49" width="158" height="17"/>
<constraints>
<constraint firstAttribute="width" constant="154" id="O8A-ky-ag4"/>
</constraints>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Toggle On/Off:" id="7OS-YI-YRe">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
</subviews>
<constraints>
<constraint firstItem="nZn-Ee-h8z" firstAttribute="leading" secondItem="Bme-MP-1H3" secondAttribute="trailing" constant="8" symbolic="YES" id="58f-cT-PLi"/>
<constraint firstItem="Bme-MP-1H3" firstAttribute="centerY" secondItem="nZn-Ee-h8z" secondAttribute="centerY" id="5WG-IQ-8Wm"/>
<constraint firstAttribute="trailing" secondItem="zcO-rg-gXN" secondAttribute="trailing" constant="20" symbolic="YES" id="7nF-aU-3ON"/>
<constraint firstAttribute="bottom" secondItem="nZn-Ee-h8z" secondAttribute="bottom" constant="20" symbolic="YES" id="8IS-fc-KM9"/>
<constraint firstItem="zcO-rg-gXN" firstAttribute="leading" secondItem="b8A-hd-VRY" secondAttribute="trailing" constant="8" symbolic="YES" id="JZG-83-he7"/>
<constraint firstItem="nZn-Ee-h8z" firstAttribute="top" secondItem="zcO-rg-gXN" secondAttribute="bottom" constant="8" symbolic="YES" id="Mw8-bt-BB0"/>
<constraint firstItem="Bme-MP-1H3" firstAttribute="top" secondItem="b8A-hd-VRY" secondAttribute="bottom" constant="11" id="cgI-f5-9dC"/>
<constraint firstItem="b8A-hd-VRY" firstAttribute="top" secondItem="se5-gp-TjO" secondAttribute="top" constant="20" symbolic="YES" id="cge-69-H4S"/>
<constraint firstItem="b8A-hd-VRY" firstAttribute="top" secondItem="zcO-rg-gXN" secondAttribute="top" id="qUk-wA-oid"/>
<constraint firstItem="zcO-rg-gXN" firstAttribute="trailing" secondItem="nZn-Ee-h8z" secondAttribute="trailing" id="qUp-IB-sst"/>
<constraint firstItem="b8A-hd-VRY" firstAttribute="leading" secondItem="se5-gp-TjO" secondAttribute="leading" constant="20" symbolic="YES" id="ugI-XY-drL"/>
<constraint firstItem="b8A-hd-VRY" firstAttribute="leading" secondItem="Bme-MP-1H3" secondAttribute="leading" id="usZ-yb-Sy2"/>
<constraint firstItem="zcO-rg-gXN" firstAttribute="leading" secondItem="nZn-Ee-h8z" secondAttribute="leading" id="v2G-eN-Shm"/>
</constraints>
</view>
<connections>
<outlet property="delegate" destination="-2" id="0bl-1N-AYu"/>
</connections>
<point key="canvasLocation" x="74.5" y="54"/>
</window>
</objects>
</document>