summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main.lua46
1 files changed, 21 insertions, 25 deletions
diff --git a/main.lua b/main.lua
index ffb7707..2c9e405 100644
--- a/main.lua
+++ b/main.lua
@@ -473,11 +473,9 @@ local function set_menu_sort(menu, bool)
end
-- refresh options when navigating up the stack to a previous favourites menu.
--- existing menu options are never removed, even if unfavourited. if the order
--- of still-favourited items is unchanged and no new favourites have been added
--- before the last still-favourited item, the existing order is kept and all
--- new favourites are appended to the end of the menu. otherwise, the new order
--- is used and any unfavourited options are moved to the end.
+-- existing menu options are never removed, even if unfavourited. the new order
+-- is always respected, while still preserving the relative order of existing
+-- options when possible.
local function refresh_favourites_menu()
local menu = menus[depth]
local opt = menu.options[menu.cursor]
@@ -487,36 +485,34 @@ local function refresh_favourites_menu()
end
local options = group_menu_options(catalogue:get(menu.group_id))
+ local pos = {}
+ for i, v in ipairs(options) do
+ pos[v.id] = i
+ end
+
+ local res = {}
local seen = {}
- for _, v in ipairs(options) do
- seen[v.id] = true
+ local function append(v)
+ if not seen[v.id] then
+ res[#res+1] = v
+ seen[v.id] = true
+ end
end
- local same = true
local ind = 1
for _, v in ipairs(menu.options) do
- if seen[v.id] then
- if v.id == options[ind].id then
+ if pos[v.id] then
+ while ind <= pos[v.id] do
+ append(options[ind])
ind = ind + 1
- else
- same = false
- break
end
end
+ append(v)
end
-
- if same then
- for i = ind, #options do
- menu.options[#menu.options+1] = options[i]
- end
- else
- for _, v in ipairs(menu.options) do
- if not seen[v.id] then
- options[#options+1] = v
- end
- end
- menu.options = options
+ for i = ind, #options do
+ append(options[i])
end
+ menu.options = res
if sorted then
set_menu_sort(menu, true)