File indexing completed on 2024-05-19 16:34:53

0001 /*
0002     KWin - the KDE window manager
0003     This file is part of the KDE project.
0004 
0005     SPDX-FileCopyrightText: 2010 Rohan Prabhu <rohan@rohanprabhu.com>
0006     SPDX-FileCopyrightText: 2012 Martin Gräßlin <mgraesslin@kde.org>
0007 
0008     SPDX-License-Identifier: GPL-2.0-or-later
0009 */
0010 
0011 #pragma once
0012 
0013 #include <QObject>
0014 #include <QQmlListProperty>
0015 #include <QRect>
0016 #include <QSize>
0017 #include <QStringList>
0018 #include <kwinglobals.h>
0019 
0020 namespace KWin
0021 {
0022 // forward declarations
0023 class TileManager;
0024 class Window;
0025 class Output;
0026 class VirtualDesktop;
0027 class X11Window;
0028 
0029 class WorkspaceWrapper : public QObject
0030 {
0031     Q_OBJECT
0032     /**
0033      * @deprecated use the currentVirtualDesktop property instead
0034      */
0035     Q_PROPERTY(int currentDesktop READ currentDesktop WRITE setCurrentDesktop NOTIFY currentDesktopChanged)
0036     Q_PROPERTY(KWin::VirtualDesktop *currentVirtualDesktop READ currentVirtualDesktop WRITE setCurrentVirtualDesktop NOTIFY currentVirtualDesktopChanged)
0037     Q_PROPERTY(KWin::Window *activeClient READ activeClient WRITE setActiveClient NOTIFY clientActivated)
0038     // TODO: write and notify?
0039     Q_PROPERTY(QSize desktopGridSize READ desktopGridSize NOTIFY desktopLayoutChanged)
0040     Q_PROPERTY(int desktopGridWidth READ desktopGridWidth NOTIFY desktopLayoutChanged)
0041     Q_PROPERTY(int desktopGridHeight READ desktopGridHeight NOTIFY desktopLayoutChanged)
0042     Q_PROPERTY(int workspaceWidth READ workspaceWidth)
0043     Q_PROPERTY(int workspaceHeight READ workspaceHeight)
0044     Q_PROPERTY(QSize workspaceSize READ workspaceSize)
0045     /**
0046      * The number of desktops currently used. Minimum number of desktops is 1, maximum 20.
0047      */
0048     Q_PROPERTY(int desktops READ numberOfDesktops WRITE setNumberOfDesktops NOTIFY numberDesktopsChanged)
0049     /**
0050      * The same of the display, that is all screens.
0051      * @deprecated since 5.0 use virtualScreenSize
0052      */
0053     Q_PROPERTY(QSize displaySize READ displaySize)
0054     /**
0055      * The width of the display, that is width of all combined screens.
0056      * @deprecated since 5.0 use virtualScreenSize
0057      */
0058     Q_PROPERTY(int displayWidth READ displayWidth)
0059     /**
0060      * The height of the display, that is height of all combined screens.
0061      * @deprecated since 5.0 use virtualScreenSize
0062      */
0063     Q_PROPERTY(int displayHeight READ displayHeight)
0064     Q_PROPERTY(int activeScreen READ activeScreen)
0065     Q_PROPERTY(int numScreens READ numScreens NOTIFY numberScreensChanged)
0066     Q_PROPERTY(QString currentActivity READ currentActivity WRITE setCurrentActivity NOTIFY currentActivityChanged)
0067     Q_PROPERTY(QStringList activities READ activityList NOTIFY activitiesChanged)
0068     /**
0069      * The bounding size of all screens combined. Overlapping areas
0070      * are not counted multiple times.
0071      * @see virtualScreenGeometry
0072      */
0073     Q_PROPERTY(QSize virtualScreenSize READ virtualScreenSize NOTIFY virtualScreenSizeChanged)
0074     /**
0075      * The bounding geometry of all outputs combined. Always starts at (0,0) and has
0076      * virtualScreenSize as it's size.
0077      * @see virtualScreenSize
0078      */
0079     Q_PROPERTY(QRect virtualScreenGeometry READ virtualScreenGeometry NOTIFY virtualScreenGeometryChanged)
0080     /**
0081      * The current position of the cursor.
0082      */
0083     Q_PROPERTY(QPoint cursorPos READ cursorPos NOTIFY cursorPosChanged)
0084 
0085 private:
0086     Q_DISABLE_COPY(WorkspaceWrapper)
0087 
0088 Q_SIGNALS:
0089     void desktopPresenceChanged(KWin::Window *client, int desktop);
0090     void currentDesktopChanged(int desktop, KWin::Window *client);
0091     void clientAdded(KWin::Window *client);
0092     void clientRemoved(KWin::Window *client);
0093     void clientManaging(KWin::X11Window *client);
0094     void clientMinimized(KWin::Window *client);
0095     void clientUnminimized(KWin::Window *client);
0096     void clientRestored(KWin::X11Window *client);
0097     void clientMaximizeSet(KWin::Window *client, bool h, bool v);
0098     void killWindowCalled(KWin::X11Window *client);
0099     void clientActivated(KWin::Window *client);
0100     void clientFullScreenSet(KWin::X11Window *client, bool fullScreen, bool user);
0101     void clientSetKeepAbove(KWin::X11Window *client, bool keepAbove);
0102     /**
0103      * Signal emitted whenever the number of desktops changed.
0104      * To get the current number of desktops use the property desktops.
0105      * @param oldNumberOfDesktops The previous number of desktops.
0106      */
0107     void numberDesktopsChanged(uint oldNumberOfDesktops);
0108     /**
0109      * Signal emitted whenever the layout of virtual desktops changed.
0110      * That is desktopGrid(Size/Width/Height) will have new values.
0111      * @since 4.11
0112      */
0113     void desktopLayoutChanged();
0114     /**
0115      * The demands attention state for Client @p c changed to @p set.
0116      * @param c The Client for which demands attention changed
0117      * @param set New value of demands attention
0118      */
0119     void clientDemandsAttentionChanged(KWin::Window *client, bool set);
0120     /**
0121      * Signal emitted when the number of screens changes.
0122      * @param count The new number of screens
0123      */
0124     void numberScreensChanged(int count);
0125 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0126     /**
0127      * This signal is emitted when the size of @p screen changes.
0128      * Don't forget to fetch an updated client area.
0129      *
0130      * @deprecated Use QScreen::geometryChanged signal instead.
0131      */
0132     void screenResized(int screen);
0133 #endif
0134     /**
0135      * Signal emitted whenever the current activity changed.
0136      * @param id id of the new activity
0137      */
0138     void currentActivityChanged(const QString &id);
0139     /**
0140      * Signal emitted whenever the list of activities changed.
0141      * @param id id of the new activity
0142      */
0143     void activitiesChanged(const QString &id);
0144     /**
0145      * This signal is emitted when a new activity is added
0146      * @param id id of the new activity
0147      */
0148     void activityAdded(const QString &id);
0149     /**
0150      * This signal is emitted when the activity
0151      * is removed
0152      * @param id id of the removed activity
0153      */
0154     void activityRemoved(const QString &id);
0155     /**
0156      * Emitted whenever the virtualScreenSize changes.
0157      * @see virtualScreenSize()
0158      * @since 5.0
0159      */
0160     void virtualScreenSizeChanged();
0161     /**
0162      * Emitted whenever the virtualScreenGeometry changes.
0163      * @see virtualScreenGeometry()
0164      * @since 5.0
0165      */
0166     void virtualScreenGeometryChanged();
0167     /**
0168      * This signal is emitted when the current virtual desktop changes.
0169      *
0170      * @since 5.23
0171      */
0172     void currentVirtualDesktopChanged();
0173     /**
0174      * This signal is emitted when the cursor position changes.
0175      * @see cursorPos()
0176      */
0177     void cursorPosChanged();
0178 
0179 public:
0180     //------------------------------------------------------------------
0181     // enums copy&pasted from kwinglobals.h because qtscript is evil
0182 
0183     enum ClientAreaOption {
0184         ///< geometry where a window will be initially placed after being mapped
0185         PlacementArea,
0186         ///< window movement snapping area?  ignore struts
0187         MovementArea,
0188         ///< geometry to which a window will be maximized
0189         MaximizeArea,
0190         ///< like MaximizeArea, but ignore struts - used e.g. for topmenu
0191         MaximizeFullArea,
0192         ///< area for fullscreen windows
0193         FullScreenArea,
0194         ///< whole workarea (all screens together)
0195         WorkArea,
0196         ///< whole area (all screens together), ignore struts
0197         FullArea,
0198         ///< one whole screen, ignore struts
0199         ScreenArea
0200     };
0201     Q_ENUM(ClientAreaOption)
0202     enum ElectricBorder {
0203         ElectricTop,
0204         ElectricTopRight,
0205         ElectricRight,
0206         ElectricBottomRight,
0207         ElectricBottom,
0208         ElectricBottomLeft,
0209         ElectricLeft,
0210         ElectricTopLeft,
0211         ELECTRIC_COUNT,
0212         ElectricNone
0213     };
0214     Q_ENUM(ElectricBorder)
0215 
0216 protected:
0217     explicit WorkspaceWrapper(QObject *parent = nullptr);
0218 
0219 public:
0220 #define GETTERSETTERDEF(rettype, getter, setter) \
0221     rettype getter() const;                      \
0222     void setter(rettype val);
0223     GETTERSETTERDEF(int, numberOfDesktops, setNumberOfDesktops)
0224     GETTERSETTERDEF(int, currentDesktop, setCurrentDesktop)
0225     GETTERSETTERDEF(QString, currentActivity, setCurrentActivity)
0226     GETTERSETTERDEF(KWin::Window *, activeClient, setActiveClient)
0227 #undef GETTERSETTERDEF
0228     QSize desktopGridSize() const;
0229     int desktopGridWidth() const;
0230     int desktopGridHeight() const;
0231     int workspaceWidth() const;
0232     int workspaceHeight() const;
0233     QSize workspaceSize() const;
0234     int displayWidth() const;
0235     int displayHeight() const;
0236     QSize displaySize() const;
0237     int activeScreen() const;
0238     int numScreens() const;
0239     QStringList activityList() const;
0240     QSize virtualScreenSize() const;
0241     QRect virtualScreenGeometry() const;
0242     QPoint cursorPos() const;
0243 
0244     VirtualDesktop *currentVirtualDesktop() const;
0245     void setCurrentVirtualDesktop(VirtualDesktop *desktop);
0246 
0247     Q_INVOKABLE int screenAt(const QPointF &pos) const;
0248 
0249     Q_INVOKABLE KWin::TileManager *tilingForScreen(const QString &screenName) const;
0250     Q_INVOKABLE KWin::TileManager *tilingForScreen(int screen) const;
0251 
0252     /**
0253      * Returns the geometry a Client can use with the specified option.
0254      * This method should be preferred over other methods providing screen sizes as the
0255      * various options take constraints such as struts set on panels into account.
0256      * This method is also multi screen aware, but there are also options to get full areas.
0257      * @param option The type of area which should be considered
0258      * @param screen The screen for which the area should be considered
0259      * @param desktop The desktop for which the area should be considered, in general there should not be a difference
0260      * @returns The specified screen geometry
0261      * @deprecated use clientArea(ClientAreaOption option, KWin::Output *output, KWin::VirtualDesktop *desktop)
0262      */
0263     Q_SCRIPTABLE QRectF clientArea(ClientAreaOption option, int screen, int desktop) const; // TODO Plasma 6: Drop
0264     Q_SCRIPTABLE QRectF clientArea(ClientAreaOption option, KWin::Output *output, KWin::VirtualDesktop *desktop) const;
0265     /**
0266      * Overloaded method for convenience.
0267      * @param option The type of area which should be considered
0268      * @param point The coordinates which have to be included in the area
0269      * @param desktop The desktop for which the area should be considered, in general there should not be a difference
0270      * @returns The specified screen geometry
0271      * @deprecated use clientArea(ClientAreaOption option, const QPoint &point, KWin::VirtualDesktop *desktop)
0272      */
0273     Q_SCRIPTABLE QRectF clientArea(ClientAreaOption option, const QPoint &point, int desktop) const; // TODO Plasma 6: Drop
0274     Q_SCRIPTABLE QRectF clientArea(ClientAreaOption option, const QPoint &point, KWin::VirtualDesktop *desktop) const;
0275     /**
0276      * Overloaded method for convenience.
0277      * @param client The Client for which the area should be retrieved
0278      * @returns The specified screen geometry
0279      */
0280     Q_SCRIPTABLE QRectF clientArea(ClientAreaOption option, KWin::Window *client) const;
0281     Q_SCRIPTABLE QRectF clientArea(ClientAreaOption option, const KWin::Window *client) const;
0282     /**
0283      * Returns the name for the given @p desktop.
0284      */
0285     Q_SCRIPTABLE QString desktopName(int desktop) const;
0286     /**
0287      * Create a new virtual desktop at the requested position.
0288      * @param position The position of the desktop. It should be in range [0, count].
0289      * @param name The name for the new desktop, if empty the default name will be used.
0290      */
0291     Q_SCRIPTABLE void createDesktop(int position, const QString &name) const;
0292     /**
0293      * Remove the virtual desktop at the requested position
0294      * @param position The position of the desktop to be removed. It should be in range [0, count - 1].
0295      */
0296     Q_SCRIPTABLE void removeDesktop(int position) const;
0297     /**
0298      * Provides support information about the currently running KWin instance.
0299      */
0300     Q_SCRIPTABLE QString supportInformation() const;
0301     /**
0302      * Finds the Client with the given @p windowId.
0303      * @param windowId The window Id of the Client
0304      * @return The found Client or @c null
0305      */
0306     Q_SCRIPTABLE KWin::X11Window *getClient(qulonglong windowId);
0307 
0308 public Q_SLOTS:
0309     // all the available key bindings
0310     void slotSwitchDesktopNext();
0311     void slotSwitchDesktopPrevious();
0312     void slotSwitchDesktopRight();
0313     void slotSwitchDesktopLeft();
0314     void slotSwitchDesktopUp();
0315     void slotSwitchDesktopDown();
0316 
0317     void slotSwitchToNextScreen();
0318     void slotSwitchToPrevScreen();
0319     void slotSwitchToRightScreen();
0320     void slotSwitchToLeftScreen();
0321     void slotSwitchToAboveScreen();
0322     void slotSwitchToBelowScreen();
0323     void slotWindowToNextScreen();
0324     void slotWindowToPrevScreen();
0325     void slotWindowToRightScreen();
0326     void slotWindowToLeftScreen();
0327     void slotWindowToAboveScreen();
0328     void slotWindowToBelowScreen();
0329 
0330     void slotToggleShowDesktop();
0331 
0332     void slotWindowMaximize();
0333     void slotWindowMaximizeVertical();
0334     void slotWindowMaximizeHorizontal();
0335     void slotWindowMinimize();
0336     void slotWindowShade();
0337     void slotWindowRaise();
0338     void slotWindowLower();
0339     void slotWindowRaiseOrLower();
0340     void slotActivateAttentionWindow();
0341 
0342     /**
0343      * @deprecated since 5.24 use slotWindowMoveLeft()
0344      */
0345     void slotWindowPackLeft()
0346     {
0347         slotWindowMoveLeft();
0348     }
0349     /**
0350      * @deprecated since 5.24 use slotWindowMoveRight()
0351      */
0352     void slotWindowPackRight()
0353     {
0354         slotWindowMoveRight();
0355     }
0356     /**
0357      * @deprecated since 5.24 use slotWindowMoveUp()
0358      */
0359     void slotWindowPackUp()
0360     {
0361         slotWindowMoveUp();
0362     }
0363     /**
0364      * @deprecated since 5.24 use slotWindowMoveDown()
0365      */
0366     void slotWindowPackDown()
0367     {
0368         slotWindowMoveDown();
0369     }
0370     /**
0371      * @deprecated since 5.24 use slotWindowExpandHorizontal()
0372      */
0373     void slotWindowGrowHorizontal()
0374     {
0375         slotWindowExpandHorizontal();
0376     }
0377     /**
0378      * @deprecated since 5.24 use slotWindowExpandVertical()
0379      */
0380     void slotWindowGrowVertical()
0381     {
0382         slotWindowExpandVertical();
0383     }
0384 
0385     void slotWindowMoveLeft();
0386     void slotWindowMoveRight();
0387     void slotWindowMoveUp();
0388     void slotWindowMoveDown();
0389     void slotWindowExpandHorizontal();
0390     void slotWindowExpandVertical();
0391     void slotWindowShrinkHorizontal();
0392     void slotWindowShrinkVertical();
0393     void slotWindowQuickTileLeft();
0394     void slotWindowQuickTileRight();
0395     void slotWindowQuickTileTop();
0396     void slotWindowQuickTileBottom();
0397     void slotWindowQuickTileTopLeft();
0398     void slotWindowQuickTileTopRight();
0399     void slotWindowQuickTileBottomLeft();
0400     void slotWindowQuickTileBottomRight();
0401 
0402     void slotSwitchWindowUp();
0403     void slotSwitchWindowDown();
0404     void slotSwitchWindowRight();
0405     void slotSwitchWindowLeft();
0406 
0407     void slotIncreaseWindowOpacity();
0408     void slotLowerWindowOpacity();
0409 
0410     void slotWindowOperations();
0411     void slotWindowClose();
0412     void slotWindowMove();
0413     void slotWindowResize();
0414     void slotWindowAbove();
0415     void slotWindowBelow();
0416     void slotWindowOnAllDesktops();
0417     void slotWindowFullScreen();
0418     void slotWindowNoBorder();
0419 
0420     void slotWindowToNextDesktop();
0421     void slotWindowToPreviousDesktop();
0422     void slotWindowToDesktopRight();
0423     void slotWindowToDesktopLeft();
0424     void slotWindowToDesktopUp();
0425     void slotWindowToDesktopDown();
0426 
0427     /**
0428      * Sends the Window to the given @p screen.
0429      */
0430     void sendClientToScreen(KWin::Window *client, int screen);
0431 
0432     /**
0433      * Shows an outline at the specified @p geometry.
0434      * If an outline is already shown the outline is moved to the new position.
0435      * Use hideOutline to remove the outline again.
0436      */
0437     void showOutline(const QRect &geometry);
0438     /**
0439      * Overloaded method for convenience.
0440      */
0441     void showOutline(int x, int y, int width, int height);
0442     /**
0443      * Hides the outline previously shown by showOutline.
0444      */
0445     void hideOutline();
0446 
0447 private Q_SLOTS:
0448     void setupClientConnections(Window *client);
0449 };
0450 
0451 class QtScriptWorkspaceWrapper : public WorkspaceWrapper
0452 {
0453     Q_OBJECT
0454 public:
0455     /**
0456      * List of Clients currently managed by KWin.
0457      */
0458     Q_INVOKABLE QList<KWin::Window *> clientList() const;
0459 
0460     explicit QtScriptWorkspaceWrapper(QObject *parent = nullptr);
0461 };
0462 
0463 class DeclarativeScriptWorkspaceWrapper : public WorkspaceWrapper
0464 {
0465     Q_OBJECT
0466 
0467     Q_PROPERTY(QQmlListProperty<KWin::Window> clients READ clients)
0468 public:
0469     QQmlListProperty<KWin::Window> clients();
0470 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
0471     static int countClientList(QQmlListProperty<KWin::Window> *clients);
0472     static KWin::Window *atClientList(QQmlListProperty<KWin::Window> *clients, int index);
0473 #else
0474     static qsizetype countClientList(QQmlListProperty<KWin::Window> *clients);
0475     static KWin::Window *atClientList(QQmlListProperty<KWin::Window> *clients, qsizetype index);
0476 #endif
0477 
0478     explicit DeclarativeScriptWorkspaceWrapper(QObject *parent = nullptr);
0479 };
0480 
0481 }