From 1e3b22ebd01f139be4ca981851c4fc339ec4a973 Mon Sep 17 00:00:00 2001 From: shenleban tongying Date: Mon, 11 Nov 2024 01:43:01 -0500 Subject: [PATCH] fix: macOS -> replace the tray icon with a dock menu --- src/config.cc | 5 +++-- src/config.hh | 7 ++++++- src/ui/mainwindow.cc | 11 ++++++++++- src/ui/preferences.cc | 5 +++++ 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/config.cc b/src/config.cc index 3c714889e..49dd4ff3f 100644 --- a/src/config.cc +++ b/src/config.cc @@ -199,7 +199,6 @@ Preferences::Preferences(): hideMenubar( false ), enableTrayIcon( true ), startToTray( false ), - closeToTray( true ), autoStart( false ), doubleClickTranslates( true ), selectWordBySingleClick( false ), @@ -906,7 +905,9 @@ Class load() c.preferences.enableTrayIcon = ( preferences.namedItem( "enableTrayIcon" ).toElement().text() == "1" ); c.preferences.startToTray = ( preferences.namedItem( "startToTray" ).toElement().text() == "1" ); - c.preferences.closeToTray = ( preferences.namedItem( "closeToTray" ).toElement().text() == "1" ); +#ifndef Q_OS_MACOS // // macOS uses the dock menu instead of the tray icon + c.preferences.closeToTray = ( preferences.namedItem( "closeToTray" ).toElement().text() == "1" ); +#endif c.preferences.autoStart = ( preferences.namedItem( "autoStart" ).toElement().text() == "1" ); c.preferences.alwaysOnTop = ( preferences.namedItem( "alwaysOnTop" ).toElement().text() == "1" ); c.preferences.searchInDock = ( preferences.namedItem( "searchInDock" ).toElement().text() == "1" ); diff --git a/src/config.hh b/src/config.hh index 9aea8f67c..497b15926 100644 --- a/src/config.hh +++ b/src/config.hh @@ -343,7 +343,12 @@ struct Preferences bool hideMenubar; bool enableTrayIcon; bool startToTray; - bool closeToTray; +#ifdef Q_OS_MACOS // macOS uses the dock menu instead of the tray icon + bool closeToTray = false; +#else + bool closeToTray = true; +#endif + bool autoStart; bool doubleClickTranslates; bool selectWordBySingleClick; diff --git a/src/ui/mainwindow.cc b/src/ui/mainwindow.cc index 16c9cca1c..c6ee00842 100644 --- a/src/ui/mainwindow.cc +++ b/src/ui/mainwindow.cc @@ -401,14 +401,18 @@ MainWindow::MainWindow( Config::Class & cfg_ ): connect( wordsZoomOut, &QAction::triggered, this, &MainWindow::doWordsZoomOut ); connect( wordsZoomBase, &QAction::triggered, this, &MainWindow::doWordsZoomBase ); - // tray icon +// tray icon +#ifndef Q_OS_MACOS // macOS uses the dock menu instead of the tray icon connect( trayIconMenu.addAction( tr( "Show &Main Window" ) ), &QAction::triggered, this, [ this ] { this->toggleMainWindow( true ); } ); +#endif trayIconMenu.addAction( enableScanningAction ); +#ifndef Q_OS_MACOS // macOS uses the dock menu instead of the tray icon trayIconMenu.addSeparator(); connect( trayIconMenu.addAction( tr( "&Quit" ) ), &QAction::triggered, this, &MainWindow::quitApp ); +#endif addGlobalAction( &escAction, [ this ]() { handleEsc(); @@ -1420,6 +1424,10 @@ void MainWindow::updateAppearances( QString const & addonStyle, void MainWindow::trayIconUpdateOrInit() { +#ifdef Q_OS_MACOS + trayIconMenu.setAsDockMenu(); +#else + if ( !cfg.preferences.enableTrayIcon ) { if ( trayIcon ) { delete trayIcon; @@ -1443,6 +1451,7 @@ void MainWindow::trayIconUpdateOrInit() // The 'Close to tray' action is associated with the tray icon, so we hide // or show it here. ui.actionCloseToTray->setVisible( cfg.preferences.enableTrayIcon ); +#endif } void MainWindow::wheelEvent( QWheelEvent * ev ) diff --git a/src/ui/preferences.cc b/src/ui/preferences.cc index 5223b3c1b..9d4578be1 100644 --- a/src/ui/preferences.cc +++ b/src/ui/preferences.cc @@ -174,6 +174,11 @@ Preferences::Preferences( QWidget * parent, Config::Class & cfg_ ): ui.hideSingleTab->setChecked( p.hideSingleTab ); ui.mruTabOrder->setChecked( p.mruTabOrder ); ui.enableTrayIcon->setChecked( p.enableTrayIcon ); + +#ifdef Q_OS_MACOS // macOS uses the dock menu instead of the tray icon + ui.enableTrayIcon->hide(); +#endif + ui.startToTray->setChecked( p.startToTray ); ui.closeToTray->setChecked( p.closeToTray ); ui.cbAutostart->setChecked( p.autoStart );