From 400c774bcc71480ac38262c957ffcded56e6da82 Mon Sep 17 00:00:00 2001 From: zwim <36999612+zwim@users.noreply.github.com> Date: Wed, 30 Jul 2025 16:29:36 +0200 Subject: [PATCH] [touchmenu] simplify code and finish early (#14113) --- frontend/apps/reader/modules/readerlink.lua | 2 +- frontend/ui/widget/button.lua | 6 +- frontend/ui/widget/checkbutton.lua | 4 +- frontend/ui/widget/iconbutton.lua | 2 +- frontend/ui/widget/touchmenu.lua | 134 +++++++++----------- 5 files changed, 67 insertions(+), 81 deletions(-) diff --git a/frontend/apps/reader/modules/readerlink.lua b/frontend/apps/reader/modules/readerlink.lua index bb6fdcccd..3811aa27d 100644 --- a/frontend/apps/reader/modules/readerlink.lua +++ b/frontend/apps/reader/modules/readerlink.lua @@ -1593,7 +1593,7 @@ function ReaderLink:getButtonsForExternalLinkDialog(link_url) local button = fn_button(self, link_url) local show, button_title - if type(button.show_in_dialog_func) == "function" then + if button.show_in_dialog_func then show, button_title = button.show_in_dialog_func(link_url) else -- If the button doesn't have the show_in_dialog_func, then assume that the button diff --git a/frontend/ui/widget/button.lua b/frontend/ui/widget/button.lua index 7e5623d55..09b752956 100644 --- a/frontend/ui/widget/button.lua +++ b/frontend/ui/widget/button.lua @@ -80,7 +80,7 @@ function Button:init() end -- Prefer an optional text_func over text - if self.text_func and type(self.text_func) == "function" then + if self.text_func then self.text = self.text_func() end @@ -507,7 +507,7 @@ function Button:onTapSelectButton() end elseif self.tap_input then self:onInput(self.tap_input) - elseif type(self.tap_input_func) == "function" then + elseif self.tap_input_func then self:onInput(self.tap_input_func()) end end @@ -546,7 +546,7 @@ function Button:onHoldSelectButton() elseif self.hold_input then self:onInput(self.hold_input, true) self._hold_handled = true - elseif type(self.hold_input_func) == "function" then + elseif self.hold_input_func then self:onInput(self.hold_input_func(), true) self._hold_handled = true end diff --git a/frontend/ui/widget/checkbutton.lua b/frontend/ui/widget/checkbutton.lua index 71168d36e..4346cff25 100644 --- a/frontend/ui/widget/checkbutton.lua +++ b/frontend/ui/widget/checkbutton.lua @@ -127,7 +127,7 @@ function CheckButton:onTapCheckButton() if not self.enabled then return true end if self.tap_input then self:onInput(self.tap_input) - elseif type(self.tap_input_func) == "function" then + elseif self.tap_input_func then self:onInput(self.tap_input_func()) else if G_reader_settings:isFalse("flash_ui") then @@ -184,7 +184,7 @@ function CheckButton:onHoldCheckButton() elseif self.hold_input then self:onInput(self.hold_input, true) self._hold_handled = true - elseif type(self.hold_input_func) == "function" then + elseif self.hold_input_func then self:onInput(self.hold_input_func(), true) self._hold_handled = true end diff --git a/frontend/ui/widget/iconbutton.lua b/frontend/ui/widget/iconbutton.lua index de887b9c1..471430caf 100644 --- a/frontend/ui/widget/iconbutton.lua +++ b/frontend/ui/widget/iconbutton.lua @@ -152,7 +152,7 @@ function IconButton:onHoldIconButton() self.hold_callback() elseif self.hold_input then self:onInput(self.hold_input) - elseif type(self.hold_input_func) == "function" then + elseif self.hold_input_func then self:onInput(self.hold_input_func()) elseif not self.hold_callback then -- nil or false return diff --git a/frontend/ui/widget/touchmenu.lua b/frontend/ui/widget/touchmenu.lua index 577e3bff4..e905c623e 100644 --- a/frontend/ui/widget/touchmenu.lua +++ b/frontend/ui/widget/touchmenu.lua @@ -229,14 +229,9 @@ function TouchMenuItem:onHoldSelect(arg, ges) end if enabled == false then -- Allow help_text to be displayed even if menu item disabled - if self.item.help_text or type(self.item.help_text_func) == "function" then - local help_text = self.item.help_text - if self.item.help_text_func then - help_text = self.item.help_text_func(self) - end - if help_text then - UIManager:show(InfoMessage:new{ text = help_text, }) - end + local help_text = self.item.help_text_func and self.item.help_text_func(self) or self.item.help_text + if help_text then + UIManager:show(InfoMessage:new{ text = help_text, }) end return true -- don't propagate end @@ -841,51 +836,46 @@ function TouchMenu:onMenuSelect(item, tap_on_checkmark) if self.touch_menu_callback then self.touch_menu_callback() end + if tap_on_checkmark and item.checkmark_callback then item.checkmark_callback() self:updateItems() return true end - if item.tap_input or type(item.tap_input_func) == "function" then + + if item.tap_input or item.tap_input_func then if not item.keep_menu_open then self:closeMenu() end - if item.tap_input then - self:onInput(item.tap_input) - else - self:onInput(item.tap_input_func()) - end - else - local sub_item_table = item.sub_item_table - if item.sub_item_table_func then - sub_item_table = item.sub_item_table_func() - end - if sub_item_table == nil then - -- keep menu opened if this item is a check option - local callback, refresh = item.callback, item.checked or item.checked_func - if item.callback_func then - callback = item.callback_func() + self:onInput(item.tap_input or item.tap_input_func()) + return true + end + + local sub_item_table = item.sub_item_table_func and item.sub_item_table_func() or item.sub_item_table + if sub_item_table then + table.insert(self.item_table_stack, self.item_table) + item.menu_item_id = item.menu_item_id or tostring(item) -- unique id + self.parent_id = item.menu_item_id + self.item_table = sub_item_table + self:updateItems(1, self.item_table.open_on_menu_item_id_func + and self.item_table.open_on_menu_item_id_func()) + return true + end + + -- keep menu opened if this item is a check option + local callback = item.callback_func and item.callback_func() or item.callback + if callback then + -- Provide callback with us, so it can call our + -- closemenu() or updateItems() when it sees fit + -- (if not providing checked or checked_func, caller + -- must set keep_menu_open=true if that is wished) + callback(self) + if item.checked or item.checked_func then -- refresh + if not (item.check_callback_updates_menu or item.check_callback_closes_menu) then + self:updateItems() end - if callback then - -- Provide callback with us, so it can call our - -- closemenu() or updateItems() when it sees fit - -- (if not providing checked or checked_func, caller - -- must set keep_menu_open=true if that is wished) - callback(self) - if refresh then - if not (item.check_callback_updates_menu or item.check_callback_closes_menu) then - self:updateItems() - end - elseif not item.keep_menu_open then - self:closeMenu() - end - end - else - table.insert(self.item_table_stack, self.item_table) - item.menu_item_id = item.menu_item_id or tostring(item) -- unique id - self.parent_id = item.menu_item_id - self.item_table = sub_item_table - self:updateItems(1, self.item_table.open_on_menu_item_id_func and self.item_table.open_on_menu_item_id_func()) + elseif not item.keep_menu_open then + self:closeMenu() end end return true @@ -895,40 +885,36 @@ function TouchMenu:onMenuHold(item, text_truncated) if self.touch_menu_callback then self.touch_menu_callback() end - if item.hold_input or type(item.hold_input_func) == "function" then + + if item.hold_input or item.hold_input_func then if item.hold_keep_menu_open == false then self:closeMenu() end - if item.hold_input then - self:onInput(item.hold_input) - else - self:onInput(item.hold_input_func()) + self:onInput(item.hold_input or item.hold_input_func()) + return true + end + + local hold_callback = item.hold_callback_func and item.hold_callback_func() or item.hold_callback + if hold_callback then + -- With hold, the default is to keep menu open, as we're + -- most often showing a ConfirmBox that can be cancelled + -- (provide hold_keep_menu_open=false to override) + if item.hold_keep_menu_open == false then + self:closeMenu() end - elseif item.hold_callback or type(item.hold_callback_func) == "function" then - local callback = item.hold_callback - if item.hold_callback_func then - callback = item.hold_callback_func() - end - if callback then - -- With hold, the default is to keep menu open, as we're - -- most often showing a ConfirmBox that can be cancelled - -- (provide hold_keep_menu_open=false to override) - if item.hold_keep_menu_open == false then - self:closeMenu() - end - -- Provide callback with us, so it can call our - -- closemenu() or updateItems() when it sees fit - callback(self, item) - end - elseif item.help_text or type(item.help_text_func) == "function" then - local help_text = item.help_text - if item.help_text_func then - help_text = item.help_text_func(self) - end - if help_text then - UIManager:show(InfoMessage:new{ text = help_text, }) - end - elseif text_truncated then + -- Provide callback with us, so it can call our + -- closemenu() or updateItems() when it sees fit + hold_callback(self, item) + return true + end + + local help_text = item.help_text_func and item.help_text_func(self) or item.help_text + if help_text then + UIManager:show(InfoMessage:new{ text = help_text, }) + return true + end + + if text_truncated then UIManager:show(InfoMessage:new{ text = getMenuText(item), show_icon = false,