From c1604a9c4fbc63e5bef86f7d40e05deaa0b80b43 Mon Sep 17 00:00:00 2001 From: Tobias Leupold Date: Tue, 22 Aug 2023 20:58:04 +0000 Subject: [PATCH] Trayicon tweaks This fixes two minor inconveniences: - When closing the chat window and re-showing it from the systray icon, the geometry was not properly restored. The window was always shown in the middle of the screen. Now, one gets the window back with it's actual last position and size. - It is now possible to not only show the window from the systray icon, but also to close it. This is the way other chat programs do it (Kopete back in the day, Konversation, Quassel IRC etc.) --- src/controller.cpp | 20 ++++++++++++++++---- src/controller.h | 2 +- src/trayicon.cpp | 4 ++-- src/trayicon.h | 4 ++-- src/trayicon_sni.cpp | 2 +- src/trayicon_sni.h | 4 ++-- src/windowcontroller.cpp | 1 + 7 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/controller.cpp b/src/controller.cpp index c8b3976b0..56a3f18b6 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -124,9 +124,21 @@ Controller &Controller::instance() return _instance; } -void Controller::showWindow() +void Controller::toggleWindow() { - WindowController::instance().showAndRaiseWindow(QString()); + auto &instance = WindowController::instance(); + auto window = instance.window(); + if (window->isVisible()) { + if (window->windowStates() & Qt::WindowMinimized) { + window->showNormal(); + window->requestActivate(); + } else { + window->close(); + } + } else { + instance.showAndRaiseWindow({}); + instance.window()->requestActivate(); + } } void Controller::logout(Connection *conn, bool serverSideLogout) @@ -399,10 +411,10 @@ void Controller::setQuitOnLastWindowClosed() if (NeoChatConfig::self()->systemTray()) { m_trayIcon = new TrayIcon(this); m_trayIcon->show(); - connect(m_trayIcon, &TrayIcon::showWindow, this, &Controller::showWindow); + connect(m_trayIcon, &TrayIcon::toggleWindow, this, &Controller::toggleWindow); } else { if (m_trayIcon) { - disconnect(m_trayIcon, &TrayIcon::showWindow, this, &Controller::showWindow); + disconnect(m_trayIcon, &TrayIcon::toggleWindow, this, &Controller::toggleWindow); delete m_trayIcon; m_trayIcon = nullptr; } diff --git a/src/controller.h b/src/controller.h index b5abb7c75..49c2b3d8c 100644 --- a/src/controller.h +++ b/src/controller.h @@ -234,7 +234,7 @@ private: private Q_SLOTS: void invokeLogin(); - void showWindow(); + void toggleWindow(); void setQuitOnLastWindowClosed(); Q_SIGNALS: diff --git a/src/trayicon.cpp b/src/trayicon.cpp index 953704e4c..454e14051 100644 --- a/src/trayicon.cpp +++ b/src/trayicon.cpp @@ -16,10 +16,10 @@ TrayIcon::TrayIcon(QObject *parent) QMenu *menu = new QMenu(); auto viewAction_ = new QAction(i18n("Show"), parent); - connect(viewAction_, &QAction::triggered, this, &TrayIcon::showWindow); + connect(viewAction_, &QAction::triggered, this, &TrayIcon::toggleWindow); connect(this, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) { if (reason == QSystemTrayIcon::Trigger) { - Q_EMIT showWindow(); + Q_EMIT toggleWindow(); } }); diff --git a/src/trayicon.h b/src/trayicon.h index 1f1844989..4cb073447 100644 --- a/src/trayicon.h +++ b/src/trayicon.h @@ -24,7 +24,7 @@ public: Q_SIGNALS: /** - * @brief Triggered when the system tray icon is clicked to request NeoChat be shown. + * @brief Triggered when the system tray icon is clicked to request NeoChat be shown or hidden. */ - void showWindow(); + void toggleWindow(); }; diff --git a/src/trayicon_sni.cpp b/src/trayicon_sni.cpp index 6d9f852e4..8ca0c7a4f 100644 --- a/src/trayicon_sni.cpp +++ b/src/trayicon_sni.cpp @@ -13,7 +13,7 @@ TrayIcon::TrayIcon(QObject *parent) setIconByName(QStringLiteral("org.kde.neochat.tray")); connect(this, &KStatusNotifierItem::activateRequested, this, [this] { KWindowSystem::setCurrentXdgActivationToken(providedToken()); - Q_EMIT showWindow(); + Q_EMIT toggleWindow(); }); connect(&WindowController::instance(), &WindowController::windowChanged, this, [this] { diff --git a/src/trayicon_sni.h b/src/trayicon_sni.h index 85effb4f7..d078dcb0f 100644 --- a/src/trayicon_sni.h +++ b/src/trayicon_sni.h @@ -30,7 +30,7 @@ public: Q_SIGNALS: /** - * @brief Triggered when the system tray icon is clicked to request NeoChat be shown. + * @brief Triggered when the system tray icon is clicked to request NeoChat be shown or hidden. */ - void showWindow(); + void toggleWindow(); }; diff --git a/src/windowcontroller.cpp b/src/windowcontroller.cpp index 676de6093..6059b82a9 100644 --- a/src/windowcontroller.cpp +++ b/src/windowcontroller.cpp @@ -53,6 +53,7 @@ void WindowController::showAndRaiseWindow(const QString &startupId) { if (!m_window->isVisible()) { m_window->show(); + restoreGeometry(); } #ifdef HAVE_WINDOWSYSTEM