2018-01-07 19:24:15 +00:00
|
|
|
--[[--
|
|
|
|
HTML widget with vertical scroll bar.
|
|
|
|
--]]
|
|
|
|
|
2019-12-06 21:55:39 +00:00
|
|
|
local BD = require("ui/bidi")
|
2018-01-07 19:24:15 +00:00
|
|
|
local Device = require("device")
|
|
|
|
local HtmlBoxWidget = require("ui/widget/htmlboxwidget")
|
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local GestureRange = require("ui/gesturerange")
|
|
|
|
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
|
|
|
local HorizontalSpan = require("ui/widget/horizontalspan")
|
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local VerticalScrollBar = require("ui/widget/verticalscrollbar")
|
|
|
|
local Input = Device.input
|
|
|
|
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 ScrollHtmlWidget = InputContainer:extend{
|
2018-01-07 19:24:15 +00:00
|
|
|
html_body = nil,
|
|
|
|
css = nil,
|
2019-03-31 20:00:07 +00:00
|
|
|
default_font_size = Screen:scaleBySize(24), -- same as infofont
|
2018-01-07 19:24:15 +00:00
|
|
|
htmlbox_widget = nil,
|
|
|
|
v_scroll_bar = nil,
|
|
|
|
dialog = nil,
|
2018-01-15 22:51:43 +00:00
|
|
|
html_link_tapped_callback = nil,
|
2018-01-07 19:24:15 +00:00
|
|
|
dimen = nil,
|
|
|
|
width = 0,
|
|
|
|
height = 0,
|
|
|
|
scroll_bar_width = Screen:scaleBySize(6),
|
|
|
|
text_scroll_span = Screen:scaleBySize(12),
|
|
|
|
}
|
|
|
|
|
|
|
|
function ScrollHtmlWidget:init()
|
|
|
|
self.htmlbox_widget = HtmlBoxWidget:new{
|
|
|
|
dimen = Geom:new{
|
|
|
|
w = self.width - self.scroll_bar_width - self.text_scroll_span,
|
|
|
|
h = self.height,
|
|
|
|
},
|
2018-01-15 22:51:43 +00:00
|
|
|
html_link_tapped_callback = self.html_link_tapped_callback,
|
2018-01-07 19:24:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.htmlbox_widget:setContent(self.html_body, self.css, self.default_font_size)
|
|
|
|
|
|
|
|
self.v_scroll_bar = VerticalScrollBar:new{
|
|
|
|
enable = self.htmlbox_widget.page_count > 1,
|
|
|
|
width = self.scroll_bar_width,
|
|
|
|
height = self.height,
|
2020-08-26 18:44:58 +00:00
|
|
|
scroll_callback = function(ratio)
|
|
|
|
self:scrollToRatio(ratio)
|
|
|
|
end
|
2018-01-07 19:24:15 +00:00
|
|
|
}
|
|
|
|
|
2021-01-31 01:51:40 +00:00
|
|
|
self:_updateScrollBar()
|
2018-01-07 19:24:15 +00:00
|
|
|
|
|
|
|
local horizontal_group = HorizontalGroup:new{}
|
|
|
|
table.insert(horizontal_group, self.htmlbox_widget)
|
|
|
|
table.insert(horizontal_group, HorizontalSpan:new{width=self.text_scroll_span})
|
|
|
|
table.insert(horizontal_group, self.v_scroll_bar)
|
|
|
|
self[1] = horizontal_group
|
|
|
|
|
|
|
|
self.dimen = Geom:new(self[1]:getSize())
|
|
|
|
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events = {
|
2018-01-09 20:37:50 +00:00
|
|
|
ScrollText = {
|
2018-01-07 19:24:15 +00:00
|
|
|
GestureRange:new{
|
|
|
|
ges = "swipe",
|
|
|
|
range = function() return self.dimen end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
TapScrollText = { -- allow scrolling with tap
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = function() return self.dimen end,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
A few graphics fixes after #4541 (#4554)
* Various FocusManager related tweaks to limit its usage to devices with a DPad, and prevent initial button highlights in Dialogs on devices where it makes no sense (i.e., those without a DPad. And even on DPad devices, I'm not even sure how we'd go about making one of those pop up anyway, because no Touch ;)!).
* One mysterious fix to text-only Buttons so that the flash_ui highlight always works, and always honors `FrameContainer`'s pill shape. (Before that, an unhighlight on a text button with a callback that didn't repaint anything [say, the find first/find last buttons in the Reader's search bar when you're already on the first/last match] would do a square black highlight, and a white pill-shaped unhighlight (leaving the black corners visible)).
The workaround makes *absolutely* no sense to me (as `self[1] -> self.frame`, AFAICT), but it works, and ensures all highlights/unhighlights are pill-shaped, so at least we're not doing maths for rounded corners for nothing ;).
2019-02-07 23:56:32 +00:00
|
|
|
if Device:hasKeys() then
|
2018-01-07 19:24:15 +00:00
|
|
|
self.key_events = {
|
2022-10-27 00:01:51 +00:00
|
|
|
ScrollDown = { { Input.group.PgFwd } },
|
|
|
|
ScrollUp = { { Input.group.PgBack } },
|
2018-01-07 19:24:15 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-01-31 01:51:40 +00:00
|
|
|
-- Not to be confused with ScrollTextWidget's updateScrollBar, which has user-visible effects.
|
|
|
|
-- This simply updates the scroll bar's internal state according to the current page & page count.
|
|
|
|
function ScrollHtmlWidget:_updateScrollBar()
|
|
|
|
self.v_scroll_bar:set((self.htmlbox_widget.page_number-1) / self.htmlbox_widget.page_count, self.htmlbox_widget.page_number / self.htmlbox_widget.page_count)
|
|
|
|
end
|
|
|
|
|
2018-10-10 16:50:24 +00:00
|
|
|
function ScrollHtmlWidget:getSinglePageHeight()
|
|
|
|
return self.htmlbox_widget:getSinglePageHeight()
|
|
|
|
end
|
|
|
|
|
2021-01-31 01:51:40 +00:00
|
|
|
-- Reset the scrolling *state* to the top of the document, but don't actually re-render/refresh anything.
|
|
|
|
-- (Useful when replacing a Scroll*Widget during an update call, c.f., DictQuickLookup).
|
|
|
|
function ScrollHtmlWidget:resetScroll()
|
|
|
|
self.htmlbox_widget.page_number = 1
|
|
|
|
self:_updateScrollBar()
|
|
|
|
|
|
|
|
self.v_scroll_bar.enable = self.htmlbox_widget.page_count > 1
|
|
|
|
end
|
|
|
|
|
2018-01-31 08:16:34 +00:00
|
|
|
function ScrollHtmlWidget:scrollToRatio(ratio)
|
|
|
|
ratio = math.max(0, math.min(1, ratio)) -- ensure ratio is between 0 and 1 (100%)
|
2020-08-26 18:44:58 +00:00
|
|
|
local page_num = 1 + math.floor((self.htmlbox_widget.page_count) * ratio)
|
|
|
|
if page_num > self.htmlbox_widget.page_count then
|
|
|
|
page_num = self.htmlbox_widget.page_count
|
|
|
|
end
|
2018-01-31 08:16:34 +00:00
|
|
|
if page_num == self.htmlbox_widget.page_number then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
self.htmlbox_widget.page_number = page_num
|
2021-01-31 01:51:40 +00:00
|
|
|
self:_updateScrollBar()
|
|
|
|
|
2018-01-31 08:16:34 +00:00
|
|
|
self.htmlbox_widget:freeBb()
|
|
|
|
self.htmlbox_widget:_render()
|
2021-01-31 01:51:40 +00:00
|
|
|
|
2021-02-02 03:27:14 +00:00
|
|
|
-- If our dialog is currently wrapped in a MovableContainer and that container has been made translucent,
|
|
|
|
-- reset the alpha and refresh the whole thing, because we assume that a scroll means the user actually wants to
|
|
|
|
-- *read* the content, which is kinda hard on a nearly transparent widget ;).
|
|
|
|
if self.dialog.movable and self.dialog.movable.alpha then
|
|
|
|
self.dialog.movable.alpha = nil
|
|
|
|
UIManager:setDirty(self.dialog, function()
|
|
|
|
return "partial", self.dialog.movable.dimen
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
UIManager:setDirty(self.dialog, function()
|
|
|
|
return "partial", self.dimen
|
|
|
|
end)
|
|
|
|
end
|
2018-01-31 08:16:34 +00:00
|
|
|
end
|
|
|
|
|
2018-01-07 19:24:15 +00:00
|
|
|
function ScrollHtmlWidget:scrollText(direction)
|
|
|
|
if direction == 0 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
if direction > 0 then
|
|
|
|
if self.htmlbox_widget.page_number >= self.htmlbox_widget.page_count then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
self.htmlbox_widget.page_number = self.htmlbox_widget.page_number + 1
|
|
|
|
elseif direction < 0 then
|
|
|
|
if self.htmlbox_widget.page_number <= 1 then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
self.htmlbox_widget.page_number = self.htmlbox_widget.page_number - 1
|
|
|
|
end
|
2021-01-31 01:51:40 +00:00
|
|
|
self:_updateScrollBar()
|
2018-01-07 19:24:15 +00:00
|
|
|
|
|
|
|
self.htmlbox_widget:freeBb()
|
|
|
|
self.htmlbox_widget:_render()
|
|
|
|
|
2021-02-02 03:27:14 +00:00
|
|
|
-- Handle the container's alpha as above...
|
|
|
|
if self.dialog.movable and self.dialog.movable.alpha then
|
|
|
|
self.dialog.movable.alpha = nil
|
|
|
|
UIManager:setDirty(self.dialog, function()
|
|
|
|
return "partial", self.dialog.movable.dimen
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
UIManager:setDirty(self.dialog, function()
|
|
|
|
return "partial", self.dimen
|
|
|
|
end)
|
|
|
|
end
|
2018-01-07 19:24:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function ScrollHtmlWidget:onScrollText(arg, ges)
|
|
|
|
if ges.direction == "north" then
|
|
|
|
self:scrollText(1)
|
|
|
|
return true
|
|
|
|
elseif ges.direction == "south" then
|
|
|
|
self:scrollText(-1)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
-- if swipe west/east, let it propagate up (e.g. for quickdictlookup to
|
|
|
|
-- go to next/prev result)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ScrollHtmlWidget:onTapScrollText(arg, ges)
|
2019-12-06 21:55:39 +00:00
|
|
|
if BD.flipIfMirroredUILayout(ges.pos.x < Screen:getWidth()/2) then
|
2021-01-09 20:59:31 +00:00
|
|
|
return self:onScrollUp()
|
2018-01-07 19:24:15 +00:00
|
|
|
else
|
2021-01-09 20:59:31 +00:00
|
|
|
return self:onScrollDown()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ScrollHtmlWidget:onScrollUp()
|
|
|
|
if self.htmlbox_widget.page_number > 1 then
|
|
|
|
self:scrollText(-1)
|
|
|
|
return true
|
2018-01-07 19:24:15 +00:00
|
|
|
end
|
|
|
|
-- if we couldn't scroll (because we're already at top or bottom),
|
|
|
|
-- let it propagate up (e.g. for quickdictlookup to go to next/prev result)
|
|
|
|
end
|
|
|
|
|
|
|
|
function ScrollHtmlWidget:onScrollDown()
|
2021-01-09 20:59:31 +00:00
|
|
|
if self.htmlbox_widget.page_number < self.htmlbox_widget.page_count then
|
|
|
|
self:scrollText(1)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
-- if we couldn't scroll (because we're already at top or bottom),
|
|
|
|
-- let it propagate up (e.g. for quickdictlookup to go to next/prev result)
|
2018-01-07 19:24:15 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
return ScrollHtmlWidget
|