2022-10-25 11:50:21 +00:00
|
|
|
--[[--
|
|
|
|
A button dialog widget that shows a grid of buttons.
|
|
|
|
|
|
|
|
@usage
|
|
|
|
local button_dialog = ButtonDialog:new{
|
|
|
|
buttons = {
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = "First row, left side",
|
|
|
|
callback = function() end,
|
|
|
|
hold_callback = function() end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = "First row, middle",
|
|
|
|
callback = function() end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = "First row, right side",
|
|
|
|
callback = function() end
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = "Second row, full span",
|
|
|
|
callback = function() end
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
{
|
|
|
|
text = "Third row, left side",
|
|
|
|
callback = function() end
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text = "Third row, right side",
|
|
|
|
callback = function() end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--]]
|
|
|
|
|
2017-09-11 08:32:39 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2014-01-22 18:03:44 +00:00
|
|
|
local ButtonTable = require("ui/widget/buttontable")
|
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
2014-10-30 18:42:18 +00:00
|
|
|
local Device = require("device")
|
2023-06-04 12:29:30 +00:00
|
|
|
local Font = require("ui/font")
|
2017-09-11 08:32:39 +00:00
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
2014-01-22 18:03:44 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2017-09-11 08:32:39 +00:00
|
|
|
local GestureRange = require("ui/gesturerange")
|
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2023-06-04 12:29:30 +00:00
|
|
|
local LineWidget = require("ui/widget/linewidget")
|
2018-01-29 20:27:24 +00:00
|
|
|
local MovableContainer = require("ui/widget/container/movablecontainer")
|
2023-05-11 18:23:45 +00:00
|
|
|
local ScrollableContainer = require("ui/widget/container/scrollablecontainer")
|
2017-09-13 14:56:20 +00:00
|
|
|
local Size = require("ui/size")
|
2023-06-04 12:29:30 +00:00
|
|
|
local TextBoxWidget = require("ui/widget/textboxwidget")
|
2014-01-22 18:03:44 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2023-05-11 18:23:45 +00:00
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
|
|
|
local VerticalSpan = require("ui/widget/verticalspan")
|
2023-06-04 12:29:30 +00:00
|
|
|
local Screen = Device.screen
|
2014-01-22 18:03:44 +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 ButtonDialog = InputContainer:extend{
|
2014-03-13 13:52:43 +00:00
|
|
|
buttons = nil,
|
2023-03-23 17:37:38 +00:00
|
|
|
width = nil,
|
|
|
|
width_factor = nil, -- number between 0 and 1, factor to the smallest of screen width and height
|
2023-03-23 17:37:40 +00:00
|
|
|
shrink_unneeded_width = false, -- have 'width' meaning 'max_width'
|
|
|
|
shrink_min_width = nil, -- default to ButtonTable's default
|
2014-03-13 13:52:43 +00:00
|
|
|
tap_close_callback = nil,
|
2018-01-29 20:27:24 +00:00
|
|
|
alpha = nil, -- passed to MovableContainer
|
2023-05-11 18:23:45 +00:00
|
|
|
-- If scrolling, prefers using this/these numbers of buttons rows per page
|
|
|
|
-- (depending on what the screen height allows) to compute the height.
|
|
|
|
rows_per_page = nil, -- number or array of numbers
|
2023-06-04 12:29:30 +00:00
|
|
|
|
|
|
|
title = nil,
|
|
|
|
title_align = "left",
|
|
|
|
title_face = Font:getFace("x_smalltfont"),
|
|
|
|
title_padding = Size.padding.large,
|
|
|
|
title_margin = Size.margin.title,
|
|
|
|
use_info_style = true, -- set to false to have bold font style of the title
|
|
|
|
info_face = Font:getFace("infofont"),
|
|
|
|
info_padding = Size.padding.default,
|
|
|
|
info_margin = Size.margin.default,
|
|
|
|
dismissable = true, -- set to false if any button callback is required
|
2014-01-22 18:03:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function ButtonDialog:init()
|
2023-03-23 17:37:38 +00:00
|
|
|
if not self.width then
|
|
|
|
if not self.width_factor then
|
|
|
|
self.width_factor = 0.9 -- default if no width specified
|
|
|
|
end
|
|
|
|
self.width = math.floor(math.min(Screen:getWidth(), Screen:getHeight()) * self.width_factor)
|
|
|
|
end
|
2023-06-04 12:29:30 +00:00
|
|
|
if self.dismissable then
|
|
|
|
if Device:hasKeys() then
|
|
|
|
local close_keys = Device:hasFewKeys() and { "Back", "Left" } or Device.input.group.Back
|
|
|
|
self.key_events.Close = { { close_keys } }
|
|
|
|
end
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events.TapClose = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight(),
|
|
|
|
}
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
2023-06-04 12:29:30 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-08 05:17:42 +00:00
|
|
|
self.buttontable = ButtonTable:new{
|
|
|
|
buttons = self.buttons,
|
|
|
|
width = self.width - 2*Size.border.window - 2*Size.padding.button,
|
|
|
|
shrink_unneeded_width = self.shrink_unneeded_width,
|
|
|
|
shrink_min_width = self.shrink_min_width,
|
|
|
|
show_parent = self,
|
|
|
|
}
|
|
|
|
local buttontable_width = self.buttontable:getSize().w -- may be shrinked
|
2023-06-04 12:29:30 +00:00
|
|
|
|
|
|
|
local title_widget, title_widget_height
|
|
|
|
if self.title then
|
|
|
|
local title_padding, title_margin, title_face
|
|
|
|
if self.use_info_style then
|
|
|
|
title_padding = self.info_padding
|
|
|
|
title_margin = self.info_margin
|
|
|
|
title_face = self.info_face
|
|
|
|
else
|
|
|
|
title_padding = self.title_padding
|
|
|
|
title_margin = self.title_margin
|
|
|
|
title_face = self.title_face
|
|
|
|
end
|
|
|
|
title_widget = FrameContainer:new{
|
|
|
|
padding = title_padding,
|
|
|
|
margin = title_margin,
|
|
|
|
bordersize = 0,
|
|
|
|
TextBoxWidget:new{
|
|
|
|
text = self.title,
|
2023-07-08 05:17:42 +00:00
|
|
|
width = buttontable_width - 2 * (title_padding + title_margin),
|
2023-06-04 12:29:30 +00:00
|
|
|
face = title_face,
|
|
|
|
alignment = self.title_align,
|
|
|
|
},
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
2023-06-04 12:29:30 +00:00
|
|
|
title_widget_height = title_widget:getSize().h + Size.line.medium
|
|
|
|
else
|
|
|
|
title_widget = VerticalSpan:new{}
|
|
|
|
title_widget_height = 0
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2023-06-04 12:29:30 +00:00
|
|
|
|
2023-05-11 18:23:45 +00:00
|
|
|
-- If the ButtonTable ends up being taller than the screen, wrap it inside a ScrollableContainer.
|
|
|
|
-- Ensure some small top and bottom padding, so the scrollbar stand out, and some outer margin
|
|
|
|
-- so the this dialog does not take the full height and stand as a popup.
|
2023-06-04 12:29:30 +00:00
|
|
|
local max_height = Screen:getHeight() - 2*Size.padding.buttontable - 2*Size.margin.default - title_widget_height
|
2023-05-11 18:23:45 +00:00
|
|
|
local height = self.buttontable:getSize().h
|
2023-06-04 12:29:30 +00:00
|
|
|
local scontainer, scrollbar_width
|
2023-05-11 18:23:45 +00:00
|
|
|
if height > max_height then
|
|
|
|
-- Adjust the ScrollableContainer to an integer multiple of the row height
|
|
|
|
-- (assuming all rows get the same height), so when scrolling per page,
|
|
|
|
-- we always end up seeing full rows.
|
|
|
|
self.buttontable:setupGridScrollBehaviour()
|
|
|
|
local step_scroll_grid = self.buttontable:getStepScrollGrid()
|
|
|
|
local row_height = step_scroll_grid[1].bottom + 1 - step_scroll_grid[1].top
|
|
|
|
local fit_rows = math.floor(max_height / row_height)
|
|
|
|
if self.rows_per_page then
|
|
|
|
if type(self.rows_per_page) == "number" then
|
|
|
|
if fit_rows > self.rows_per_page then
|
|
|
|
fit_rows = self.rows_per_page
|
|
|
|
end
|
|
|
|
else
|
|
|
|
for _, nb in ipairs(self.rows_per_page) do
|
|
|
|
if fit_rows >= nb then
|
|
|
|
fit_rows = nb
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- (Comment the next line to test ScrollableContainer behaviour when things do not fit)
|
|
|
|
max_height = row_height * fit_rows
|
2023-06-04 12:29:30 +00:00
|
|
|
scrollbar_width = ScrollableContainer:getScrollbarWidth()
|
2023-05-11 18:23:45 +00:00
|
|
|
self.cropping_widget = ScrollableContainer:new{
|
|
|
|
dimen = Geom:new{
|
|
|
|
-- We'll be exceeding the provided width in this case (let's not bother
|
|
|
|
-- ensuring it, we'd need to re-setup the ButtonTable...)
|
2023-07-08 05:17:42 +00:00
|
|
|
w = buttontable_width + scrollbar_width,
|
2023-05-11 18:23:45 +00:00
|
|
|
h = max_height,
|
|
|
|
},
|
|
|
|
show_parent = self,
|
|
|
|
step_scroll_grid = step_scroll_grid,
|
|
|
|
self.buttontable,
|
|
|
|
}
|
|
|
|
scontainer = VerticalGroup:new{
|
|
|
|
VerticalSpan:new{ width=Size.padding.buttontable },
|
|
|
|
self.cropping_widget,
|
|
|
|
VerticalSpan:new{ width=Size.padding.buttontable },
|
|
|
|
}
|
|
|
|
end
|
2023-06-04 12:29:30 +00:00
|
|
|
local separator
|
|
|
|
if self.title then
|
|
|
|
separator = LineWidget:new{
|
|
|
|
background = Blitbuffer.COLOR_GRAY,
|
|
|
|
dimen = Geom:new{
|
2023-07-08 05:17:42 +00:00
|
|
|
w = buttontable_width + (scrollbar_width or 0),
|
2023-06-04 12:29:30 +00:00
|
|
|
h = Size.line.medium,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
else
|
|
|
|
separator = VerticalSpan:new{}
|
|
|
|
end
|
2021-02-02 03:27:14 +00:00
|
|
|
self.movable = MovableContainer:new{
|
2023-06-04 12:29:30 +00:00
|
|
|
alpha = self.alpha,
|
|
|
|
anchor = self.anchor,
|
|
|
|
FrameContainer:new{
|
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
|
|
|
bordersize = Size.border.window,
|
|
|
|
radius = Size.radius.window,
|
|
|
|
padding = Size.padding.button,
|
|
|
|
-- No padding at top or bottom to make all buttons
|
|
|
|
-- look the same size
|
|
|
|
padding_top = 0,
|
|
|
|
padding_bottom = 0,
|
|
|
|
VerticalGroup:new{
|
|
|
|
title_widget,
|
|
|
|
separator,
|
2023-05-11 18:23:45 +00:00
|
|
|
scontainer or self.buttontable,
|
2023-06-04 12:29:30 +00:00
|
|
|
},
|
|
|
|
}
|
2021-02-02 03:27:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self[1] = CenterContainer:new{
|
|
|
|
dimen = Screen:getSize(),
|
|
|
|
self.movable,
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
2014-01-22 18:03:44 +00:00
|
|
|
end
|
|
|
|
|
2023-11-19 07:52:51 +00:00
|
|
|
function ButtonDialog:getContentSize()
|
|
|
|
return self.movable.dimen
|
|
|
|
end
|
|
|
|
|
2023-05-11 18:23:45 +00:00
|
|
|
function ButtonDialog:getButtonById(id)
|
|
|
|
return self.buttontable:getButtonById(id)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ButtonDialog:getScrolledOffset()
|
|
|
|
if self.cropping_widget then
|
|
|
|
return self.cropping_widget:getScrolledOffset()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ButtonDialog:setScrolledOffset(offset_point)
|
|
|
|
if offset_point and self.cropping_widget then
|
|
|
|
return self.cropping_widget:setScrolledOffset(offset_point)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-04 12:29:30 +00:00
|
|
|
function ButtonDialog:setTitle(title)
|
|
|
|
self.title = title
|
|
|
|
self:free()
|
|
|
|
self:init()
|
|
|
|
UIManager:setDirty("all", "ui")
|
|
|
|
end
|
|
|
|
|
2014-12-01 14:39:41 +00:00
|
|
|
function ButtonDialog:onShow()
|
|
|
|
UIManager:setDirty(self, function()
|
2021-02-02 03:27:14 +00:00
|
|
|
return "ui", self.movable.dimen
|
2014-12-01 14:39:41 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ButtonDialog:onCloseWidget()
|
|
|
|
UIManager:setDirty(nil, function()
|
2021-02-02 03:27:14 +00:00
|
|
|
return "flashui", self.movable.dimen
|
2014-12-01 14:39:41 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2023-11-12 05:44:56 +00:00
|
|
|
function ButtonDialog:onClose()
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.tap_close_callback then
|
|
|
|
self.tap_close_callback()
|
|
|
|
end
|
2023-11-12 05:44:56 +00:00
|
|
|
UIManager:close(self)
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
2014-01-22 18:03:44 +00:00
|
|
|
end
|
|
|
|
|
2023-11-12 05:44:56 +00:00
|
|
|
function ButtonDialog:onTapClose(arg, ges)
|
|
|
|
if ges.pos:notIntersectWith(self.movable.dimen) then
|
|
|
|
self:onClose()
|
|
|
|
end
|
2014-06-10 07:57:10 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2014-11-30 18:26:29 +00:00
|
|
|
function ButtonDialog:paintTo(...)
|
|
|
|
InputContainer.paintTo(self, ...)
|
2021-02-02 03:27:14 +00:00
|
|
|
self.dimen = self.movable.dimen
|
2014-11-30 18:26:29 +00:00
|
|
|
end
|
|
|
|
|
2014-01-22 18:03:44 +00:00
|
|
|
return ButtonDialog
|