From b707d0f593a72c826230cc051170da8bb0842b20 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Sun, 16 Mar 2025 08:03:14 +0200 Subject: [PATCH] Menu widget: "Go to letter" everywhere (#13405) --- frontend/apps/filemanager/filemanager.lua | 3 + .../filemanager/filemanagercollection.lua | 3 + .../apps/filemanager/filemanagerhistory.lua | 4 + frontend/ui/widget/filechooser.lua | 2 +- frontend/ui/widget/menu.lua | 101 +++++++++--------- 5 files changed, 61 insertions(+), 52 deletions(-) diff --git a/frontend/apps/filemanager/filemanager.lua b/frontend/apps/filemanager/filemanager.lua index 97f85d7a7..94897adaa 100644 --- a/frontend/apps/filemanager/filemanager.lua +++ b/frontend/apps/filemanager/filemanager.lua @@ -149,6 +149,9 @@ function FileManager:setupLayout() filemanager = self, -- Tell FileChooser (i.e., Menu) to use our own title bar instead of Menu's default one custom_title_bar = self.title_bar, + search_callback = function(search_string) + self.filesearcher:onShowFileSearch(search_string) + end, } self.file_chooser = file_chooser self.focused_file = nil -- use it only once diff --git a/frontend/apps/filemanager/filemanagercollection.lua b/frontend/apps/filemanager/filemanagercollection.lua index 65dd5bbcd..241effa7c 100644 --- a/frontend/apps/filemanager/filemanagercollection.lua +++ b/frontend/apps/filemanager/filemanagercollection.lua @@ -92,6 +92,9 @@ function FileManagerCollection:onShowColl(collection_name) ui = self.ui, _manager = self, _recreate_func = function() self:onShowColl(collection_name) end, + search_callback = function(search_string) + self:onShowCollectionsSearchDialog(search_string, collection_name) + end, } table.insert(self.booklist_menu.paths, true) -- enable onReturn button self.booklist_menu.close_callback = function() diff --git a/frontend/apps/filemanager/filemanagerhistory.lua b/frontend/apps/filemanager/filemanagerhistory.lua index e68a2083e..4efdddcf1 100644 --- a/frontend/apps/filemanager/filemanagerhistory.lua +++ b/frontend/apps/filemanager/filemanagerhistory.lua @@ -68,6 +68,10 @@ function FileManagerHistory:onShowHist(search_info) ui = self.ui, _manager = self, _recreate_func = function() self:onShowHist(search_info) end, + search_callback = function(search_string) + self.search_string = search_string + self:onSearchHistory() + end, } self.booklist_menu.close_callback = function() self:refreshFileManager() diff --git a/frontend/ui/widget/filechooser.lua b/frontend/ui/widget/filechooser.lua index 53a3765b7..31873773d 100644 --- a/frontend/ui/widget/filechooser.lua +++ b/frontend/ui/widget/filechooser.lua @@ -73,7 +73,6 @@ local FileChooser = BookList:extend{ "^%.metadata%.json$", }, path_items = nil, -- hash, store last browsed location (item index) for each path - goto_letter = true, } -- Cache of content we knew of for directories that are not readable @@ -186,6 +185,7 @@ function FileChooser:getListItem(dirpath, f, fullpath, attributes, collate) else item.text = item.text.."/" item.bidi_wrap_func = BD.directory + item.is_file = false if collate.can_collate_mixed and collate.item_func ~= nil then collate.item_func(item) end diff --git a/frontend/ui/widget/menu.lua b/frontend/ui/widget/menu.lua index 8d3cb08c4..96890d11f 100644 --- a/frontend/ui/widget/menu.lua +++ b/frontend/ui/widget/menu.lua @@ -4,7 +4,6 @@ local BottomContainer = require("ui/widget/container/bottomcontainer") local Button = require("ui/widget/button") local CenterContainer = require("ui/widget/container/centercontainer") local Device = require("device") -local Event = require("ui/event") local FocusManager = require("ui/widget/focusmanager") local Font = require("ui/font") local FrameContainer = require("ui/widget/container/framecontainer") @@ -761,8 +760,34 @@ function Menu:init() self.page_info_first_chev:hide() self.page_info_last_chev:hide() - local title_goto, type_goto, hint_func local buttons = { + { + { + text = self.search_callback and _("Search…") or _("Search"), + callback = function() + local search_string = self.page_info_text.input_dialog:getInputText() + if self.search_callback then + self.search_callback(search_string) + self.page_info_text:closeInputDialog() + else + if search_string ~= "" then + self:goToMenuItemMatching(search_string) + self.page_info_text:closeInputDialog() + end + end + end, + }, + { + text = _("Go to letter"), + callback = function() + local search_string = self.page_info_text.input_dialog:getInputText() + if search_string ~= "" then + self:goToMenuItemMatching(search_string, true) + self.page_info_text:closeInputDialog() + end + end, + }, + }, { { text = _("Cancel"), @@ -773,7 +798,6 @@ function Menu:init() }, { text = _("Go to page"), - is_enter_default = not self.goto_letter, callback = function() local page = tonumber(self.page_info_text.input_dialog:getInputText()) if page and page >= 1 and page <= self.page_num then @@ -784,59 +808,19 @@ function Menu:init() }, }, } - - if self.goto_letter then - title_goto = _("Enter letter or page number") - hint_func = function() - -- @translators First group is the standard range for alphabetic searches, second group is a page number range - return T(_("(a - z) or (1 - %1)"), self.page_num) - end - table.insert(buttons, 1, { - { - text = _("File search"), - callback = function() - self.page_info_text:closeInputDialog() - UIManager:sendEvent(Event:new("ShowFileSearch", self.page_info_text.input_dialog:getInputText())) - end, - }, - { - text = _("Go to letter"), - is_enter_default = true, - callback = function() - local search_string = self.page_info_text.input_dialog:getInputText() - if search_string == "" then return end - search_string = Utf8Proc.lowercase(util.fixUtf8(search_string, "?")) - for k, v in ipairs(self.item_table) do - local filename = Utf8Proc.lowercase(util.fixUtf8(ffiUtil.basename(v.path), "?")) - local i = filename:find(search_string) - if i == 1 and not v.is_go_up then - self:onGotoPage(self:getPageNumber(k)) - break - end - end - self.page_info_text:closeInputDialog() - end, - }, - }) - else - title_goto = _("Enter page number") - type_goto = "number" - hint_func = function() - return string.format("(1 - %s)", self.page_num) - end - end - self.page_info_text = self.page_info_text or Button:new{ text = "", + text_font_bold = false, + bordersize = 0, + call_hold_input_on_tap = true, hold_input = { - title = title_goto, - input_type = type_goto, - hint_func = hint_func, + title = _("Enter text, letter or page number"), + hint_func = function() + -- @translators First group is the standard range for alphabetic searches, second group is a page number range + return T(_("(a - z) or (1 - %1)"), self.page_num) + end, buttons = buttons, }, - call_hold_input_on_tap = true, - bordersize = 0, - text_font_bold = false, } self.page_info = HorizontalGroup:new{ self.page_info_first_chev, @@ -1315,6 +1299,21 @@ function Menu:onSelectByShortCut(_, keyevent) return true end +function Menu:goToMenuItemMatching(search_string, goto_letter) + search_string = Utf8Proc.lowercase(util.fixUtf8(search_string, "?")) + for i, item in ipairs(self.item_table) do + if not item.is_go_up and not (goto_letter and item.is_file == false) then -- skip folders in "Go to letter" + local item_text = Utf8Proc.lowercase(util.fixUtf8(item.text, "?")) + local idx = item_text:find(search_string) + if idx and (idx == 1 or not goto_letter) then + self.itemnumber = i -- draw focus + self:onGotoPage(self:getPageNumber(i)) + break + end + end + end +end + function Menu:onShowGotoDialog() if self.page_info_text and self.page_info_text.hold_input then self.page_info_text:onInput(self.page_info_text.hold_input)