2
0
mirror of https://github.com/koreader/koreader synced 2024-10-31 21:20:20 +00:00
koreader/frontend/ui/widget/iconbutton.lua

141 lines
4.1 KiB
Lua
Raw Normal View History

2017-04-26 14:21:36 +00:00
--[[--
Button with a big icon image! Designed for touch devices.
--]]
local Device = require("device")
local HorizontalGroup = require("ui/widget/horizontalgroup")
local HorizontalSpan = require("ui/widget/horizontalspan")
2013-10-18 20:38:07 +00:00
local InputContainer = require("ui/widget/container/inputcontainer")
local ImageWidget = require("ui/widget/imagewidget")
local GestureRange = require("ui/gesturerange")
local UIManager = require("ui/uimanager")
local VerticalGroup = require("ui/widget/verticalgroup")
local VerticalSpan = require("ui/widget/verticalspan")
2013-03-14 02:52:09 +00:00
2013-10-18 20:38:07 +00:00
local IconButton = InputContainer:new{
2014-03-13 13:52:43 +00:00
icon_file = "resources/info-confirm.png",
dimen = nil,
-- show_parent is used for UIManager:setDirty, so we can trigger repaint
show_parent = nil,
2017-09-23 20:01:30 +00:00
width = nil,
height = nil,
scale_for_dpi = true,
padding = 0,
padding_top = nil,
padding_right = nil,
padding_bottom = nil,
padding_left = nil,
enabled = true,
callback = nil,
2013-03-14 02:52:09 +00:00
}
function IconButton:init()
2014-03-13 13:52:43 +00:00
self.image = ImageWidget:new{
2014-11-20 07:52:05 +00:00
file = self.icon_file,
scale_for_dpi = self.scale_for_dpi,
2017-09-23 20:01:30 +00:00
width = self.width,
height = self.height,
2014-03-13 13:52:43 +00:00
}
2013-03-14 02:52:09 +00:00
2014-03-13 13:52:43 +00:00
self.show_parent = self.show_parent or self
2013-03-14 02:52:09 +00:00
self.horizontal_group = HorizontalGroup:new{}
table.insert(self.horizontal_group, HorizontalSpan:new{})
table.insert(self.horizontal_group, self.image)
table.insert(self.horizontal_group, HorizontalSpan:new{})
self.button = VerticalGroup:new{}
table.insert(self.button, VerticalSpan:new{})
table.insert(self.button, self.horizontal_group)
table.insert(self.button, VerticalSpan:new{})
self[1] = self.button
self:update()
end
function IconButton:update()
if not self.padding_top then self.padding_top = self.padding end
if not self.padding_right then self.padding_right = self.padding end
if not self.padding_bottom then self.padding_bottom = self.padding end
if not self.padding_left then self.padding_left = self.padding end
self.horizontal_group[1].width = self.padding_left
self.horizontal_group[3].width = self.padding_right
self.dimen = self.image:getSize()
self.dimen.w = self.dimen.w + self.padding_left+self.padding_right
2013-03-14 02:52:09 +00:00
self.button[1].width = self.padding_top
self.button[3].width = self.padding_bottom
self.dimen.h = self.dimen.h + self.padding_top+self.padding_bottom
self:initGesListener()
2013-03-14 02:52:09 +00:00
end
function IconButton:initGesListener()
if Device:isTouchDevice() then
self.ges_events = {
TapIconButton = {
GestureRange:new{
ges = "tap",
range = self.dimen,
},
doc = "Tap IconButton",
},
HoldIconButton = {
GestureRange:new{
ges = "hold",
range = self.dimen,
},
doc = "Hold IconButton",
2014-03-13 13:52:43 +00:00
}
}
end
2013-03-14 02:52:09 +00:00
end
function IconButton:onTapIconButton()
if G_reader_settings:isFalse("flash_ui") then
2014-03-13 13:52:43 +00:00
self.callback()
else
self.image.invert = true
UIManager:setDirty(self.show_parent, function()
return "fast", self.dimen
end)
-- Make sure button reacts before doing callback
UIManager:tickAfterNext(function()
self.callback()
self.image.invert = false
UIManager:setDirty(self.show_parent, function()
return "fast", self.dimen
end)
end)
end
2014-03-13 13:52:43 +00:00
return true
2013-03-14 02:52:09 +00:00
end
function IconButton:onHoldIconButton()
if self.enabled and self.hold_callback then
self.hold_callback()
elseif self.hold_input then
self:onInput(self.hold_input)
elseif type(self.hold_input_func) == "function" then
self:onInput(self.hold_input_func())
end
return true
end
function IconButton:onFocus()
--quick and dirty, need better way to show focus
self.image.invert = true
return true
end
function IconButton:onUnfocus()
self.image.invert = false
return true
end
function IconButton:onTapSelect()
self:onTapIconButton()
end
2013-10-18 20:38:07 +00:00
return IconButton