From c1c89dd6113285a2b27f21ea30b409cbcd80c408 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Tue, 1 Feb 2022 21:56:28 +0200 Subject: [PATCH] TouchMenu: use radio buttons for single choice menu items (#8757) --- .../apps/reader/modules/readerhighlight.lua | 5 +++- frontend/ui/elements/page_overlap.lua | 1 + .../elements/screen_rotation_menu_table.lua | 4 +++ frontend/ui/widget/radiomark.lua | 28 +++++++++++++++++++ frontend/ui/widget/touchmenu.lua | 20 +++++++++---- 5 files changed, 52 insertions(+), 6 deletions(-) create mode 100644 frontend/ui/widget/radiomark.lua diff --git a/frontend/apps/reader/modules/readerhighlight.lua b/frontend/apps/reader/modules/readerhighlight.lua index 22dea784b..fe16e28f6 100644 --- a/frontend/apps/reader/modules/readerhighlight.lua +++ b/frontend/apps/reader/modules/readerhighlight.lua @@ -299,7 +299,7 @@ function ReaderHighlight:addToMainMenu(menu_items) text = _("Highlight style"), sub_item_table = {}, } - for _, v in ipairs(highlight_style) do + for i, v in ipairs(highlight_style) do table.insert(menu_items.highlight_options.sub_item_table, { text_func = function() local text = v[1] @@ -311,6 +311,7 @@ function ReaderHighlight:addToMainMenu(menu_items) checked_func = function() return self.view.highlight.saved_drawer == v[2] end, + radio = true, callback = function() self.view.highlight.saved_drawer = v[2] end, @@ -318,6 +319,7 @@ function ReaderHighlight:addToMainMenu(menu_items) G_reader_settings:saveSetting("highlight_drawing_style", v[2]) if touchmenu_instance then touchmenu_instance:updateItems() end end, + separator = i == #highlight_style, }) end table.insert(menu_items.highlight_options.sub_item_table, { @@ -388,6 +390,7 @@ function ReaderHighlight:addToMainMenu(menu_items) checked_func = function() return G_reader_settings:readSetting("default_highlight_action", "ask") == v[2] end, + radio = true, callback = function() G_reader_settings:saveSetting("default_highlight_action", v[2]) self.view.highlight.disabled = v[2] == "nothing" diff --git a/frontend/ui/elements/page_overlap.lua b/frontend/ui/elements/page_overlap.lua index 15df94329..fd1f94d2b 100644 --- a/frontend/ui/elements/page_overlap.lua +++ b/frontend/ui/elements/page_overlap.lua @@ -88,6 +88,7 @@ for k, v in pairs(page_overlap_styles) do checked_func = function() return ReaderUI.instance.view.page_overlap_style == k end, + radio = true, callback = function() ReaderUI.instance.view.page_overlap_style = k end, diff --git a/frontend/ui/elements/screen_rotation_menu_table.lua b/frontend/ui/elements/screen_rotation_menu_table.lua index 9e06e6579..2140a5bec 100644 --- a/frontend/ui/elements/screen_rotation_menu_table.lua +++ b/frontend/ui/elements/screen_rotation_menu_table.lua @@ -72,6 +72,7 @@ When unchecked, the default rotation of the file browser and the default/saved r checked_func = function() return Screen:getRotationMode() == Screen.ORIENTATION_LANDSCAPE_ROTATED end, + radio = true, callback = function(touchmenu_instance) UIManager:broadcastEvent(Event:new("SetRotationMode", Screen.ORIENTATION_LANDSCAPE_ROTATED)) if touchmenu_instance then touchmenu_instance:closeMenu() end @@ -92,6 +93,7 @@ When unchecked, the default rotation of the file browser and the default/saved r checked_func = function() return Screen:getRotationMode() == Screen.ORIENTATION_PORTRAIT end, + radio = true, callback = function(touchmenu_instance) UIManager:broadcastEvent(Event:new("SetRotationMode", Screen.ORIENTATION_PORTRAIT)) if touchmenu_instance then touchmenu_instance:closeMenu() end @@ -112,6 +114,7 @@ When unchecked, the default rotation of the file browser and the default/saved r checked_func = function() return Screen:getRotationMode() == Screen.ORIENTATION_LANDSCAPE end, + radio = true, callback = function(touchmenu_instance) UIManager:broadcastEvent(Event:new("SetRotationMode", Screen.ORIENTATION_LANDSCAPE)) if touchmenu_instance then touchmenu_instance:closeMenu() end @@ -132,6 +135,7 @@ When unchecked, the default rotation of the file browser and the default/saved r checked_func = function() return Screen:getRotationMode() == Screen.ORIENTATION_PORTRAIT_ROTATED end, + radio = true, callback = function(touchmenu_instance) UIManager:broadcastEvent(Event:new("SetRotationMode", Screen.ORIENTATION_PORTRAIT_ROTATED)) if touchmenu_instance then touchmenu_instance:closeMenu() end diff --git a/frontend/ui/widget/radiomark.lua b/frontend/ui/widget/radiomark.lua new file mode 100644 index 000000000..16882f438 --- /dev/null +++ b/frontend/ui/widget/radiomark.lua @@ -0,0 +1,28 @@ +local BD = require("ui/bidi") +local Blitbuffer = require("ffi/blitbuffer") +local Font = require("ui/font") +local InputContainer = require("ui/widget/container/inputcontainer") +local TextWidget = require("ui/widget/textwidget") + +local RadioMark = InputContainer:new{ + checkable = true, -- empty space when false + checked = false, + enabled = true, + face = Font:getFace("smallinfofont"), + baseline = 0, + _mirroredUI = BD.mirroredUILayout(), +} + +function RadioMark:init() + local widget = TextWidget:new{ + text = self.checkable and (self.checked and "◉ " or "◯ ") or "", + face = self.face, + fgcolor = self.enabled and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_DARK_GRAY, + para_direction_rtl = self._mirroredUI, + } + self.baseline = widget:getBaseline() + self[1] = widget + self.dimen = widget:getSize() +end + +return RadioMark diff --git a/frontend/ui/widget/touchmenu.lua b/frontend/ui/widget/touchmenu.lua index 61b6516c7..9a1d9ae52 100644 --- a/frontend/ui/widget/touchmenu.lua +++ b/frontend/ui/widget/touchmenu.lua @@ -20,6 +20,7 @@ local InfoMessage = require("ui/widget/infomessage") local InputContainer = require("ui/widget/container/inputcontainer") local LeftContainer = require("ui/widget/container/leftcontainer") local LineWidget = require("ui/widget/linewidget") +local RadioMark = require("ui/widget/radiomark") local RightContainer = require("ui/widget/container/rightcontainer") local Size = require("ui/size") local TextWidget = require("ui/widget/textwidget") @@ -74,11 +75,20 @@ function TouchMenuItem:init() item_checkable = true item_checked = self.item.checked_func() end - local checkmark_widget = CheckMark:new{ - checkable = item_checkable, - checked = item_checked, - enabled = item_enabled, - } + local checkmark_widget + if self.item.radio then + checkmark_widget = RadioMark:new{ + checkable = item_checkable, + checked = item_checked, + enabled = item_enabled, + } + else + checkmark_widget = CheckMark:new{ + checkable = item_checkable, + checked = item_checked, + enabled = item_enabled, + } + end local checked_widget = CheckMark:new{ -- for layout, to :getSize() checked = true,