diff --git a/virtualdesktops.cpp b/virtualdesktops.cpp index c51958d21..55c3d0cb0 100644 --- a/virtualdesktops.cpp +++ b/virtualdesktops.cpp @@ -1,529 +1,577 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2009 Lucas Murray Copyright (C) 2012 Martin Gräßlin This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . *********************************************************************/ #include "virtualdesktops.h" #include "input.h" // KDE #include #include #include #include // Qt #include namespace KWin { extern int screen_number; +VirtualDesktop::VirtualDesktop(QObject *parent) + : QObject(parent) +{ +} + +VirtualDesktop::~VirtualDesktop() = default; + +void VirtualDesktop::setId(const QByteArray &id) +{ + Q_ASSERT(m_id.isEmpty()); + m_id = id; +} + +void VirtualDesktop::setX11DesktopNumber(uint number) +{ + Q_ASSERT(m_x11DesktopNumber == 0); + m_x11DesktopNumber = number; +} + +void VirtualDesktop::setName(const QString &name) +{ + if (m_name == name) { + return; + } + m_name = name; + emit nameChanged(); +} + VirtualDesktopGrid::VirtualDesktopGrid() : m_size(1, 2) // Default to tow rows , m_grid(new uint[2]) { // Initializing grid array m_grid[0] = 0; m_grid[1] = 0; } VirtualDesktopGrid::~VirtualDesktopGrid() { delete[] m_grid; } void VirtualDesktopGrid::update(const QSize &size, Qt::Orientation orientation) { // Set private variables delete[] m_grid; m_size = size; const uint width = size.width(); const uint height = size.height(); const uint length = width * height; const uint desktopCount = VirtualDesktopManager::self()->count(); m_grid = new uint[length]; // Populate grid uint desktop = 1; if (orientation == Qt::Horizontal) { for (uint y = 0; y < height; ++y) { for (uint x = 0; x < width; ++x) { m_grid[y * width + x] = (desktop <= desktopCount ? desktop++ : 0); } } } else { for (uint x = 0; x < width; ++x) { for (uint y = 0; y < height; ++y) { m_grid[y * width + x] = (desktop <= desktopCount ? desktop++ : 0); } } } } QPoint VirtualDesktopGrid::gridCoords(uint id) const { for (int y = 0; y < m_size.height(); ++y) { for (int x = 0; x < m_size.width(); ++x) { if (m_grid[y * m_size.width() + x] == id) { return QPoint(x, y); } } } return QPoint(-1, -1); } KWIN_SINGLETON_FACTORY_VARIABLE(VirtualDesktopManager, s_manager) VirtualDesktopManager::VirtualDesktopManager(QObject *parent) : QObject(parent) - , m_current(0) - , m_count(0) , m_navigationWrapsAround(false) , m_rootInfo(NULL) { } VirtualDesktopManager::~VirtualDesktopManager() { s_manager = NULL; } QString VirtualDesktopManager::name(uint desktop) const { if (!m_rootInfo) { return defaultName(desktop); } return QString::fromUtf8(m_rootInfo->desktopName(desktop)); } uint VirtualDesktopManager::above(uint id, bool wrap) const { if (id == 0) { id = current(); } QPoint coords = m_grid.gridCoords(id); Q_ASSERT(coords.x() >= 0); while (true) { coords.ry()--; if (coords.y() < 0) { if (wrap) { coords.setY(m_grid.height() - 1); } else { return id; // Already at the top-most desktop } } const uint desktop = m_grid.at(coords); if (desktop > 0) { return desktop; } } } uint VirtualDesktopManager::toRight(uint id, bool wrap) const { if (id == 0) { id = current(); } QPoint coords = m_grid.gridCoords(id); Q_ASSERT(coords.x() >= 0); while (true) { coords.rx()++; if (coords.x() >= m_grid.width()) { if (wrap) { coords.setX(0); } else { return id; // Already at the right-most desktop } } const uint desktop = m_grid.at(coords); if (desktop > 0) { return desktop; } } } uint VirtualDesktopManager::below(uint id, bool wrap) const { if (id == 0) { id = current(); } QPoint coords = m_grid.gridCoords(id); Q_ASSERT(coords.x() >= 0); while (true) { coords.ry()++; if (coords.y() >= m_grid.height()) { if (wrap) { coords.setY(0); } else { // Already at the bottom-most desktop return id; } } const uint desktop = m_grid.at(coords); if (desktop > 0) { return desktop; } } } uint VirtualDesktopManager::toLeft(uint id, bool wrap) const { if (id == 0) { id = current(); } QPoint coords = m_grid.gridCoords(id); Q_ASSERT(coords.x() >= 0); while (true) { coords.rx()--; if (coords.x() < 0) { if (wrap) { coords.setX(m_grid.width() - 1); } else { return id; // Already at the left-most desktop } } const uint desktop = m_grid.at(coords); if (desktop > 0) { return desktop; } } } uint VirtualDesktopManager::next(uint id, bool wrap) const { if (id == 0) { id = current(); } const uint desktop = id + 1; if (desktop > count()) { if (wrap) { return 1; } else { // are at the last desktop, without wrap return current return id; } } return desktop; } uint VirtualDesktopManager::previous(uint id, bool wrap) const { if (id == 0) { id = current(); } const uint desktop = id - 1; if (desktop == 0) { if (wrap) { return count(); } else { // are at the first desktop, without wrap return current return id; } } return desktop; } +uint VirtualDesktopManager::current() const +{ + return m_current ? m_current->x11DesktopNumber() : 0; +} + bool VirtualDesktopManager::setCurrent(uint newDesktop) { - if (newDesktop < 1 || newDesktop > count() || newDesktop == m_current) { + if (newDesktop < 1 || newDesktop > count() || newDesktop == current()) { return false; } - const uint oldDesktop = m_current; + auto it = std::find_if(m_desktops.constBegin(), m_desktops.constEnd(), + [newDesktop] (VirtualDesktop *d) { + return d->x11DesktopNumber() == newDesktop; + } + ); + Q_ASSERT(it != m_desktops.constEnd()); + const uint oldDesktop = current(); + // change the desktop - m_current = newDesktop; + m_current = *it; emit currentChanged(oldDesktop, newDesktop); return true; } void VirtualDesktopManager::setCount(uint count) { count = qBound(1, count, VirtualDesktopManager::maximum()); - if (count == m_count) { + if (count == uint(m_desktops.count())) { // nothing to change return; } - const uint oldCount = m_count; - m_count = count; - - if (oldCount > m_count) { - handleDesktopsRemoved(oldCount); + const uint oldCount = m_desktops.count(); + const uint oldCurrent = current(); + while (uint(m_desktops.count()) > count) { + delete m_desktops.takeLast(); } + while (uint(m_desktops.count()) < count) { + auto vd = new VirtualDesktop(this); + vd->setX11DesktopNumber(m_desktops.count() + 1); + m_desktops << vd; + } + if (oldCount > count) { + handleDesktopsRemoved(oldCount, oldCurrent); + } + updateRootInfo(); save(); - emit countChanged(oldCount, m_count); + emit countChanged(oldCount, m_desktops.count()); } -void VirtualDesktopManager::handleDesktopsRemoved(uint previousCount) +void VirtualDesktopManager::handleDesktopsRemoved(uint previousCount, uint previousCurrent) { - if (current() > count()) { - setCurrent(count()); + if (!m_current) { + m_current = m_desktops.last(); + emit currentChanged(previousCurrent, m_current->x11DesktopNumber()); } emit desktopsRemoved(previousCount); } void VirtualDesktopManager::updateRootInfo() { if (!m_rootInfo) { // Make sure the layout is still valid updateLayout(); return; } const int n = count(); m_rootInfo->setNumberOfDesktops(n); NETPoint *viewports = new NETPoint[n]; m_rootInfo->setDesktopViewport(n, *viewports); delete[] viewports; // Make sure the layout is still valid updateLayout(); } void VirtualDesktopManager::updateLayout() { int width = 0; int height = 0; Qt::Orientation orientation = Qt::Horizontal; if (m_rootInfo) { // TODO: Is there a sane way to avoid overriding the existing grid? width = m_rootInfo->desktopLayoutColumnsRows().width(); height = m_rootInfo->desktopLayoutColumnsRows().height(); orientation = m_rootInfo->desktopLayoutOrientation() == NET::OrientationHorizontal ? Qt::Horizontal : Qt::Vertical; } if (width == 0 && height == 0) { // Not given, set default layout height = count() == 1u ? 1 : 2; } setNETDesktopLayout(orientation, width, height, 0 //rootInfo->desktopLayoutCorner() // Not really worth implementing right now. ); } static bool s_loadingDesktopSettings = false; void VirtualDesktopManager::load() { s_loadingDesktopSettings = true; if (!m_config) { return; } QString groupname; if (screen_number == 0) { groupname = QStringLiteral("Desktops"); } else { groupname.sprintf("Desktops-screen-%d", screen_number); } KConfigGroup group(m_config, groupname); const int n = group.readEntry("Number", 1); setCount(n); if (m_rootInfo) { for (int i = 1; i <= n; i++) { QString s = group.readEntry(QStringLiteral("Name_%1").arg(i), i18n("Desktop %1", i)); m_rootInfo->setDesktopName(i, s.toUtf8().data()); // TODO: update desktop focus chain, why? // m_desktopFocusChain.value()[i-1] = i; } int rows = group.readEntry("Rows", 2); rows = qBound(1, rows, n); // avoid weird cases like having 3 rows for 4 desktops, where the last row is unused int columns = n / rows; if (n % rows > 0) { columns++; } m_rootInfo->setDesktopLayout(NET::OrientationHorizontal, columns, rows, NET::DesktopLayoutCornerTopLeft); m_rootInfo->activate(); } s_loadingDesktopSettings = false; } void VirtualDesktopManager::save() { if (s_loadingDesktopSettings) { return; } if (!m_config) { return; } QString groupname; if (screen_number == 0) { groupname = QStringLiteral("Desktops"); } else { groupname.sprintf("Desktops-screen-%d", screen_number); } KConfigGroup group(m_config, groupname); group.writeEntry("Number", count()); for (uint i = 1; i <= count(); ++i) { QString s = name(i); const QString defaultvalue = defaultName(i); if (s.isEmpty()) { s = defaultvalue; if (m_rootInfo) { m_rootInfo->setDesktopName(i, s.toUtf8().data()); } } if (s != defaultvalue) { group.writeEntry(QStringLiteral("Name_%1").arg(i), s); } else { QString currentvalue = group.readEntry(QStringLiteral("Name_%1").arg(i), QString()); if (currentvalue != defaultvalue) { group.deleteEntry(QStringLiteral("Name_%1").arg(i)); } } } // Save to disk group.sync(); } QString VirtualDesktopManager::defaultName(int desktop) const { return i18n("Desktop %1", desktop); } void VirtualDesktopManager::setNETDesktopLayout(Qt::Orientation orientation, uint width, uint height, int startingCorner) { Q_UNUSED(startingCorner); // Not really worth implementing right now. + const uint count = m_desktops.count(); // Calculate valid grid size Q_ASSERT(width > 0 || height > 0); if ((width <= 0) && (height > 0)) { - width = (m_count + height - 1) / height; + width = (count + height - 1) / height; } else if ((height <= 0) && (width > 0)) { - height = (m_count + width - 1) / width; + height = (count + width - 1) / width; } - while (width * height < m_count) { + while (width * height < count) { if (orientation == Qt::Horizontal) { ++width; } else { ++height; } } m_grid.update(QSize(width, height), orientation); // TODO: why is there no call to m_rootInfo->setDesktopLayout? emit layoutChanged(width, height); } void VirtualDesktopManager::initShortcuts() { initSwitchToShortcuts(); addAction(QStringLiteral("Switch to Next Desktop"), i18n("Switch to Next Desktop"), &VirtualDesktopManager::slotNext); addAction(QStringLiteral("Switch to Previous Desktop"), i18n("Switch to Previous Desktop"), &VirtualDesktopManager::slotPrevious); addAction(QStringLiteral("Switch One Desktop to the Right"), i18n("Switch One Desktop to the Right"), &VirtualDesktopManager::slotRight); addAction(QStringLiteral("Switch One Desktop to the Left"), i18n("Switch One Desktop to the Left"), &VirtualDesktopManager::slotLeft); addAction(QStringLiteral("Switch One Desktop Up"), i18n("Switch One Desktop Up"), &VirtualDesktopManager::slotUp); addAction(QStringLiteral("Switch One Desktop Down"), i18n("Switch One Desktop Down"), &VirtualDesktopManager::slotDown); // axis events input()->registerAxisShortcut(Qt::ControlModifier | Qt::AltModifier, PointerAxisDown, findChild(QStringLiteral("Switch to Next Desktop"))); input()->registerAxisShortcut(Qt::ControlModifier | Qt::AltModifier, PointerAxisUp, findChild(QStringLiteral("Switch to Previous Desktop"))); } void VirtualDesktopManager::initSwitchToShortcuts() { const QString toDesktop = QStringLiteral("Switch to Desktop %1"); const KLocalizedString toDesktopLabel = ki18n("Switch to Desktop %1"); addAction(toDesktop, toDesktopLabel, 1, QKeySequence(Qt::CTRL + Qt::Key_F1), &VirtualDesktopManager::slotSwitchTo); addAction(toDesktop, toDesktopLabel, 2, QKeySequence(Qt::CTRL + Qt::Key_F2), &VirtualDesktopManager::slotSwitchTo); addAction(toDesktop, toDesktopLabel, 3, QKeySequence(Qt::CTRL + Qt::Key_F3), &VirtualDesktopManager::slotSwitchTo); addAction(toDesktop, toDesktopLabel, 4, QKeySequence(Qt::CTRL + Qt::Key_F4), &VirtualDesktopManager::slotSwitchTo); for (uint i = 5; i <= maximum(); ++i) { addAction(toDesktop, toDesktopLabel, i, QKeySequence(), &VirtualDesktopManager::slotSwitchTo); } } void VirtualDesktopManager::addAction(const QString &name, const KLocalizedString &label, uint value, const QKeySequence &key, void (VirtualDesktopManager::*slot)()) { QAction *a = new QAction(this); a->setProperty("componentName", QStringLiteral(KWIN_NAME)); a->setObjectName(name.arg(value)); a->setText(label.subs(value).toString()); a->setData(value); KGlobalAccel::setGlobalShortcut(a, key); input()->registerShortcut(key, a, this, slot); } void VirtualDesktopManager::addAction(const QString &name, const QString &label, void (VirtualDesktopManager::*slot)()) { QAction *a = new QAction(this); a->setProperty("componentName", QStringLiteral(KWIN_NAME)); a->setObjectName(name); a->setText(label); KGlobalAccel::setGlobalShortcut(a, QKeySequence()); input()->registerShortcut(QKeySequence(), a, this, slot); } void VirtualDesktopManager::slotSwitchTo() { QAction *act = qobject_cast(sender()); if (!act) { return; } bool ok = false; const uint i = act->data().toUInt(&ok); if (!ok) { return; } setCurrent(i); } void VirtualDesktopManager::setNavigationWrappingAround(bool enabled) { if (enabled == m_navigationWrapsAround) { return; } m_navigationWrapsAround = enabled; emit navigationWrappingAroundChanged(); } void VirtualDesktopManager::slotDown() { moveTo(isNavigationWrappingAround()); } void VirtualDesktopManager::slotLeft() { moveTo(isNavigationWrappingAround()); } void VirtualDesktopManager::slotPrevious() { moveTo(isNavigationWrappingAround()); } void VirtualDesktopManager::slotNext() { moveTo(isNavigationWrappingAround()); } void VirtualDesktopManager::slotRight() { moveTo(isNavigationWrappingAround()); } void VirtualDesktopManager::slotUp() { moveTo(isNavigationWrappingAround()); } } // KWin diff --git a/virtualdesktops.h b/virtualdesktops.h index 0d9f1277a..745400070 100644 --- a/virtualdesktops.h +++ b/virtualdesktops.h @@ -1,567 +1,598 @@ /******************************************************************** KWin - the KDE window manager This file is part of the KDE project. Copyright (C) 2012 Martin Gräßlin This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . *********************************************************************/ #ifndef KWIN_VIRTUAL_DESKTOPS_H #define KWIN_VIRTUAL_DESKTOPS_H // KWin #include // Qt includes #include #include +#include #include // KDE includes #include #include class KLocalizedString; class NETRootInfo; namespace KWin { +class VirtualDesktop : public QObject +{ + Q_OBJECT + Q_PROPERTY(QByteArray id READ id CONSTANT) + Q_PROPERTY(uint x11DesktopNumber READ x11DesktopNumber CONSTANT) + Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged) +public: + explicit VirtualDesktop(QObject *parent = nullptr); + virtual ~VirtualDesktop(); + + void setId(const QByteArray &id); + QByteArray id() const { + return m_id; + } + + void setName(const QString &name); + QString name() const { + return m_name; + } + + void setX11DesktopNumber(uint number); + uint x11DesktopNumber() const { + return m_x11DesktopNumber; + } + +Q_SIGNALS: + void nameChanged(); + +private: + QByteArray m_id; + QString m_name; + int m_x11DesktopNumber = 0; + +}; + /** * @brief Two dimensional grid containing the ID of the virtual desktop at a specific position * in the grid. * * The VirtualDesktopGrid represents a visual layout of the Virtual Desktops as they are in e.g. * a Pager. This grid is used for getting a desktop next to a given desktop in any direction by * making use of the layout information. This allows navigation like move to desktop on left. **/ class VirtualDesktopGrid { public: VirtualDesktopGrid(); ~VirtualDesktopGrid(); void update(const QSize &size, Qt::Orientation orientation); /** * @returns The coords of desktop @a id in grid units. */ QPoint gridCoords(uint id) const; /** * @returns The ID of the desktop at the point @a coords or 0 if no desktop exists at that * point. @a coords is to be in grid units. */ uint at(QPoint coords) const; int width() const; int height() const; const QSize &size() const; private: QSize m_size; uint *m_grid; }; /** * @brief Manages the number of available virtual desktops, the layout of those and which virtual * desktop is the current one. * * This manager is responsible for Virtual Desktop handling inside KWin. It has a property for the * count of available virtual desktops and a property for the currently active virtual desktop. All * changes to the number of virtual desktops and the current virtual desktop need to go through this * manager. * * On all changes a signal is emitted and interested parties should connect to the signal. The manager * itself does not interact with other parts of the system. E.g. it does not hide/show windows of * desktop changes. This is outside the scope of this manager. * * Internally the manager organizes the virtual desktops in a grid allowing to navigate over the * virtual desktops. For this a set of convenient methods are available which allow to get the id * of an adjacent desktop or to switch to an adjacent desktop. Interested parties should make use of * these methods and not replicate the logic to switch to the next desktop. **/ class VirtualDesktopManager : public QObject { Q_OBJECT /** * The number of virtual desktops currently available. * The ids of the virtual desktops are in the range [1, VirtualDesktopManager::maximum()]. **/ Q_PROPERTY(uint count READ count WRITE setCount NOTIFY countChanged) /** * The id of the virtual desktop which is currently in use. **/ Q_PROPERTY(uint current READ current WRITE setCurrent NOTIFY currentChanged) /** * Whether navigation in the desktop layout wraps around at the borders. **/ Q_PROPERTY(bool navigationWrappingAround READ isNavigationWrappingAround WRITE setNavigationWrappingAround NOTIFY navigationWrappingAroundChanged) public: virtual ~VirtualDesktopManager(); /** * @internal **/ void setRootInfo(NETRootInfo *info); /** * @internal **/ void setConfig(KSharedConfig::Ptr config); /** * @returns Total number of desktops currently in existence. * @see setCount * @see countChanged */ uint count() const; /** * @returns The ID of the current desktop. * @see setCurrent * @see currentChanged */ uint current() const; /** * Moves to the desktop through the algorithm described by Direction. * @param wrap If @c true wraps around to the other side of the layout * @see setCurrent **/ template void moveTo(bool wrap = false); /** * @returns The name of the @p desktop **/ QString name(uint desktop) const; /** * @returns @c true if navigation at borders of layout wraps around, @c false otherwise * @see setNavigationWrappingAround * @see navigationWrappingAroundChanged **/ bool isNavigationWrappingAround() const; /** * @returns The layout aware virtual desktop grid used by this manager. **/ const VirtualDesktopGrid &grid() const; /** * @returns The ID of the desktop above desktop @a id. Wraps around to the bottom of * the layout if @a wrap is set. If @a id is not set use the current one. */ uint above(uint id = 0, bool wrap = true) const; /** * @returns The ID of the desktop to the right of desktop @a id. Wraps around to the * left of the layout if @a wrap is set. If @a id is not set use the current one. */ uint toRight(uint id = 0, bool wrap = true) const; /** * @returns The ID of the desktop below desktop @a id. Wraps around to the top of the * layout if @a wrap is set. If @a id is not set use the current one. */ uint below(uint id = 0, bool wrap = true) const; /** * @returns The ID of the desktop to the left of desktop @a id. Wraps around to the * right of the layout if @a wrap is set. If @a id is not set use the current one. */ uint toLeft(uint id = 0, bool wrap = true) const; /** * @returns The ID of the desktop after the desktop @a id. Wraps around to the first * desktop if @a wrap is set. If @a id is not set use the current desktop. **/ uint next(uint id = 0, bool wrap = true) const; /** * @returns The ID of the desktop in front of the desktop @a id. Wraps around to the * last desktop if @a wrap is set. If @a id is not set use the current desktop. **/ uint previous(uint id = 0, bool wrap = true) const; void initShortcuts(); /** * @returns The maximum number of desktops that KWin supports. */ static uint maximum(); public Q_SLOTS: /** * Set the number of available desktops to @a count. This function overrides any previous * grid layout. * There needs to be at least one virtual desktop and the new value is capped at the maximum * number of desktops. A caller of this function cannot expect that the change has been applied. * It is the callers responsibility to either check the @link numberOfDesktops or connect to the * @link countChanged signal. * * In case the @link current desktop is on a desktop higher than the new count, the current desktop * is changed to be the new desktop with highest id. In that situation the signal @link desktopsRemoved * is emitted. * @param count The new number of desktops to use * @see count * @see maximum * @see countChanged * @see desktopsRemoved */ void setCount(uint count); /** * Set the current desktop to @a current. * @returns True on success, false otherwise. * @see current * @see currentChanged * @see moveTo */ bool setCurrent(uint current); /** * Called from within setCount() to ensure the desktop layout is still valid. */ void updateLayout(); /** * @param enable wrapping around borders for navigation in desktop layout * @see isNavigationWrappingAround * @see navigationWrappingAroundChanged **/ void setNavigationWrappingAround(bool enabled); /** * Loads number of desktops and names from configuration file **/ void load(); /** * Saves number of desktops and names to configuration file **/ void save(); Q_SIGNALS: /** * Signal emitted whenever the number of virtual desktops changes. * @param previousCount The number of desktops prior to the change * @param newCount The new current number of desktops **/ void countChanged(uint previousCount, uint newCount); /** * Signal emitted whenever the number of virtual desktops changes in a way * that existing desktops are removed. * * The signal is emitted after the @c count property has been updated but prior * to the @link countChanged signal being emitted. * @param previousCount The number of desktops prior to the change. * @see countChanged * @see setCount * @see count **/ void desktopsRemoved(uint previousCount); /** * Signal emitted whenever the current desktop changes. * @param previousDesktop The virtual desktop changed from * @param newDesktop The virtual desktop changed to **/ void currentChanged(uint previousDesktop, uint newDesktop); /** * Signal emitted whenever the desktop layout changes. * @param columns The new number of columns in the layout * @param rows The new number of rows in the layout **/ void layoutChanged(int columns, int rows); /** * Signal emitted whenever the navigationWrappingAround property changes. **/ void navigationWrappingAroundChanged(); private Q_SLOTS: /** * Common slot for all "Switch to Desktop n" shortcuts. * This method uses the sender() method to access some data. * DO NOT CALL DIRECTLY! ONLY TO BE USED FROM AN ACTION! **/ void slotSwitchTo(); /** * Slot for switch to next desktop action. **/ void slotNext(); /** * Slot for switch to previous desktop action. **/ void slotPrevious(); /** * Slot for switch to right desktop action. **/ void slotRight(); /** * Slot for switch to left desktop action. **/ void slotLeft(); /** * Slot for switch to desktop above action. **/ void slotUp(); /** * Slot for switch to desktop below action. **/ void slotDown(); private: /** * This method is called when the number of desktops is updated in a way that desktops * are removed. At the time when this method is invoked the count property is already * updated but the corresponding signal has not been emitted yet. * * Ensures that in case the current desktop is on one of the removed * desktops the last desktop after the change becomes the new desktop. * Emits the signal @link desktopsRemoved. * * @param previousCount The number of desktops prior to the change. + * @param previousCurrent The number of the previously current desktop. * @see setCount * @see desktopsRemoved **/ - void handleDesktopsRemoved(uint previousCount); + void handleDesktopsRemoved(uint previousCount, uint previousCurrent); /** * Generate a desktop layout from EWMH _NET_DESKTOP_LAYOUT property parameters. */ void setNETDesktopLayout(Qt::Orientation orientation, uint width, uint height, int startingCorner); /** * Updates the net root info for new number of desktops **/ void updateRootInfo(); /** * @returns A default name for the given @p desktop **/ QString defaultName(int desktop) const; /** * Creates all the global keyboard shortcuts for "Switch To Desktop n" actions. **/ void initSwitchToShortcuts(); /** * Creates an action and connects it to the @p slot in this Manager. This method is * meant to be used for the case that an additional information needs to be stored in * the action and the label. * @param name The name of the action to be created * @param label The localized name for the action to be created * @param value An additional value added to the label and to the created action * @param key The global shortcut for the action * @param slot The slot to invoke when the action is triggered **/ void addAction(const QString &name, const KLocalizedString &label, uint value, const QKeySequence &key, void (VirtualDesktopManager::*slot)()); /** * Creates an action and connects it to the @p slot in this Manager. * Overloaded method for the case that no additional value needs to be passed to the action and * no global shortcut is defined by default. * @param name The name of the action to be created * @param label The localized name for the action to be created * @param slot The slot to invoke when the action is triggered **/ void addAction(const QString &name, const QString &label, void (VirtualDesktopManager::*slot)()); - uint m_current; - uint m_count; + QVector m_desktops; + QPointer m_current; bool m_navigationWrapsAround; VirtualDesktopGrid m_grid; // TODO: QPointer NETRootInfo *m_rootInfo; KSharedConfig::Ptr m_config; KWIN_SINGLETON_VARIABLE(VirtualDesktopManager, s_manager) }; /** * Function object to select the desktop above in the layout. * Note: does not switch to the desktop! **/ class DesktopAbove { public: DesktopAbove() {} /** * @param desktop The desktop from which the desktop above should be selected. If @c 0 the current desktop is used * @param wrap Whether to wrap around if already topmost desktop * @returns Id of the desktop above @p desktop **/ uint operator() (uint desktop, bool wrap) { return VirtualDesktopManager::self()->above(desktop, wrap); } }; /** * Function object to select the desktop below in the layout. * Note: does not switch to the desktop! **/ class DesktopBelow { public: DesktopBelow() {} /** * @param desktop The desktop from which the desktop below should be selected. If @c 0 the current desktop is used * @param wrap Whether to wrap around if already lowest desktop * @returns Id of the desktop below @p desktop **/ uint operator() (uint desktop, bool wrap) { return VirtualDesktopManager::self()->below(desktop, wrap); } }; /** * Function object to select the desktop to the left in the layout. * Note: does not switch to the desktop! **/ class DesktopLeft { public: DesktopLeft() {} /** * @param desktop The desktop from which the desktop on the left should be selected. If @c 0 the current desktop is used * @param wrap Whether to wrap around if already leftmost desktop * @returns Id of the desktop left of @p desktop **/ uint operator() (uint desktop, bool wrap) { return VirtualDesktopManager::self()->toLeft(desktop, wrap); } }; /** * Function object to select the desktop to the right in the layout. * Note: does not switch to the desktop! **/ class DesktopRight { public: DesktopRight() {} /** * @param desktop The desktop from which the desktop on the right should be selected. If @c 0 the current desktop is used * @param wrap Whether to wrap around if already rightmost desktop * @returns Id of the desktop right of @p desktop **/ uint operator() (uint desktop, bool wrap) { return VirtualDesktopManager::self()->toRight(desktop, wrap); } }; /** * Function object to select the next desktop in the layout. * Note: does not switch to the desktop! **/ class DesktopNext { public: DesktopNext() {} /** * @param desktop The desktop from which the next desktop should be selected. If @c 0 the current desktop is used * @param wrap Whether to wrap around if already last desktop * @returns Id of the next desktop **/ uint operator() (uint desktop, bool wrap) { return VirtualDesktopManager::self()->next(desktop, wrap); } }; /** * Function object to select the previous desktop in the layout. * Note: does not switch to the desktop! **/ class DesktopPrevious { public: DesktopPrevious() {} /** * @param desktop The desktop from which the previous desktop should be selected. If @c 0 the current desktop is used * @param wrap Whether to wrap around if already first desktop * @returns Id of the previous desktop **/ uint operator() (uint desktop, bool wrap) { return VirtualDesktopManager::self()->previous(desktop, wrap); } }; /** * Helper function to get the ID of a virtual desktop in the direction from * the given @p desktop. If @c 0 the current desktop is used as a starting point. * @param desktop The desktop from which the desktop in given Direction should be selected. * @param wrap Whether desktop navigation wraps around at the borders of the layout * @returns The next desktop in specified direction **/ template uint getDesktop(int desktop = 0, bool wrap = true); template uint getDesktop(int d, bool wrap) { Direction direction; return direction(d, wrap); } inline int VirtualDesktopGrid::width() const { return m_size.width(); } inline int VirtualDesktopGrid::height() const { return m_size.height(); } inline const QSize &VirtualDesktopGrid::size() const { return m_size; } inline uint VirtualDesktopGrid::at(QPoint coords) const { const int index = coords.y() * m_size.width() + coords.x(); if (index > m_size.width() * m_size.height() || coords.x() >= width() || coords.y() >= height()) { return 0; } return m_grid[index]; } inline uint VirtualDesktopManager::maximum() { return 20; } -inline -uint VirtualDesktopManager::current() const -{ - return m_current; -} - inline uint VirtualDesktopManager::count() const { - return m_count; + return m_desktops.count(); } inline bool VirtualDesktopManager::isNavigationWrappingAround() const { return m_navigationWrapsAround; } inline void VirtualDesktopManager::setRootInfo(NETRootInfo *info) { m_rootInfo = info; } inline void VirtualDesktopManager::setConfig(KSharedConfig::Ptr config) { m_config = config; } inline const VirtualDesktopGrid &VirtualDesktopManager::grid() const { return m_grid; } template void VirtualDesktopManager::moveTo(bool wrap) { Direction functor; setCurrent(functor(0, wrap)); } } // namespace KWin #endif