diff --git a/shell/scripting/panel.cpp b/shell/scripting/panel.cpp --- a/shell/scripting/panel.cpp +++ b/shell/scripting/panel.cpp @@ -117,11 +117,13 @@ { int screenNum = qMax(screen(), 0); //if we don't have a screen (-1) we'll be put on screen 0 - if (QGuiApplication::screens().size() < screenNum) { + QScreen *s = corona()->screenForId(screenNum); + if (s == NULL) { return KConfigGroup(); } - QScreen *s = QGuiApplication::screens().at(screenNum); - return PanelView::panelConfig(corona(), containment(), s); + else { + return PanelView::panelConfig(corona(), containment(), s); + } } QString Panel::alignment() const diff --git a/shell/scripting/scriptengine.cpp b/shell/scripting/scriptengine.cpp --- a/shell/scripting/scriptengine.cpp +++ b/shell/scripting/scriptengine.cpp @@ -426,7 +426,14 @@ //that relies on it //NOTE: if we'll allow setting a panel screen from JS, it will have to use the following lines as well KConfigGroup cg=c->config(); - cg.writeEntry(QStringLiteral("lastScreen"), 0); + + // Only force lastScreen to 0 if the newly created containment doesn't have one, yet. + // Otherwise newly created panels would always be created on screen 0. + int lastScreen = cg.readEntry(QStringLiteral("lastScreen"), -1); + if (lastScreen < 0) { + cg.writeEntry(QStringLiteral("lastScreen"), 0); + } + c->restore(cg); } c->updateConstraints(Plasma::Types::AllConstraints | Plasma::Types::StartupCompletedConstraint); diff --git a/shell/shellcorona.h b/shell/shellcorona.h --- a/shell/shellcorona.h +++ b/shell/shellcorona.h @@ -113,6 +113,8 @@ QString defaultContainmentPlugin() const; + QScreen *screenForId(int screenNum); + Q_SIGNALS: void glInitializationFailed(); @@ -249,6 +251,7 @@ KWayland::Client::PlasmaShell *m_waylandPlasmaShell; bool m_closingDown : 1; + bool m_panelCreatedManually : 1; }; #endif // SHELLCORONA_H diff --git a/shell/shellcorona.cpp b/shell/shellcorona.cpp --- a/shell/shellcorona.cpp +++ b/shell/shellcorona.cpp @@ -95,7 +95,8 @@ m_addPanelsMenu(nullptr), m_interactiveConsole(nullptr), m_waylandPlasmaShell(nullptr), - m_closingDown(false) + m_closingDown(false), + m_panelCreatedManually(false) { setupWaylandIntegration(); qmlRegisterUncreatableType("org.kde.plasma.shell", 2, 0, "Desktop", QStringLiteral("It is not possible to create objects of type Desktop")); @@ -1786,13 +1787,15 @@ KPluginInfo::List panelPlugins = Plasma::PluginLoader::listContainmentsOfType(QStringLiteral("Panel")); if (!panelPlugins.isEmpty()) { + m_panelCreatedManually = true; addPanel(panelPlugins.first().pluginName()); } } void ShellCorona::addPanel(QAction *action) { const QString plugin = action->data().toString(); + m_panelCreatedManually = true; if (plugin.startsWith(QLatin1String("plasma-desktop-template:"))) { WorkspaceScripting::ScriptEngine scriptEngine(this); @@ -1814,7 +1817,21 @@ Plasma::Containment *ShellCorona::addPanel(const QString &plugin) { - Plasma::Containment *panel = createContainment(plugin); + int screenNum = -1; + + if (m_panelCreatedManually) { + m_panelCreatedManually = false; + + // find out current screen to create new panels on + foreach (QScreen *screen, QGuiApplication::screens()) { + if (screen->geometry().contains(QApplication::focusWindow()->geometry())) { + screenNum = m_screenPool->id(screen->name()); + break; + } + } + } + + Plasma::Containment *panel = createContainmentForScreen(screenNum, plugin); if (!panel) { return nullptr; } @@ -2006,6 +2023,11 @@ return m_desktopViewforId.keys(); } +QScreen *ShellCorona::screenForId(int screenNum) { + QScreen *screen = m_desktopViewforId.value(screenNum)->screenToFollow(); + return screen; +} + QString ShellCorona::defaultContainmentPlugin() const { QString plugin = m_lnfDefaultsConfig.readEntry("Containment", QString());