From 0c5240074cb2e8e8ca481dd10859eb8e18cd2a24 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Fri, 29 Sep 2023 07:44:28 +0300 Subject: [PATCH] screen_rotation_menu_table: deduplicate (#10954) --- .../elements/screen_rotation_menu_table.lua | 110 ++++-------------- 1 file changed, 24 insertions(+), 86 deletions(-) diff --git a/frontend/ui/elements/screen_rotation_menu_table.lua b/frontend/ui/elements/screen_rotation_menu_table.lua index be3aa1006..8fbbac63c 100644 --- a/frontend/ui/elements/screen_rotation_menu_table.lua +++ b/frontend/ui/elements/screen_rotation_menu_table.lua @@ -6,6 +6,26 @@ local _ = require("gettext") local C_ = _.pgettext local Screen = Device.screen +local function genMenuItem(text, mode) + return { + text_func = function() + return G_reader_settings:readSetting("fm_rotation_mode") == mode and text .. " ★" or text + end, + checked_func = function() + return Screen:getRotationMode() == mode + end, + radio = true, + callback = function(touchmenu_instance) + UIManager:broadcastEvent(Event:new("SetRotationMode", mode)) + touchmenu_instance:closeMenu() + end, + hold_callback = function(touchmenu_instance) + G_reader_settings:saveSetting("fm_rotation_mode", mode) + touchmenu_instance:updateItems() + end, + } +end + return { text = _("Rotation"), sub_item_table_func = function() @@ -22,9 +42,7 @@ return { UIManager:broadcastEvent(Event:new("ToggleGSensor")) end, }) - end - if Device:hasGSensor() then table.insert(rotation_table, { text = _("Lock auto rotation to current orientation"), help_text = _([[ @@ -61,90 +79,10 @@ When unchecked, the default rotation of the file browser and the default/saved r }) if FileManager.instance then - table.insert(rotation_table, { - text_func = function() - local text = C_("Rotation", "⤹ 90°") - if G_reader_settings:readSetting("fm_rotation_mode") == Screen.DEVICE_ROTATED_COUNTER_CLOCKWISE then - text = text .. " ★" - end - return text - end, - checked_func = function() - return Screen:getRotationMode() == Screen.DEVICE_ROTATED_COUNTER_CLOCKWISE - end, - radio = true, - callback = function(touchmenu_instance) - UIManager:broadcastEvent(Event:new("SetRotationMode", Screen.DEVICE_ROTATED_COUNTER_CLOCKWISE)) - if touchmenu_instance then touchmenu_instance:closeMenu() end - end, - hold_callback = function(touchmenu_instance) - G_reader_settings:saveSetting("fm_rotation_mode", Screen.DEVICE_ROTATED_COUNTER_CLOCKWISE) - if touchmenu_instance then touchmenu_instance:updateItems() end - end, - }) - table.insert(rotation_table, { - text_func = function() - local text = C_("Rotation", "↑ 0°") - if G_reader_settings:readSetting("fm_rotation_mode") == Screen.DEVICE_ROTATED_UPRIGHT then - text = text .. " ★" - end - return text - end, - checked_func = function() - return Screen:getRotationMode() == Screen.DEVICE_ROTATED_UPRIGHT - end, - radio = true, - callback = function(touchmenu_instance) - UIManager:broadcastEvent(Event:new("SetRotationMode", Screen.DEVICE_ROTATED_UPRIGHT)) - if touchmenu_instance then touchmenu_instance:closeMenu() end - end, - hold_callback = function(touchmenu_instance) - G_reader_settings:saveSetting("fm_rotation_mode", Screen.DEVICE_ROTATED_UPRIGHT) - if touchmenu_instance then touchmenu_instance:updateItems() end - end, - }) - table.insert(rotation_table, { - text_func = function() - local text = C_("Rotation", "⤸ 90°") - if G_reader_settings:readSetting("fm_rotation_mode") == Screen.DEVICE_ROTATED_CLOCKWISE then - text = text .. " ★" - end - return text - end, - checked_func = function() - return Screen:getRotationMode() == Screen.DEVICE_ROTATED_CLOCKWISE - end, - radio = true, - callback = function(touchmenu_instance) - UIManager:broadcastEvent(Event:new("SetRotationMode", Screen.DEVICE_ROTATED_CLOCKWISE)) - if touchmenu_instance then touchmenu_instance:closeMenu() end - end, - hold_callback = function(touchmenu_instance) - G_reader_settings:saveSetting("fm_rotation_mode", Screen.DEVICE_ROTATED_CLOCKWISE) - if touchmenu_instance then touchmenu_instance:updateItems() end - end, - }) - table.insert(rotation_table, { - text_func = function() - local text = C_("Rotation", "↓ 180°") - if G_reader_settings:readSetting("fm_rotation_mode") == Screen.DEVICE_ROTATED_UPSIDE_DOWN then - text = text .. " ★" - end - return text - end, - checked_func = function() - return Screen:getRotationMode() == Screen.DEVICE_ROTATED_UPSIDE_DOWN - end, - radio = true, - callback = function(touchmenu_instance) - UIManager:broadcastEvent(Event:new("SetRotationMode", Screen.DEVICE_ROTATED_UPSIDE_DOWN)) - if touchmenu_instance then touchmenu_instance:closeMenu() end - end, - hold_callback = function(touchmenu_instance) - G_reader_settings:saveSetting("fm_rotation_mode", Screen.DEVICE_ROTATED_UPSIDE_DOWN) - if touchmenu_instance then touchmenu_instance:updateItems() end - end, - }) + table.insert(rotation_table, genMenuItem(C_("Rotation", "⤹ 90°"), Screen.DEVICE_ROTATED_COUNTER_CLOCKWISE)) + table.insert(rotation_table, genMenuItem(C_("Rotation", "↑ 0°"), Screen.DEVICE_ROTATED_UPRIGHT)) + table.insert(rotation_table, genMenuItem(C_("Rotation", "⤸ 90°"), Screen.DEVICE_ROTATED_CLOCKWISE)) + table.insert(rotation_table, genMenuItem(C_("Rotation", "↓ 180°"), Screen.DEVICE_ROTATED_UPSIDE_DOWN)) end return rotation_table