2017-04-04 07:57:14 +00:00
|
|
|
--[[--
|
2017-04-29 14:30:16 +00:00
|
|
|
Widget that displays an informational message.
|
2012-06-10 15:52:09 +00:00
|
|
|
|
2017-04-29 14:30:16 +00:00
|
|
|
It vanishes on key press or after a given timeout.
|
2017-04-02 06:50:24 +00:00
|
|
|
|
|
|
|
Example:
|
|
|
|
local UIManager = require("ui/uimanager")
|
2017-04-04 07:57:14 +00:00
|
|
|
local _ = require("gettext")
|
2017-09-11 08:32:39 +00:00
|
|
|
local Screen = require("device").screen
|
2017-04-02 06:50:24 +00:00
|
|
|
local sample
|
|
|
|
sample = InfoMessage:new{
|
|
|
|
text = _("Some message"),
|
|
|
|
-- Usually the hight of a InfoMessage is self-adaptive. If this field is actively set, a
|
|
|
|
-- scrollbar may be shown. This variable is usually helpful to display a large chunk of text
|
|
|
|
-- which may exceed the height of the screen.
|
2017-09-11 08:32:39 +00:00
|
|
|
height = Screen:scaleBySize(400),
|
2017-04-02 06:50:24 +00:00
|
|
|
-- Set to false to hide the icon, and also the span between the icon and text.
|
|
|
|
show_icon = false,
|
|
|
|
timeout = 5, -- This widget will vanish in 5 seconds.
|
|
|
|
}
|
|
|
|
UIManager:show(sample_input)
|
2018-03-30 10:46:36 +00:00
|
|
|
sample_input:onShowKeyboard()
|
2012-06-10 15:52:09 +00:00
|
|
|
]]
|
2017-04-04 07:57:14 +00:00
|
|
|
|
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
|
|
|
local Device = require("device")
|
|
|
|
local Font = require("ui/font")
|
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
|
|
|
local Geom = require("ui/geometry")
|
|
|
|
local GestureRange = require("ui/gesturerange")
|
|
|
|
local HorizontalGroup = require("ui/widget/horizontalgroup")
|
|
|
|
local HorizontalSpan = require("ui/widget/horizontalspan")
|
|
|
|
local ImageWidget = require("ui/widget/imagewidget")
|
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2018-01-29 20:27:24 +00:00
|
|
|
local MovableContainer = require("ui/widget/container/movablecontainer")
|
2017-04-04 07:57:14 +00:00
|
|
|
local ScrollTextWidget = require("ui/widget/scrolltextwidget")
|
2017-09-13 14:56:20 +00:00
|
|
|
local Size = require("ui/size")
|
2017-04-04 07:57:14 +00:00
|
|
|
local TextBoxWidget = require("ui/widget/textboxwidget")
|
|
|
|
local UIManager = require("ui/uimanager")
|
|
|
|
local WidgetContainer = require("ui/widget/container/widgetcontainer")
|
|
|
|
local _ = require("gettext")
|
|
|
|
local Input = Device.input
|
|
|
|
local Screen = Device.screen
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
local InfoMessage = InputContainer:new{
|
2014-10-30 08:01:01 +00:00
|
|
|
modal = true,
|
2017-04-29 08:38:09 +00:00
|
|
|
face = Font:getFace("infofont"),
|
2014-03-13 13:52:43 +00:00
|
|
|
text = "",
|
|
|
|
timeout = nil, -- in seconds
|
2017-04-02 06:50:24 +00:00
|
|
|
width = nil, -- The width of the InfoMessage. Keep it nil to use default value.
|
|
|
|
height = nil, -- The height of the InfoMessage. If this field is set, a scrollbar may be shown.
|
2017-05-08 07:43:34 +00:00
|
|
|
-- The image shows at the left of the InfoMessage. Image data will be freed
|
|
|
|
-- by InfoMessage, caller should not manage its lifecycle
|
|
|
|
image = nil,
|
2017-04-02 06:50:24 +00:00
|
|
|
image_width = nil, -- The image width if image is used. Keep it nil to use original width.
|
|
|
|
image_height = nil, -- The image height if image is used. Keep it nil to use original height.
|
|
|
|
-- Whether the icon should be shown. If it is false, self.image will be ignored.
|
|
|
|
show_icon = true,
|
2017-09-16 15:25:48 +00:00
|
|
|
dismiss_callback = function() end,
|
2012-06-10 15:52:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function InfoMessage:init()
|
2014-06-10 07:57:10 +00:00
|
|
|
if Device:hasKeys() then
|
2014-03-13 13:52:43 +00:00
|
|
|
self.key_events = {
|
|
|
|
AnyKeyPressed = { { Input.group.Any },
|
2014-06-05 06:58:53 +00:00
|
|
|
seqtext = "any key", doc = "close dialog" }
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
if Device:isTouchDevice() then
|
|
|
|
self.ges_events.TapClose = {
|
|
|
|
GestureRange:new{
|
|
|
|
ges = "tap",
|
|
|
|
range = Geom:new{
|
|
|
|
x = 0, y = 0,
|
|
|
|
w = Screen:getWidth(),
|
|
|
|
h = Screen:getHeight(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
2017-04-02 06:50:24 +00:00
|
|
|
|
2016-01-03 07:44:23 +00:00
|
|
|
local image_widget
|
2017-04-02 06:50:24 +00:00
|
|
|
if self.show_icon then
|
2017-05-08 07:43:34 +00:00
|
|
|
-- TODO: remove self.image support, only used in filemanagersearch
|
|
|
|
-- this requires self.image's lifecycle to be managed by ImageWidget
|
|
|
|
-- instead of caller, which is easy to introduce bugs
|
2017-04-02 06:50:24 +00:00
|
|
|
if self.image then
|
|
|
|
image_widget = ImageWidget:new{
|
|
|
|
image = self.image,
|
|
|
|
width = self.image_width,
|
|
|
|
height = self.image_height,
|
|
|
|
}
|
|
|
|
else
|
|
|
|
image_widget = ImageWidget:new{
|
|
|
|
file = "resources/info-i.png",
|
2017-09-11 08:32:39 +00:00
|
|
|
scale_for_dpi = true,
|
2017-04-02 06:50:24 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
image_widget = WidgetContainer:new()
|
|
|
|
end
|
|
|
|
|
|
|
|
local text_width
|
|
|
|
if self.width == nil then
|
|
|
|
text_width = Screen:getWidth() * 2 / 3
|
|
|
|
else
|
|
|
|
text_width = self.width - image_widget:getSize().w
|
|
|
|
if text_width < 0 then
|
|
|
|
text_width = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local text_widget
|
|
|
|
if self.height then
|
|
|
|
text_widget = ScrollTextWidget:new{
|
|
|
|
text = self.text,
|
|
|
|
face = self.face,
|
|
|
|
width = text_width,
|
|
|
|
height = self.height,
|
|
|
|
dialog = self,
|
2014-08-27 03:07:25 +00:00
|
|
|
}
|
|
|
|
else
|
2017-04-02 06:50:24 +00:00
|
|
|
text_widget = TextBoxWidget:new{
|
|
|
|
text = self.text,
|
|
|
|
face = self.face,
|
|
|
|
width = text_width,
|
2014-08-27 03:07:25 +00:00
|
|
|
}
|
|
|
|
end
|
2018-01-29 20:27:24 +00:00
|
|
|
self.movable = MovableContainer:new{
|
2014-03-13 13:52:43 +00:00
|
|
|
FrameContainer:new{
|
2014-10-22 13:34:11 +00:00
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
2014-03-13 13:52:43 +00:00
|
|
|
HorizontalGroup:new{
|
|
|
|
align = "center",
|
2014-08-27 03:07:25 +00:00
|
|
|
image_widget,
|
2017-09-13 14:56:20 +00:00
|
|
|
HorizontalSpan:new{ width = (self.show_icon and Size.span.horizontal_default or 0) },
|
2017-04-02 06:50:24 +00:00
|
|
|
text_widget,
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-29 20:27:24 +00:00
|
|
|
self[1] = CenterContainer:new{
|
|
|
|
dimen = Screen:getSize(),
|
|
|
|
self.movable,
|
|
|
|
}
|
2012-06-10 15:52:09 +00:00
|
|
|
end
|
|
|
|
|
2014-12-01 14:39:41 +00:00
|
|
|
function InfoMessage:onCloseWidget()
|
|
|
|
UIManager:setDirty(nil, function()
|
2017-10-07 13:23:39 +00:00
|
|
|
return "ui", self[1][1].dimen
|
2014-12-01 14:39:41 +00:00
|
|
|
end)
|
|
|
|
return true
|
2014-11-30 22:25:23 +00:00
|
|
|
end
|
|
|
|
|
2012-06-10 15:52:09 +00:00
|
|
|
function InfoMessage:onShow()
|
2014-03-13 13:52:43 +00:00
|
|
|
-- triggered by the UIManager after we got successfully shown (not yet painted)
|
2014-12-01 14:39:41 +00:00
|
|
|
UIManager:setDirty(self, function()
|
2015-04-26 18:07:17 +00:00
|
|
|
return "ui", self[1][1].dimen
|
2014-12-01 14:39:41 +00:00
|
|
|
end)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.timeout then
|
2014-12-01 14:39:41 +00:00
|
|
|
UIManager:scheduleIn(self.timeout, function() UIManager:close(self) end)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
|
|
|
return true
|
2012-06-10 15:52:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function InfoMessage:onAnyKeyPressed()
|
2014-03-13 13:52:43 +00:00
|
|
|
-- triggered by our defined key events
|
2017-09-16 15:25:48 +00:00
|
|
|
self.dismiss_callback()
|
2014-12-01 14:39:41 +00:00
|
|
|
UIManager:close(self)
|
2017-12-17 17:27:24 +00:00
|
|
|
if self.readonly ~= true then
|
|
|
|
return true
|
|
|
|
end
|
2012-06-10 15:52:09 +00:00
|
|
|
end
|
|
|
|
|
2013-03-17 04:15:21 +00:00
|
|
|
function InfoMessage:onTapClose()
|
2017-09-16 15:25:48 +00:00
|
|
|
self.dismiss_callback()
|
2014-12-01 14:39:41 +00:00
|
|
|
UIManager:close(self)
|
2017-12-17 17:27:24 +00:00
|
|
|
if self.readonly ~= true then
|
|
|
|
return true
|
|
|
|
end
|
2013-03-17 04:15:21 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
return InfoMessage
|