File indexing completed on 2024-05-12 05:36:11

0001 /*
0002  *  SPDX-FileCopyrightText: 2014 Antonis Tsiapaliokas <antonis.tsiapaliokas@kde.org>
0003  *  SPDX-FileCopyrightText: 2022 Devin Lin <devin@kde.org>
0004  *
0005  *  SPDX-License-Identifier: GPL-2.0-or-later
0006  */
0007 
0008 #pragma once
0009 
0010 #include <QObject>
0011 #include <QPointer>
0012 #include <QQuickItem>
0013 #include <QQuickWindow>
0014 #include <QTimer>
0015 #include <qqmlregistration.h>
0016 
0017 #include <KConfigWatcher>
0018 #include <KSharedConfig>
0019 
0020 #include <KWayland/Client/connection_thread.h>
0021 #include <KWayland/Client/plasmawindowmanagement.h>
0022 #include <KWayland/Client/registry.h>
0023 #include <KWayland/Client/surface.h>
0024 
0025 /**
0026  * Utility class that provides useful functions related to windows and KWin+KWayland.
0027  *
0028  * @author Devin Lin <devin@kde.org>
0029  **/
0030 class WindowUtil : public QObject
0031 {
0032     Q_OBJECT
0033     QML_ELEMENT
0034     QML_SINGLETON
0035 
0036     Q_PROPERTY(bool isShowingDesktop READ isShowingDesktop WRITE requestShowingDesktop NOTIFY showingDesktopChanged)
0037     Q_PROPERTY(bool hasCloseableActiveWindow READ hasCloseableActiveWindow NOTIFY hasCloseableActiveWindowChanged)
0038     Q_PROPERTY(bool activeWindowIsShell READ activeWindowIsShell NOTIFY activeWindowIsShellChanged)
0039 
0040 public:
0041     WindowUtil(QObject *parent = nullptr);
0042 
0043     /**
0044      * Whether the shell is in "desktop showing" mode, where all windows
0045      * are moved aside.
0046      */
0047     bool isShowingDesktop() const;
0048 
0049     /**
0050      * Whether the active window being shown is a shell window.
0051      */
0052     bool activeWindowIsShell() const;
0053 
0054     /**
0055      * Whether the current active window can be closed.
0056      */
0057     bool hasCloseableActiveWindow() const;
0058 
0059     /**
0060      * Get the list of windows associated to a storage id.
0061      */
0062     QList<KWayland::Client::PlasmaWindow *> windowsFromStorageId(const QString &storageId) const;
0063 
0064     /**
0065      * Activates the first window by its associated storage id.
0066      *
0067      * @param storageId the window's storage id
0068      * @returns whether a window was activated
0069      */
0070     Q_INVOKABLE bool activateWindowByStorageId(const QString &storageId);
0071 
0072     /**
0073      * Close the current active window.
0074      */
0075     Q_INVOKABLE void closeActiveWindow();
0076 
0077     /**
0078      * Toggle whether we are in the "desktop showing" mode.
0079      *
0080      * @param showingDesktop Whether "desktop showing" mode should be enabled.
0081      */
0082     Q_INVOKABLE void requestShowingDesktop(bool showingDesktop);
0083 
0084     /**
0085      * Minimize all windows.
0086      */
0087     Q_INVOKABLE void minimizeAll();
0088 
0089     /**
0090      * Unset minimized geometries of all windows for an item's window.
0091      *
0092      * @param parent The parent item, which is of the same window that will have geometries unset.
0093      */
0094     Q_INVOKABLE void unsetAllMinimizedGeometries(QQuickItem *parent);
0095 
0096 Q_SIGNALS:
0097     // Emitted when a window has been opened
0098     void windowCreated(KWayland::Client::PlasmaWindow *window);
0099     void showingDesktopChanged(bool showingDesktop);
0100     void hasCloseableActiveWindowChanged();
0101     void activeWindowChanged();
0102     void activeWindowIsShellChanged();
0103 
0104     // Emitted on window open or close
0105     void windowChanged(QString storageId);
0106 
0107     // Emitted when an application is launched
0108     void appActivationStarted(const QString &appId, const QString &iconName);
0109 
0110     // Emitted the application has finished launching
0111     void appActivationFinished();
0112 
0113 private Q_SLOTS:
0114     void updateActiveWindowIsShell();
0115     void forgetActiveWindow();
0116     void updateShowingDesktop(bool showing);
0117     void windowCreatedSlot(KWayland::Client::PlasmaWindow *window);
0118 
0119 private:
0120     void initWayland();
0121     void updateActiveWindow();
0122 
0123     KWayland::Client::PlasmaWindowManagement *m_windowManagement = nullptr;
0124     QPointer<KWayland::Client::PlasmaWindow> m_activeWindow;
0125     QTimer *m_activeWindowTimer;
0126 
0127     bool m_showingDesktop = false;
0128     bool m_activeWindowIsShell = false;
0129 
0130     QHash<QString, QList<KWayland::Client::PlasmaWindow *>> m_windows; // <storageId, window>
0131 };