Skip to content

Commit

Permalink
fix: tray icon <-> toggleMainWindow connected twice (#1535)
Browse files Browse the repository at this point in the history
  • Loading branch information
shenlebantongying authored May 28, 2024
1 parent bd3918d commit 05d9a39
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 46 deletions.
62 changes: 17 additions & 45 deletions src/ui/mainwindow.cc
Original file line number Diff line number Diff line change
Expand Up @@ -597,21 +597,6 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
connect( ui.historyPane, &QDockWidget::visibilityChanged, this, &MainWindow::updateHistoryMenu );
connect( ui.showHideHistory, &QAction::triggered, this, &MainWindow::toggle_historyPane );


#if !defined( HAVE_X11 )
// Show tray icon early so the user would be happy. It won't be functional
// though until the program inits fully.
// Do not create dummy tray icon in X. Cause QT5 failed to upgrade systemtray context menu.
// And as result menu for some DEs apppear to be empty, for example in MATE DE.

if ( cfg.preferences.enableTrayIcon ) {
trayIcon =
new QSystemTrayIcon( QIcon::fromTheme( "goldendict-tray", QIcon( ":/icons/programicon_old.png" ) ), this );
trayIcon->setToolTip( tr( "Loading..." ) );
trayIcon->show();
}
#endif

connect( navBack, &QAction::triggered, this, &MainWindow::backClicked );
connect( navForward, &QAction::triggered, this, &MainWindow::forwardClicked );

Expand Down Expand Up @@ -852,7 +837,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
#endif

installHotKeys();
updateTrayIcon();
trayIconUpdateOrInit();
} );

if ( cfg.preferences.startWithScanPopupOn ) {
Expand All @@ -862,16 +847,7 @@ MainWindow::MainWindow( Config::Class & cfg_ ):
updateSearchPaneAndBar( cfg.preferences.searchInDock );
ui.searchPane->setVisible( cfg.preferences.searchInDock );

if ( trayIcon ) {
// Upgrade existing dummy tray icon into a full-functional one

trayIcon->setContextMenu( &trayIconMenu );
trayIcon->show();

connect( trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::trayIconActivated );
}

updateTrayIcon();
trayIconUpdateOrInit();

// Update zoomers
adjustCurrentZoomFactor();
Expand Down Expand Up @@ -1391,30 +1367,26 @@ void MainWindow::updateAppearances( QString const & addonStyle,
}
}

void MainWindow::updateTrayIcon()
void MainWindow::trayIconUpdateOrInit()
{
if ( !trayIcon && cfg.preferences.enableTrayIcon ) {
// Need to show it
trayIcon =
new QSystemTrayIcon( QIcon::fromTheme( "goldendict-tray", QIcon( ":/icons/programicon_old.png" ) ), this );
trayIcon->setContextMenu( &trayIconMenu );
trayIcon->show();

connect( trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::trayIconActivated );
}
else if ( trayIcon && !cfg.preferences.enableTrayIcon ) {
// Need to hide it
delete trayIcon;

trayIcon = nullptr;
if ( !cfg.preferences.enableTrayIcon ) {
if ( trayIcon ) {
delete trayIcon;
trayIcon = nullptr;
}
}
if ( trayIcon ) {
else {
if ( !trayIcon ) {
trayIcon = new QSystemTrayIcon( this );
trayIcon->setContextMenu( &trayIconMenu );
trayIcon->setToolTip( QApplication::applicationName() );
connect( trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::trayIconActivated );
trayIcon->show();
}
// Update the icon to reflect the scanning mode
trayIcon->setIcon( enableScanningAction->isChecked() ?
QIcon::fromTheme( "goldendict-scan-tray", QIcon( ":/icons/programicon_scan.png" ) ) :
QIcon::fromTheme( "goldendict-tray", QIcon( ":/icons/programicon_old.png" ) ) );

trayIcon->setToolTip( "GoldenDict-ng" );
}

// The 'Close to tray' action is associated with the tray icon, so we hide
Expand Down Expand Up @@ -2282,7 +2254,7 @@ void MainWindow::editPreferences()

audioPlayerFactory.setPreferences( cfg.preferences );

updateTrayIcon();
trayIconUpdateOrInit();
applyProxySettings();

ui.tabWidget->setHideSingleTab( cfg.preferences.hideSingleTab );
Expand Down
2 changes: 1 addition & 1 deletion src/ui/mainwindow.hh
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private:

/// Creates, destroys or otherwise updates tray icon, according to the
/// current configuration and situation.
void updateTrayIcon();
void trayIconUpdateOrInit();

void wheelEvent( QWheelEvent * );
void closeEvent( QCloseEvent * );
Expand Down

0 comments on commit 05d9a39

Please sign in to comment.