Skip to content

[SortWidget] NT: add new keyEvents to widget#13154

Merged
Frenzie merged 3 commits into
koreader:masterfrom
Commodore64user:sort-shortcuts
Jan 29, 2025
Merged

[SortWidget] NT: add new keyEvents to widget#13154
Frenzie merged 3 commits into
koreader:masterfrom
Commodore64user:sort-shortcuts

Conversation

@Commodore64user

@Commodore64user Commodore64user commented Jan 27, 2025

Copy link
Copy Markdown
Member

what's new

just a few QoL changes to the SortWidget class for non-touch users

  • Renamed goToPage to onGoToPage and updated all references to this method.
  • Introduced a new method registerKeyEvents to handle the registration of key events.
  • Moved the key event registration logic from init to the new registerKeyEvents method.
  • Introduced the onMoveItemKB method to handle item movement via keyboard events, ensuring the marked item is updated correctly.
  • Adjusted the focus management in _populateItems to ensure the focus moves to the correct item after an item move operation.

This change is Reviewable

@Frenzie Frenzie added the NT Non Touch devices label Jan 29, 2025
Comment thread frontend/ui/widget/sortwidget.lua Outdated

function SortWidget:moveItem(diff)
function SortWidget:onMoveItem(diff)
if self.marked == 0 then return end -- don't crash when called with key events on NT

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I played around with it a bit and I thought something like this might be nicer. It gets rid of the somewhat awkward touch-like approach and simply moves things around based on current focus.

diff --git a/frontend/ui/widget/sortwidget.lua b/frontend/ui/widget/sortwidget.lua
index f4b4c6edf..5535b100e 100644
--- a/frontend/ui/widget/sortwidget.lua
+++ b/frontend/ui/widget/sortwidget.lua
@@ -353,13 +353,13 @@ function SortWidget:registerKeyEvents()
         self.key_events.PrevPage = { { Device.input.group.PgBack } }
         self.key_events.ShowWidgetMenu = { { "Menu" } }
         if Device:hasScreenKB() then
-            self.key_events.MoveUp = { { "ScreenKB", "Up" }, event = "MoveItem", args = -1 }
-            self.key_events.MoveDown = { { "ScreenKB", "Down" }, event = "MoveItem", args = 1 }
+            self.key_events.MoveUp = { { "ScreenKB", "Up" }, event = "MoveItemKB", args = -1 }
+            self.key_events.MoveDown = { { "ScreenKB", "Down" }, event = "MoveItemKB", args = 1 }
             self.key_events.FirstPage = { { "ScreenKB", Device.input.group.PgBack }, event = "GoToPage", args = 1 }
             self.key_events.LastPage = { { "ScreenKB", Device.input.group.PgFwd }, event = "GoToPage", args = self.pages }
         elseif Device:hasKeyboard() then
-            self.key_events.MoveUp = { { "Shift", "Up" }, event = "MoveItem", args = -1 }
-            self.key_events.MoveDown = { { "Shift", "Down" }, event = "MoveItem", args = 1 }
+            self.key_events.MoveUp = { { "Shift", "Up" }, event = "MoveItemKB", args = -1 }
+            self.key_events.MoveDown = { { "Shift", "Down" }, event = "MoveItemKB", args = 1 }
             self.key_events.FirstPage = { { "Shift", Device.input.group.PgBack }, event = "GoToPage", args = 1 }
             self.key_events.LastPage = { { "Shift", Device.input.group.PgFwd }, event = "GoToPage", args = self.pages }
         end
@@ -394,7 +394,6 @@ function SortWidget:onGoToPage(page)
 end
 
 function SortWidget:onMoveItem(diff)
-    if self.marked == 0 then return end -- don't crash when called with key events on NT
     local move_to = self.marked + diff
     if move_to > 0 and move_to <= #self.item_table then
         -- Remember the original state to support Cancel
@@ -408,6 +407,12 @@ function SortWidget:onMoveItem(diff)
     end
 end
 
+function SortWidget:onMoveItemKB(diff)
+    -- set self.marked to the item with focus
+    self.marked = self.selected.y + (self.show_page - 1) * self.items_per_page
+    self:onMoveItem(diff)
+end
+
 -- make sure self.item_margin and self.item_height are set before calling this
 function SortWidget:_populateItems()
     self.main_content:clear()
@@ -440,11 +445,8 @@ function SortWidget:_populateItems()
         -- 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)
+        -- Move focus to the moved item.
+        self:moveFocusTo(1, self.marked - idx_offset)
     end
 
     -- NOTE: We forgo our usual "Page x of y" wording because of space constraints given the way the widget is currently built

@Frenzie Frenzie added this to the 2025.01 milestone Jan 29, 2025

@Frenzie Frenzie left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm: but when I look at the unified changes on GH it seems to ignore part of the second commit O.o

Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on @Commodore64user)

@Frenzie Frenzie merged commit bc7ef61 into koreader:master Jan 29, 2025
@Frenzie

Frenzie commented Jan 29, 2025

Copy link
Copy Markdown
Member

Edit: never mind, I was confused by onGoToPage, is all. It's past midnight. :-)

@Commodore64user Commodore64user deleted the sort-shortcuts branch January 29, 2025 23:28
@Frenzie

Frenzie commented Feb 1, 2025

Copy link
Copy Markdown
Member

Something about this may not be quite right, or in any case trying to switch pages seems to crash.

@Commodore64user

Copy link
Copy Markdown
Member Author

I am not getting any crashes, I'm assuming you mean page turns inside the widget, which they are fine on my end.

@Frenzie

Frenzie commented Feb 1, 2025

Copy link
Copy Markdown
Member

The good (?) news is that it was already an issue, but only in arrange statusbar items. That explains why I didn't notice it previously wrt this PR since I tested it (even if only summarily) on favorites or collections.

@Commodore64user

Copy link
Copy Markdown
Member Author

I still don't understand what the problem is... i had tested it with multiple pages (when i over engineered the separator thing, i had like 4 pages worth of them) and it worked fine. No crashes. I actually think the self.pages ones isn't working, but unsure why since it also worked before.

Going through the "arrange items" thing, made me realise that we should also have shortcuts for back and select when something is selected, but that is another matter.

@Frenzie

Frenzie commented Feb 1, 2025

Copy link
Copy Markdown
Member

See #13186 it's probably not related to SortWidget at all.

0xstillb pushed a commit to 0xstillb/koreader-thai that referenced this pull request May 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

NT Non Touch devices

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants