2017-08-15 07:28:27 +00:00
|
|
|
--[[--
|
|
|
|
Widget that shows a checkmark (`✓`), an empty box (`□`)
|
|
|
|
or nothing of the same size.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
|
|
|
|
local CheckMark = require("ui/widget/CheckMark")
|
|
|
|
local parent_widget = FrameContainer:new{}
|
|
|
|
table.insert(parent_widget, CheckMark:new{
|
|
|
|
checkable = false, -- shows nothing when false, defaults to true
|
|
|
|
checked = function() end, -- whether the box has a checkmark in it
|
|
|
|
})
|
|
|
|
UIManager:show(parent_widget)
|
|
|
|
|
|
|
|
]]
|
|
|
|
|
2019-12-06 21:55:39 +00:00
|
|
|
local BD = require("ui/bidi")
|
2019-01-15 17:37:36 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2017-08-15 07:28:27 +00:00
|
|
|
local Font = require("ui/font")
|
|
|
|
local OverlapGroup = require("ui/widget/overlapgroup")
|
|
|
|
local TextWidget = require("ui/widget/textwidget")
|
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 WidgetContainer = require("ui/widget/container/widgetcontainer")
|
2017-08-15 07:28:27 +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 CheckMark = WidgetContainer:extend{
|
2017-08-15 07:28:27 +00:00
|
|
|
checkable = true,
|
|
|
|
checked = false,
|
2019-01-15 17:37:36 +00:00
|
|
|
enabled = true,
|
2017-08-15 07:28:27 +00:00
|
|
|
face = Font:getFace("smallinfofont"),
|
|
|
|
width = 0,
|
|
|
|
height = 0,
|
2021-08-17 13:20:41 +00:00
|
|
|
baseline = 0,
|
2017-08-15 07:28:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function CheckMark:init()
|
2019-12-06 21:55:39 +00:00
|
|
|
-- Adjust these checkmarks if mirroring UI (para_direction_rtl should
|
|
|
|
-- follow BD.mirroredUILayout(), and not the set or reverted text
|
|
|
|
-- direction, for proper rendering on the right).
|
2022-01-15 23:42:17 +00:00
|
|
|
local para_direction_rtl = BD.mirroredUILayout()
|
2017-08-15 07:28:27 +00:00
|
|
|
local checked_widget = TextWidget:new{
|
2017-08-14 19:42:15 +00:00
|
|
|
text = " ✓", -- preceded by thin space for better alignment
|
2017-08-15 07:28:27 +00:00
|
|
|
face = self.face,
|
2019-12-06 21:55:39 +00:00
|
|
|
para_direction_rtl = para_direction_rtl,
|
2017-08-15 07:28:27 +00:00
|
|
|
}
|
2021-08-17 13:20:41 +00:00
|
|
|
self.baseline = checked_widget:getBaseline()
|
2017-08-15 07:28:27 +00:00
|
|
|
local unchecked_widget = TextWidget:new{
|
|
|
|
text = "▢ ",
|
|
|
|
face = self.face,
|
2019-12-06 21:55:39 +00:00
|
|
|
para_direction_rtl = para_direction_rtl,
|
2017-08-15 07:28:27 +00:00
|
|
|
}
|
2019-01-15 17:37:36 +00:00
|
|
|
local disabled_checked_widget = TextWidget:new{
|
|
|
|
text = " ✓", -- preceded by thin space for better alignment
|
|
|
|
face = self.face,
|
2019-03-14 19:58:45 +00:00
|
|
|
fgcolor = Blitbuffer.COLOR_DARK_GRAY,
|
2019-12-06 21:55:39 +00:00
|
|
|
para_direction_rtl = para_direction_rtl,
|
2019-01-15 17:37:36 +00:00
|
|
|
}
|
|
|
|
local disabled_unchecked_widget = TextWidget:new{
|
|
|
|
text = "▢ ",
|
|
|
|
face = self.face,
|
2019-03-14 19:58:45 +00:00
|
|
|
fgcolor = Blitbuffer.COLOR_DARK_GRAY,
|
2019-12-06 21:55:39 +00:00
|
|
|
para_direction_rtl = para_direction_rtl,
|
2019-01-15 17:37:36 +00:00
|
|
|
}
|
2017-08-15 07:28:27 +00:00
|
|
|
local empty_widget = TextWidget:new{
|
|
|
|
text = "",
|
|
|
|
face = self.face,
|
2019-12-06 21:55:39 +00:00
|
|
|
para_direction_rtl = para_direction_rtl,
|
2017-08-15 07:28:27 +00:00
|
|
|
}
|
2019-01-15 17:37:36 +00:00
|
|
|
local widget
|
|
|
|
if self.checkable then
|
|
|
|
if self.enabled then
|
|
|
|
widget = OverlapGroup:new{
|
|
|
|
(self.checked and checked_widget or empty_widget),
|
|
|
|
unchecked_widget
|
|
|
|
}
|
|
|
|
else
|
|
|
|
widget = OverlapGroup:new{
|
|
|
|
(self.checked and disabled_checked_widget or empty_widget),
|
|
|
|
disabled_unchecked_widget
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
widget = empty_widget
|
|
|
|
end
|
|
|
|
self[1] = widget
|
2017-08-14 19:42:15 +00:00
|
|
|
self.dimen = unchecked_widget:getSize()
|
2017-08-15 07:28:27 +00:00
|
|
|
end
|
|
|
|
|
2022-12-31 00:01:17 +00:00
|
|
|
function CheckMark:paintTo(bb, x, y)
|
|
|
|
-- NOTE: Account for alignment/offsets computation being tacked on to self.dimen...
|
|
|
|
-- This is dumb and probably means we're doing something wonky... somewhere, but it works,
|
|
|
|
-- and allows us to keep sensible coordinates in dimen, so that they can be used for hitbox checks.
|
|
|
|
WidgetContainer.paintTo(self, bb, x - self.dimen.x, y - self.dimen.y)
|
|
|
|
self.dimen.x = x
|
|
|
|
self.dimen.y = y
|
|
|
|
end
|
|
|
|
|
2017-08-15 07:28:27 +00:00
|
|
|
return CheckMark
|