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

138 lines
5.2 KiB
Lua
Raw Normal View History

local Blitbuffer = require("ffi/blitbuffer")
local Button = require("ui/widget/button")
local Device = require("device")
local FocusManager = require("ui/widget/focusmanager")
2013-10-18 20:38:07 +00:00
local HorizontalGroup = require("ui/widget/horizontalgroup")
local LineWidget = require("ui/widget/linewidget")
2017-09-13 14:56:20 +00:00
local Size = require("ui/size")
local VerticalGroup = require("ui/widget/verticalgroup")
2013-10-18 20:38:07 +00:00
local VerticalSpan = require("ui/widget/verticalspan")
local Geom = require("ui/geometry")
local Screen = Device.screen
2013-06-15 15:13:19 +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 ButtonTable = FocusManager:extend{
width = nil,
2014-03-13 13:52:43 +00:00
buttons = {
{
{text="OK", enabled=true, callback=nil},
{text="Cancel", enabled=false, callback=nil},
},
},
2017-09-13 14:56:20 +00:00
sep_width = Size.line.medium,
padding = Size.padding.default,
2014-03-13 13:52:43 +00:00
zero_sep = false,
2013-06-15 15:13:19 +00:00
}
function ButtonTable:init()
self.width = self.width or math.floor(math.min(Screen:getWidth(), Screen:getHeight()) * 0.9)
self.buttons_layout = {}
self.button_by_id = {}
self.container = VerticalGroup:new{ width = self.width }
table.insert(self, self.container)
2014-03-13 13:52:43 +00:00
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)
2014-03-13 13:52:43 +00:00
end
local row_cnt = #self.buttons
for i = 1, row_cnt do
local buttons_layout_line = {}
2014-03-13 13:52:43 +00:00
local horizontal_group = HorizontalGroup:new{}
local row = self.buttons[i]
local column_cnt = #row
local sizer_space = self.sep_width * (column_cnt - 1) + 2
for j = 1, column_cnt do
local btn_entry = row[j]
2014-03-13 13:52:43 +00:00
local button = Button:new{
text = btn_entry.text,
text_func = btn_entry.text_func,
icon = btn_entry.icon,
icon_width = btn_entry.icon_width,
icon_height = btn_entry.icon_height,
2022-09-17 22:11:03 +00:00
align = btn_entry.align,
enabled = btn_entry.enabled,
callback = function()
if self.show_parent and self.show_parent.movable then
self.show_parent.movable:resetEventState()
end
btn_entry.callback()
end,
hold_callback = btn_entry.hold_callback,
allow_hold_when_disabled = btn_entry.allow_hold_when_disabled,
vsync = btn_entry.vsync,
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
width = math.ceil((self.width - sizer_space)/column_cnt),
max_width = math.ceil((self.width - sizer_space)/column_cnt - 2*self.sep_width - 2*self.padding),
2014-03-13 13:52:43 +00:00
bordersize = 0,
margin = 0,
padding = Size.padding.buttontable, -- a bit taller than standalone buttons, for easier tap
padding_h = 0, -- allow text to take more of the horizontal space
2022-09-17 22:11:03 +00:00
text_font_face = btn_entry.font_face,
text_font_size = btn_entry.font_size,
text_font_bold = btn_entry.font_bold,
2014-05-01 10:37:12 +00:00
show_parent = self.show_parent,
2014-03-13 13:52:43 +00:00
}
if btn_entry.id then
self.button_by_id[btn_entry.id] = button
end
2014-03-13 13:52:43 +00:00
local button_dim = button:getSize()
local vertical_sep = LineWidget:new{
background = Blitbuffer.COLOR_GRAY,
2014-03-13 13:52:43 +00:00
dimen = Geom:new{
w = self.sep_width,
h = button_dim.h,
}
}
buttons_layout_line[j] = button
2014-03-13 13:52:43 +00:00
table.insert(horizontal_group, button)
if j < column_cnt then
2014-03-13 13:52:43 +00:00
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)
2014-03-13 13:52:43 +00:00
end
if column_cnt > 0 then
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
-- Only add lines that are not separator to the focusmanager
table.insert(self.buttons_layout, buttons_layout_line)
end
2014-03-13 13:52:43 +00:00
end -- end for each button line
self:addHorizontalSep(true, false, false)
if Device:hasDPad() then
self.layout = self.buttons_layout
self:refocusWidget()
else
self.key_events = {} -- deregister all key press event listeners
end
2013-06-15 15:13:19 +00:00
end
function ButtonTable: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{
background = black_line and Blitbuffer.COLOR_BLACK or Blitbuffer.COLOR_GRAY,
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 ButtonTable:getButtonById(id)
return self.button_by_id[id] -- nil if not found
end
2013-10-18 20:38:07 +00:00
return ButtonTable