2
0
mirror of https://github.com/koreader/koreader synced 2024-11-16 06:12:56 +00:00
koreader/frontend/ui/widget/listview.lua
NiLuJe fadee1f5dc
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 02:14:48 +02:00

128 lines
3.8 KiB
Lua

--[[--
Widget component that handles pagination for a list of items.
Example:
local list_view = ListView:new{
height = Screen:scaleBySize(400),
width = Screen:scaleBySize(200),
page_update_cb = function(curr_page_num, total_pages)
-- This callback function will be called whenever a page
-- turn event is triggered. You can use it to update
-- information on the parent widget.
end,
items = {
FrameContainer:new{
bordersize = 0,
background = Blitbuffer.COLOR_WHITE,
TextWidget:new{
text = "foo",
face = Font:getFace("cfont"),
}
},
FrameContainer:new{
bordersize = 0,
background = Blitbuffer.COLOR_LIGHT_GRAY,
TextWidget:new{
text = "bar",
face = Font:getFace("cfont"),
}
},
-- You can add as many widgets as you want here...
}
}
Note that ListView is created mainly to be used as a building block for other
widgets like @{ui.widget.networksetting|NetworkSetting}, so they can share the same pagination code.
]]
local BD = require("ui/bidi")
local Blitbuffer = require("ffi/blitbuffer")
local Device = require("device")
local FrameContainer = require("ui/widget/container/framecontainer")
local Geom = require("ui/geometry")
local GestureRange = require("ui/gesturerange")
local InputContainer = require("ui/widget/container/inputcontainer")
local Size = require("ui/size")
local VerticalGroup = require("ui/widget/verticalgroup")
local ListView = InputContainer:extend{
width = nil,
height = nil,
padding = nil,
item_height = nil,
itmes = nil,
}
function ListView:init()
if #self.items <= 0 then return end
self.show_page = 1
self.dimen = Geom:new{x = 0, y = 0, w = self.width, h = self.height}
if Device:isTouchDevice() then
self.ges_events.Swipe = {
GestureRange:new{
ges = "swipe",
range = self.dimen,
}
}
end
local padding = self.padding or Size.padding.large
self.item_height = self.item_height or self.items[1]:getSize().h
self.item_width = self.dimen.w - 2 * padding
self.items_per_page = math.floor(self.height / self.item_height)
self.main_content = VerticalGroup:new{}
self:_populateItems()
self[1] = FrameContainer:new{
height = self.dimen.h,
padding = padding,
bordersize = 0,
background = Blitbuffer.COLOR_WHITE,
self.main_content,
}
end
-- make sure self.item_height are set before calling this
function ListView:_populateItems()
self.pages = math.ceil(#self.items / self.items_per_page)
self.main_content:clear()
local idx_offset = (self.show_page - 1) * self.items_per_page
for idx = 1, self.items_per_page do
local item = self.items[idx_offset + idx]
if item == nil then break end
table.insert(self.main_content, item)
end
self.page_update_cb(self.show_page, self.pages)
end
function ListView:nextPage()
local new_page = math.min(self.show_page+1, self.pages)
if new_page > self.show_page then
self.show_page = new_page
self:_populateItems()
end
end
function ListView:prevPage()
local new_page = math.max(self.show_page-1, 1)
if new_page < self.show_page then
self.show_page = new_page
self:_populateItems()
end
end
function ListView:onSwipe(arg, ges_ev)
local direction = BD.flipDirectionIfMirroredUILayout(ges_ev.direction)
if direction == "west" then
self:nextPage()
return true
elseif direction == "east" then
self:prevPage()
return true
end
end
return ListView