2
0
mirror of https://github.com/koreader/koreader synced 2024-11-13 19:11:25 +00:00
koreader/frontend/ui/widget/container/underlinecontainer.lua

48 lines
1.4 KiB
Lua
Raw Normal View History

2017-04-26 14:21:36 +00:00
--[[--
An UnderlineContainer is a WidgetContainer that is able to paint
a line under its child node.
2013-10-18 20:38:07 +00:00
--]]
2017-04-26 14:21:36 +00:00
local Blitbuffer = require("ffi/blitbuffer")
local Geom = require("ui/geometry")
2017-09-13 14:56:20 +00:00
local Size = require("ui/size")
2017-04-26 14:21:36 +00:00
local WidgetContainer = require("ui/widget/container/widgetcontainer")
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 UnderlineContainer = WidgetContainer:extend{
2017-09-13 14:56:20 +00:00
linesize = Size.line.thick,
padding = Size.padding.tiny,
--- @todo shouldn't this default to black instead?
color = Blitbuffer.COLOR_WHITE,
2014-03-13 13:52:43 +00:00
vertical_align = "top",
2013-10-18 20:38:07 +00:00
}
function UnderlineContainer:getSize()
2014-03-13 13:52:43 +00:00
local contentSize = self[1]:getSize()
return Geom:new{
w = contentSize.w,
2017-09-13 14:56:20 +00:00
h = contentSize.h + self.linesize + 2*self.padding
2014-03-13 13:52:43 +00:00
}
2013-10-18 20:38:07 +00:00
end
function UnderlineContainer:paintTo(bb, x, y)
2014-03-13 13:52:43 +00:00
local container_size = self:getSize()
2014-12-02 12:30:55 +00:00
self.dimen = Geom:new{
x = x, y = y,
w = container_size.w,
h = container_size.h
}
local content_size = self[1]:getSize()
2014-03-13 13:52:43 +00:00
local p_y = y
if self.vertical_align == "center" then
p_y = math.floor((container_size.h - content_size.h) / 2) + y
elseif self.vertical_align == "bottom" then
p_y = (container_size.h - content_size.h) + y
end
self[1]:paintTo(bb, x, p_y)
bb:paintRect(x, y + container_size.h - self.linesize,
container_size.w, self.linesize, self.color)
2013-10-18 20:38:07 +00:00
end
return UnderlineContainer