Menu widget: "Go to letter" everywhere (#13405)

This commit is contained in:
hius07
2025-03-16 08:03:14 +02:00
committed by GitHub
parent 135453776a
commit b707d0f593
5 changed files with 61 additions and 52 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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()

View File

@@ -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

View File

@@ -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)