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

112 lines
3.4 KiB
Lua
Raw Normal View History

2013-10-18 20:38:07 +00:00
local InputContainer = require("ui/widget/container/inputcontainer")
local TextBoxWidget = require("ui/widget/textboxwidget")
local VerticalScrollBar = require("ui/widget/verticalscrollbar")
local Geom = require("ui/geometry")
local GestureRange = require("ui/gesturerange")
local UIManager = require("ui/uimanager")
local Device = require("device")
local Screen = Device.screen
local Input = Device.input
local HorizontalGroup = require("ui/widget/horizontalgroup")
local HorizontalSpan = require("ui/widget/horizontalspan")
local Blitbuffer = require("ffi/blitbuffer")
2013-10-18 20:38:07 +00:00
--[[
Text widget with vertical scroll bar
--]]
local ScrollTextWidget = InputContainer:new{
2014-03-13 13:52:43 +00:00
text = nil,
face = nil,
fgcolor = Blitbuffer.COLOR_BLACK,
2014-03-13 13:52:43 +00:00
width = 400,
height = 20,
scroll_bar_width = Screen:scaleBySize(6),
text_scroll_span = Screen:scaleBySize(6),
2014-03-13 13:52:43 +00:00
dialog = nil,
2013-10-18 20:38:07 +00:00
}
function ScrollTextWidget:init()
2014-03-13 13:52:43 +00:00
self.text_widget = TextBoxWidget:new{
text = self.text,
face = self.face,
fgcolor = self.fgcolor,
width = self.width - self.scroll_bar_width - self.text_scroll_span,
height = self.height
}
local visible_line_count = self.text_widget:getVisLineCount()
local total_line_count = self.text_widget:getAllLineCount()
self.v_scroll_bar = VerticalScrollBar:new{
enable = visible_line_count < total_line_count,
low = 0,
high = visible_line_count/total_line_count,
width = Screen:scaleBySize(6),
2014-03-13 13:52:43 +00:00
height = self.height,
}
local horizontal_group = HorizontalGroup:new{}
table.insert(horizontal_group, self.text_widget)
table.insert(horizontal_group, HorizontalSpan:new{width = Screen:scaleBySize(6)})
2014-03-13 13:52:43 +00:00
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 = {
ScrollText = {
2014-03-13 13:52:43 +00:00
GestureRange:new{
ges = "swipe",
range = function() return self.dimen end,
2014-03-13 13:52:43 +00:00
},
},
}
end
if Device:hasKeyboard() or Device:hasKeys() then
self.key_events = {
ScrollDown = {{Input.group.PgFwd}, doc = "scroll down"},
ScrollUp = {{Input.group.PgBack}, doc = "scroll up"},
}
end
2013-10-18 20:38:07 +00:00
end
function ScrollTextWidget:updateScrollBar(text)
2014-03-13 13:52:43 +00:00
local virtual_line_num = text:getVirtualLineNum()
local visible_line_count = text:getVisLineCount()
local all_line_count = text:getAllLineCount()
self.v_scroll_bar:set(
(virtual_line_num - 1) / all_line_count,
(virtual_line_num - 1 + visible_line_count) / all_line_count
)
2013-10-18 20:38:07 +00:00
end
function ScrollTextWidget:scrollText(direction)
if direction == 0 then return end
if direction > 0 then
2014-03-13 13:52:43 +00:00
self.text_widget:scrollDown()
else
2014-03-13 13:52:43 +00:00
self.text_widget:scrollUp()
end
self:updateScrollBar(self.text_widget)
UIManager:setDirty(self.dialog, function()
return "partial", self.dimen
end)
2013-10-18 20:38:07 +00:00
end
2013-10-23 14:37:55 +00:00
function ScrollTextWidget:onScrollText(arg, ges)
if ges.direction == "north" then
self:scrollText(1)
elseif ges.direction == "south" then
self:scrollText(-1)
end
return true
end
function ScrollTextWidget:onScrollDown()
self:scrollText(1)
return true
end
function ScrollTextWidget:onScrollUp()
self:scrollText(-1)
return true
end
2013-10-23 14:37:55 +00:00
return ScrollTextWidget