File indexing completed on 2024-10-06 07:36:10

0001 // SPDX-FileCopyrightText: 2022 Nicolas Fella <nicolas.fella@gmx.de>
0002 // SPDX-License-Identifier: GPL-2.0-or-later
0003 
0004 #include "windowcontroller.h"
0005 
0006 #include <KConfig>
0007 #include <KWindowConfig>
0008 
0009 #ifdef HAVE_WINDOWSYSTEM
0010 #if HAVE_X11
0011 #include <KStartupInfo>
0012 #endif
0013 #include <KWindowSystem>
0014 #endif
0015 
0016 #include <KSharedConfig>
0017 #include <QStandardPaths>
0018 
0019 WindowController &WindowController::instance()
0020 {
0021     static WindowController instance;
0022     return instance;
0023 }
0024 
0025 void WindowController::setWindow(QWindow *window)
0026 {
0027     m_window = window;
0028 
0029     Q_EMIT windowChanged();
0030 }
0031 
0032 QWindow *WindowController::window() const
0033 {
0034     return m_window;
0035 }
0036 
0037 void WindowController::restoreGeometry()
0038 {
0039     const auto stateConfig = KSharedConfig::openStateConfig();
0040     const KConfigGroup windowGroup = stateConfig->group(QStringLiteral("Window"));
0041 
0042     KWindowConfig::restoreWindowSize(m_window, windowGroup);
0043     KWindowConfig::restoreWindowPosition(m_window, windowGroup);
0044 }
0045 
0046 void WindowController::saveGeometry()
0047 {
0048     const auto stateConfig = KSharedConfig::openStateConfig();
0049     KConfigGroup windowGroup = stateConfig->group(QStringLiteral("Window"));
0050 
0051     KWindowConfig::saveWindowPosition(m_window, windowGroup);
0052     KWindowConfig::saveWindowSize(m_window, windowGroup);
0053 
0054     stateConfig->sync();
0055 }
0056 
0057 void WindowController::showAndRaiseWindow(const QString &startupId)
0058 {
0059     if (m_window == nullptr) {
0060         return;
0061     }
0062     if (!m_window->isVisible()) {
0063         m_window->show();
0064         restoreGeometry();
0065     }
0066 
0067 #ifdef HAVE_WINDOWSYSTEM
0068     if (!startupId.isEmpty()) {
0069         if (KWindowSystem::isPlatformX11()) {
0070 #if HAVE_X11
0071             KStartupInfo::setNewStartupId(m_window, startupId.toUtf8());
0072 #endif
0073         } else if (KWindowSystem::isPlatformWayland()) {
0074             KWindowSystem::setCurrentXdgActivationToken(startupId);
0075         }
0076     }
0077 
0078     KWindowSystem::activateWindow(m_window);
0079 #endif
0080 }
0081 
0082 bool WindowController::hasWindowSystem() const
0083 {
0084 #ifdef HAVE_WINDOWSYSTEM
0085     return true;
0086 #else
0087     return false;
0088 #endif
0089 }
0090 
0091 void WindowController::setBlur(QQuickItem *item, bool blur)
0092 {
0093 #ifdef HAVE_WINDOWSYSTEM
0094     auto setWindows = [item, blur]() {
0095         auto reg = QRect(QPoint(0, 0), item->window()->size());
0096         KWindowEffects::enableBackgroundContrast(item->window(), blur, 1, 1, 1, reg);
0097         KWindowEffects::enableBlurBehind(item->window(), blur, reg);
0098     };
0099 
0100     disconnect(item->window(), &QQuickWindow::heightChanged, this, nullptr);
0101     disconnect(item->window(), &QQuickWindow::widthChanged, this, nullptr);
0102     connect(item->window(), &QQuickWindow::heightChanged, this, setWindows);
0103     connect(item->window(), &QQuickWindow::widthChanged, this, setWindows);
0104     setWindows();
0105 #endif
0106 }
0107 
0108 void WindowController::toggleWindow()
0109 {
0110     if (m_window == nullptr) {
0111         return;
0112     }
0113     if (window()->isVisible()) {
0114         if (window()->windowStates() & Qt::WindowMinimized) {
0115             window()->showNormal();
0116             window()->requestActivate();
0117         } else {
0118             window()->close();
0119         }
0120     } else {
0121         showAndRaiseWindow({});
0122         window()->requestActivate();
0123     }
0124 }
0125 
0126 #include "moc_windowcontroller.cpp"