2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/ui/widget/keyboardlayoutdialog.lua

197 lines
6.6 KiB
Lua
Raw Normal View History

--[[--
This widget displays a keyboard layout dialog.
]]
local Blitbuffer = require("ffi/blitbuffer")
local ButtonTable = require("ui/widget/buttontable")
local CenterContainer = require("ui/widget/container/centercontainer")
2020-09-15 18:39:32 +00:00
local FFIUtil = require("ffi/util")
local FocusManager = require("ui/widget/focusmanager")
local FrameContainer = require("ui/widget/container/framecontainer")
local Geom = require("ui/geometry")
local Language = require("ui/language")
2021-06-30 13:54:24 +00:00
local MovableContainer = require("ui/widget/container/movablecontainer")
local RadioButtonTable = require("ui/widget/radiobuttontable")
local ScrollableContainer = require("ui/widget/container/scrollablecontainer")
local Size = require("ui/size")
2022-01-24 11:35:11 +00:00
local TitleBar = require("ui/widget/titlebar")
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 Device = require("device")
local Screen = Device.screen
Clarify our OOP semantics across the codebase (#9586) Basically: * Use `extend` for class definitions * Use `new` for object instantiations That includes some minor code cleanups along the way: * Updated `Widget`'s docs to make the semantics clearer. * Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283) * Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass). * Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events. * Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier. * Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references. * ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak). * Terminal: Make sure the shell is killed on plugin teardown. * InputText: Fix Home/End/Del physical keys to behave sensibly. * InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...). * OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of. * ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed! * Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
local KeyboardLayoutDialog = FocusManager:extend{
is_always_active = true,
modal = true,
2021-06-30 13:54:24 +00:00
stop_events_propagation = true,
keyboard_state = nil,
width = nil,
}
function KeyboardLayoutDialog:init()
self.layout = {}
self.width = self.width or math.floor(math.min(Screen:getWidth(), Screen:getHeight()) * 0.8)
2022-01-24 11:35:11 +00:00
self.title_bar = TitleBar:new{
width = self.width,
with_bottom_line = true,
title = _("Keyboard layout"),
bottom_v_padding = 0,
show_parent = self,
}
local buttons = {}
local radio_buttons = {}
local keyboard_layouts = G_reader_settings:readSetting("keyboard_layouts", {})
2022-01-24 11:35:11 +00:00
local default_layout = G_reader_settings:readSetting("keyboard_layout_default")
self.keyboard_state.force_current_layout = true
2022-01-24 11:35:11 +00:00
local current_layout = self.parent.keyboard:getKeyboardLayout()
self.keyboard_state.force_current_layout = false
2020-09-15 18:39:32 +00:00
for k, _ in FFIUtil.orderedPairs(self.parent.keyboard.lang_to_keyboard_layout) do
local text = Language:getLanguageName(k) .. " (" .. string.sub(k, 1, 2) .. ")"
if util.arrayContains(keyboard_layouts, k) then
text = text .. ""
end
2022-01-24 11:35:11 +00:00
if k == default_layout then
text = text .. ""
end
table.insert(radio_buttons, {
{
2022-01-24 11:35:11 +00:00
text = text,
checked = k == current_layout,
provider = k,
},
})
end
table.insert(buttons, {
{
text = _("Cancel"),
id = "close",
callback = function()
UIManager:close(self.parent.keyboard_layout_dialog)
end,
},
{
text = _("Switch to layout"),
is_enter_default = true,
callback = function()
local provider = self.parent.keyboard_layout_dialog.radio_button_table.checked_button.provider
self.parent.keyboard:setKeyboardLayout(provider)
UIManager:close(self.parent.keyboard_layout_dialog)
end,
},
})
-- (RadioButtonTable's width and padding setup is a bit fishy: we get
-- this to look ok by using a CenterContainer to ensure some padding)
2022-01-24 11:35:11 +00:00
local scroll_container_inner_width = self.width - ScrollableContainer:getScrollbarWidth()
self.radio_button_table = RadioButtonTable:new{
radio_buttons = radio_buttons,
width = scroll_container_inner_width - 2*Size.padding.large,
focused = true,
parent = self,
show_parent = self,
}
self:mergeLayoutInVertical(self.radio_button_table)
-- Buttons Table
self.button_table = ButtonTable:new{
2022-01-24 11:35:11 +00:00
width = self.width - 2*Size.padding.default,
buttons = buttons,
zero_sep = true,
show_parent = self,
}
self:mergeLayoutInVertical(self.button_table)
local max_radio_button_container_height = math.floor(Screen:getHeight()*0.9
2022-01-24 11:35:11 +00:00
- self.title_bar:getHeight()
- Size.span.vertical_large*4 - self.button_table:getSize().h)
local radio_button_container_height = math.min(self.radio_button_table:getSize().h, max_radio_button_container_height)
-- Our scrollable container needs to be known as widget.cropping_widget in
-- the widget that is passed to UIManager:show() for UIManager to ensure
-- proper interception of inner widget self repainting/invert (mostly used
-- when flashing for UI feedback that we want to limit to the cropped area).
self.cropping_widget = ScrollableContainer:new{
dimen = Geom:new{
2022-01-24 11:35:11 +00:00
w = self.width,
h = radio_button_container_height,
},
show_parent = self,
CenterContainer:new{
dimen = Geom:new{
w = scroll_container_inner_width,
h = self.radio_button_table:getSize().h,
},
self.radio_button_table,
},
}
self.dialog_frame = FrameContainer:new{
radius = Size.radius.window,
bordersize = Size.border.window,
padding = 0,
margin = 0,
background = Blitbuffer.COLOR_WHITE,
VerticalGroup:new{
align = "center",
self.title_bar,
VerticalSpan:new{
width = Size.span.vertical_large*2,
},
self.cropping_widget, -- our ScrollableContainer
VerticalSpan:new{
width = Size.span.vertical_large*2,
},
-- buttons
CenterContainer:new{
dimen = Geom:new{
2022-01-24 11:35:11 +00:00
w = self.width,
h = self.button_table:getSize().h,
},
self.button_table,
}
}
}
2021-06-30 13:54:24 +00:00
self.movable = MovableContainer:new{
self.dialog_frame,
}
self[1] = CenterContainer:new{
dimen = Geom:new{
w = Screen:getWidth(),
h = Screen:getHeight(),
},
2021-06-30 13:54:24 +00:00
self.movable,
}
if Device:hasKeys() then
self.key_events.CloseDialog = { { Device.input.group.Back } }
end
end
function KeyboardLayoutDialog:onShow()
UIManager:setDirty(self, function()
return "ui", self.dialog_frame.dimen
end)
end
function KeyboardLayoutDialog:onCloseWidget()
UIManager:setDirty(nil, function()
Tame some ButtonTable users into re-using Buttontable instances if possible (#7166) * QuickDictLookup, ImageViewer, NumberPicker: Smarter `update` that will re-use most of the widget's layout instead of re-instantiating all the things. * SpinWidget/DoubleSpinWidget: The NumberPicker change above renders a hack to preserve alpha on these widgets almost unnecessary. Also fixed said hack to also apply to the center, value button. * Button: Don't re-instantiate the frame in setText/setIcon when unnecessary (e.g., no change at all, or no layout change). * Button: Add a refresh method that repaints and refreshes a *specific* Button (provided it's been painted once) all on its lonesome. * ConfigDialog: Free everything that's going to be re-instatiated on update * A few more post #7118 fixes: * SkimTo: Always flag the chapter nav buttons as vsync * Button: Fix the highlight on rounded buttons when vsync is enabled (e.g., it's now entirely visible, instead of showing a weird inverted corner glitch). * Some more heuristic tweaks in Menu/TouchMenu/Button/IconButton * ButtonTable: fix the annoying rounding issue I'd noticed in #7054 ;). * Enable dithering in TextBoxWidget (e.g., in the Wikipedia full view). This involved moving the HW dithering align fixup to base, where it always ought to have been ;). * Switch a few widgets that were using "partial" on close to "ui", or, more rarely, "flashui". The intent being to limit "partial" purely to the Reader, because it has a latency cost when mixed with other refreshes, which happens often enough in UI ;). * Minor documentation tweaks around UIManager's `setDirty` to reflect that change. * ReaderFooter: Force a footer repaint on resume if it is visible (otherwise, just update it). * ReaderBookmark: In the same vein, don't repaint an invisible footer on bookmark count changes.
2021-01-28 23:20:15 +00:00
return "ui", self[1][1].dimen
end)
end
function KeyboardLayoutDialog:onCloseDialog()
local close_button = self.button_table:getButtonById("close")
if close_button and close_button.enabled then
close_button.callback()
return true
end
return false
end
return KeyboardLayoutDialog