Revamp system tray icon menu handling

Update system tray icon menu without re-create it.

PR #20597.
Closes #20516.
This commit is contained in:
Vladimir Golovnev 2024-03-26 15:24:43 +03:00 committed by GitHub
parent 489bacd766
commit 5e8b9df859
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 14 additions and 82 deletions

View file

@ -1,6 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 Vladimir Golovnev <glassez@yandex.ru> * Copyright (C) 2015-2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez * Copyright (C) 2006 Christophe Dumez
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -832,7 +832,7 @@ int Application::exec()
m_desktopIntegration = new DesktopIntegration; m_desktopIntegration = new DesktopIntegration;
m_desktopIntegration->setToolTip(tr("Loading torrents...")); m_desktopIntegration->setToolTip(tr("Loading torrents..."));
#ifndef Q_OS_MACOS #ifndef Q_OS_MACOS
auto *desktopIntegrationMenu = new QMenu; auto *desktopIntegrationMenu = m_desktopIntegration->menu();
auto *actionExit = new QAction(tr("E&xit"), desktopIntegrationMenu); auto *actionExit = new QAction(tr("E&xit"), desktopIntegrationMenu);
actionExit->setIcon(UIThemeManager::instance()->getIcon(u"application-exit"_s)); actionExit->setIcon(UIThemeManager::instance()->getIcon(u"application-exit"_s));
actionExit->setMenuRole(QAction::QuitRole); actionExit->setMenuRole(QAction::QuitRole);
@ -843,8 +843,6 @@ int Application::exec()
}); });
desktopIntegrationMenu->addAction(actionExit); desktopIntegrationMenu->addAction(actionExit);
m_desktopIntegration->setMenu(desktopIntegrationMenu);
const bool isHidden = m_desktopIntegration->isActive() && (startUpWindowState() == WindowState::Hidden); const bool isHidden = m_desktopIntegration->isActive() && (startUpWindowState() == WindowState::Hidden);
#else #else
const bool isHidden = false; const bool isHidden = false;

View file

@ -1,6 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru> * Copyright (C) 2022-2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org> * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -73,6 +73,7 @@ using namespace std::chrono_literals;
DesktopIntegration::DesktopIntegration(QObject *parent) DesktopIntegration::DesktopIntegration(QObject *parent)
: QObject(parent) : QObject(parent)
, m_storeNotificationEnabled {NOTIFICATIONS_SETTINGS_KEY(u"Enabled"_s), true} , m_storeNotificationEnabled {NOTIFICATIONS_SETTINGS_KEY(u"Enabled"_s), true}
, m_menu {new QMenu}
#ifdef QBT_USES_DBUS #ifdef QBT_USES_DBUS
, m_storeNotificationTimeOut {NOTIFICATIONS_SETTINGS_KEY(u"Timeout"_s), -1} , m_storeNotificationTimeOut {NOTIFICATIONS_SETTINGS_KEY(u"Timeout"_s), -1}
#endif #endif
@ -80,6 +81,7 @@ DesktopIntegration::DesktopIntegration(QObject *parent)
#ifdef Q_OS_MACOS #ifdef Q_OS_MACOS
desktopIntegrationInstance = this; desktopIntegrationInstance = this;
MacUtils::overrideDockClickHandler(handleDockClicked); MacUtils::overrideDockClickHandler(handleDockClicked);
m_menu->setAsDockMenu();
#else #else
if (Preferences::instance()->systemTrayEnabled()) if (Preferences::instance()->systemTrayEnabled())
createTrayIcon(); createTrayIcon();
@ -132,46 +134,6 @@ QMenu *DesktopIntegration::menu() const
return m_menu; return m_menu;
} }
void DesktopIntegration::setMenu(QMenu *menu)
{
if (menu == m_menu)
return;
#if defined Q_OS_MACOS
if (m_menu)
delete m_menu;
m_menu = menu;
if (m_menu)
m_menu->setAsDockMenu();
#elif defined Q_OS_UNIX
const bool systemTrayEnabled = m_systrayIcon;
if (m_menu)
{
if (m_systrayIcon)
{
delete m_systrayIcon;
m_systrayIcon = nullptr;
}
delete m_menu;
}
m_menu = menu;
if (systemTrayEnabled && !m_systrayIcon)
createTrayIcon();
#else
if (m_menu)
delete m_menu;
m_menu = menu;
if (m_systrayIcon)
m_systrayIcon->setContextMenu(m_menu);
#endif
}
bool DesktopIntegration::isNotificationsEnabled() const bool DesktopIntegration::isNotificationsEnabled() const
{ {
return m_storeNotificationEnabled; return m_storeNotificationEnabled;

View file

@ -1,6 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru> * Copyright (C) 2022-2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org> * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -56,7 +56,6 @@ public:
void setToolTip(const QString &toolTip); void setToolTip(const QString &toolTip);
QMenu *menu() const; QMenu *menu() const;
void setMenu(QMenu *menu);
bool isNotificationsEnabled() const; bool isNotificationsEnabled() const;
void setNotificationsEnabled(bool value); void setNotificationsEnabled(bool value);

View file

@ -1,6 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022-2023 Vladimir Golovnev <glassez@yandex.ru> * Copyright (C) 2022-2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org> * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -293,9 +293,6 @@ MainWindow::MainWindow(IGUIApplication *app, const WindowState initialState, con
connect(m_ui->actionIncreaseQueuePos, &QAction::triggered, m_transferListWidget, &TransferListWidget::increaseQueuePosSelectedTorrents); connect(m_ui->actionIncreaseQueuePos, &QAction::triggered, m_transferListWidget, &TransferListWidget::increaseQueuePosSelectedTorrents);
connect(m_ui->actionDecreaseQueuePos, &QAction::triggered, m_transferListWidget, &TransferListWidget::decreaseQueuePosSelectedTorrents); connect(m_ui->actionDecreaseQueuePos, &QAction::triggered, m_transferListWidget, &TransferListWidget::decreaseQueuePosSelectedTorrents);
connect(m_ui->actionBottomQueuePos, &QAction::triggered, m_transferListWidget, &TransferListWidget::bottomQueuePosSelectedTorrents); connect(m_ui->actionBottomQueuePos, &QAction::triggered, m_transferListWidget, &TransferListWidget::bottomQueuePosSelectedTorrents);
#ifndef Q_OS_MACOS
connect(m_ui->actionToggleVisibility, &QAction::triggered, this, &MainWindow::toggleVisibility);
#endif
connect(m_ui->actionMinimize, &QAction::triggered, this, &MainWindow::minimizeWindow); connect(m_ui->actionMinimize, &QAction::triggered, this, &MainWindow::minimizeWindow);
connect(m_ui->actionUseAlternativeSpeedLimits, &QAction::triggered, this, &MainWindow::toggleAlternativeSpeeds); connect(m_ui->actionUseAlternativeSpeedLimits, &QAction::triggered, this, &MainWindow::toggleAlternativeSpeeds);
@ -387,7 +384,7 @@ MainWindow::MainWindow(IGUIApplication *app, const WindowState initialState, con
// Load Window state and sizes // Load Window state and sizes
loadSettings(); loadSettings();
app->desktopIntegration()->setMenu(createDesktopIntegrationMenu()); populateDesktopIntegrationMenu();
#ifndef Q_OS_MACOS #ifndef Q_OS_MACOS
m_ui->actionLock->setVisible(app->desktopIntegration()->isActive()); m_ui->actionLock->setVisible(app->desktopIntegration()->isActive());
connect(app->desktopIntegration(), &DesktopIntegration::stateChanged, this, [this, app]() connect(app->desktopIntegration(), &DesktopIntegration::stateChanged, this, [this, app]()
@ -1326,12 +1323,6 @@ void MainWindow::dragEnterEvent(QDragEnterEvent *event)
event->acceptProposedAction(); event->acceptProposedAction();
} }
/*****************************************************
* *
* Torrent *
* *
*****************************************************/
// Display a dialog to allow user to add // Display a dialog to allow user to add
// torrents to download list // torrents to download list
void MainWindow::on_actionOpen_triggered() void MainWindow::on_actionOpen_triggered()
@ -1527,33 +1518,23 @@ void MainWindow::reloadTorrentStats(const QVector<BitTorrent::Torrent *> &torren
} }
} }
/*****************************************************
* *
* Utils *
* *
*****************************************************/
void MainWindow::downloadFromURLList(const QStringList &urlList) void MainWindow::downloadFromURLList(const QStringList &urlList)
{ {
for (const QString &url : urlList) for (const QString &url : urlList)
app()->addTorrentManager()->addTorrent(url); app()->addTorrentManager()->addTorrent(url);
} }
/***************************************************** void MainWindow::populateDesktopIntegrationMenu()
* *
* Options *
* *
*****************************************************/
QMenu *MainWindow::createDesktopIntegrationMenu()
{ {
auto *menu = new QMenu; auto *menu = app()->desktopIntegration()->menu();
menu->clear();
#ifndef Q_OS_MACOS #ifndef Q_OS_MACOS
connect(menu, &QMenu::aboutToShow, this, [this]() connect(menu, &QMenu::aboutToShow, this, [this]()
{ {
m_ui->actionToggleVisibility->setText(isVisible() ? tr("Hide") : tr("Show")); m_ui->actionToggleVisibility->setText(isVisible() ? tr("Hide") : tr("Show"));
}); });
connect(m_ui->actionToggleVisibility, &QAction::triggered, this, &MainWindow::toggleVisibility);
menu->addAction(m_ui->actionToggleVisibility); menu->addAction(m_ui->actionToggleVisibility);
menu->addSeparator(); menu->addSeparator();
@ -1577,8 +1558,6 @@ QMenu *MainWindow::createDesktopIntegrationMenu()
if (m_uiLocked) if (m_uiLocked)
menu->setEnabled(false); menu->setEnabled(false);
return menu;
} }
void MainWindow::updateAltSpeedsBtn(const bool alternative) void MainWindow::updateAltSpeedsBtn(const bool alternative)
@ -1696,12 +1675,6 @@ void MainWindow::on_actionSearchWidget_triggered()
displaySearchTab(m_ui->actionSearchWidget->isChecked()); displaySearchTab(m_ui->actionSearchWidget->isChecked());
} }
/*****************************************************
* *
* HTTP Downloader *
* *
*****************************************************/
// Display an input dialog to prompt user for // Display an input dialog to prompt user for
// an url // an url
void MainWindow::on_actionDownloadFromURL_triggered() void MainWindow::on_actionDownloadFromURL_triggered()

View file

@ -1,6 +1,6 @@
/* /*
* Bittorrent Client using Qt and libtorrent. * Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2022 Vladimir Golovnev <glassez@yandex.ru> * Copyright (C) 2022-2024 Vladimir Golovnev <glassez@yandex.ru>
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org> * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
* *
* This program is free software; you can redistribute it and/or * This program is free software; you can redistribute it and/or
@ -186,7 +186,7 @@ private slots:
#endif #endif
private: private:
QMenu *createDesktopIntegrationMenu(); void populateDesktopIntegrationMenu();
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
void installPython(); void installPython();
#endif #endif