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

Add status bar icon toggle #103

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
85 changes: 78 additions & 7 deletions Lemon/Preference/LMPreferenceStatusBarViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//

#import "LMPreferenceStatusBarViewController.h"
#import "LemonDaemonConst.h"
#import <QMUICommon/LMAppThemeHelper.h>
#import <QMUICommon/COSwitch.h>
#import <QMCoreFunction/LanguageHelper.h>
Expand Down Expand Up @@ -65,14 +66,18 @@ - (void)viewWillLayout{
}
[LMAppThemeHelper setDivideLineColorFor:_bootMonitorLineView];
}
-(void)initView{
NSTextField *showStatusBarIconText = [self buildLabel:NSLocalizedStringFromTableInBundle(@"PreferenceViewController_setupViews_bootMonitorTitle _16", nil, [NSBundle bundleForClass:[self class]], @"") font:[NSFont systemFontOfSize:14] color:[LMAppThemeHelper getTitleColor]];
-(void)initView {

NSTextField *showStatusBarIconOnBootText = [self buildLabel:NSLocalizedStringFromTableInBundle(@"PreferenceViewController_setupViews_bootMonitorTitle _16", nil, [NSBundle bundleForClass:[self class]], @"") font:[NSFont systemFontOfSize:14] color:[LMAppThemeHelper getTitleColor]];

// 状态栏开机启动项设置
_myStatusType = [[[NSUserDefaults standardUserDefaults] objectForKey:kLemonShowMonitorCfg] integerValue];
__weak typeof(self) weakSelf = self;
COSwitch *bootMonitorSwitch = [[COSwitch alloc] init];
bootMonitorSwitch.on = (_myStatusType & STATUS_TYPE_BOOTSHOW) > 0 ? YES : NO;
__weak typeof(self) weakSelf = self;
bootMonitorSwitch.wantsLayer = YES;
bootMonitorSwitch.isEnable = (_myStatusType & STATUS_TYPE_GLOBAL) > 0 ? YES : NO;
bootMonitorSwitch.layer.opacity = (_myStatusType & STATUS_TYPE_GLOBAL) > 0 ? 1 : 0.5;
[bootMonitorSwitch setOnValueChanged:^(COSwitch *button) {
dispatch_async(dispatch_get_main_queue(), ^{
button.isEnable = NO;
Expand All @@ -95,10 +100,52 @@ -(void)initView{
});
}];


NSTextField *showStatusBarIconText = [self buildLabel:NSLocalizedStringFromTableInBundle(@"PreferenceViewController_toggleStatusBarVisibility", nil, [NSBundle bundleForClass:[self class]], @"Toggle status bar visibility") font:[NSFont systemFontOfSize:14] color:[LMAppThemeHelper getTitleColor]];

// 状态栏显示设置
COSwitch *statusBarVisibilitySwitch = [[COSwitch alloc] init];
statusBarVisibilitySwitch.on = (_myStatusType & STATUS_TYPE_GLOBAL) > 0 ? YES : NO;
[statusBarVisibilitySwitch setOnValueChanged:^(COSwitch *button) {
dispatch_async(dispatch_get_main_queue(), ^{
button.isEnable = NO;
NSLog(@"statusBarVisibilitySwitch setOnValueChanged: %d", button.on);

if (button.on) {
NSLog(@"preference:global=%d", button.on);

weakSelf.myStatusType |= STATUS_TYPE_GLOBAL;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:weakSelf.myStatusType] forKey:kLemonShowMonitorCfg];

[self openMonitor];

bootMonitorSwitch.isEnable = YES;
bootMonitorSwitch.layer.opacity = 1.0;
} else {
NSLog(@"preference:global=%d", button.on);

weakSelf.myStatusType &= ~STATUS_TYPE_GLOBAL;
[[NSUserDefaults standardUserDefaults] setObject:[NSNumber numberWithInteger:weakSelf.myStatusType] forKey:kLemonShowMonitorCfg];

// 去掉开机启动,并且设置为不可修改
bootMonitorSwitch.on = NO;
bootMonitorSwitch.isEnable = NO;
bootMonitorSwitch.layer.opacity = 0.5;
}

// send to monitor process
NSDictionary* dict = @{@"type":[NSNumber numberWithInteger: [weakSelf myStatusType]]};
[[NSDistributedNotificationCenter defaultCenter] postNotificationName:kStatusChangedNotification object:nil userInfo:dict deliverImmediately:YES];

dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
button.isEnable = YES;
});
});
}];

NSView* bootMonitorLineView = [[NSView alloc] init];
self.bootMonitorLineView = bootMonitorLineView;


// 状态栏图标显示
NSTextField *tfMonitorTitle = [self buildLabel:NSLocalizedStringFromTableInBundle(@"PreferenceViewController_setupViews_tfMonitorTitle _9", nil, [NSBundle bundleForClass:[self class]], @"") font:[NSFont systemFontOfSize:14] color:[LMAppThemeHelper getTitleColor]];
NSImageView *monitorImageView = [[NSImageView alloc] init];
Expand Down Expand Up @@ -134,6 +181,8 @@ -(void)initView{


[self.view addSubview:showStatusBarIconText];
[self.view addSubview:statusBarVisibilitySwitch];
[self.view addSubview:showStatusBarIconOnBootText];
[self.view addSubview:bootMonitorSwitch];
[self.view addSubview:bootMonitorLineView];

Expand All @@ -151,21 +200,33 @@ -(void)initView{

NSView *cView = self.view;

// 状态栏自启设置
[showStatusBarIconText mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(cView).offset(20);
make.leading.equalTo(cView).offset(29);
}];

[bootMonitorSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
[statusBarVisibilitySwitch mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(showStatusBarIconText.mas_centerY);
make.right.equalTo(cView).offset(-30);
make.width.equalTo(@(40));
make.height.equalTo(@(19));
}];

// 状态栏自启设置
[showStatusBarIconOnBootText mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(showStatusBarIconText.mas_bottom).offset(20);
make.leading.equalTo(cView).offset(29);
}];

[bootMonitorSwitch mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.equalTo(showStatusBarIconOnBootText.mas_centerY);
make.right.equalTo(cView).offset(-30);
make.width.equalTo(@(40));
make.height.equalTo(@(19));
}];

[bootMonitorLineView mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo(showStatusBarIconText.mas_bottom).offset(20);
make.top.equalTo(showStatusBarIconOnBootText.mas_bottom).offset(20);
make.centerX.equalTo(cView);
make.width.equalTo(cView);
make.height.equalTo(@(1));
Expand Down Expand Up @@ -256,6 +317,16 @@ -(void)initView{

}

-(void)openMonitor{
NSLog(@"%s, open monitor", __FUNCTION__);
NSError *error = NULL;
NSRunningApplication *app = [[NSWorkspace sharedWorkspace] launchApplicationAtURL:[NSURL fileURLWithPath:MONITOR_APP_PATH]
options:NSWorkspaceLaunchWithoutAddingToRecents | NSWorkspaceLaunchWithoutActivation
configuration:@{NSWorkspaceLaunchConfigurationArguments: @[[NSString stringWithFormat:@"%lu", LemonMonitorRunningMenu]]}
error:&error];
NSLog(@"%s, open lemon monitor: %@, %@",__FUNCTION__, app, error);
}

-(NSView*)getOptionView:(NSString*)image :(NSString*)tittle :(NSInteger)type
{
//
Expand Down
3 changes: 2 additions & 1 deletion Lemon/Resources/Base.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"PreferenceViewController_setupViews_1553049563_13" = "CPU\n temperature";
"PreferenceViewController_setupViews_1553049563_14" = "Fan\n speed";
"PreferenceViewController_setupViews_1553049563_15" = "Network\n speed";
"PreferenceViewController_setupViews_bootMonitorTitle _16" = "Show icons on menu bar on startup";
"PreferenceViewController_setupViews_bootMonitorTitle _16" = "Show on startup";
"PreferenceViewController_toggleStatusBarVisibility" = "Show status bar icons";
"About_windowAboutTitle_lemon" = "About Lemon Cleaner";
"Appdelegate_about_windowAboutTitle_lemonlite" = "About Lemon Cleaner";
"AppDelegate_fetchResearchChannel_notification_1" = "Close";
Expand Down
3 changes: 2 additions & 1 deletion Lemon/Resources/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"PreferenceViewController_setupViews_1553049563_15" = "Network\n speed";
"PreferenceViewController_setupViews_1553049563_16" = "CPU\n usage";
"PreferenceViewController_setupViews_1553049563_17" = "GPU\n usage";
"PreferenceViewController_setupViews_bootMonitorTitle _16" = "Show icons on menu bar on startup";
"PreferenceViewController_setupViews_bootMonitorTitle _16" = "Show on startup";
"PreferenceViewController_toggleStatusBarVisibility" = "Show status bar icons";
"About_windowAboutTitle_lemon" = "About Lemon Cleaner";
"Appdelegate_about_windowAboutTitle_lemonlite" = "About Lemon Cleaner Lite";
"AppDelegate_fetchResearchChannel_notification_1" = "Close";
Expand Down
3 changes: 2 additions & 1 deletion Lemon/Resources/zh-Hans.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@
"PreferenceViewController_setupViews_1553049563_15" = "网速";
"PreferenceViewController_setupViews_1553049563_16" = "CPU占用";
"PreferenceViewController_setupViews_1553049563_17" = "GPU占用";
"PreferenceViewController_setupViews_bootMonitorTitle _16" = "开机时显示状态栏图标";
"PreferenceViewController_setupViews_bootMonitorTitle _16" = "开机时显示";
"PreferenceViewController_toggleStatusBarVisibility" = "显示状态栏图标";
"About_windowAboutTitle_lemon" = "关于腾讯柠檬清理";
"Appdelegate_about_windowAboutTitle_lemonlite" = "关于Lemon Cleaner Lite";
"AppDelegate_fetchResearchChannel_notification_1" = "关闭";
Expand Down
12 changes: 12 additions & 0 deletions Tools/LemonMonitor/LemonMonitor/Monitor/LMMonitorController.m
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,13 @@ - (id)init

-(void)setupData
{
NSNumber *type = [[NSUserDefaults standardUserDefaults] objectForKey:kLemonShowMonitorCfg];
if (([type integerValue] & STATUS_TYPE_GLOBAL) == 0) {
// 直接关闭进程(如果用户关闭了显示状态栏的开关)
[[NSApplication sharedApplication] terminate:nil];
return;
}

_upSpeedHistory = [[QMValueHistory alloc] initWithCapacity:32];
_downSpeedHistory = [[QMValueHistory alloc] initWithCapacity:32];
dataCenter = [QMDataCenter defaultCenter];
Expand Down Expand Up @@ -618,6 +625,11 @@ -(void)receivedStatusChanged:(NSNotification *)notification
NSInteger type = 0;
NSDictionary* dict = notification.userInfo;
type = ((NSNumber*)dict[@"type"]).integerValue;
if ((type & STATUS_TYPE_GLOBAL) == 0)
{
[[NSApplication sharedApplication] terminate:nil];
return;
}
[statusView setStatusType:type];
[[McStatMonitor shareMonitor] setTrayType:type];
// NSLog(@"receivedStatusChanged type=%lu", type);
Expand Down
3 changes: 3 additions & 0 deletions localPod/QMUICommon/QMUICommon/Classes/COSwitch.m
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ - (void)mouseDown:(NSEvent *)theEvent

- (void)mouseEntered:(NSEvent *)theEvent
{
if (!self.isEnable) {
return;
}
if (self.isAnimator) {
[NSAnimationContext runAnimationGroup:^(NSAnimationContext *context) {
context.duration = 0.1;
Expand Down