mirror of
https://github.com/koreader/koreader.git
synced 2025-08-10 00:52:38 +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.
This commit is contained in:
@@ -15,28 +15,8 @@ It handles event propagation and painting (with different alignments) for its ch
|
||||
|
||||
local Geom = require("ui/geometry")
|
||||
local Widget = require("ui/widget/widget")
|
||||
local Device = require("device")
|
||||
|
||||
if Device.should_restrict_JIT then
|
||||
jit.off(true, true)
|
||||
end
|
||||
|
||||
local WidgetContainer = Widget:new()
|
||||
|
||||
function WidgetContainer:init()
|
||||
if self.dimen then
|
||||
if self.initDimen then
|
||||
self:initDimen()
|
||||
else
|
||||
if not self.dimen.w then
|
||||
self.dimen.w = self[1]:getSize().w
|
||||
end
|
||||
if not self.dimen.h then
|
||||
self.dimen.h = self[1]:getSize().h
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local WidgetContainer = Widget:extend{}
|
||||
|
||||
function WidgetContainer:getSize()
|
||||
if self.dimen then
|
||||
@@ -46,7 +26,7 @@ function WidgetContainer:getSize()
|
||||
-- return size of first child widget
|
||||
return self[1]:getSize()
|
||||
else
|
||||
return Geom:new{ w = 0, h = 0 }
|
||||
return Geom:new{ x= 0, y = 0, w = 0, h = 0 }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -65,35 +45,42 @@ function WidgetContainer:clear(skip_free)
|
||||
end
|
||||
|
||||
function WidgetContainer:paintTo(bb, x, y)
|
||||
-- default to pass request to first child widget
|
||||
if self[1] then
|
||||
x = x + (self.dimen.x or 0)
|
||||
y = y + (self.dimen.y or 0)
|
||||
if self.align == "top" then
|
||||
local contentSize = self[1]:getSize()
|
||||
self[1]:paintTo(bb,
|
||||
x + math.floor((self.dimen.w - contentSize.w)/2), y)
|
||||
elseif self.align == "bottom" then
|
||||
local contentSize = self[1]:getSize()
|
||||
self[1]:paintTo(bb,
|
||||
x + math.floor((self.dimen.w - contentSize.w)/2),
|
||||
y + (self.dimen.h - contentSize.h))
|
||||
elseif self.align == "center" then
|
||||
local contentSize = self[1]:getSize()
|
||||
self[1]:paintTo(bb,
|
||||
x + math.floor((self.dimen.w - contentSize.w)/2),
|
||||
y + math.floor((self.dimen.h - contentSize.h)/2))
|
||||
else
|
||||
return self[1]:paintTo(bb, x, y)
|
||||
end
|
||||
-- Forward painting duties to our first child widget
|
||||
if self[1] == nil then
|
||||
return
|
||||
end
|
||||
|
||||
if not self.dimen then
|
||||
local content_size = self[1]:getSize()
|
||||
self.dimen = Geom:new{x = 0, y = 0, w = content_size.w, h = content_size.h}
|
||||
end
|
||||
|
||||
x = x + (self.dimen.x or 0)
|
||||
y = y + (self.dimen.y or 0)
|
||||
if self.align == "top" then
|
||||
local contentSize = self[1]:getSize()
|
||||
self[1]:paintTo(bb,
|
||||
x + math.floor((self.dimen.w - contentSize.w)/2), y)
|
||||
elseif self.align == "bottom" then
|
||||
local contentSize = self[1]:getSize()
|
||||
self[1]:paintTo(bb,
|
||||
x + math.floor((self.dimen.w - contentSize.w)/2),
|
||||
y + (self.dimen.h - contentSize.h))
|
||||
elseif self.align == "center" then
|
||||
local contentSize = self[1]:getSize()
|
||||
self[1]:paintTo(bb,
|
||||
x + math.floor((self.dimen.w - contentSize.w)/2),
|
||||
y + math.floor((self.dimen.h - contentSize.h)/2))
|
||||
else
|
||||
return self[1]:paintTo(bb, x, y)
|
||||
end
|
||||
end
|
||||
|
||||
function WidgetContainer:propagateEvent(event)
|
||||
-- propagate to children
|
||||
-- Propagate to children
|
||||
for _, widget in ipairs(self) do
|
||||
if widget:handleEvent(event) then
|
||||
-- stop propagating when an event handler returns true
|
||||
-- Stop propagating when an event handler returns true
|
||||
return true
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user