2022-11-02 19:50:39 +00:00
|
|
|
--[[--
|
|
|
|
A button table to be used in dialogs and widgets.
|
|
|
|
]]
|
|
|
|
|
2018-02-11 19:46:18 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2022-02-05 09:22:31 +00:00
|
|
|
local CheckButton = require("ui/widget/checkbutton")
|
2018-02-11 19:46:18 +00:00
|
|
|
local Device = require("device")
|
|
|
|
local FocusManager = require("ui/widget/focusmanager")
|
2018-07-15 20:48:19 +00:00
|
|
|
local Font = require("ui/font")
|
2018-02-11 19:46:18 +00:00
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
|
|
|
local LineWidget = require("ui/widget/linewidget")
|
|
|
|
local Size = require("ui/size")
|
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
|
|
|
local VerticalSpan = require("ui/widget/verticalspan")
|
|
|
|
local dbg = require("dbg")
|
|
|
|
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 RadioButtonTable = FocusManager:extend{
|
2018-02-11 19:46:18 +00:00
|
|
|
width = Screen:getWidth(),
|
|
|
|
radio_buttons = {
|
|
|
|
{
|
|
|
|
{text="Cancel", enabled=false, callback=nil},
|
|
|
|
{text="OK", enabled=true, callback=nil},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sep_width = Size.line.medium,
|
|
|
|
padding = Size.padding.button,
|
|
|
|
|
|
|
|
zero_sep = false,
|
2018-07-15 20:48:19 +00:00
|
|
|
face = Font:getFace("cfont", 22),
|
2018-02-11 19:46:18 +00:00
|
|
|
_first_button = nil,
|
|
|
|
checked_button = nil,
|
2020-08-29 16:25:38 +00:00
|
|
|
button_select_callback = nil,
|
2018-02-11 19:46:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function RadioButtonTable:init()
|
|
|
|
self.radio_buttons_layout = {}
|
|
|
|
self.container = VerticalGroup:new{ width = self.width }
|
|
|
|
table.insert(self, self.container)
|
|
|
|
|
|
|
|
if self.zero_sep then
|
|
|
|
-- If we're asked to add a first line, don't add a vspan before: caller
|
|
|
|
-- must do its own padding before.
|
|
|
|
-- Things look better when the first line is gray like the others.
|
|
|
|
self:addHorizontalSep(false, true, true)
|
|
|
|
else
|
|
|
|
self:addHorizontalSep(false, false, true)
|
|
|
|
end
|
|
|
|
|
|
|
|
local row_cnt = #self.radio_buttons
|
|
|
|
|
|
|
|
for i = 1, row_cnt do
|
|
|
|
self.radio_buttons_layout[i] = {}
|
|
|
|
local horizontal_group = HorizontalGroup:new{}
|
|
|
|
local row = self.radio_buttons[i]
|
|
|
|
local column_cnt = #row
|
2022-02-05 09:22:31 +00:00
|
|
|
local sizer_space = (self.sep_width + 2 * self.padding) * (column_cnt - 1)
|
2018-02-11 19:46:18 +00:00
|
|
|
for j = 1, column_cnt do
|
|
|
|
local btn_entry = row[j]
|
2022-02-05 09:22:31 +00:00
|
|
|
local button = CheckButton:new{
|
2018-02-11 19:46:18 +00:00
|
|
|
text = btn_entry.text,
|
2022-02-05 09:22:31 +00:00
|
|
|
checkable = btn_entry.checkable,
|
2018-02-11 19:46:18 +00:00
|
|
|
checked = btn_entry.checked,
|
2022-02-05 09:22:31 +00:00
|
|
|
enabled = btn_entry.enabled,
|
|
|
|
radio = true,
|
2018-02-11 19:46:18 +00:00
|
|
|
provider = btn_entry.provider,
|
|
|
|
|
2022-02-05 09:22:31 +00:00
|
|
|
width = (self.width - sizer_space) / column_cnt,
|
2018-02-11 19:46:18 +00:00
|
|
|
bordersize = 0,
|
|
|
|
margin = 0,
|
|
|
|
padding = 0,
|
2018-07-15 20:48:19 +00:00
|
|
|
face = self.face,
|
2018-02-11 19:46:18 +00:00
|
|
|
|
|
|
|
show_parent = self.show_parent or self,
|
|
|
|
parent = self.parent or self,
|
|
|
|
}
|
|
|
|
local button_callback = function()
|
|
|
|
self:_checkButton(button)
|
2020-08-29 16:25:38 +00:00
|
|
|
if self.button_select_callback then
|
|
|
|
self.button_select_callback(btn_entry)
|
|
|
|
end
|
2018-02-11 19:46:18 +00:00
|
|
|
end
|
|
|
|
button.callback = button_callback
|
|
|
|
|
|
|
|
if i == 1 and j == 1 then
|
|
|
|
self._first_button = button
|
|
|
|
end
|
|
|
|
|
|
|
|
if button.checked and not self.checked_button then
|
|
|
|
self.checked_button = button
|
|
|
|
elseif dbg.is_on and
|
|
|
|
button.checked and self.checked_button then
|
|
|
|
error("RadioButtonGroup: multiple checked RadioButtons")
|
|
|
|
end
|
|
|
|
|
|
|
|
local button_dim = button:getSize()
|
|
|
|
local vertical_sep = LineWidget:new{
|
2019-03-14 19:58:45 +00:00
|
|
|
background = Blitbuffer.COLOR_DARK_GRAY,
|
2018-02-11 19:46:18 +00:00
|
|
|
dimen = Geom:new{
|
|
|
|
w = self.sep_width,
|
|
|
|
h = button_dim.h,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.radio_buttons_layout[i][j] = button
|
|
|
|
table.insert(horizontal_group, button)
|
|
|
|
if j < column_cnt then
|
|
|
|
table.insert(horizontal_group, vertical_sep)
|
|
|
|
end
|
|
|
|
end -- end for each button
|
|
|
|
table.insert(self.container, horizontal_group)
|
|
|
|
--if i < row_cnt then
|
|
|
|
--self:addHorizontalSep(true, true, true)
|
|
|
|
--end
|
|
|
|
end -- end for each button line
|
|
|
|
self:addHorizontalSep(true, false, false)
|
|
|
|
|
|
|
|
-- check first entry unless otherwise specified
|
|
|
|
if not self.checked_button then
|
2022-02-05 09:22:31 +00:00
|
|
|
self._first_button:toggleCheck()
|
2018-02-11 19:46:18 +00:00
|
|
|
self.checked_button = self._first_button
|
|
|
|
end
|
|
|
|
|
|
|
|
if Device:hasDPad() or Device:hasKeyboard() then
|
|
|
|
self.layout = self.radio_buttons_layout
|
2022-03-04 20:20:00 +00:00
|
|
|
self:refocusWidget()
|
2018-02-11 19:46:18 +00:00
|
|
|
else
|
|
|
|
self.key_events = {} -- deregister all key press event listeners
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function RadioButtonTable:addHorizontalSep(vspan_before, add_line, vspan_after, black_line)
|
|
|
|
if vspan_before then
|
|
|
|
table.insert(self.container,
|
|
|
|
VerticalSpan:new{ width = Size.span.vertical_default })
|
|
|
|
end
|
|
|
|
if add_line then
|
|
|
|
table.insert(self.container, LineWidget:new{
|
2019-03-14 19:58:45 +00:00
|
|
|
background = black_line and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_DARK_GRAY,
|
2018-02-11 19:46:18 +00:00
|
|
|
dimen = Geom:new{
|
|
|
|
w = self.width,
|
|
|
|
h = self.sep_width,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
end
|
|
|
|
if vspan_after then
|
|
|
|
table.insert(self.container,
|
|
|
|
VerticalSpan:new{ width = Size.span.vertical_default })
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function RadioButtonTable:_checkButton(button)
|
|
|
|
-- nothing to do
|
|
|
|
if button.checked then return end
|
|
|
|
|
2022-02-05 09:22:31 +00:00
|
|
|
self.checked_button:toggleCheck()
|
|
|
|
button:toggleCheck()
|
2018-02-11 19:46:18 +00:00
|
|
|
self.checked_button = button
|
|
|
|
end
|
|
|
|
|
|
|
|
return RadioButtonTable
|