@@ -40,12 +40,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
4040
4141 @IBOutlet weak var lanchAtLoginMenuItem : NSMenuItem !
4242
43+
4344 @IBOutlet weak var hudWindow : NSPanel !
4445 @IBOutlet weak var panelView : NSView !
4546 @IBOutlet weak var isNameTextField : NSTextField !
46-
47+
4748 let kProfileMenuItemIndexBase = 100
48-
49+
4950 var statusItem : NSStatusItem !
5051 static let StatusItemIconWidth : CGFloat = NSStatusItem . variableLength
5152
@@ -133,7 +134,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
133134 self . updateServersMenu ( )
134135 self . updateRunningModeMenu ( )
135136 SyncSSLocal ( )
136- }
137+ }
137138 )
138139 _ = notifyCenter. rx. notification ( NOTIFY_TOGGLE_RUNNING_SHORTCUT)
139140 . subscribe ( onNext: { noti in
@@ -180,9 +181,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
180181 ProxyConfHelper . install ( )
181182 ProxyConfHelper . startMonitorPAC ( )
182183 applyConfig ( )
183-
184+
184185 // Register global hotkey
185186 ShortcutsController . bindShortcuts ( )
187+
188+ // Start API Server
189+ HTTPUserProxy . shard. start ( )
186190 }
187191
188192 func applicationWillTerminate( _ aNotification: Notification ) {
@@ -191,7 +195,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
191195 StopPrivoxy ( )
192196 ProxyConfHelper . disableProxy ( )
193197 }
194-
198+
195199 func applyConfig( ) {
196200 SyncSSLocal ( )
197201
@@ -211,7 +215,21 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
211215 ProxyConfHelper . disableProxy ( )
212216 }
213217 }
214-
218+
219+ func changeMode( mode: String ! ) {
220+ let defaults = UserDefaults . standard
221+
222+ switch mode{
223+ case " auto " : defaults. setValue ( " auto " , forKey: " ShadowsocksRunningMode " )
224+ case " global " : defaults. setValue ( " global " , forKey: " ShadowsocksRunningMode " )
225+ case " manual " : defaults. setValue ( " manual " , forKey: " ShadowsocksRunningMode " )
226+ default : fatalError ( )
227+ }
228+
229+ updateRunningModeMenu ( )
230+ applyConfig ( )
231+ }
232+
215233 // MARK: - UI Methods
216234 @IBAction func toggleRunning( _ sender: NSMenuItem ) {
217235 self . doToggleRunning ( showToast: false )
@@ -324,26 +342,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
324342 ] )
325343 }
326344 }
327-
345+
328346 @IBAction func selectPACMode( _ sender: NSMenuItem ) {
329- let defaults = UserDefaults . standard
330- defaults. setValue ( " auto " , forKey: " ShadowsocksRunningMode " )
331- updateRunningModeMenu ( )
332- applyConfig ( )
347+ changeMode ( mode: " auto " )
333348 }
334349
335350 @IBAction func selectGlobalMode( _ sender: NSMenuItem ) {
336- let defaults = UserDefaults . standard
337- defaults. setValue ( " global " , forKey: " ShadowsocksRunningMode " )
338- updateRunningModeMenu ( )
339- applyConfig ( )
351+ changeMode ( mode: " global " )
340352 }
341353
342354 @IBAction func selectManualMode( _ sender: NSMenuItem ) {
343- let defaults = UserDefaults . standard
344- defaults. setValue ( " manual " , forKey: " ShadowsocksRunningMode " )
345- updateRunningModeMenu ( )
346- applyConfig ( )
355+ changeMode ( mode: " manual " )
347356 }
348357
349358 @IBAction func editServerPreferences( _ sender: NSMenuItem ) {
@@ -369,19 +378,27 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
369378 allInOnePreferencesWinCtrl. window? . makeKeyAndOrderFront ( self )
370379 }
371380
372- @IBAction func selectServer( _ sender: NSMenuItem ) {
373- let index = sender. tag - kProfileMenuItemIndexBase
381+ func changeServer( @objc uuid: String ) {
374382 let spMgr = ServerProfileManager . instance
375- let newProfile = spMgr . profiles [ index ]
376- if newProfile . uuid != spMgr. activeProfileId {
377- spMgr. setActiveProfiledId ( newProfile . uuid)
383+
384+ if uuid != spMgr. activeProfileId {
385+ spMgr. setActiveProfiledId ( uuid)
378386 updateServersMenu ( )
379387 SyncSSLocal ( )
380388 applyConfig ( )
381389 }
390+
382391 updateRunningModeMenu ( )
383392 }
384393
394+ @IBAction func selectServer( _ sender: NSMenuItem ) {
395+ let index = sender. tag - kProfileMenuItemIndexBase
396+ let spMgr = ServerProfileManager . instance
397+ let newProfileId = spMgr. profiles [ index] . uuid
398+
399+ changeServer ( uuid: newProfileId)
400+ }
401+
385402 @IBAction func copyExportCommand( _ sender: NSMenuItem ) {
386403 // Get the Http proxy config.
387404 let defaults = UserDefaults . standard
@@ -427,7 +444,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
427444
428445 func updateRunningModeMenu( ) {
429446 let defaults = UserDefaults . standard
430- let mode = defaults. string ( forKey: " ShadowsocksRunningMode " )
447+ let mode = defaults. string ( forKey: " ShadowsocksRunningMosde " )
431448
432449 var serverMenuText = " Servers " . localized
433450
@@ -468,12 +485,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
468485 if isOn {
469486 if let m = mode {
470487 switch m {
471- case " auto " :
472- statusItem. image = NSImage ( named: NSImage . Name ( rawValue: " menu_p_icon " ) )
473- case " global " :
474- statusItem. image = NSImage ( named: NSImage . Name ( rawValue: " menu_g_icon " ) )
475- case " manual " :
476- statusItem. image = NSImage ( named: NSImage . Name ( rawValue: " menu_m_icon " ) )
488+ case " auto " :
489+ statusItem. image = NSImage ( named: NSImage . Name ( rawValue: " menu_p_icon " ) )
490+ case " global " :
491+ statusItem. image = NSImage ( named: NSImage . Name ( rawValue: " menu_g_icon " ) )
492+ case " manual " :
493+ statusItem. image = NSImage ( named: NSImage . Name ( rawValue: " menu_m_icon " ) )
477494 default : break
478495 }
479496 statusItem. image? . isTemplate = true
@@ -512,17 +529,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
512529 func updateServersMenu( ) {
513530 guard let menu = serversMenuItem. submenu else { return }
514531
532+
515533 let mgr = ServerProfileManager . instance
516534 let profiles = mgr. profiles
517-
518535 // Remove all profile menu items
519536 let beginIndex = menu. index ( of: serverProfilesBeginSeparatorMenuItem) + 1
520537 let endIndex = menu. index ( of: serverProfilesEndSeparatorMenuItem)
521538 // Remove from end to begin, so the index won't change :)
522539 for index in ( beginIndex..< endIndex) . reversed ( ) {
523540 menu. removeItem ( at: index)
524541 }
525-
542+
526543 // Insert all profile menu items
527544 for (i, profile) in profiles. enumerated ( ) . reversed ( ) {
528545 let item = NSMenuItem ( )
@@ -534,7 +551,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
534551
535552 menu. insertItem ( item, at: beginIndex)
536553 }
537-
554+
538555 // End separator is redundant if profile section is empty
539556 serverProfilesEndSeparatorMenuItem. isHidden = profiles. isEmpty
540557 }
0 commit comments