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

429 lines
14 KiB
Lua
Raw Normal View History

2017-09-13 14:56:20 +00:00
--[[--
Displays some text in a scrollable view.
@usage
local textviewer = TextViewer:new{
title = _("I can scroll!"),
text = _("I'll need to be longer than this example to scroll."),
}
UIManager:show(textviewer)
]]
local BD = require("ui/bidi")
local Blitbuffer = require("ffi/blitbuffer")
local ButtonTable = require("ui/widget/buttontable")
local CenterContainer = require("ui/widget/container/centercontainer")
2022-09-13 21:09:49 +00:00
local CheckButton = require("ui/widget/checkbutton")
local Device = require("device")
local Geom = require("ui/geometry")
local Font = require("ui/font")
local FrameContainer = require("ui/widget/container/framecontainer")
local GestureRange = require("ui/gesturerange")
local InputContainer = require("ui/widget/container/inputcontainer")
2022-09-13 21:09:49 +00:00
local InputDialog = require("ui/widget/inputdialog")
local MovableContainer = require("ui/widget/container/movablecontainer")
2022-09-13 21:09:49 +00:00
local Notification = require("ui/widget/notification")
local ScrollTextWidget = require("ui/widget/scrolltextwidget")
2017-09-13 14:56:20 +00:00
local Size = require("ui/size")
2022-01-08 16:45:18 +00:00
local TitleBar = require("ui/widget/titlebar")
local UIManager = require("ui/uimanager")
local VerticalGroup = require("ui/widget/verticalgroup")
local WidgetContainer = require("ui/widget/container/widgetcontainer")
2022-09-13 21:09:49 +00:00
local T = require("ffi/util").template
local util = require("util")
local _ = require("gettext")
local Screen = Device.screen
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 TextViewer = InputContainer:extend{
title = nil,
text = nil,
width = nil,
height = nil,
buttons_table = nil,
-- See TextBoxWidget for details about these options
-- We default to justified and auto_para_direction to adapt
-- to any kind of text we are given (book descriptions,
-- bookmarks' text, translation results...).
-- When used to display more technical text (HTML, CSS,
-- application logs...), it's best to reset them to false.
alignment = "left",
2018-10-08 16:58:43 +00:00
justified = true,
lang = nil,
para_direction_rtl = nil,
auto_para_direction = true,
alignment_strict = false,
2022-01-08 16:45:18 +00:00
title_face = nil, -- use default from TitleBar
title_multilines = nil, -- see TitleBar for details
title_shrink_font_to_fit = nil, -- see TitleBar for details
text_face = Font:getFace("x_smallinfofont"),
fgcolor = Blitbuffer.COLOR_BLACK,
2017-09-13 14:56:20 +00:00
text_padding = Size.padding.large,
text_margin = Size.margin.small,
button_padding = Size.padding.default,
2022-09-13 21:09:49 +00:00
-- Bottom row with Close, Find buttons. Also added when no caller's buttons defined.
add_default_buttons = nil,
default_hold_callback = nil, -- on each default button
find_centered_lines_count = 5, -- line with find results to be not far from the center
}
function TextViewer:init()
-- calculate window dimension
self.align = "center"
self.region = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
self.width = self.width or Screen:getWidth() - Screen:scaleBySize(30)
self.height = self.height or Screen:getHeight() - Screen:scaleBySize(30)
2022-09-13 21:09:49 +00:00
self._find_next = false
self._find_next_button = false
self._old_virtual_line_num = 1
if Device:hasKeys() then
self.key_events.Close = { { Device.input.group.Back } }
end
if Device:isTouchDevice() then
self.ges_events = {
TapClose = {
GestureRange:new{
ges = "tap",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
},
},
Swipe = {
GestureRange:new{
ges = "swipe",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
},
},
MultiSwipe = {
GestureRange:new{
ges = "multiswipe",
range = Geom:new{
x = 0, y = 0,
w = Screen:getWidth(),
h = Screen:getHeight(),
}
},
},
}
end
2022-01-08 16:45:18 +00:00
local titlebar = TitleBar:new{
width = self.width,
align = "left",
with_bottom_line = true,
title = self.title,
title_face = self.title_face,
title_multilines = self.title_multilines,
title_shrink_font_to_fit = self.title_shrink_font_to_fit,
close_callback = function() self:onClose() end,
show_parent = self,
}
-- Callback to enable/disable buttons, for at-top/at-bottom feedback
local prev_at_top = false -- Buttons were created enabled
local prev_at_bottom = false
local function button_update(id, enable)
local button = self.button_table:getButtonById(id)
if button then
if enable then
button:enable()
else
button:disable()
end
button:refresh()
end
end
self._buttons_scroll_callback = function(low, high)
if prev_at_top and low > 0 then
button_update("top", true)
prev_at_top = false
elseif not prev_at_top and low <= 0 then
button_update("top", false)
prev_at_top = true
end
if prev_at_bottom and high < 1 then
button_update("bottom", true)
prev_at_bottom = false
elseif not prev_at_bottom and high >= 1 then
button_update("bottom", false)
prev_at_bottom = true
end
end
-- buttons
2022-09-13 21:09:49 +00:00
local default_buttons =
2022-01-08 16:45:18 +00:00
{
2022-09-13 21:09:49 +00:00
{
text = _("Find"),
id = "find",
callback = function()
if self._find_next then
self:findCallback()
else
self:findDialog()
end
end,
hold_callback = function()
if self._find_next then
self:findDialog()
else
if self.default_hold_callback then
self.default_hold_callback()
end
end
end,
},
{
text = "",
id = "top",
callback = function()
self.scroll_text_w:scrollToTop()
end,
hold_callback = self.default_hold_callback,
allow_hold_when_disabled = true,
},
{
text = "",
id = "bottom",
callback = function()
self.scroll_text_w:scrollToBottom()
end,
hold_callback = self.default_hold_callback,
allow_hold_when_disabled = true,
},
{
text = _("Close"),
callback = function()
self:onClose()
end,
hold_callback = self.default_hold_callback,
},
}
2022-09-13 21:09:49 +00:00
local buttons = self.buttons_table or {}
if self.add_default_buttons or not self.buttons_table then
table.insert(buttons, default_buttons)
end
self.button_table = ButtonTable:new{
2017-09-13 14:56:20 +00:00
width = self.width - 2*self.button_padding,
buttons = buttons,
zero_sep = true,
show_parent = self,
}
2022-09-13 21:09:49 +00:00
local textw_height = self.height - titlebar:getHeight() - self.button_table:getSize().h
self.scroll_text_w = ScrollTextWidget:new{
text = self.text,
face = self.text_face,
fgcolor = self.fgcolor,
width = self.width - 2*self.text_padding - 2*self.text_margin,
height = textw_height - 2*self.text_padding -2*self.text_margin,
dialog = self,
alignment = self.alignment,
2018-10-08 16:58:43 +00:00
justified = self.justified,
lang = self.lang,
para_direction_rtl = self.para_direction_rtl,
auto_para_direction = self.auto_para_direction,
alignment_strict = self.alignment_strict,
scroll_callback = self._buttons_scroll_callback,
}
self.textw = FrameContainer:new{
padding = self.text_padding,
margin = self.text_margin,
bordersize = 0,
self.scroll_text_w
}
self.frame = FrameContainer:new{
2017-09-13 14:56:20 +00:00
radius = Size.radius.window,
padding = 0,
margin = 0,
background = Blitbuffer.COLOR_WHITE,
VerticalGroup:new{
2022-01-08 16:45:18 +00:00
titlebar,
CenterContainer:new{
dimen = Geom:new{
w = self.width,
h = self.textw:getSize().h,
},
self.textw,
},
CenterContainer:new{
dimen = Geom:new{
w = self.width,
2022-09-13 21:09:49 +00:00
h = self.button_table:getSize().h,
},
2022-09-13 21:09:49 +00:00
self.button_table,
}
}
}
self.movable = MovableContainer:new{
ignore_events = {"swipe"},
self.frame,
}
self[1] = WidgetContainer:new{
align = self.align,
dimen = self.region,
self.movable,
}
Enable HW dithering in a few key places (#4541) * Enable HW dithering on supported devices (Clara HD, Forma; Oasis 2, PW4) * FileManager and co. (where appropriate, i.e., when covers are shown) * Book Status * Reader, where appropriate: * CRe: on pages whith image content (for over 7.5% of the screen area, should hopefully leave stuff like bullet points or small scene breaks alone). * Other engines: on user-request (in the gear tab of the bottom menu), via the new "Dithering" knob (will only appear on supported devices). * ScreenSaver * ImageViewer * Minimize repaints when flash_ui is enabled (by, almost everywhere, only repainting the flashing element, and not the toplevel window which hosts it). (The first pass of this involved fixing a few Button instances whose show_parent was wrong, in particular, chevrons in the FM & TopMenu). * Hunted down a few redundant repaints (unneeded setDirty("all") calls), either by switching the widget to nil when only a refresh was needed, and not a repaint, or by passing the appropritate widget to setDirty. (Note to self: Enable *verbose* debugging to catch broken setDirty calls via its post guard). There were also a few instances of 'em right behind a widget close. * Don't repaint the underlying widget when initially showing TopMenu & ConfigDialog. We unfortunately do need to do it when switching tabs, because of their variable heights. * On Kobo, disabled the extra and completely useless full refresh before suspend/reboot/poweroff, as well as on resume. No more double refreshes! * Fix another debug guard in Kobo sysfs_light * Switch ImageWidget & ImageViewer mostly to "ui" updates, which will be better suited to image content pretty much everywhere, REAGL or not. PS: (Almost :100: commits! :D)
2019-02-07 00:14:37 +00:00
UIManager:setDirty(self, function()
return "partial", self.frame.dimen
end)
end
function TextViewer:onCloseWidget()
UIManager:setDirty(nil, function()
return "partial", self.frame.dimen
end)
end
function TextViewer:onShow()
UIManager:setDirty(self, function()
return "ui", self.frame.dimen
end)
return true
end
function TextViewer:onTapClose(arg, ges_ev)
if ges_ev.pos:notIntersectWith(self.frame.dimen) then
self:onClose()
end
return true
end
function TextViewer:onMultiSwipe(arg, ges_ev)
-- For consistency with other fullscreen widgets where swipe south can't be
-- used to close and where we then allow any multiswipe to close, allow any
-- multiswipe to close this widget too.
self:onClose()
return true
end
function TextViewer:onClose()
UIManager:close(self)
2022-04-17 06:59:41 +00:00
if self.close_callback then
self.close_callback()
end
return true
end
function TextViewer:onSwipe(arg, ges)
if ges.pos:intersectWith(self.textw.dimen) then
local direction = BD.flipDirectionIfMirroredUILayout(ges.direction)
if direction == "west" then
self.scroll_text_w:scrollText(1)
return true
elseif direction == "east" then
self.scroll_text_w:scrollText(-1)
return true
else
-- trigger a full-screen HQ flashing refresh
UIManager:setDirty(nil, "full")
-- a long diagonal swipe may also be used for taking a screenshot,
-- so let it propagate
return false
end
end
-- Let our MovableContainer handle swipe outside of text
return self.movable:onMovableSwipe(arg, ges)
end
2022-09-13 21:09:49 +00:00
function TextViewer:findDialog()
local input_dialog
input_dialog = InputDialog:new{
title = _("Enter text to search for"),
input = self.search_value,
buttons = {
{
{
text = _("Cancel"),
callback = function()
UIManager:close(input_dialog)
end,
},
{
text = _("Find first"),
callback = function()
self._find_next = false
self:findCallback(input_dialog)
end,
},
{
text = _("Find next"),
is_enter_default = true,
callback = function()
self._find_next = true
self:findCallback(input_dialog)
end,
},
},
},
}
self.check_button_case = CheckButton:new{
text = _("Case sensitive"),
checked = self.case_sensitive,
parent = input_dialog,
callback = function()
self.case_sensitive = self.check_button_case.checked
end,
}
input_dialog:addWidget(self.check_button_case)
UIManager:show(input_dialog)
input_dialog:onShowKeyboard(true)
2022-09-13 21:09:49 +00:00
end
function TextViewer:findCallback(input_dialog)
if input_dialog then
self.search_value = input_dialog:getInputText()
if self.search_value == "" then return end
UIManager:close(input_dialog)
end
local start_pos = 1
if self._find_next then
local charpos, new_virtual_line_num = self.scroll_text_w:getCharPos()
if math.abs(new_virtual_line_num - self._old_virtual_line_num) > self.find_centered_lines_count then
start_pos = self.scroll_text_w:getCharPosAtXY(0, 0) -- first char of the top line
else
start_pos = (charpos or 0) + 1 -- previous search result
end
end
local char_pos = util.stringSearch(self.text, self.search_value, self.case_sensitive, start_pos)
local msg
if char_pos > 0 then
self.scroll_text_w:moveCursorToCharPos(char_pos, self.find_centered_lines_count)
msg = T(_("Found, screen line %1."), self.scroll_text_w:getCharPosLineNum())
self._find_next = true
self._old_virtual_line_num = select(2, self.scroll_text_w:getCharPos())
else
msg = _("Not found.")
self._find_next = false
self._old_virtual_line_num = 1
end
UIManager:show(Notification:new{
text = msg,
})
if self._find_next_button ~= self._find_next then
self._find_next_button = self._find_next
local button_text = self._find_next and _("Find next") or _("Find")
local find_button = self.button_table:getButtonById("find")
find_button:setText(button_text, find_button.width)
find_button:refresh()
2022-09-13 21:09:49 +00:00
end
end
return TextViewer