From ca1c9da41c86ac72869f8b203352bee2324ac99a Mon Sep 17 00:00:00 2001 From: idotobi <16611056+idotobi@users.noreply.github.com> Date: Sat, 27 Mar 2021 16:41:04 +0100 Subject: [PATCH] Fix off-by-one error in toolbar customization Fix the behaviour that any addition to the toolbar during customization would show one position further to the right after reopening the application. (Notable exception: the very first position) --- src/gui/toolbarMenubar/model/ToolbarEntry.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/gui/toolbarMenubar/model/ToolbarEntry.cpp b/src/gui/toolbarMenubar/model/ToolbarEntry.cpp index a7410f6a..fcf3fe46 100644 --- a/src/gui/toolbarMenubar/model/ToolbarEntry.cpp +++ b/src/gui/toolbarMenubar/model/ToolbarEntry.cpp @@ -30,9 +30,7 @@ ToolbarEntry& ToolbarEntry::operator=(ToolbarEntry&& e) { ToolbarEntry::~ToolbarEntry() { clearList(); } void ToolbarEntry::clearList() { - for (ToolbarItem* item: entries) { - delete item; - } + for (ToolbarItem* item: entries) { delete item; } entries.clear(); } @@ -60,6 +58,13 @@ auto ToolbarEntry::removeItemById(int id) -> bool { auto ToolbarEntry::insertItem(string item, int position) -> int { auto* it = new ToolbarItem(std::move(item)); + // This is required as the position generated by gtk_toolbar_get_drop_index + // is 1 indexed with the notable exception that the beginning of the toolbar + // is still 0. + // Hence, the possible input positions are [0, 2, 3, 4, ...] which need to be mapped + // to [0, 1, 2, 3, ...] to work consistently with the the entries vector. + if (position >= 1) + position--; if (position >= static_cast(entries.size())) { entries.push_back(it); return it->getId();