[plasma/plasma-workspace] shell: ScreenPool: unsigned int for idForName as std::optional

Nate Graham <[email protected]>
Newsgroups gmane.comp.kde.cvs
Message-ID <[email protected]>
Git commit 1b547c5736c50b6f05acfeb221ec37c8cd9826b8 by Nate Graham, on behalf of Marco Martin.
Committed on 21/07/2026 at 13:45.
Pushed by ngraham into branch 'master'.

ScreenPool: unsigned int for idForName as std::optional

finish the unsigned migration having this also for ScreenPool.

since we can always pass invalid names to idForName, make it a std::optional

checking at every usage the optional has actually a value

M  +5    -1    shell/panelview.cpp
M  +14   -9    shell/screenpool.cpp
M  +3    -3    shell/screenpool.h
M  +1    -1    shell/scripting/scriptengine_v1.cpp
M  +29   -15   shell/shellcorona.cpp
M  +14   -5    shell/strutmanager.cpp

https://invent.kde.org/plasma/plasma-workspace/-/commit/1b547c5736c50b6f05acfeb221ec37c8cd9826b8

diff --git a/shell/panelview.cpp b/shell/panelview.cpp
index dd8c367693..013538ab42 100644
--- a/shell/panelview.cpp
+++ b/shell/panelview.cpp
@@ -1080,7 +1080,11 @@ void PanelView::showEvent(QShowEvent *event)
 void PanelView::moveToScreen(QScreen *screen)
 {
     if (containment()) {
-        const int screenId = m_corona->screenPool()->idForScreen(screen);
+        const auto screenOpt = m_corona->screenPool()->idForScreen(screen);
+        if (!screenOpt.has_value()) {
+            return;
+        }
+        const int screenId = screenOpt.value();
         containment()->setScreen(screenId);
     }
     setScreenToFollow(screen);
diff --git a/shell/screenpool.cpp b/shell/screenpool.cpp
index 16a7d55c72..757f817a88 100644
--- a/shell/screenpool.cpp
+++ b/shell/screenpool.cpp
@@ -35,16 +35,16 @@ ScreenPool::ScreenPool(QObject *parent)
 
 ScreenPool::~ScreenPool() = default;
 
-int ScreenPool::idForName(const QString &connector) const
+std::optional<uint> ScreenPool::idForName(const QString &connector) const
 {
-    int i = 0;
+    uint i = 0;
     for (auto *s : m_availableScreens) {
         if (s->name() == connector) {
             return i;
         }
         ++i;
     }
-    return -1;
+    return {};
 }
 
 QList<QScreen *> ScreenPool::screenOrder() const
@@ -89,9 +89,9 @@ void ScreenPool::handleUpdate()
         return;
     }
 
-    QHash<QString, int> order;
+    QHash<QString, uint> order;
     order.reserve(outputOrder.count());
-    for (int i = 0; i < outputOrder.count(); i++) {
+    for (uint i = 0; i < outputOrder.count(); i++) {
         order.insert(outputOrder[i], i);
     }
 
@@ -109,18 +109,23 @@ void ScreenPool::handleUpdate()
     }
 }
 
-QScreen *ScreenPool::screenForId(int id) const
+QScreen *ScreenPool::screenForId(uint id) const
 {
-    if (id < 0 || m_availableScreens.size() <= id) {
+    if (m_availableScreens.size() <= id) {
         return nullptr;
     }
 
     return m_availableScreens[id];
 }
 
-int ScreenPool::idForScreen(const QScreen *screen) const
+std::optional<uint> ScreenPool::idForScreen(const QScreen *screen) const
 {
-    return m_availableScreens.indexOf(screen);
+    const int idx = m_availableScreens.indexOf(screen);
+    if (idx >= 0) {
+        return uint(idx);
+    }
+
+    return {};
 }
 
 QDebug operator<<(QDebug debug, const ScreenPool *pool)
diff --git a/shell/screenpool.h b/shell/screenpool.h
index 7d9aa32006..339fe654c9 100644
--- a/shell/screenpool.h
+++ b/shell/screenpool.h
@@ -34,10 +34,10 @@ public:
     explicit ScreenPool(QObject *parent = nullptr);
     ~ScreenPool() override;
 
-    int idForName(const QString &connector) const;
-    int idForScreen(const QScreen *screen) const;
+    std::optional<uint> idForName(const QString &connector) const;
+    std::optional<uint> idForScreen(const QScreen *screen) const;
 
-    QScreen *screenForId(int id) const;
+    QScreen *screenForId(uint id) const;
 
     QList<QScreen *> screenOrder() const;
     QScreen *primaryScreen() const;
diff --git a/shell/scripting/scriptengine_v1.cpp b/shell/scripting/scriptengine_v1.cpp
index 9455429822..4ddbc9f1dd 100644
--- a/shell/scripting/scriptengine_v1.cpp
+++ b/shell/scripting/scriptengine_v1.cpp
@@ -208,7 +208,7 @@ QJSValue ScriptEngine::V1::screenForConnector(const QJSValue &param) const
     const QString connector = param.toString();
     auto *sc = qobject_cast<ShellCorona *>(m_engine->m_corona);
     if (sc) {
-        return m_engine->toScriptValue<int>(sc->screenPool()->idForName(connector));
+        return m_engine->toScriptValue<int>(sc->screenPool()->idForName(connector).value_or(-1));
     }
     return m_engine->toScriptValue<int>(-1);
 }
diff --git a/shell/shellcorona.cpp b/shell/shellcorona.cpp
index 867c787cc4..82a550a58b 100644
--- a/shell/shellcorona.cpp
+++ b/shell/shellcorona.cpp
@@ -881,7 +881,9 @@ void ShellCorona::screenInvariants() const
 
     QSet<QScreen *> screens;
     for (QScreen *knownScreen : managedScreens) {
-        const uint id = m_screenPool->idForScreen(knownScreen);
+        const auto idOpt = m_screenPool->idForScreen(knownScreen);
+        Q_ASSERT_X(idOpt.has_value(), Q_FUNC_INFO, qUtf8Printable(debugMessage()));
+        const uint id = idOpt.value();
         const DesktopView *view = desktopForScreen(knownScreen);
         Q_ASSERT_X(view->isVisible(), Q_FUNC_INFO, qUtf8Printable(debugMessage()));
         QScreen *screen = view->screenToFollow();
@@ -1380,7 +1382,11 @@ DesktopView *ShellCorona::desktopForScreen(QScreen *screen) const
     if (!screen) {
         return nullptr;
     }
-    if (auto *v = m_desktopViewForScreen.value(m_screenPool->idForScreen(screen))) {
+    const auto id = m_screenPool->idForScreen(screen);
+    if (!id.has_value()) {
+        return nullptr;
+    }
+    if (auto *v = m_desktopViewForScreen.value(id.value())) {
         return v;
     }
 
@@ -1462,23 +1468,29 @@ void ShellCorona::handleScreenOrderChanged(QList<QScreen *> screens)
 void ShellCorona::addOutput(QScreen *screen)
 {
     Q_ASSERT(screen);
+    if (!m_screenPool->idForScreen(screen).has_value()) {
+        return;
+    }
+    uint insertPosition = m_screenPool->idForScreen(screen).value();
+
     if (desktopForScreen(screen)) {
-        Q_EMIT screenAdded(m_screenPool->idForScreen(screen));
+        Q_EMIT screenAdded(insertPosition);
         return;
     }
     Q_ASSERT(!screen->geometry().isNull());
 
-    int insertPosition = m_screenPool->idForScreen(screen);
-    Q_ASSERT(insertPosition >= 0);
-
     auto *view = new DesktopView(this, screen);
 
     if (view->rendererInterface()->graphicsApi() != QSGRendererInterface::Software) {
         connect(view, &QQuickWindow::sceneGraphError, this, &ShellCorona::glInitializationFailed);
     }
     connect(view, &DesktopView::geometryChanged, this, [this, view]() {
-        const int id = m_screenPool->idForScreen(view->screen());
-        if (id >= 0 && !m_screenReorderInProgress) {
+        const auto idOpt = m_screenPool->idForScreen(view->screen());
+        if (!idOpt.has_value()) {
+            return;
+        }
+        const uint id = m_screenPool->idForScreen(view->screen()).value();
+        if (!m_screenReorderInProgress) {
             Q_EMIT screenGeometryChanged(id);
             Q_EMIT availableScreenRectChanged(id);
         }
@@ -1502,9 +1514,9 @@ void ShellCorona::addOutput(QScreen *screen)
     connect(containment, &Plasma::Containment::uiReadyChanged, this, &ShellCorona::checkAllDesktopsUiReady);
 
     if (!m_screenReorderInProgress) {
-        Q_EMIT availableScreenRectChanged(m_screenPool->idForScreen(screen));
+        Q_EMIT availableScreenRectChanged(insertPosition);
     }
-    Q_EMIT screenAdded(m_screenPool->idForScreen(screen));
+    Q_EMIT screenAdded(insertPosition);
 }
 
 void ShellCorona::checkAllDesktopsUiReady()
@@ -2581,7 +2593,7 @@ Plasma::Containment *ShellCorona::addPanel(const QString &plugin)
     }
 
     Q_ASSERT(panel);
-    const int screen = std::max(0, m_screenPool->idForScreen(wantedScreen));
+    const int screen = m_screenPool->idForScreen(wantedScreen).value_or(0);
     panel->setScreen(screen);
     m_waitingPanels << panel;
     if (m_screensWithUiReady.contains(screen)) {
@@ -2943,8 +2955,8 @@ void ShellCorona::activateLauncherMenu(const QString &screenName)
         return false;
     };
 
-    const int rawId = m_screenPool->idForName(screenName);
-    const uint screenId = rawId >= 0 ? uint(rawId) : 0;
+    const auto screenOpt = m_screenPool->idForName(screenName);
+    const uint screenId = screenOpt.value_or(0);
 
     QList<Plasma::Containment *> conts = containments();
 
@@ -2998,13 +3010,15 @@ bool ShellCorona::grabContainmentImage(const QString &name, int width, int heigh
 {
     Q_ASSERT(calledFromDBus());
 
-    auto screenId = m_screenPool->idForName(name);
-    if (screenId < 0) {
+    const auto screenIdOpt = m_screenPool->idForName(name);
+    if (!screenIdOpt.has_value()) {
         qCWarning(PLASMASHELL) << "grabContainmentImage: unknown screen name" << name;
         sendErrorReply(QDBusError::InvalidArgs, QStringLiteral("Unknown screen name"));
         return false;
     }
 
+    const uint screenId = screenIdOpt.value();
+
     auto currentActivity = m_activityController->currentActivity();
     currentActivity = QUuid::fromString(currentActivity).isNull() ? QString() : currentActivity;
 
diff --git a/shell/strutmanager.cpp b/shell/strutmanager.cpp
index 128559e96a..cf710c9f9f 100644
--- a/shell/strutmanager.cpp
+++ b/shell/strutmanager.cpp
@@ -51,7 +51,7 @@ QRect StrutManager::availableScreenRect(int id) const
 
 QRect StrutManager::availableScreenRect(const QString &screenName) const
 {
-    return availableScreenRect(m_plasmashellCorona->screenPool()->idForName(screenName));
+    return availableScreenRect(m_plasmashellCorona->screenPool()->idForName(screenName).value_or(0));
 }
 
 QRegion StrutManager::availableScreenRegion(int id) const
@@ -67,8 +67,12 @@ QRegion StrutManager::availableScreenRegion(int id) const
 
 void StrutManager::setAvailableScreenRect(const QString &service, const QString &screenName, const QRect &rect)
 {
-    int id = m_plasmashellCorona->screenPool()->idForName(screenName);
-    if (id == -1 || m_availableScreenRects.value(service).value(id) == rect || !addWatchedService(service)) {
+    const auto idOpt = m_plasmashellCorona->screenPool()->idForName(screenName);
+    if (!idOpt.has_value()) {
+        return;
+    }
+    const uint id = idOpt.value();
+    if (m_availableScreenRects.value(service).value(id) == rect || !addWatchedService(service)) {
         return;
     }
     m_availableScreenRects[service][id] = rect;
@@ -77,13 +81,18 @@ void StrutManager::setAvailableScreenRect(const QString &service, const QString
 
 void StrutManager::setAvailableScreenRegion(const QString &service, const QString &screenName, const QList<QRect> &rects)
 {
-    int id = m_plasmashellCorona->screenPool()->idForName(screenName);
+    const auto idOpt = m_plasmashellCorona->screenPool()->idForName(screenName);
+    if (!idOpt.has_value()) {
+        return;
+    }
+    const uint id = idOpt.value();
+
     QRegion region;
     for (const QRect &rect : rects) {
         region += rect;
     }
 
-    if (id == -1 || m_availableScreenRegions.value(service).value(id) == region || !addWatchedService(service)) {
+    if (m_availableScreenRegions.value(service).value(id) == region || !addWatchedService(service)) {
         return;
     }
     m_availableScreenRegions[service][id] = region;
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.