File indexing completed on 2024-04-28 16:52:25

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 
0016 #include <KConfigWatcher>
0017 #include <KSharedConfig>
0018 
0019 #include <KWayland/Client/connection_thread.h>
0020 #include <KWayland/Client/plasmawindowmanagement.h>
0021 #include <KWayland/Client/registry.h>
0022 #include <KWayland/Client/surface.h>
0023 
0024 /**
0025  * Utility class that provides useful functions related to windows and KWin+KWayland.
0026  *
0027  * TODO: Add per-screen support
0028  *
0029  * @author Devin Lin <devin@kde.org>
0030  **/
0031 class WindowUtil : public QObject
0032 {
0033     Q_OBJECT
0034     Q_PROPERTY(bool showDesktop READ isShowingDesktop WRITE requestShowingDesktop NOTIFY showingDesktopChanged)
0035     Q_PROPERTY(bool allWindowsMinimized READ allWindowsMinimized NOTIFY allWindowsMinimizedChanged)
0036     Q_PROPERTY(bool allWindowsMinimizedExcludingShell READ allWindowsMinimizedExcludingShell NOTIFY allWindowsMinimizedExcludingShellChanged)
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     static WindowUtil *instance();
0043 
0044     /**
0045      * Whether the shell is in "desktop showing" mode, where all windows
0046      * are moved aside.
0047      */
0048     bool isShowingDesktop() const;
0049 
0050     /**
0051      * Whether all windows are minimized, including shell windows.
0052      */
0053     bool allWindowsMinimized() const;
0054 
0055     /**
0056      * Whether all windows are minimized, ignoring shell windows.
0057      */
0058     bool allWindowsMinimizedExcludingShell() const;
0059 
0060     /**
0061      * Whether the active window being shown is a shell window.
0062      */
0063     bool activeWindowIsShell() const;
0064 
0065     /**
0066      * Whether the current active window can be closed.
0067      */
0068     bool hasCloseableActiveWindow() const;
0069 
0070     /**
0071      * Get the list of windows associated to a storage id.
0072      */
0073     QList<KWayland::Client::PlasmaWindow *> windowsFromStorageId(const QString &storageId) const;
0074 
0075     /**
0076      * Close the current active window.
0077      */
0078     Q_INVOKABLE void closeActiveWindow();
0079 
0080     /**
0081      * Toggle whether we are in the "desktop showing" mode.
0082      *
0083      * @param showingDesktop Whether "desktop showing" mode should be enabled.
0084      */
0085     Q_INVOKABLE void requestShowingDesktop(bool showingDesktop);
0086 
0087     /**
0088      * Minimize all windows.
0089      */
0090     Q_INVOKABLE void minimizeAll();
0091 
0092     /**
0093      * Unset minimized geometries of all windows for an item's window.
0094      *
0095      * @param parent The parent item, which is of the same window that will have geometries unset.
0096      */
0097     Q_INVOKABLE void unsetAllMinimizedGeometries(QQuickItem *parent);
0098 
0099 Q_SIGNALS:
0100     void windowCreated(KWayland::Client::PlasmaWindow *window);
0101     void showingDesktopChanged(bool showingDesktop);
0102     void allWindowsMinimizedChanged();
0103     void allWindowsMinimizedExcludingShellChanged();
0104     void hasCloseableActiveWindowChanged();
0105     void activeWindowChanged();
0106     void activeWindowIsShellChanged();
0107     void windowChanged(QString storageId); // emitted on window open or close
0108 
0109 private Q_SLOTS:
0110     void updateActiveWindowIsShell();
0111     void forgetActiveWindow();
0112     void updateShowingDesktop(bool showing);
0113     void windowCreatedSlot(KWayland::Client::PlasmaWindow *window);
0114 
0115 private:
0116     void initWayland();
0117     void updateActiveWindow();
0118 
0119     KWayland::Client::PlasmaWindowManagement *m_windowManagement = nullptr;
0120     QPointer<KWayland::Client::PlasmaWindow> m_activeWindow;
0121     QTimer *m_activeWindowTimer;
0122 
0123     bool m_showingDesktop = false;
0124     bool m_allWindowsMinimized = true;
0125     bool m_allWindowsMinimizedExcludingShell = true;
0126     bool m_activeWindowIsShell = false;
0127 
0128     QHash<QString, QList<KWayland::Client::PlasmaWindow *>> m_windows; // <storageId, window>
0129 };