2
0
mirror of https://github.com/koreader/koreader synced 2024-11-18 03:25:46 +00:00
koreader/frontend/ui/widget/buttondialog.lua

295 lines
9.9 KiB
Lua
Raw Normal View History

--[[--
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
}
}
}
}
--]]
local Blitbuffer = require("ffi/blitbuffer")
local ButtonTable = require("ui/widget/buttontable")
local CenterContainer = require("ui/widget/container/centercontainer")
local Device = require("device")
2023-06-04 12:29:30 +00:00
local Font = require("ui/font")
local FrameContainer = require("ui/widget/container/framecontainer")
local Geom = require("ui/geometry")
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")
local MovableContainer = require("ui/widget/container/movablecontainer")
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")
local UIManager = require("ui/uimanager")
local VerticalGroup = require("ui/widget/verticalgroup")
local VerticalSpan = require("ui/widget/verticalspan")
2023-06-04 12:29:30 +00:00
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 ButtonDialog = InputContainer:extend{
2014-03-13 13:52:43 +00:00
buttons = nil,
width = nil,
width_factor = nil, -- number between 0 and 1, factor to the smallest of screen width and height
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,
alpha = nil, -- passed to MovableContainer
-- 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
}
function ButtonDialog:init()
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
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,
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
-- 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
local height = self.buttontable:getSize().h
2023-06-04 12:29:30 +00:00
local scontainer, scrollbar_width
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()
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...)
w = buttontable_width + scrollbar_width,
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{
w = buttontable_width + (scrollbar_width or 0),
2023-06-04 12:29:30 +00:00
h = Size.line.medium,
},
}
else
separator = VerticalSpan:new{}
end
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,
scontainer or self.buttontable,
2023-06-04 12:29:30 +00:00
},
}
}
self[1] = CenterContainer:new{
dimen = Screen:getSize(),
self.movable,
2014-03-13 13:52:43 +00:00
}
end
function ButtonDialog:getContentSize()
return self.movable.dimen
end
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
function ButtonDialog:onShow()
UIManager:setDirty(self, function()
return "ui", self.movable.dimen
end)
end
function ButtonDialog:onCloseWidget()
UIManager:setDirty(nil, function()
return "flashui", self.movable.dimen
end)
end
function ButtonDialog:onClose()
2014-03-13 13:52:43 +00:00
if self.tap_close_callback then
self.tap_close_callback()
end
UIManager:close(self)
2014-03-13 13:52:43 +00:00
return true
end
function ButtonDialog:onTapClose(arg, ges)
if ges.pos:notIntersectWith(self.movable.dimen) then
self:onClose()
end
return true
end
2014-11-30 18:26:29 +00:00
function ButtonDialog:paintTo(...)
InputContainer.paintTo(self, ...)
self.dimen = self.movable.dimen
2014-11-30 18:26:29 +00:00
end
return ButtonDialog