From c21ad4edcc5612e2961cdaab1c60306ba665c757 Mon Sep 17 00:00:00 2001 From: NiLuJe Date: Fri, 16 Aug 2024 04:47:51 +0200 Subject: [PATCH] SortWidget: Cleaner FocusManager behavior With less code, yay. --- frontend/ui/widget/sortwidget.lua | 34 ++++++++++++++++--------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/frontend/ui/widget/sortwidget.lua b/frontend/ui/widget/sortwidget.lua index c083c30d3..3d0a9c8b9 100644 --- a/frontend/ui/widget/sortwidget.lua +++ b/frontend/ui/widget/sortwidget.lua @@ -349,9 +349,9 @@ function SortWidget:nextPage() if self.show_page < self.pages then self.show_page = self.show_page + 1 if self.marked > 0 then -- put selected item first in the page - self:moveItem(self.items_per_page * (self.show_page - 1) + 1 - self.marked, true) + self:moveItem(self.items_per_page * (self.show_page - 1) + 1 - self.marked) else - self:_populateItems(true) + self:_populateItems() end end end @@ -360,19 +360,19 @@ function SortWidget:prevPage() if self.show_page > 1 then self.show_page = self.show_page - 1 if self.marked > 0 then -- put selected item first in the page - self:moveItem(self.items_per_page * (self.show_page - 1) + 1 - self.marked, true) + self:moveItem(self.items_per_page * (self.show_page - 1) + 1 - self.marked) else - self:_populateItems(true) + self:_populateItems() end end end function SortWidget:goToPage(page) self.show_page = page - self:_populateItems(true) + self:_populateItems() end -function SortWidget:moveItem(diff, reset_focus) +function SortWidget:moveItem(diff) local move_to = self.marked + diff if move_to > 0 and move_to <= #self.item_table then -- Remember the original state to support Cancel @@ -380,15 +380,14 @@ function SortWidget:moveItem(diff, reset_focus) self.orig_item_table = util.tableDeepCopy(self.item_table) end table.insert(self.item_table, move_to, table.remove(self.item_table, self.marked)) - local current_page = self.show_page self.show_page = math.ceil(move_to / self.items_per_page) self.marked = move_to - self:_populateItems(reset_focus or current_page ~= self.show_page) + self:_populateItems() end end -- make sure self.item_margin and self.item_height are set before calling this -function SortWidget:_populateItems(reset_focus) +function SortWidget:_populateItems() self.main_content:clear() self.layout = { self.layout[#self.layout] } -- keep footer local idx_offset = (self.show_page - 1) * self.items_per_page @@ -415,12 +414,15 @@ function SortWidget:_populateItems(reset_focus) table.insert(self.layout, #self.layout, {item}) table.insert(self.main_content, item) end - -- Reset the focus on page changes (#12342) - -- NOTE: We can't really know whether a page turn button was clicked via a key or a touch, - -- so this *will* also move the focus to the top of the item list instead of re-focusing the footer - -- when moving an item and changing pages via the button... - if reset_focus then - self:moveFocusTo(1, 1) -- reset selected for the re-created layout + if self.marked == 0 then + -- Reset the focus to the top of the page when we're not moving an item (#12342) + self:moveFocusTo(1, 1) + else + -- When we're moving an item, move the focus to the footer (last row), + -- while keeping the focus on the current button (or cancel for the initial move, + -- as there's only one column of items, so x == 1, which points to the first button, which is cancel). + -- even when we change pages and the amount of rows may have changed + self:moveFocusTo(self.selected.x, #self.layout) end -- NOTE: We forgo our usual "Page x of y" wording because of space constraints given the way the widget is currently built @@ -542,7 +544,7 @@ function SortWidget:sortItems(collate, reverse_collate) table.sort(self.item_table, sort_func) self.show_page = 1 self.marked = 1 -- enable cancel button - self:_populateItems(true) + self:_populateItems() end function SortWidget:onClose()