From 4c35a451e9c65b2e4d6ea5403471d0ee81323347 Mon Sep 17 00:00:00 2001 From: arm64v8a <48624112+arm64v8a@users.noreply.github.com> Date: Wed, 9 Nov 2022 12:24:43 +0900 Subject: [PATCH] feat: autorun minimize to tray --- main/NekoRay.cpp | 1 - main/NekoRay_DataStore.hpp | 2 +- main/main.cpp | 5 ++-- sys/AutoRun.cpp | 53 +++++++++++++++--------------------- sys/AutoRun.hpp | 4 +-- translations/zh_CN.ts | 6 +--- ui/dialog_basic_settings.cpp | 2 -- ui/dialog_basic_settings.ui | 14 ---------- ui/mainwindow.cpp | 18 ++++++++---- ui/mainwindow.h | 2 ++ ui/mainwindow.ui | 2 +- 11 files changed, 44 insertions(+), 65 deletions(-) diff --git a/main/NekoRay.cpp b/main/NekoRay.cpp index 916ea63..aa96369 100644 --- a/main/NekoRay.cpp +++ b/main/NekoRay.cpp @@ -39,7 +39,6 @@ namespace NekoRay { _add(new configItem("enhance_domain", &enhance_resolve_server_domain, itemType::boolean)); _add(new configItem("remember_id", &remember_id, itemType::integer)); _add(new configItem("remember_enable", &remember_enable, itemType::boolean)); - _add(new configItem("start_minimal", &start_minimal, itemType::boolean)); _add(new configItem("language", &language, itemType::integer)); _add(new configItem("spmode", &remember_spmode, itemType::integer)); _add(new configItem("insecure_hint", &insecure_hint, itemType::boolean)); diff --git a/main/NekoRay_DataStore.hpp b/main/NekoRay_DataStore.hpp index 5524eb8..7f26a58 100644 --- a/main/NekoRay_DataStore.hpp +++ b/main/NekoRay_DataStore.hpp @@ -56,6 +56,7 @@ namespace NekoRay { // Flags bool flag_use_appdata = false; bool flag_many = false; + bool flag_tray = false; // Saved @@ -87,7 +88,6 @@ namespace NekoRay { int remember_spmode = NekoRay::SystemProxyMode::DISABLE; int remember_id = -1919; bool remember_enable = false; - bool start_minimal = false; // Socks & HTTP Inbound QString inbound_address = "127.0.0.1"; diff --git a/main/main.cpp b/main/main.cpp index 30d1d4c..a879bea 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -1,4 +1,4 @@ -#include "ui/mainwindow.h" +#include "ui/mainwindow_interface.h" #include @@ -51,6 +51,7 @@ int main(int argc, char *argv[]) { auto args = QApplication::arguments(); if (args.contains("-many")) NekoRay::dataStore->flag_many = true; if (args.contains("-appdata")) NekoRay::dataStore->flag_use_appdata = true; + if (args.contains("-tray")) NekoRay::dataStore->flag_tray = true; #ifdef NKR_CPP_USE_APPDATA NekoRay::dataStore->flag_use_appdata = true; #endif @@ -155,6 +156,6 @@ int main(int argc, char *argv[]) { signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); - MainWindow w; + UI_InitMainWindow(); return QApplication::exec(); } diff --git a/sys/AutoRun.cpp b/sys/AutoRun.cpp index 56cda07..3f6a297 100644 --- a/sys/AutoRun.cpp +++ b/sys/AutoRun.cpp @@ -13,50 +13,41 @@ #include -//设置程序自启动 appPath程序路径 -void SetProcessAutoRunSelf(bool enable) { +QString Windows_GenAutoRunString() { auto appPath = QApplication::applicationFilePath(); + appPath = "\"" + QDir::toNativeSeparators(appPath) + "\""; + appPath += " -tray"; + return appPath; +} + +void AutoRun_SetEnabled(bool enable) { + //以程序名称作为注册表中的键 + //根据键获取对应的值(程序路径) + auto appPath = QApplication::applicationFilePath(); + QFileInfo fInfo(appPath); + QString name = fInfo.baseName(); QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); - //以程序名称作为注册表中的键 - //根据键获取对应的值(程序路径) - QFileInfo fInfo(appPath); - QString name = fInfo.baseName(); - QString path = settings.value(name).toString(); - - //如果注册表中的路径和当前程序路径不一样, - //则表示没有设置自启动或自启动程序已经更换了路径 - //toNativeSeparators的意思是将"/"替换为"\" - QString newPath = QDir::toNativeSeparators(appPath); - if (enable) { - if (path != newPath) { - settings.setValue(name, newPath); - } + settings.setValue(name, Windows_GenAutoRunString()); } else { settings.remove(name); } } -bool GetProcessAutoRunSelf() { - auto appPath = QApplication::applicationFilePath(); - +bool AutoRun_IsEnabled() { QSettings settings("HKEY_CURRENT_USER\\SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", QSettings::NativeFormat); //以程序名称作为注册表中的键 //根据键获取对应的值(程序路径) + auto appPath = QApplication::applicationFilePath(); QFileInfo fInfo(appPath); QString name = fInfo.baseName(); - QString path = settings.value(name).toString(); - //如果注册表中的路径和当前程序路径不一样, - //则表示没有设置自启动或自启动程序已经更换了路径 - //toNativeSeparators的意思是将"/"替换为"\" - QString newPath = QDir::toNativeSeparators(appPath); - return path == newPath; + return settings.value(name).toString() == Windows_GenAutoRunString(); } @@ -64,7 +55,7 @@ bool GetProcessAutoRunSelf() { #ifdef Q_OS_MACOS -void SetProcessAutoRunSelf(bool enable) { +void AutoRun_SetEnabled(bool enable) { // From // https://github.com/nextcloud/desktop/blob/master/src/common/utility_mac.cpp QString filePath = QDir(QCoreApplication::applicationDirPath() + QLatin1String("/../..")).absolutePath(); @@ -109,7 +100,7 @@ void SetProcessAutoRunSelf(bool enable) { CFRelease(urlRef); } -bool GetProcessAutoRunSelf() { +bool AutoRun_IsEnabled() { // From // https://github.com/nextcloud/desktop/blob/master/src/common/utility_mac.cpp // this is quite some duplicate code with setLaunchOnStartup, at some @@ -171,17 +162,17 @@ QString getUserAutostartDir_private() { return config; } -void SetProcessAutoRunSelf(bool enable) { +void AutoRun_SetEnabled(bool enable) { // From https://github.com/nextcloud/desktop/blob/master/src/common/utility_unix.cpp QString appName = QCoreApplication::applicationName(); QString userAutoStartPath = getUserAutostartDir_private(); QString desktopFileLocation = userAutoStartPath + appName + QLatin1String(".desktop"); - QStringList appCmdList = {QApplication::applicationFilePath()}; + QStringList appCmdList = {QApplication::applicationFilePath(), "-tray"}; // nekoray: launcher auto launcherPath = QApplication::applicationDirPath() + "/launcher"; if (QFile::exists(launcherPath)) { - appCmdList = QStringList{launcherPath}; + appCmdList = QStringList{launcherPath, "--", "-tray"}; } if (enable) { @@ -218,7 +209,7 @@ void SetProcessAutoRunSelf(bool enable) { } } -bool GetProcessAutoRunSelf() { +bool AutoRun_IsEnabled() { QString appName = QCoreApplication::applicationName(); QString desktopFileLocation = getUserAutostartDir_private() + appName + QLatin1String(".desktop"); return QFile::exists(desktopFileLocation); diff --git a/sys/AutoRun.hpp b/sys/AutoRun.hpp index ef92937..7fd25a9 100644 --- a/sys/AutoRun.hpp +++ b/sys/AutoRun.hpp @@ -1,5 +1,5 @@ #pragma once -void SetProcessAutoRunSelf(bool enable); +void AutoRun_SetEnabled(bool enable); -bool GetProcessAutoRunSelf(); +bool AutoRun_IsEnabled(); diff --git a/translations/zh_CN.ts b/translations/zh_CN.ts index f57a554..d9c11ec 100644 --- a/translations/zh_CN.ts +++ b/translations/zh_CN.ts @@ -151,10 +151,6 @@ Connection statistics 连接统计 - - Minimize to tray icon on startup - 启动时最小化到托盘图标 - Include Pre-release when checking update 检查更新时包括 Pre-release @@ -1090,7 +1086,7 @@ End: %2 重置流量 - Delete Repeat + Remove Duplicates 删除重复的配置 diff --git a/ui/dialog_basic_settings.cpp b/ui/dialog_basic_settings.cpp index af672f9..c8d5d9d 100644 --- a/ui/dialog_basic_settings.cpp +++ b/ui/dialog_basic_settings.cpp @@ -100,7 +100,6 @@ DialogBasicSettings::DialogBasicSettings(QWidget *parent) ui->connection_statistics_box->setDisabled(true); } // - D_LOAD_BOOL(start_minimal) D_LOAD_BOOL(check_include_pre) D_LOAD_BOOL(connection_statistics) // @@ -253,7 +252,6 @@ void DialogBasicSettings::accept() { // Style NekoRay::dataStore->language = ui->language->currentIndex(); - D_SAVE_BOOL(start_minimal) D_SAVE_BOOL(connection_statistics) D_SAVE_BOOL(check_include_pre) diff --git a/ui/dialog_basic_settings.ui b/ui/dialog_basic_settings.ui index 4df4177..382c4fb 100644 --- a/ui/dialog_basic_settings.ui +++ b/ui/dialog_basic_settings.ui @@ -334,20 +334,6 @@ - - - - Minimize to tray icon on startup - - - - - - - Qt::Vertical - - - diff --git a/ui/mainwindow.cpp b/ui/mainwindow.cpp index 6bf0ee4..930d4a4 100644 --- a/ui/mainwindow.cpp +++ b/ui/mainwindow.cpp @@ -51,6 +51,10 @@ #include #include +void UI_InitMainWindow() { + mainwindow = new MainWindow; +} + MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { mainwindow = this; @@ -261,7 +265,7 @@ MainWindow::MainWindow(QWidget *parent) // connect(ui->menu_program, &QMenu::aboutToShow, this, [=]() { ui->actionRemember_last_proxy->setChecked(NekoRay::dataStore->remember_enable); - ui->actionStart_with_system->setChecked(GetProcessAutoRunSelf()); + ui->actionStart_with_system->setChecked(AutoRun_IsEnabled()); ui->actionAllow_LAN->setChecked(QStringList{"::", "0.0.0.0"}.contains(NekoRay::dataStore->inbound_address)); // active server for (const auto &old: ui->menuActive_Server->actions()) { @@ -324,7 +328,7 @@ MainWindow::MainWindow(QWidget *parent) NekoRay::dataStore->Save(); }); connect(ui->actionStart_with_system, &QAction::triggered, this, [=](bool checked) { - SetProcessAutoRunSelf(checked); + AutoRun_SetEnabled(checked); }); connect(ui->actionAllow_LAN, &QAction::triggered, this, [=](bool checked) { NekoRay::dataStore->inbound_address = checked ? "::" : "127.0.0.1"; @@ -403,7 +407,7 @@ MainWindow::MainWindow(QWidget *parent) connect(qApp, &QGuiApplication::commitDataRequest, this, &MainWindow::on_commitDataRequest); - if (!NekoRay::dataStore->start_minimal) show(); + if (!NekoRay::dataStore->flag_tray) show(); } void MainWindow::closeEvent(QCloseEvent *event) { @@ -572,7 +576,8 @@ void MainWindow::on_menu_exit_triggered() { QDir::setCurrent(QApplication::applicationDirPath()); QProcess::startDetached("./nekoray", QStringList{}); } - qApp->quit(); + tray->hide(); + QCoreApplication::quit(); } #define neko_set_spmode_FAILED refresh_status(); return; @@ -612,7 +617,8 @@ void MainWindow::neko_set_spmode(int mode, bool save) { SetSystemProxy(http_port, socks_port); } else if (mode == NekoRay::SystemProxyMode::VPN) { if (NekoRay::dataStore->need_keep_vpn_off) { - MessageBoxWarning(software_name, tr("Current server is incompatible with VPN. Please stop the server first, enable VPN mode, and then restart.")); + MessageBoxWarning(software_name, + tr("Current server is incompatible with VPN. Please stop the server first, enable VPN mode, and then restart.")); neko_set_spmode_FAILED } if (!StartVPNProcess()) { @@ -1498,7 +1504,7 @@ bool MainWindow::StartVPNProcess() { } // auto vpn_process = new QProcess; - QProcess::connect(vpn_process, &QProcess::stateChanged, mainwindow, [=](QProcess::ProcessState state) { + QProcess::connect(vpn_process, &QProcess::stateChanged, this, [=](QProcess::ProcessState state) { if (state == QProcess::NotRunning) { vpn_pid = 0; vpn_process->deleteLater(); diff --git a/ui/mainwindow.h b/ui/mainwindow.h index fb23003..39eb9e5 100644 --- a/ui/mainwindow.h +++ b/ui/mainwindow.h @@ -188,3 +188,5 @@ protected: inline MainWindow *GetMainWindow() { return (MainWindow *) mainwindow; } + +void UI_InitMainWindow(); diff --git a/ui/mainwindow.ui b/ui/mainwindow.ui index 8591673..6835463 100644 --- a/ui/mainwindow.ui +++ b/ui/mainwindow.ui @@ -705,7 +705,7 @@ - Delete Repeat + Remove Duplicates