TextEditor: allow scrolling by lines with Pan (#4145)

Feature of ScrollTextWidget, only used for now by TextEditor.
Pan is like Swipe, but wait a bit at end of gesture to release:
the line on which Pan was started will be moved to where Pan is
released.

May conflict with MovableContainer (so not enabled for DictQuickLookup,
where it could have been nice - but it would work only with text
dictionaries, not with HTML ones, as ScrollHtmlWidget can't really
do that).
This commit is contained in:
poire-z
2018-08-10 18:05:09 +02:00
committed by GitHub
parent d222fe25b6
commit adceda15b7
5 changed files with 71 additions and 0 deletions

View File

@@ -9,6 +9,7 @@ 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 Math = require("optmath")
local TextBoxWidget = require("ui/widget/textboxwidget")
local VerticalScrollBar = require("ui/widget/verticalscrollbar")
local UIManager = require("ui/uimanager")
@@ -22,6 +23,8 @@ local ScrollTextWidget = InputContainer:new{
top_line_num = nil,
editable = false,
justified = false,
scroll_callback = nil, -- called with (low, high) when view is scrolled
scroll_by_pan = false, -- allow scrolling by lines with Pan
face = nil,
fgcolor = Blitbuffer.COLOR_BLACK,
width = Screen:scaleBySize(400),
@@ -79,6 +82,20 @@ function ScrollTextWidget:init()
},
},
}
if self.scroll_by_pan then
self.ges_events.PanText = {
GestureRange:new{
ges = "pan",
range = function() return self.dimen end,
},
}
self.ges_events.PanReleaseText = {
GestureRange:new{
ges = "pan_release",
range = function() return self.dimen end,
},
}
end
end
if Device:hasKeyboard() or Device:hasKeys() then
self.key_events = {
@@ -235,4 +252,26 @@ function ScrollTextWidget:onScrollUp()
return true
end
function ScrollTextWidget:onPanText(arg, ges)
self._pan_direction = ges.direction
self._pan_relative_x = ges.relative.x
self._pan_relative_y = ges.relative.y
return true
end
function ScrollTextWidget:onPanReleaseText(arg, ges)
if self._pan_direction and self._pan_relative_y then -- went thru onPanText
if self._pan_direction == "north" or self._pan_direction == "south" then
local nb_lines = Math.round(self._pan_relative_y / self:getLineHeight())
self.text_widget:scrollLines(-nb_lines)
self:updateScrollBar(true)
end
self._pan_direction = nil
self._pan_relative_x = nil
self._pan_relative_y = nil
return true
end
return false
end
return ScrollTextWidget