mirror of
https://github.com/MatsuriDayo/nekoray.git
synced 2025-12-17 12:34:37 +03:00
feat: autorun minimize to tray
This commit is contained in:
@@ -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));
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
#include "ui/mainwindow.h"
|
||||
#include "ui/mainwindow_interface.h"
|
||||
|
||||
#include <csignal>
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -13,50 +13,41 @@
|
||||
|
||||
#include <QSettings>
|
||||
|
||||
//设置程序自启动 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);
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
void SetProcessAutoRunSelf(bool enable);
|
||||
void AutoRun_SetEnabled(bool enable);
|
||||
|
||||
bool GetProcessAutoRunSelf();
|
||||
bool AutoRun_IsEnabled();
|
||||
|
||||
@@ -151,10 +151,6 @@
|
||||
<source>Connection statistics</source>
|
||||
<translation>连接统计</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Minimize to tray icon on startup</source>
|
||||
<translation>启动时最小化到托盘图标</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Include Pre-release when checking update</source>
|
||||
<translation>检查更新时包括 Pre-release</translation>
|
||||
@@ -1090,7 +1086,7 @@ End: %2</source>
|
||||
<translation>重置流量</translation>
|
||||
</message>
|
||||
<message>
|
||||
<source>Delete Repeat</source>
|
||||
<source>Remove Duplicates</source>
|
||||
<translation>删除重复的配置</translation>
|
||||
</message>
|
||||
<message>
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
@@ -334,20 +334,6 @@
|
||||
<item row="3" column="0">
|
||||
<widget class="QGroupBox" name="horizontalGroupBox2">
|
||||
<layout class="QHBoxLayout" name="horizontalLayout_20">
|
||||
<item>
|
||||
<widget class="QCheckBox" name="start_minimal">
|
||||
<property name="text">
|
||||
<string>Minimize to tray icon on startup</string>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="Line" name="line">
|
||||
<property name="orientation">
|
||||
<enum>Qt::Vertical</enum>
|
||||
</property>
|
||||
</widget>
|
||||
</item>
|
||||
<item>
|
||||
<widget class="QPushButton" name="set_custom_icon">
|
||||
<property name="text">
|
||||
|
||||
@@ -51,6 +51,10 @@
|
||||
#include <QDir>
|
||||
#include <QFileInfo>
|
||||
|
||||
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();
|
||||
|
||||
@@ -188,3 +188,5 @@ protected:
|
||||
inline MainWindow *GetMainWindow() {
|
||||
return (MainWindow *) mainwindow;
|
||||
}
|
||||
|
||||
void UI_InitMainWindow();
|
||||
|
||||
@@ -705,7 +705,7 @@
|
||||
</action>
|
||||
<action name="menu_delete_repeat">
|
||||
<property name="text">
|
||||
<string>Delete Repeat</string>
|
||||
<string>Remove Duplicates</string>
|
||||
</property>
|
||||
</action>
|
||||
<action name="actionfake">
|
||||
|
||||
Reference in New Issue
Block a user