feat: auto hide/show dock icon on MacOS

This commit is contained in:
parhelia512 2025-12-09 05:45:29 +08:00
parent 257bd917e6
commit fce3cf02be
3 changed files with 19 additions and 3 deletions

View File

@ -169,6 +169,8 @@ int MessageBoxInfo(const QString &title, const QString &text);
void ActivateWindow(QWidget *w);
void HideWindow(QWidget *w);
//
void runOnUiThread(const std::function<void()> &callback, bool wait = false);

View File

@ -21,6 +21,9 @@
#ifdef Q_OS_WIN
#include "include/sys/windows/guihelper.h"
#endif
#ifdef Q_OS_MAC
#include <ApplicationServices/ApplicationServices.h>
#endif
QStringList SplitLines(const QString &_string) {
return _string.split(QRegularExpression("[\r\n]"), Qt::SplitBehaviorFlags::SkipEmptyParts);
@ -257,11 +260,22 @@ void ActivateWindow(QWidget *w) {
w->setVisible(true);
#ifdef Q_OS_WIN
Windows_QWidget_SetForegroundWindow(w);
#elif defined(Q_OS_MAC)
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
#endif
w->raise();
w->activateWindow();
}
void HideWindow(QWidget *w) {
w->hide();
#ifdef Q_OS_MAC
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToUIElementApplication);
#endif
}
void runOnUiThread(const std::function<void()> &callback, bool wait) {
// any thread
auto *timer = new QTimer();

View File

@ -383,7 +383,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
ui->actionStart_with_system->setChecked(AutoRun_IsEnabled());
ui->actionAllow_LAN->setChecked(QStringList{"::", "0.0.0.0"}.contains(Configs::dataStore->inbound_address));
connect(ui->actionHide_window, &QAction::triggered, this, [=, this](){ this->hide(); });
connect(ui->actionHide_window, &QAction::triggered, this, [=, this](){ HideWindow(this); });
connect(ui->menu_open_config_folder, &QAction::triggered, this, [=,this] { QDesktopServices::openUrl(QUrl::fromLocalFile(QDir::currentPath())); });
connect(ui->menu_add_from_clipboard2, &QAction::triggered, ui->menu_add_from_clipboard, &QAction::trigger);
connect(ui->actionRestart_Proxy, &QAction::triggered, this, [=,this] { if (Configs::dataStore->started_id>=0) profile_start(Configs::dataStore->started_id); });
@ -638,7 +638,7 @@ MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWi
void MainWindow::closeEvent(QCloseEvent *event) {
if (tray->isVisible()) {
hide();
HideWindow(this);
event->ignore();
} else {
on_menu_exit_triggered();
@ -969,7 +969,7 @@ void MainWindow::prepare_exit()
mu_exit.unlock();
return;
}
hide();
HideWindow(this);
Configs::dataStore->prepare_exit = true;
//
RegisterHiddenMenuShortcuts(true);