2
0
mirror of https://github.com/koreader/koreader synced 2024-11-10 01:10:34 +00:00
koreader/frontend/ui/widget/progresswidget.lua

119 lines
3.3 KiB
Lua
Raw Normal View History

--[[--
Widget for displaying progress bar.
Configurable attributes:
* width
* height
* margin_v -- vertical margin for solid infill
* margin_h -- horizontal margin for solid infill
* radius
* bordersize
* bordercolor
* bgcolor
* rectcolor -- infill color
* ticks (list) -- default to nil, use this if you want to insert markers
* tick_width
* last -- maximum tick, used with ticks
Example:
local foo_bar = ProgressWidget:new{
width = Screen:scaleBySize(400),
height = Screen:scaleBySize(10),
percentage = 50/100,
}
UIManager:show(foo_bar)
]]
local Blitbuffer = require("ffi/blitbuffer")
local Geom = require("ui/geometry")
local Widget = require("ui/widget/widget")
local Screen = require("device").screen
2013-03-12 17:18:53 +00:00
2013-10-18 20:38:07 +00:00
local ProgressWidget = Widget:new{
2014-03-13 13:52:43 +00:00
width = nil,
height = nil,
margin_h = Screen:scaleBySize(3),
margin_v = Screen:scaleBySize(1),
radius = Screen:scaleBySize(2),
bordersize = Screen:scaleBySize(1),
bordercolor = Blitbuffer.COLOR_BLACK,
bgcolor = Blitbuffer.COLOR_WHITE,
rectcolor = Blitbuffer.COLOR_DIM_GRAY,
2014-03-13 13:52:43 +00:00
percentage = nil,
ticks = nil,
tick_width = Screen:scaleBySize(3),
2014-08-09 06:58:44 +00:00
last = nil,
2013-03-12 17:18:53 +00:00
}
function ProgressWidget:getSize()
2014-03-13 13:52:43 +00:00
return { w = self.width, h = self.height }
2013-03-12 17:18:53 +00:00
end
function ProgressWidget:paintTo(bb, x, y)
2014-03-13 13:52:43 +00:00
local my_size = self:getSize()
self.dimen = Geom:new{
x = x, y = y,
w = my_size.w,
h = my_size.h
2014-03-13 13:52:43 +00:00
}
if self.dimen.w == 0 or self.dimen.h == 0 then return end
-- fill background
2014-03-13 13:52:43 +00:00
bb:paintRoundedRect(x, y, my_size.w, my_size.h, self.bgcolor, self.radius)
-- paint border
bb:paintBorder(x, y,
my_size.w, my_size.h,
self.bordersize, self.bordercolor, self.radius)
-- paint percentage infill
if self.percentage >= 0 and self.percentage <= 1 then
bb:paintRect(x+self.margin_h, math.ceil(y+self.margin_v+self.bordersize),
math.ceil((my_size.w-2*self.margin_h)*self.percentage),
my_size.h-2*(self.margin_v+self.bordersize), self.rectcolor)
end
if self.ticks and self.last and self.last > 0 then
local bar_width = (my_size.w-2*self.margin_h)
local y_pos = y + self.margin_v + self.bordersize
local bar_height = my_size.h-2*(self.margin_v+self.bordersize)
for i=1, #self.ticks do
bb:paintRect(
x + bar_width*(self.ticks[i]/self.last) + self.margin_h,
y_pos,
self.tick_width,
bar_height,
self.bordercolor)
end
2014-08-09 06:58:44 +00:00
end
2013-03-12 17:18:53 +00:00
end
function ProgressWidget:setPercentage(percentage)
2014-03-13 13:52:43 +00:00
self.percentage = percentage
2013-03-12 17:18:53 +00:00
end
function ProgressWidget:updateStyle(thick, height)
if thick then
if height then
self.height = Screen:scaleBySize(height)
end
self.margin_h = Screen:scaleBySize(3)
self.margin_v = Screen:scaleBySize(1)
self.bordersize = Screen:scaleBySize(1)
self.radius = Screen:scaleBySize(2)
self.bgcolor = Blitbuffer.COLOR_WHITE
else
if height then
self.height = Screen:scaleBySize(height)
end
self.margin_h = 0
self.margin_v = 0
self.bordersize = 0
self.radius = 0
self.bgcolor = Blitbuffer.COLOR_GRAY
self.ticks = nil
end
end
2013-10-18 20:38:07 +00:00
return ProgressWidget