Skip to content

Commit 7698584

Browse files
committed
Fix potential invalid index when restoring layout
We save the index of each FloatingWindow when saving a layout, but when restoring we might not want to restore all FloatingWindows, for example, if we use a LayoutSaver with another affinity. So, the index in Position::deserialize() should be a index to LayoutSaver::Layout::floatingWindows, and not to DockRegistry::self()->floatingWindows() since the later might be smaller.
1 parent d034722 commit 7698584

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

Diff for: src/LayoutSaver.cpp

+15-1
Original file line numberDiff line numberDiff line change
@@ -258,14 +258,15 @@ bool LayoutSaver::restoreLayout(const QByteArray &data)
258258
}
259259

260260
// 2. Restore FloatingWindows
261-
for (const LayoutSaver::FloatingWindow &fw : qAsConst(layout.floatingWindows)) {
261+
for (LayoutSaver::FloatingWindow &fw : layout.floatingWindows) {
262262
if (!d->matchesAffinity(fw.affinities))
263263
continue;
264264

265265
MainWindowBase *parent = fw.parentIndex == -1 ? nullptr
266266
: DockRegistry::self()->mainwindows().at(fw.parentIndex);
267267

268268
auto floatingWindow = Config::self().frameworkWidgetFactory()->createFloatingWindow(parent);
269+
fw.floatingWindowInstance = floatingWindow;
269270
d->deserializeWindowGeometry(fw, floatingWindow);
270271
if (!floatingWindow->deserialize(fw)) {
271272
qWarning() << Q_FUNC_INFO << "Failed to deserialize floating window";
@@ -471,6 +472,19 @@ LayoutSaver::MainWindow LayoutSaver::Layout::mainWindowForIndex(int index) const
471472
return mainWindows.at(index);
472473
}
473474

475+
LayoutSaver::FloatingWindow LayoutSaver::Layout::floatingWindowForIndex(int index) const
476+
{
477+
if (index < 0 || index >= floatingWindows.size())
478+
return {};
479+
480+
return floatingWindows.at(index);
481+
}
482+
483+
FloatingWindow *LayoutSaver::Layout::floatingWindowInstanceForIndex(int index) const
484+
{
485+
return floatingWindowForIndex(index).floatingWindowInstance;
486+
}
487+
474488
QStringList LayoutSaver::Layout::mainWindowNames() const
475489
{
476490
QStringList names;

Diff for: src/LayoutSaver_p.h

+7
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@
3636

3737
namespace KDDockWidgets {
3838

39+
class FloatingWindow;
40+
3941
template <typename T>
4042
typename T::List fromVariantList(const QVariantList &listV)
4143
{
@@ -233,6 +235,9 @@ struct LayoutSaver::FloatingWindow
233235
int screenIndex;
234236
QSize screenSize; // for relative-size restoring
235237
bool isVisible = true;
238+
239+
// The instance that was created during a restore:
240+
KDDockWidgets::FloatingWindow *floatingWindowInstance = nullptr;
236241
};
237242

238243
struct LayoutSaver::MainWindow
@@ -313,6 +318,8 @@ struct LayoutSaver::Layout
313318
static LayoutSaver::Layout* s_currentLayoutBeingRestored;
314319

315320
LayoutSaver::MainWindow mainWindowForIndex(int index) const;
321+
LayoutSaver::FloatingWindow floatingWindowForIndex(int index) const;
322+
KDDockWidgets::FloatingWindow* floatingWindowInstanceForIndex(int index) const;
316323

317324
QStringList mainWindowNames() const;
318325
QStringList dockWidgetNames() const;

Diff for: src/private/Position.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,8 @@ void Position::deserialize(const LayoutSaver::Position &lp)
126126
if (index == -1) {
127127
continue; // Skip
128128
} else {
129-
const auto floatingWindows = DockRegistry::self()->floatingWindows();
130-
if (index >= 0 && index < floatingWindows.size()) {
131-
FloatingWindow *fw = floatingWindows.at(index);
129+
FloatingWindow *fw = LayoutSaver::Layout::s_currentLayoutBeingRestored->floatingWindowInstanceForIndex(index);
130+
if (fw) {
132131
layout = fw->multiSplitter();
133132
} else {
134133
qWarning() << "Invalid floating window position to restore" << index;

0 commit comments

Comments
 (0)