From 85cfb552cb2356a5c6d8e6a160ed16bd9d24b332 Mon Sep 17 00:00:00 2001 From: hius07 <62179190+hius07@users.noreply.github.com> Date: Wed, 1 Sep 2021 23:55:59 +0300 Subject: [PATCH] Keyboard preferred layouts: usability fixes (#8159) Store list of layouts in settings file as array of enabled layouts only (up to 4 elements). Optimize code. Allows sorting of the abbreviations in the globe popup: just check layouts in the desired order (the first checked will be northeast). Requires onetime migration to clean up the settings. --- frontend/ui/data/onetime_migration.lua | 18 +++++- frontend/ui/elements/menu_keyboard_layout.lua | 19 +++---- frontend/ui/widget/keyboardlayoutdialog.lua | 3 +- frontend/ui/widget/virtualkeyboard.lua | 56 +++++++------------ 4 files changed, 46 insertions(+), 50 deletions(-) diff --git a/frontend/ui/data/onetime_migration.lua b/frontend/ui/data/onetime_migration.lua index ce2becaac..be50f7a2a 100644 --- a/frontend/ui/data/onetime_migration.lua +++ b/frontend/ui/data/onetime_migration.lua @@ -7,7 +7,7 @@ local lfs = require("libs/libkoreader-lfs") local logger = require("logger") -- Date at which the last migration snippet was added -local CURRENT_MIGRATION_DATE = 20210720 +local CURRENT_MIGRATION_DATE = 20210831 -- Retrieve the date of the previous migration, if any local last_migration_date = G_reader_settings:readSetting("last_migration_date", 0) @@ -296,5 +296,21 @@ if last_migration_date < 20210720 then G_reader_settings:saveSetting("duration_format", "classic") end +-- 20210831, Clean VirtualKeyboard settings of disabled layouts, https://github.com/koreader/koreader/pull/8159 +if last_migration_date < 20210831 then + logger.info("Performing one-time migration for 20210831") + local FFIUtil = require("ffi/util") + local keyboard_layouts = G_reader_settings:readSetting("keyboard_layouts") or {} + local keyboard_layouts_new = {} + local selected_layouts_count = 0 + for k, v in FFIUtil.orderedPairs(keyboard_layouts) do + if v == true and selected_layouts_count < 4 then + selected_layouts_count = selected_layouts_count + 1 + keyboard_layouts_new[selected_layouts_count] = k + end + end + G_reader_settings:saveSetting("keyboard_layouts", keyboard_layouts_new) +end + -- We're done, store the current migration date G_reader_settings:saveSetting("last_migration_date", CURRENT_MIGRATION_DATE) diff --git a/frontend/ui/elements/menu_keyboard_layout.lua b/frontend/ui/elements/menu_keyboard_layout.lua index db3d57b4b..e72029ffa 100644 --- a/frontend/ui/elements/menu_keyboard_layout.lua +++ b/frontend/ui/elements/menu_keyboard_layout.lua @@ -6,6 +6,7 @@ local Language = require("ui/language") local UIManager = require("ui/uimanager") local VerticalGroup = require("ui/widget/verticalgroup") local VirtualKeyboard = require("ui/widget/virtualkeyboard") +local util = require("util") local _ = require("gettext") local input_dialog, check_button_bold, check_button_border, check_button_compact @@ -111,12 +112,7 @@ local sub_item_table = { }, } -local selected_layouts_count = 0 -local _keyboard_layouts = G_reader_settings:readSetting("keyboard_layouts") or {} for k, __ in FFIUtil.orderedPairs(VirtualKeyboard.lang_to_keyboard_layout) do - if _keyboard_layouts[k] == true then - selected_layouts_count = selected_layouts_count + 1 - end table.insert(sub_item_table[1].sub_item_table, { text_func = function() local text = Language:getLanguageName(k) @@ -127,17 +123,16 @@ for k, __ in FFIUtil.orderedPairs(VirtualKeyboard.lang_to_keyboard_layout) do end, checked_func = function() local keyboard_layouts = G_reader_settings:readSetting("keyboard_layouts") or {} - return keyboard_layouts[k] == true + return util.arrayContains(keyboard_layouts, k) end, callback = function() local keyboard_layouts = G_reader_settings:readSetting("keyboard_layouts") or {} - if keyboard_layouts[k] == true then - keyboard_layouts[k] = false - selected_layouts_count = selected_layouts_count - 1 + local layout_index = util.arrayContains(keyboard_layouts, k) + if layout_index then + table.remove(keyboard_layouts, layout_index) else - if selected_layouts_count < 4 then - keyboard_layouts[k] = true - selected_layouts_count = selected_layouts_count + 1 + if #keyboard_layouts < 4 then + table.insert(keyboard_layouts, k) else -- no more space in the 'globe' popup UIManager:show(require("ui/widget/infomessage"):new{ text = _("Up to four layouts can be enabled."), diff --git a/frontend/ui/widget/keyboardlayoutdialog.lua b/frontend/ui/widget/keyboardlayoutdialog.lua index c11fb04f1..83e98ee9a 100644 --- a/frontend/ui/widget/keyboardlayoutdialog.lua +++ b/frontend/ui/widget/keyboardlayoutdialog.lua @@ -19,6 +19,7 @@ local TextWidget = require("ui/widget/textwidget") local UIManager = require("ui/uimanager") local VerticalGroup = require("ui/widget/verticalgroup") local VerticalSpan = require("ui/widget/verticalspan") +local util = require("util") local _ = require("gettext") local Screen = require("device").screen @@ -64,7 +65,7 @@ function KeyboardLayoutDialog:init() self.keyboard_state.force_current_layout = true for k, _ in FFIUtil.orderedPairs(self.parent.keyboard.lang_to_keyboard_layout) do local text = Language:getLanguageName(k) .. " (" .. string.sub(k, 1, 2) .. ")" - if keyboard_layouts[k] == true then + if util.arrayContains(keyboard_layouts, k) then text = text .. " ✓" end if k == G_reader_settings:readSetting("keyboard_layout_default") then diff --git a/frontend/ui/widget/virtualkeyboard.lua b/frontend/ui/widget/virtualkeyboard.lua index 226541322..91e86e5bd 100644 --- a/frontend/ui/widget/virtualkeyboard.lua +++ b/frontend/ui/widget/virtualkeyboard.lua @@ -3,7 +3,6 @@ local BottomContainer = require("ui/widget/container/bottomcontainer") local CenterContainer = require("ui/widget/container/centercontainer") local Device = require("device") local Event = require("ui/event") -local FFIUtil = require("ffi/util") local FocusManager = require("ui/widget/focusmanager") local Font = require("ui/font") local FrameContainer = require("ui/widget/container/framecontainer") @@ -62,38 +61,28 @@ function VirtualKey:init() self.callback = function () self.keyboard:setLayer("Shift") end self.skiptap = true elseif self.keyboard.utf8mode_keys[self.label] ~= nil then - self.key_chars = self:genkeyboardLayoutKeyChars() + self.key_chars = self:genKeyboardLayoutKeyChars() self.callback = function () local current = G_reader_settings:readSetting("keyboard_layout") local default = G_reader_settings:readSetting("keyboard_layout_default") local keyboard_layouts = G_reader_settings:readSetting("keyboard_layouts") or {} - local enabled = false local next_layout = nil - if not keyboard_layouts[current] and current ~= default then - next_layout = default - end - if not next_layout then - for k, v in FFIUtil.orderedPairs(keyboard_layouts) do - if enabled and v == true then - next_layout = k - break - end - if k == current then - enabled = true - end + local layout_index = util.arrayContains(keyboard_layouts, current) + if layout_index then + if layout_index == #keyboard_layouts then + layout_index = 1 + else + layout_index = layout_index + 1 end - end - if not next_layout then - for k, v in FFIUtil.orderedPairs(keyboard_layouts) do - if v == true then - next_layout = k - break - end + else + if default and current ~= default then + next_layout = default + else + layout_index = 1 end end - if next_layout then - self.keyboard:setKeyboardLayout(next_layout) - end + next_layout = next_layout or keyboard_layouts[layout_index] or "en" + self.keyboard:setKeyboardLayout(next_layout) end self.hold_callback = function() if util.tableSize(self.key_chars) > 5 then -- 2 or more layouts enabled @@ -287,7 +276,7 @@ function VirtualKey:init() self.flash_keyboard = G_reader_settings:nilOrTrue("flash_keyboard") end -function VirtualKey:genkeyboardLayoutKeyChars() +function VirtualKey:genKeyboardLayoutKeyChars() local positions = { "northeast", "north", @@ -308,16 +297,11 @@ function VirtualKey:genkeyboardLayoutKeyChars() UIManager:show(self.keyboard_layout_dialog) end, } - local index = 1 - for k, v in FFIUtil.orderedPairs(keyboard_layouts) do - if v == true then - key_chars[positions[index]] = string.sub(k, 1, 2) - key_chars[positions[index] .. "_func"] = function() - UIManager:close(self.popup) - self.keyboard:setKeyboardLayout(k) - end - if index >= 4 then break end - index = index + 1 + for i = 1, #keyboard_layouts do + key_chars[positions[i]] = string.sub(keyboard_layouts[i], 1, 2) + key_chars[positions[i] .. "_func"] = function() + UIManager:close(self.popup) + self.keyboard:setKeyboardLayout(keyboard_layouts[i]) end end return key_chars