@@ -40,12 +40,13 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
40
40
41
41
@IBOutlet weak var lanchAtLoginMenuItem : NSMenuItem !
42
42
43
+
43
44
@IBOutlet weak var hudWindow : NSPanel !
44
45
@IBOutlet weak var panelView : NSView !
45
46
@IBOutlet weak var isNameTextField : NSTextField !
46
-
47
+
47
48
let kProfileMenuItemIndexBase = 100
48
-
49
+
49
50
var statusItem : NSStatusItem !
50
51
static let StatusItemIconWidth : CGFloat = NSStatusItem . variableLength
51
52
@@ -133,7 +134,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
133
134
self . updateServersMenu ( )
134
135
self . updateRunningModeMenu ( )
135
136
SyncSSLocal ( )
136
- }
137
+ }
137
138
)
138
139
_ = notifyCenter. rx. notification ( NOTIFY_TOGGLE_RUNNING_SHORTCUT)
139
140
. subscribe ( onNext: { noti in
@@ -180,9 +181,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
180
181
ProxyConfHelper . install ( )
181
182
ProxyConfHelper . startMonitorPAC ( )
182
183
applyConfig ( )
183
-
184
+
184
185
// Register global hotkey
185
186
ShortcutsController . bindShortcuts ( )
187
+
188
+ // Start API Server
189
+ HTTPUserProxy . shard. start ( )
186
190
}
187
191
188
192
func applicationWillTerminate( _ aNotification: Notification ) {
@@ -191,7 +195,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
191
195
StopPrivoxy ( )
192
196
ProxyConfHelper . disableProxy ( )
193
197
}
194
-
198
+
195
199
func applyConfig( ) {
196
200
SyncSSLocal ( )
197
201
@@ -211,7 +215,21 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
211
215
ProxyConfHelper . disableProxy ( )
212
216
}
213
217
}
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
+
215
233
// MARK: - UI Methods
216
234
@IBAction func toggleRunning( _ sender: NSMenuItem ) {
217
235
self . doToggleRunning ( showToast: false )
@@ -324,26 +342,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
324
342
] )
325
343
}
326
344
}
327
-
345
+
328
346
@IBAction func selectPACMode( _ sender: NSMenuItem ) {
329
- let defaults = UserDefaults . standard
330
- defaults. setValue ( " auto " , forKey: " ShadowsocksRunningMode " )
331
- updateRunningModeMenu ( )
332
- applyConfig ( )
347
+ changeMode ( mode: " auto " )
333
348
}
334
349
335
350
@IBAction func selectGlobalMode( _ sender: NSMenuItem ) {
336
- let defaults = UserDefaults . standard
337
- defaults. setValue ( " global " , forKey: " ShadowsocksRunningMode " )
338
- updateRunningModeMenu ( )
339
- applyConfig ( )
351
+ changeMode ( mode: " global " )
340
352
}
341
353
342
354
@IBAction func selectManualMode( _ sender: NSMenuItem ) {
343
- let defaults = UserDefaults . standard
344
- defaults. setValue ( " manual " , forKey: " ShadowsocksRunningMode " )
345
- updateRunningModeMenu ( )
346
- applyConfig ( )
355
+ changeMode ( mode: " manual " )
347
356
}
348
357
349
358
@IBAction func editServerPreferences( _ sender: NSMenuItem ) {
@@ -369,19 +378,27 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
369
378
allInOnePreferencesWinCtrl. window? . makeKeyAndOrderFront ( self )
370
379
}
371
380
372
- @IBAction func selectServer( _ sender: NSMenuItem ) {
373
- let index = sender. tag - kProfileMenuItemIndexBase
381
+ func changeServer( @objc uuid: String ) {
374
382
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)
378
386
updateServersMenu ( )
379
387
SyncSSLocal ( )
380
388
applyConfig ( )
381
389
}
390
+
382
391
updateRunningModeMenu ( )
383
392
}
384
393
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
+
385
402
@IBAction func copyExportCommand( _ sender: NSMenuItem ) {
386
403
// Get the Http proxy config.
387
404
let defaults = UserDefaults . standard
@@ -427,7 +444,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
427
444
428
445
func updateRunningModeMenu( ) {
429
446
let defaults = UserDefaults . standard
430
- let mode = defaults. string ( forKey: " ShadowsocksRunningMode " )
447
+ let mode = defaults. string ( forKey: " ShadowsocksRunningMosde " )
431
448
432
449
var serverMenuText = " Servers " . localized
433
450
@@ -468,12 +485,12 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
468
485
if isOn {
469
486
if let m = mode {
470
487
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 " ) )
477
494
default : break
478
495
}
479
496
statusItem. image? . isTemplate = true
@@ -512,17 +529,17 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
512
529
func updateServersMenu( ) {
513
530
guard let menu = serversMenuItem. submenu else { return }
514
531
532
+
515
533
let mgr = ServerProfileManager . instance
516
534
let profiles = mgr. profiles
517
-
518
535
// Remove all profile menu items
519
536
let beginIndex = menu. index ( of: serverProfilesBeginSeparatorMenuItem) + 1
520
537
let endIndex = menu. index ( of: serverProfilesEndSeparatorMenuItem)
521
538
// Remove from end to begin, so the index won't change :)
522
539
for index in ( beginIndex..< endIndex) . reversed ( ) {
523
540
menu. removeItem ( at: index)
524
541
}
525
-
542
+
526
543
// Insert all profile menu items
527
544
for (i, profile) in profiles. enumerated ( ) . reversed ( ) {
528
545
let item = NSMenuItem ( )
@@ -534,7 +551,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, NSUserNotificationCenterDele
534
551
535
552
menu. insertItem ( item, at: beginIndex)
536
553
}
537
-
554
+
538
555
// End separator is redundant if profile section is empty
539
556
serverProfilesEndSeparatorMenuItem. isHidden = profiles. isEmpty
540
557
}
0 commit comments