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: 2012 Martin Gräßlin <mgraesslin@kde.org>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 */
0009 #pragma once
0010 // Qt
0011 #include <QHash>
0012 #include <QObject>
0013 #include <QVector>
0014 
0015 namespace KWin
0016 {
0017 namespace TabBox
0018 {
0019 
0020 /**
0021  * @brief A chain for last recently used virtual desktops.
0022  */
0023 class DesktopChain
0024 {
0025 public:
0026     /**
0027      * Creates a last recently used virtual desktop chain with the given @p initialSize.
0028      */
0029     explicit DesktopChain(uint initialSize = 0);
0030     /**
0031      * Returns the next desktop in the chain starting from @p indexDesktop.
0032      * In case that the @p indexDesktop is the last desktop of the chain, the method wraps around
0033      * and returns the first desktop stored in the chain.
0034      * In case the chain is valid, but does not contain the @p indexDesktop, the first element of
0035      * the chain is returned.
0036      * In case the chain is not valid, the always valid virtual desktop with identifier @c 1
0037      * is returned.
0038      * @param indexDesktop The id of the virtual desktop which should be used as a starting point
0039      * @return The next virtual desktop in the chain
0040      */
0041     uint next(uint indexDesktop) const;
0042     /**
0043      * Adds the @p desktop to the chain. The @p desktop becomes the first element of the
0044      * chain. All desktops in the chain from the previous index of @p desktop are moved
0045      * one position in the chain.
0046      * @param desktop The new desktop to be the top most element in the chain.
0047      */
0048     void add(uint desktop);
0049     /**
0050      * Resizes the chain from @p previousSize to @p newSize.
0051      * In case the chain grows new elements are added with a meaning full id in the range
0052      * [previousSize, newSize].
0053      * In case the chain shrinks it is ensured that no element points to a virtual desktop
0054      * with an id larger than @p newSize.
0055      * @param previousSize The previous size of the desktop chain
0056      * @param newSize The size to be used for the desktop chain
0057      */
0058     void resize(uint previousSize, uint newSize);
0059 
0060 private:
0061     /**
0062      * Initializes the chain with default values.
0063      */
0064     void init();
0065     QVector<uint> m_chain;
0066 };
0067 
0068 /**
0069  * @brief A manager for multiple desktop chains.
0070  *
0071  * This manager keeps track of multiple desktop chains which have a given identifier.
0072  * A common usage for this is to have a different desktop chain for each Activity.
0073  */
0074 class DesktopChainManager : public QObject
0075 {
0076     Q_OBJECT
0077 
0078 public:
0079     explicit DesktopChainManager(QObject *parent = nullptr);
0080     ~DesktopChainManager() override;
0081 
0082     /**
0083      * Returns the next virtual desktop starting from @p indexDesktop in the currently used chain.
0084      * @param indexDesktop The id of the virtual desktop which should be used as a starting point
0085      * @return The next virtual desktop in the currently used chain
0086      * @see DesktopChain::next
0087      */
0088     uint next(uint indexDesktop) const;
0089 
0090 public Q_SLOTS:
0091     /**
0092      * Adds the @p currentDesktop to the currently used desktop chain.
0093      * @param previousDesktop The previously used desktop, should be the top element of the chain
0094      * @param currentDesktop The desktop which should be the new top element of the chain
0095      */
0096     void addDesktop(uint previousDesktop, uint currentDesktop);
0097     /**
0098      * Resizes all managed desktop chains from @p previousSize to @p newSize.
0099      * @param previousSize The previously used size for the chains
0100      * @param newSize The size to be used for the chains
0101      * @see DesktopChain::resize
0102      */
0103     void resize(uint previousSize, uint newSize);
0104     /**
0105      * Switches to the desktop chain identified by the given @p identifier.
0106      * If there is no chain yet for the given @p identifier a new chain is created and used.
0107      * @param identifier The identifier of the desktop chain to be used
0108      */
0109     void useChain(const QString &identifier);
0110 
0111 private:
0112     typedef QHash<QString, DesktopChain> DesktopChains;
0113     /**
0114      * Creates a new desktop chain for the given @p identifier and adds it to the list
0115      * of identifiers.
0116      * @returns Position of the new chain in the managed list of chains
0117      */
0118     DesktopChains::Iterator addNewChain(const QString &identifier);
0119     /**
0120      * Creates the very first list to be used when an @p identifier comes in.
0121      * The dummy chain which is used by default gets copied and used for this chain.
0122      */
0123     void createFirstChain(const QString &identifier);
0124 
0125     DesktopChains::Iterator m_currentChain;
0126     DesktopChains m_chains;
0127     /**
0128      * The maximum size to be used for a new desktop chain
0129      */
0130     uint m_maxChainSize;
0131 };
0132 
0133 } // TabBox
0134 } // namespace KWin