SortWidget: Cleaner FocusManager behavior

With less code, yay.
reviewable/pr12372/r1
NiLuJe 1 month ago
parent 0ae11f8cf4
commit c21ad4edcc

@ -349,9 +349,9 @@ function SortWidget:nextPage()
if self.show_page < self.pages then if self.show_page < self.pages then
self.show_page = self.show_page + 1 self.show_page = self.show_page + 1
if self.marked > 0 then -- put selected item first in the page 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 else
self:_populateItems(true) self:_populateItems()
end end
end end
end end
@ -360,19 +360,19 @@ function SortWidget:prevPage()
if self.show_page > 1 then if self.show_page > 1 then
self.show_page = self.show_page - 1 self.show_page = self.show_page - 1
if self.marked > 0 then -- put selected item first in the page 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 else
self:_populateItems(true) self:_populateItems()
end end
end end
end end
function SortWidget:goToPage(page) function SortWidget:goToPage(page)
self.show_page = page self.show_page = page
self:_populateItems(true) self:_populateItems()
end end
function SortWidget:moveItem(diff, reset_focus) function SortWidget:moveItem(diff)
local move_to = self.marked + diff local move_to = self.marked + diff
if move_to > 0 and move_to <= #self.item_table then if move_to > 0 and move_to <= #self.item_table then
-- Remember the original state to support Cancel -- 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) self.orig_item_table = util.tableDeepCopy(self.item_table)
end end
table.insert(self.item_table, move_to, table.remove(self.item_table, self.marked)) 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.show_page = math.ceil(move_to / self.items_per_page)
self.marked = move_to self.marked = move_to
self:_populateItems(reset_focus or current_page ~= self.show_page) self:_populateItems()
end end
end end
-- make sure self.item_margin and self.item_height are set before calling this -- 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.main_content:clear()
self.layout = { self.layout[#self.layout] } -- keep footer self.layout = { self.layout[#self.layout] } -- keep footer
local idx_offset = (self.show_page - 1) * self.items_per_page 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.layout, #self.layout, {item})
table.insert(self.main_content, item) table.insert(self.main_content, item)
end end
-- Reset the focus on page changes (#12342) if self.marked == 0 then
-- NOTE: We can't really know whether a page turn button was clicked via a key or a touch, -- Reset the focus to the top of the page when we're not moving an item (#12342)
-- so this *will* also move the focus to the top of the item list instead of re-focusing the footer self:moveFocusTo(1, 1)
-- when moving an item and changing pages via the button... else
if reset_focus then -- When we're moving an item, move the focus to the footer (last row),
self:moveFocusTo(1, 1) -- reset selected for the re-created layout -- 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 end
-- NOTE: We forgo our usual "Page x of y" wording because of space constraints given the way the widget is currently built -- 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) table.sort(self.item_table, sort_func)
self.show_page = 1 self.show_page = 1
self.marked = 1 -- enable cancel button self.marked = 1 -- enable cancel button
self:_populateItems(true) self:_populateItems()
end end
function SortWidget:onClose() function SortWidget:onClose()

Loading…
Cancel
Save