File indexing completed on 2024-09-29 10:12:48

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 <QStandardPaths>
0017 
0018 WindowController &WindowController::instance()
0019 {
0020     static WindowController instance;
0021     return instance;
0022 }
0023 
0024 void WindowController::setWindow(QWindow *window)
0025 {
0026     m_window = window;
0027 
0028     Q_EMIT windowChanged();
0029 }
0030 
0031 QWindow *WindowController::window() const
0032 {
0033     return m_window;
0034 }
0035 
0036 void WindowController::restoreGeometry()
0037 {
0038     KConfig dataResource("data", KConfig::SimpleConfig, QStandardPaths::AppDataLocation);
0039     KConfigGroup windowGroup(&dataResource, "Window");
0040     KWindowConfig::restoreWindowSize(m_window, windowGroup);
0041     KWindowConfig::restoreWindowPosition(m_window, windowGroup);
0042 }
0043 
0044 void WindowController::saveGeometry()
0045 {
0046     KConfig dataResource("data", KConfig::SimpleConfig, QStandardPaths::AppDataLocation);
0047     KConfigGroup windowGroup(&dataResource, "Window");
0048     KWindowConfig::saveWindowPosition(m_window, windowGroup);
0049     KWindowConfig::saveWindowSize(m_window, windowGroup);
0050 }
0051 
0052 void WindowController::showAndRaiseWindow(const QString &startupId)
0053 {
0054     if (!m_window->isVisible()) {
0055         m_window->show();
0056     }
0057 
0058 #ifdef HAVE_WINDOWSYSTEM
0059     if (!startupId.isEmpty()) {
0060         if (KWindowSystem::isPlatformX11()) {
0061 #if HAVE_X11
0062             KStartupInfo::setNewStartupId(m_window, startupId.toUtf8());
0063 #endif
0064         } else if (KWindowSystem::isPlatformWayland()) {
0065             KWindowSystem::setCurrentXdgActivationToken(startupId);
0066         }
0067     }
0068 
0069     KWindowSystem::activateWindow(m_window);
0070 #endif
0071 }
0072 
0073 #include "moc_windowcontroller.cpp"