Move Controller::toggleWindow to WindowController

This commit is contained in:
Tobias Fella
2023-12-23 14:50:36 +00:00
parent d02eee6daa
commit 4c3d7ab011
8 changed files with 28 additions and 44 deletions

View File

@@ -136,23 +136,6 @@ Controller &Controller::instance()
return _instance;
}
void Controller::toggleWindow()
{
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::addConnection(NeoChatConnection *c)
{
Q_ASSERT_X(c, __FUNCTION__, "Attempt to add a null connection");
@@ -314,10 +297,8 @@ void Controller::setQuitOnLastWindowClosed()
if (NeoChatConfig::self()->systemTray()) {
m_trayIcon = new TrayIcon(this);
m_trayIcon->show();
connect(m_trayIcon, &TrayIcon::toggleWindow, this, &Controller::toggleWindow);
} else {
if (m_trayIcon) {
disconnect(m_trayIcon, &TrayIcon::toggleWindow, this, &Controller::toggleWindow);
delete m_trayIcon;
m_trayIcon = nullptr;
}

View File

@@ -9,23 +9,15 @@
#include "neochatconnection.h"
#include <Quotient/accountregistry.h>
#include <Quotient/jobs/basejob.h>
#include <Quotient/settings.h>
#ifdef HAVE_KUNIFIEDPUSH
#include <kunifiedpush/connector.h>
#endif
class NeoChatRoom;
class TrayIcon;
class QQuickTextDocument;
namespace Quotient
{
class Room;
class User;
}
namespace QKeychain
{
class ReadPasswordJob;
@@ -135,7 +127,6 @@ private:
private Q_SLOTS:
void invokeLogin();
void toggleWindow();
void setQuitOnLastWindowClosed();
Q_SIGNALS:

View File

@@ -9,6 +9,8 @@
#include <KLocalizedString>
#include "windowcontroller.h"
TrayIcon::TrayIcon(QObject *parent)
: QSystemTrayIcon(parent)
{
@@ -16,10 +18,12 @@ TrayIcon::TrayIcon(QObject *parent)
QMenu *menu = new QMenu();
auto viewAction_ = new QAction(i18n("Show"), parent);
connect(viewAction_, &QAction::triggered, this, &TrayIcon::toggleWindow);
connect(this, &QSystemTrayIcon::activated, this, [this](QSystemTrayIcon::ActivationReason reason) {
connect(viewAction_, &QAction::triggered, this, [] {
WindowController::instance().toggleWindow();
});
connect(this, &QSystemTrayIcon::activated, this, [](QSystemTrayIcon::ActivationReason reason) {
if (reason == QSystemTrayIcon::Trigger) {
Q_EMIT toggleWindow();
WindowController::instance().toggleWindow();
}
});

View File

@@ -21,10 +21,4 @@ class TrayIcon : public QSystemTrayIcon
Q_OBJECT
public:
TrayIcon(QObject *parent = nullptr);
Q_SIGNALS:
/**
* @brief Triggered when the system tray icon is clicked to request NeoChat be shown or hidden.
*/
void toggleWindow();
};

View File

@@ -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 toggleWindow();
WindowController::instance().toggleWindow();
});
connect(&WindowController::instance(), &WindowController::windowChanged, this, [this] {

View File

@@ -27,10 +27,4 @@ public:
* @brief Hide the tray icon.
*/
void hide();
Q_SIGNALS:
/**
* @brief Triggered when the system tray icon is clicked to request NeoChat be shown or hidden.
*/
void toggleWindow();
};

View File

@@ -102,4 +102,19 @@ void WindowController::setBlur(QQuickItem *item, bool blur)
#endif
}
void WindowController::toggleWindow()
{
if (window()->isVisible()) {
if (window()->windowStates() & Qt::WindowMinimized) {
window()->showNormal();
window()->requestActivate();
} else {
window()->close();
}
} else {
showAndRaiseWindow({});
window()->requestActivate();
}
}
#include "moc_windowcontroller.cpp"

View File

@@ -66,6 +66,11 @@ public:
*/
Q_INVOKABLE void setBlur(QQuickItem *item, bool blur);
/**
* Toggles the window between hidden and visible.
*/
void toggleWindow();
Q_SIGNALS:
/**
* @brief Triggered if the managed window is changed.