Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mac: add display realtime status (ui) #1066

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/config/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ bool ReadConfig() {
}
config_impl->Read("tls13_early_data", &FLAGS_tls13_early_data);

#if BUILDFLAG(IS_MAC)
config_impl->Read("ui_display_realtime_status", &FLAGS_ui_display_realtime_status);
#endif

/* close fields */
config_impl->Close();

Expand Down Expand Up @@ -145,6 +149,10 @@ bool SaveConfig() {
}
all_fields_written &= config_impl->Write("tls13_early_data", FLAGS_tls13_early_data);

#if BUILDFLAG(IS_MAC)
all_fields_written &= config_impl->Write("ui_display_realtime_status", FLAGS_ui_display_realtime_status);
#endif

all_fields_written &= config_impl->Close();

return all_fields_written;
Expand Down
4 changes: 4 additions & 0 deletions src/config/config_core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,7 @@ ABSL_FLAG(CipherMethodFlag, method, CipherMethodFlag(CRYPTO_DEFAULT), kCipherMet

ABSL_FLAG(uint32_t, parallel_max, 512, "Maximum concurrency for parallel connections");
ABSL_FLAG(RateFlag, limit_rate, RateFlag(0), "Limit transfer speed to RATE");

#if BUILDFLAG(IS_MAC)
ABSL_FLAG(bool, ui_display_realtime_status, true, "Display Realtime Status in Status Bar (UI)");
#endif
5 changes: 5 additions & 0 deletions src/config/config_core.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <absl/flags/declare.h>
#include <absl/strings/string_view.h>
#include <base/compiler_specific.h>
#include <stdint.h>
#include <string>
#include <string_view>
Expand Down Expand Up @@ -36,4 +37,8 @@ bool AbslParseFlag(absl::string_view text, RateFlag* flag, std::string* err);

std::string AbslUnparseFlag(const RateFlag&);

#if BUILDFLAG(IS_MAC)
ABSL_DECLARE_FLAG(bool, ui_display_realtime_status);
#endif

#endif // H_CONFIG_CONFIG_CORE
160 changes: 93 additions & 67 deletions src/mac/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions src/mac/YassAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

enum YASSState { STOPPED, STARTED, STARTING, START_FAILED, STOPPING };

@class YassViewController;
@interface YassAppDelegate : NSObject <NSApplicationDelegate>
- (enum YASSState)getState;
- (NSString*)getStatus;
- (void)OnStart:(YassViewController*)viewController;
- (void)OnStop:(YassViewController*)viewController withOption:(BOOL)quiet;
- (void)OnStart;
- (void)OnStop:(BOOL)quiet;

@end

Expand Down
23 changes: 11 additions & 12 deletions src/mac/YassAppDelegate.mm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "version.h"

@interface YassAppDelegate ()
- (std::string)SaveConfig:(YassViewController*)viewController;
- (std::string)SaveConfig;
- (void)OnStarted;
- (void)OnStartFailed:(std::string)error_msg;
- (void)OnStopped;
Expand All @@ -44,7 +44,7 @@ - (void)applicationDidFinishLaunching:(NSNotification*)aNotification {

- (void)applicationWillTerminate:(NSNotification*)aNotification {
LOG(WARNING) << "Application exiting";
[self OnStop:nil withOption:TRUE];
[self OnStop:TRUE];
}

- (BOOL)applicationSupportsSecureRestorableState:(NSApplication*)app {
Expand Down Expand Up @@ -116,9 +116,9 @@ - (NSString*)getStatus {
return SysUTF8ToNSString(ss.str());
}

- (void)OnStart:(YassViewController*)viewController {
- (void)OnStart {
state_ = STARTING;
auto err_msg = [self SaveConfig:viewController];
auto err_msg = [self SaveConfig];
if (!err_msg.empty()) {
[self OnStartFailed:err_msg];
return;
Expand Down Expand Up @@ -147,7 +147,7 @@ - (void)OnStart:(YassViewController*)viewController {
worker_.Start(std::move(callback));
}

- (void)OnStop:(YassViewController*)viewController withOption:(BOOL)quiet {
- (void)OnStop:(BOOL)quiet {
state_ = STOPPING;

absl::AnyInvocable<void()> callback;
Expand All @@ -165,17 +165,15 @@ - (void)OnStarted {
state_ = STARTED;
config::SaveConfig();

YassWindowController* windowController =
(YassWindowController*)NSApplication.sharedApplication.mainWindow.windowController;
YassWindowController* windowController = [YassWindowController instance];
[windowController Started];
}

- (void)OnStartFailed:(std::string)error_msg {
state_ = START_FAILED;

error_msg_ = error_msg;
YassWindowController* windowController =
(YassWindowController*)NSApplication.sharedApplication.mainWindow.windowController;
YassWindowController* windowController = [YassWindowController instance];
[windowController StartFailed];
NSAlert* alert = [[NSAlert alloc] init];
alert.messageText = @(error_msg.c_str());
Expand All @@ -186,12 +184,13 @@ - (void)OnStartFailed:(std::string)error_msg {

- (void)OnStopped {
state_ = STOPPED;
YassWindowController* windowController =
(YassWindowController*)NSApplication.sharedApplication.mainWindow.windowController;
YassWindowController* windowController = [YassWindowController instance];
[windowController Stopped];
}

- (std::string)SaveConfig:(YassViewController*)viewController {
- (std::string)SaveConfig {
YassViewController* viewController = [YassViewController instance];

auto server_host = SysNSStringToUTF8(viewController.serverHost.stringValue);
auto server_sni = SysNSStringToUTF8(viewController.serverSNI.stringValue);
auto server_port = SysNSStringToUTF8(viewController.serverPort.stringValue);
Expand Down
3 changes: 3 additions & 0 deletions src/mac/YassViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@
@property(weak) IBOutlet NSTextField* timeout;
@property(weak) IBOutlet NSButton* autoStart;
@property(weak) IBOutlet NSButton* systemProxy;
@property(weak) IBOutlet NSButton* displayStatus;

+ (YassViewController* __weak)instance;
@end

#endif // YASS_MAC_VIEW_CONTROLLER
24 changes: 20 additions & 4 deletions src/mac/YassViewController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,22 @@
#include "mac/YassWindowController.h"
#include "mac/utils.h"

static YassViewController* __weak _instance;

@interface YassViewController ()
@end

@implementation YassViewController {
bool should_startup_at_app_load;
}

+ (YassViewController* __weak)instance {
return _instance;
}

- (void)viewDidLoad {
[super viewDidLoad];
_instance = self;

[self.cipherMethod removeAllItems];
NSString* methodStrings[] = {
Expand Down Expand Up @@ -101,6 +108,12 @@ - (IBAction)OnSystemProxyChecked:(id)sender {
});
}

- (IBAction)OnDisplayStatusClicked:(id)sender {
YassWindowController* windowController = [YassWindowController instance];
bool enable = self.displayStatus.state == NSControlStateValueOn;
[windowController toggleDisplayStatus:enable];
}

- (void)OnStart {
[self.startButton setEnabled:FALSE];
[self.stopButton setEnabled:FALSE];
Expand All @@ -118,16 +131,16 @@ - (void)OnStart {
[self.limitRate setEnabled:FALSE];
[self.timeout setEnabled:FALSE];

YassWindowController* windowController = (YassWindowController*)self.view.window.windowController;
[windowController OnStart:self];
YassWindowController* windowController = [YassWindowController instance];
[windowController OnStart];
}

- (void)OnStop {
[self.startButton setEnabled:FALSE];
[self.stopButton setEnabled:FALSE];

YassWindowController* windowController = (YassWindowController*)self.view.window.windowController;
[windowController OnStop:self];
YassWindowController* windowController = [YassWindowController instance];
[windowController OnStop];
}

- (void)Started {
Expand Down Expand Up @@ -184,6 +197,9 @@ - (void)LoadChanges {
self.dotHost.stringValue = SysUTF8ToNSString(absl::GetFlag(FLAGS_dot_host));
self.limitRate.stringValue = SysUTF8ToNSString(std::string(absl::GetFlag(FLAGS_limit_rate)));
self.timeout.intValue = absl::GetFlag(FLAGS_connect_timeout);

BOOL enable_status_bar = absl::GetFlag(FLAGS_ui_display_realtime_status) ? TRUE : FALSE;
self.displayStatus.state = enable_status_bar ? NSControlStateValueOn : NSControlStateValueOff;
}

@end
7 changes: 5 additions & 2 deletions src/mac/YassWindowController.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

@class YassViewController;
@interface YassWindowController : NSWindowController <NSWindowDelegate>
- (void)OnStart:(YassViewController*)viewController;
- (void)OnStop:(YassViewController*)viewController;
- (void)OnStart;
- (void)OnStop;
- (void)Started;
- (void)StartFailed;
- (void)Stopped;
- (void)toggleDisplayStatus:(BOOL)enable;

+ (YassWindowController* __weak)instance;
@end

#endif // YASS_MAC_WINDOW_CONTROLLER
46 changes: 38 additions & 8 deletions src/mac/YassWindowController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
#import "mac/YassViewController.h"
#include "mac/utils.h"

static YassWindowController* __weak _instance;

@interface YassWindowController ()
- (void)toggleDisplayStatusInternal:(BOOL)enable;
@end

@implementation YassWindowController {
Expand All @@ -33,8 +36,14 @@ @implementation YassWindowController {
BOOL hide_on_closed_;
}

+ (YassWindowController* __weak)instance {
return _instance;
}

- (void)windowDidLoad {
[super windowDidLoad];
_instance = self;

[self refreshTimerExceeded];

y_status_bar_item_ = [[NSStatusBar systemStatusBar] statusItemWithLength:NSSquareStatusItemLength];
Expand All @@ -45,7 +54,8 @@ - (void)windowDidLoad {
[y_status_bar_item_.button
sendActionOn:NSEventMaskLeftMouseDown | NSEventMaskRightMouseDown | NSEventMaskOtherMouseDown];

status_bar_item_ = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
BOOL enable_status_bar = absl::GetFlag(FLAGS_ui_display_realtime_status) ? TRUE : FALSE;
[self toggleDisplayStatusInternal:enable_status_bar];

hide_on_closed_ = FALSE;
}
Expand Down Expand Up @@ -77,6 +87,10 @@ - (void)statusItemClicked {
}

- (void)UpdateStatusBar {
if (status_bar_item_ == nil) {
status_bar_text_ = nil;
return;
}
NSString* next_status_bar_text = [self getStatusMessage];
// prevent deep copy on unchanged title
if ([next_status_bar_text isEqualToString:status_bar_text_]) {
Expand Down Expand Up @@ -131,33 +145,49 @@ - (void)refreshTimerExceeded {
repeats:NO];
}

- (void)OnStart:(YassViewController*)viewController {
- (void)OnStart {
YassAppDelegate* appDelegate = (YassAppDelegate*)NSApplication.sharedApplication.delegate;

[appDelegate OnStart:viewController];
[appDelegate OnStart];
}

- (void)OnStop:(YassViewController*)viewController {
- (void)OnStop {
YassAppDelegate* appDelegate = (YassAppDelegate*)NSApplication.sharedApplication.delegate;
[appDelegate OnStop:viewController withOption:FALSE];
[appDelegate OnStop:FALSE];
}

- (void)Started {
[self UpdateStatusBar];
YassViewController* viewController = (YassViewController*)self.contentViewController;
YassViewController* viewController = [YassViewController instance];
[viewController Started];
}

- (void)StartFailed {
[self UpdateStatusBar];
YassViewController* viewController = (YassViewController*)self.contentViewController;
YassViewController* viewController = [YassViewController instance];
[viewController StartFailed];
}

- (void)Stopped {
[self UpdateStatusBar];
YassViewController* viewController = (YassViewController*)self.contentViewController;
YassViewController* viewController = [YassViewController instance];
[viewController Stopped];
}

- (void)toggleDisplayStatus:(BOOL)enable {
[self toggleDisplayStatusInternal:enable];
absl::SetFlag(&FLAGS_ui_display_realtime_status, (enable == TRUE ? true : false));
config::SaveConfig();
}

- (void)toggleDisplayStatusInternal:(BOOL)enable {
if (enable && status_bar_item_ == nil) {
status_bar_item_ = [[NSStatusBar systemStatusBar] statusItemWithLength:NSVariableStatusItemLength];
[self UpdateStatusBar];
} else if (enable == FALSE && status_bar_item_ != nil) {
status_bar_item_ = nil;
status_bar_text_ = nil;
}
}

@end
4 changes: 4 additions & 0 deletions src/mac/en.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@
"zY6-tD-aUd.title" = "Timeout";

"bxG-gg-LTc.title" = "Server SNI";

"mw5-nB-ary.title" = "Display Realtime Status";

"71A-aG-Rt6.title" = "Enable";
4 changes: 4 additions & 0 deletions src/mac/zh-Hans.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,7 @@
"zY6-tD-aUd.title" = "超时时间";

"bxG-gg-LTc.title" = "服务器名称指示";

"mw5-nB-ary.title" = "显示实时状态";

"71A-aG-Rt6.title" = "启用";