2019-11-18 16:16:06 +00:00
|
|
|
--[[--
|
|
|
|
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")
|
2022-03-04 20:20:00 +00:00
|
|
|
local FocusManager = require("ui/widget/focusmanager")
|
2019-11-18 16:16:06 +00:00
|
|
|
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")
|
2019-11-18 16:16:06 +00:00
|
|
|
local RadioButtonTable = require("ui/widget/radiobuttontable")
|
2021-10-10 13:09:42 +00:00
|
|
|
local ScrollableContainer = require("ui/widget/container/scrollablecontainer")
|
2019-11-18 16:16:06 +00:00
|
|
|
local Size = require("ui/size")
|
2022-01-24 11:35:11 +00:00
|
|
|
local TitleBar = require("ui/widget/titlebar")
|
2019-11-18 16:16:06 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
|
|
|
local VerticalSpan = require("ui/widget/verticalspan")
|
2021-09-01 20:55:59 +00:00
|
|
|
local util = require("util")
|
2019-11-18 16:16:06 +00:00
|
|
|
local _ = require("gettext")
|
2022-03-04 20:20:00 +00:00
|
|
|
local Device = require("device")
|
|
|
|
local Screen = Device.screen
|
2019-11-18 16:16:06 +00:00
|
|
|
|
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{
|
2019-11-18 16:16:06 +00:00
|
|
|
is_always_active = true,
|
|
|
|
modal = true,
|
2021-06-30 13:54:24 +00:00
|
|
|
stop_events_propagation = true,
|
2021-08-29 10:29:41 +00:00
|
|
|
keyboard_state = nil,
|
2021-09-25 08:51:58 +00:00
|
|
|
width = nil,
|
2019-11-18 16:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function KeyboardLayoutDialog:init()
|
2022-03-04 20:20:00 +00:00
|
|
|
self.layout = {}
|
2021-09-25 08:51:58 +00:00
|
|
|
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,
|
2019-11-18 16:16:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
local buttons = {}
|
|
|
|
local radio_buttons = {}
|
2021-08-29 10:29:41 +00:00
|
|
|
|
2021-11-23 20:17:07 +00:00
|
|
|
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")
|
2021-08-29 10:29:41 +00:00
|
|
|
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
|
2021-08-29 10:29:41 +00:00
|
|
|
local text = Language:getLanguageName(k) .. " (" .. string.sub(k, 1, 2) .. ")"
|
2021-09-01 20:55:59 +00:00
|
|
|
if util.arrayContains(keyboard_layouts, k) then
|
2021-08-29 10:29:41 +00:00
|
|
|
text = text .. " ✓"
|
|
|
|
end
|
2022-01-24 11:35:11 +00:00
|
|
|
if k == default_layout then
|
2021-08-29 10:29:41 +00:00
|
|
|
text = text .. " ★"
|
|
|
|
end
|
2019-11-18 16:16:06 +00:00
|
|
|
table.insert(radio_buttons, {
|
|
|
|
{
|
2022-01-24 11:35:11 +00:00
|
|
|
text = text,
|
|
|
|
checked = k == current_layout,
|
|
|
|
provider = k,
|
2019-11-18 16:16:06 +00:00
|
|
|
},
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(buttons, {
|
|
|
|
{
|
|
|
|
text = _("Cancel"),
|
2022-03-04 20:20:00 +00:00
|
|
|
id = "close",
|
2019-11-18 16:16:06 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
2021-10-10 13:09:42 +00:00
|
|
|
-- (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()
|
2019-11-18 16:16:06 +00:00
|
|
|
self.radio_button_table = RadioButtonTable:new{
|
|
|
|
radio_buttons = radio_buttons,
|
2021-10-10 13:09:42 +00:00
|
|
|
width = scroll_container_inner_width - 2*Size.padding.large,
|
2019-11-18 16:16:06 +00:00
|
|
|
focused = true,
|
|
|
|
parent = self,
|
2021-10-10 13:09:42 +00:00
|
|
|
show_parent = self,
|
2019-11-18 16:16:06 +00:00
|
|
|
}
|
2022-03-04 20:20:00 +00:00
|
|
|
self:mergeLayoutInVertical(self.radio_button_table)
|
2019-11-18 16:16:06 +00:00
|
|
|
|
|
|
|
-- Buttons Table
|
|
|
|
self.button_table = ButtonTable:new{
|
2022-01-24 11:35:11 +00:00
|
|
|
width = self.width - 2*Size.padding.default,
|
2019-11-18 16:16:06 +00:00
|
|
|
buttons = buttons,
|
|
|
|
zero_sep = true,
|
|
|
|
show_parent = self,
|
|
|
|
}
|
2022-03-04 20:20:00 +00:00
|
|
|
self:mergeLayoutInVertical(self.button_table)
|
2019-11-18 16:16:06 +00:00
|
|
|
|
2021-10-10 13:09:42 +00:00
|
|
|
local max_radio_button_container_height = math.floor(Screen:getHeight()*0.9
|
2022-01-24 11:35:11 +00:00
|
|
|
- self.title_bar:getHeight()
|
2021-10-10 13:09:42 +00:00
|
|
|
- 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,
|
2021-10-10 13:09:42 +00:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
2019-11-18 16:16:06 +00:00
|
|
|
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,
|
|
|
|
},
|
2021-10-10 13:09:42 +00:00
|
|
|
self.cropping_widget, -- our ScrollableContainer
|
2019-11-18 16:16:06 +00:00
|
|
|
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,
|
2019-11-18 16:16:06 +00:00
|
|
|
h = self.button_table:getSize().h,
|
|
|
|
},
|
|
|
|
self.button_table,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-30 13:54:24 +00:00
|
|
|
self.movable = MovableContainer:new{
|
|
|
|
self.dialog_frame,
|
|
|
|
}
|
2019-11-18 16:16:06 +00:00
|
|
|
self[1] = CenterContainer:new{
|
|
|
|
dimen = Geom:new{
|
|
|
|
w = Screen:getWidth(),
|
2021-10-01 18:33:39 +00:00
|
|
|
h = Screen:getHeight(),
|
2019-11-18 16:16:06 +00:00
|
|
|
},
|
2021-06-30 13:54:24 +00:00
|
|
|
self.movable,
|
2019-11-18 16:16:06 +00:00
|
|
|
}
|
2022-03-04 20:20:00 +00:00
|
|
|
if Device:hasKeys() then
|
2022-10-27 00:01:51 +00:00
|
|
|
self.key_events.CloseDialog = { { Device.input.group.Back } }
|
2022-03-04 20:20:00 +00:00
|
|
|
end
|
2019-11-18 16:16:06 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function KeyboardLayoutDialog:onShow()
|
|
|
|
UIManager:setDirty(self, function()
|
|
|
|
return "ui", self.dialog_frame.dimen
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function KeyboardLayoutDialog:onCloseWidget()
|
|
|
|
UIManager:setDirty(nil, function()
|
2023-02-07 00:01:05 +00:00
|
|
|
return "ui", self.movable.dimen
|
2019-11-18 16:16:06 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2022-03-04 20:20:00 +00:00
|
|
|
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
|
|
|
|
|
2019-11-18 16:16:06 +00:00
|
|
|
return KeyboardLayoutDialog
|