diff --git a/files/builtin_scripts/scripts/omw/settings/common.lua b/files/builtin_scripts/scripts/omw/settings/common.lua index 96bfd747a2..51e22b4465 100644 --- a/files/builtin_scripts/scripts/omw/settings/common.lua +++ b/files/builtin_scripts/scripts/omw/settings/common.lua @@ -40,6 +40,9 @@ local function validateGroupOptions(options) if type(options.page) ~= 'string' then error('Group must belong to a page') end + if type(options.order) ~= 'number' and type(options.order) ~= 'nil' then + error('Group order must be a number') + end if type(options.l10n) ~= 'string' then error('Group must have a localization context') end @@ -78,6 +81,7 @@ local function registerGroup(options) local group = { key = options.key, page = options.page, + order = options.order or 0, l10n = options.l10n, name = options.name, description = options.description, diff --git a/files/builtin_scripts/scripts/omw/settings/render.lua b/files/builtin_scripts/scripts/omw/settings/render.lua index ba537b2099..95b4bf2ff1 100644 --- a/files/builtin_scripts/scripts/omw/settings/render.lua +++ b/files/builtin_scripts/scripts/omw/settings/render.lua @@ -154,6 +154,12 @@ local function renderGroup(group, global) return layout end +local function pageGroupComparator(a, b) + return a.order < b.order or ( + a.order == b.order and a.key < b.key + ) +end + local function renderPage(page) local l10n = core.l10n(page.l10n) local layout = { @@ -197,7 +203,11 @@ local function renderPage(page) }, } local groupsContent = layout.content.groups.content - for _, pageGroup in ipairs(groups[page.key]) do + local pageGroups = groups[page.key] + local sortedGroups = {} + for i, v in ipairs(pageGroups) do sortedGroups[i] = v end + table.sort(sortedGroups, pageGroupComparator) + for _, pageGroup in ipairs(sortedGroups) do local group = common.getSection(pageGroup.global, common.groupSectionKey):get(pageGroup.key) groupsContent:add(renderGroup(group, pageGroup.global)) end @@ -224,17 +234,25 @@ end local function onGroupRegistered(global, key) local group = common.getSection(global, common.groupSectionKey):get(key) groups[group.page] = groups[group.page] or {} - table.insert(groups[group.page], { + local pageGroup = { key = group.key, global = global, - }) + order = group.order, + } + table.insert(groups[group.page], pageGroup) common.getSection(global, group.key):subscribe(onSettingChanged(global)) + local index = 1 + for _, g in ipairs(groups[group.page]) do + if pageGroupComparator(pageGroup, g) then + index = index + 1 + end + end + if not pageOptions[group.page] then return end local element = pageOptions[group.page].element local groupsLayout = element.layout.content.groups - -- TODO: make group order deterministic - groupsLayout.content:add(renderGroup(group, global)) + groupsLayout.content:insert(index, renderGroup(group, global)) element:update() end local globalGroups = storage.globalSection(common.groupSectionKey)