Skip to content

Commit

Permalink
fix: macOS -> replace the tray icon with a dock menu
Browse files Browse the repository at this point in the history
  • Loading branch information
shenlebantongying committed Nov 11, 2024
1 parent 720f66c commit 1e3b22e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
5 changes: 3 additions & 2 deletions src/config.cc
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,6 @@ Preferences::Preferences():
hideMenubar( false ),
enableTrayIcon( true ),
startToTray( false ),
closeToTray( true ),
autoStart( false ),
doubleClickTranslates( true ),
selectWordBySingleClick( false ),
Expand Down Expand Up @@ -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" );
Expand Down
7 changes: 6 additions & 1 deletion src/config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 10 additions & 1 deletion src/ui/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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 )
Expand Down
5 changes: 5 additions & 0 deletions src/ui/preferences.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down

0 comments on commit 1e3b22e

Please sign in to comment.