2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/frontend/ui/widget/container/framecontainer.lua

170 lines
5.6 KiB
Lua
Raw Normal View History

2017-04-26 14:21:36 +00:00
--[[--
A FrameContainer is some graphics content (1 widget) that is surrounded by a
frame
Example:
local frame
frame = FrameContainer:new{
2017-09-13 14:56:20 +00:00
radius = Size.radius.window,
bordersize = Size.border.window,
2017-04-26 14:21:36 +00:00
padding = 0,
margin = 0,
background = Blitbuffer.COLOR_WHITE,
VerticalGroup:new{
-- etc
}
}
--]]
local BD = require("ui/bidi")
local Blitbuffer = require("ffi/blitbuffer")
2017-09-11 18:54:27 +00:00
local Geom = require("ui/geometry")
2017-09-13 14:56:20 +00:00
local Size = require("ui/size")
2017-09-11 18:54:27 +00:00
local WidgetContainer = require("ui/widget/container/widgetcontainer")
2013-10-18 20:38:07 +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 FrameContainer = WidgetContainer:extend{
2014-03-13 13:52:43 +00:00
background = nil,
color = Blitbuffer.COLOR_BLACK,
2014-03-13 13:52:43 +00:00
margin = 0,
radius = nil,
inner_bordersize = 0,
2017-09-13 14:56:20 +00:00
bordersize = Size.border.window,
padding = Size.padding.default,
padding_top = nil,
padding_right = nil,
padding_bottom = nil,
padding_left = nil,
2014-03-13 13:52:43 +00:00
width = nil,
height = nil,
invert = false,
allow_mirroring = true,
focusable = false,
focus_border_size = Size.border.window * 2,
focus_border_color = Blitbuffer.COLOR_BLACK,
-- paint hatched background if provided
stripe_color = nil,
stripe_width = nil,
stripe_over = nil, -- draw stripes *after* content is drawn
stripe_over_alpha = 1,
2013-10-18 20:38:07 +00:00
}
function FrameContainer:getSize()
2014-03-13 13:52:43 +00:00
local content_size = self[1]:getSize()
self._padding_top = self.padding_top or self.padding
self._padding_right = self.padding_right or self.padding
self._padding_bottom = self.padding_bottom or self.padding
self._padding_left = self.padding_left or self.padding
2022-01-15 23:42:17 +00:00
if BD.mirroredUILayout() and self.allow_mirroring then
self._padding_left, self._padding_right = self._padding_right, self._padding_left
end
2014-03-13 13:52:43 +00:00
return Geom:new{
w = content_size.w + ( self.margin + self.bordersize ) * 2 + self._padding_left + self._padding_right,
h = content_size.h + ( self.margin + self.bordersize ) * 2 + self._padding_top + self._padding_bottom
2014-03-13 13:52:43 +00:00
}
2013-10-18 20:38:07 +00:00
end
function FrameContainer:onFocus()
if not self.focusable then
return false
end
self._origin_bordersize = self.bordersize
self._origin_border_color = self.color
self.bordersize = self.focus_border_size
self.color = self.focus_border_color
self._focused = true
return true
end
function FrameContainer:onUnfocus()
if not self.focusable then
return false
end
if self._focused then
self.bordersize = self._origin_bordersize
self.color = self._origin_border_color
self._focused = nil
return true
end
return false
end
2013-10-18 20:38:07 +00:00
function FrameContainer:paintTo(bb, x, y)
2014-03-13 13:52:43 +00:00
local my_size = self:getSize()
if not self.dimen then
self.dimen = Geom:new{
x = x, y = y,
2024-02-26 18:51:01 +00:00
w = my_size.w, h = my_size.h,
}
else
self.dimen.x = x
self.dimen.y = y
end
2014-03-13 13:52:43 +00:00
local container_width = self.width or my_size.w
local container_height = self.height or my_size.h
2013-10-18 20:38:07 +00:00
local shift_x = 0
2022-01-15 23:42:17 +00:00
if BD.mirroredUILayout() and self.allow_mirroring then
shift_x = container_width - my_size.w
end
2014-03-13 13:52:43 +00:00
if self.background then
2024-02-26 18:51:01 +00:00
if not self.radius or not self.bordersize then
bb:paintRoundedRect(x, y,
container_width, container_height,
self.background, self.radius)
else
bb:paintRoundedRect(x, y,
container_width, container_height,
self.background, self.radius + self.bordersize)
end
2014-03-13 13:52:43 +00:00
end
if self.stripe_width and self.stripe_color and not self.stripe_over then
-- (No support for radius when hatched/stripe)
bb:hatchRect(x, y,
container_width, container_height,
self.stripe_width, self.stripe_color)
end
if self.inner_bordersize > 0 then
--- @warning This doesn't actually support radius, it'll always be a square.
bb:paintInnerBorder(x + self.margin, y + self.margin,
container_width - self.margin * 2,
container_height - self.margin * 2,
self.inner_bordersize, self.color, self.radius)
end
2014-03-13 13:52:43 +00:00
if self.bordersize > 0 then
2024-02-26 18:51:01 +00:00
local anti_alias = G_reader_settings:nilOrTrue("anti_alias_ui")
2014-03-13 13:52:43 +00:00
bb:paintBorder(x + self.margin, y + self.margin,
container_width - self.margin * 2,
container_height - self.margin * 2,
2024-02-26 18:51:01 +00:00
self.bordersize, self.color, self.radius, anti_alias)
2014-03-13 13:52:43 +00:00
end
if self[1] then
self[1]:paintTo(bb,
x + self.margin + self.bordersize + self._padding_left + shift_x,
y + self.margin + self.bordersize + self._padding_top)
2014-03-13 13:52:43 +00:00
end
if self.stripe_width and self.stripe_color and self.stripe_over then
-- (No support for radius when hatched/stripe)
-- We don't want to draw the stripes over any border
local pad = self.margin + math.max(self.bordersize, self.inner_bordersize)
bb:hatchRect(x + pad, y + pad,
container_width - pad * 2, container_height - pad * 2,
self.stripe_width, self.stripe_color, self.stripe_over_alpha)
end
2014-03-13 13:52:43 +00:00
if self.invert then
bb:invertRect(x + self.bordersize, y + self.bordersize,
container_width - 2*self.bordersize,
container_height - 2*self.bordersize)
end
if self.dim then
bb:dimRect(x + self.bordersize, y + self.bordersize,
container_width - 2*self.bordersize,
container_height - 2*self.bordersize)
end
2013-10-18 20:38:07 +00:00
end
return FrameContainer