Skip to content

Commit 4352871

Browse files
author
clowwindy
committed
implement menu
1 parent cc873f6 commit 4352871

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

ShadowWeb/zh-Hans.lproj/Localizable.strings

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
"Turn Shadowsocks Off" = "关闭 Shadowsocks";
4848
"Auto Proxy Mode" = "自动代理模式";
4949
"Global Mode" = "全局模式";
50+
"Public Server" = "公共服务器";
51+
"Servers" = "服务器";
5052
"Open Server Preferences..." = "打开服务器设定...";
5153
"Please fill in the blanks." = "请填写以下内容。";
5254
"Show Logs..." = "显示日志...";

ShadowsocksX/ProfileManager.m

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,22 @@ + (Configuration *)configuration {
3232

3333
+ (void)saveConfiguration:(Configuration *)configuration {
3434
[[NSUserDefaults standardUserDefaults] setObject:[configuration JSONData] forKey:CONFIG_DATA_KEY];
35+
[ProfileManager reloadShadowsocksRunner];
3536
}
3637

3738
+ (void)reloadShadowsocksRunner {
3839
Configuration *configuration = [ProfileManager configuration];
3940
if (configuration.current == -1) {
4041
[ShadowsocksRunner setUsingPublicServer:YES];
42+
[ShadowsocksRunner reloadConfig];
4143
} else {
4244
Profile *profile = configuration.profiles[configuration.current];
4345
[ShadowsocksRunner setUsingPublicServer:NO];
4446
[ShadowsocksRunner saveConfigForKey:kShadowsocksIPKey value:profile.server];
4547
[ShadowsocksRunner saveConfigForKey:kShadowsocksPortKey value:[NSString stringWithFormat:@"%ld", (long)profile.serverPort]];
4648
[ShadowsocksRunner saveConfigForKey:kShadowsocksPasswordKey value:profile.password];
4749
[ShadowsocksRunner saveConfigForKey:kShadowsocksEncryptionKey value:profile.method];
50+
[ShadowsocksRunner reloadConfig];
4851
}
4952
}
5053

ShadowsocksX/SWBAppDelegate.m

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import "SWBAppDelegate.h"
1313
#import "GCDWebServer.h"
1414
#import "ShadowsocksRunner.h"
15+
#import "ProfileManager.h"
1516

1617
#define kShadowsocksIsRunningKey @"ShadowsocksIsRunning"
1718
#define kShadowsocksRunningModeKey @"ShadowsocksMode"
@@ -26,6 +27,7 @@ @implementation SWBAppDelegate {
2627
NSMenuItem *autoMenuItem;
2728
NSMenuItem *globalMenuItem;
2829
NSMenuItem *qrCodeMenuItem;
30+
NSMenu *serversMenu;
2931
BOOL isRunning;
3032
NSString *runningMode;
3133
NSData *originalPACData;
@@ -79,7 +81,14 @@ - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
7981
[menu addItem:globalMenuItem];
8082

8183
[menu addItem:[NSMenuItem separatorItem]];
82-
[menu addItemWithTitle:_L(Open Server Preferences...) action:@selector(showConfigWindow) keyEquivalent:@""];
84+
85+
serversMenu = [[NSMenu alloc] init];
86+
NSMenuItem *serversItem = [[NSMenuItem alloc] init];
87+
[serversItem setTitle:@"Servers"];
88+
[serversItem setSubmenu:serversMenu];
89+
[menu addItem:serversItem];
90+
91+
[menu addItem:[NSMenuItem separatorItem]];
8392
[menu addItemWithTitle:_L(Edit PAC for Auto Proxy Mode...) action:@selector(editPAC) keyEquivalent:@""];
8493
qrCodeMenuItem = [[NSMenuItem alloc] initWithTitle:_L(Show QR Code...) action:@selector(showQRCode) keyEquivalent:@""];
8594
[menu addItem:qrCodeMenuItem];
@@ -122,6 +131,39 @@ - (void)enableGlobal {
122131
[self reloadSystemProxy];
123132
}
124133

134+
- (void)chooseServer:(id)sender {
135+
NSInteger tag = [sender tag];
136+
Configuration *configuration = [ProfileManager configuration];
137+
if (tag == -1 || tag < configuration.profiles.count) {
138+
configuration.current = tag;
139+
}
140+
[ProfileManager saveConfiguration:configuration];
141+
[self updateServersMenu];
142+
}
143+
144+
- (void)updateServersMenu {
145+
Configuration *configuration = [ProfileManager configuration];
146+
[serversMenu removeAllItems];
147+
int i = 0;
148+
NSMenuItem *publicItem = [[NSMenuItem alloc] initWithTitle:_L(Public Server) action:@selector(chooseServer:) keyEquivalent:@""];
149+
publicItem.tag = -1;
150+
if (-1 == configuration.current) {
151+
[publicItem setState:1];
152+
}
153+
[serversMenu addItem:publicItem];
154+
for (Profile *profile in configuration.profiles) {
155+
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:profile.server action:@selector(chooseServer:) keyEquivalent:@""];
156+
item.tag = i;
157+
if (i == configuration.current) {
158+
[item setState:1];
159+
}
160+
[serversMenu addItem:item];
161+
i++;
162+
}
163+
[serversMenu addItem:[NSMenuItem separatorItem]];
164+
[serversMenu addItemWithTitle:_L(Open Server Preferences...) action:@selector(showConfigWindow) keyEquivalent:@""];
165+
}
166+
125167
- (void)updateMenu {
126168
if (isRunning) {
127169
statusMenuItem.title = _L(Shadowsocks: On);
@@ -151,7 +193,7 @@ - (void)updateMenu {
151193
[qrCodeMenuItem setTarget:self];
152194
[qrCodeMenuItem setAction:@selector(showQRCode)];
153195
}
154-
196+
[self updateServersMenu];
155197
}
156198

157199
void onPACChange(

0 commit comments

Comments
 (0)