2017-04-29 08:38:09 +00:00
|
|
|
--[[--
|
|
|
|
Widget that displays a tiny notification at the top of the screen.
|
|
|
|
--]]
|
|
|
|
|
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local CenterContainer = require("ui/widget/container/centercontainer")
|
2017-04-29 08:38:09 +00:00
|
|
|
local Device = require("device")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Font = require("ui/font")
|
2017-04-29 08:38:09 +00:00
|
|
|
local FrameContainer = require("ui/widget/container/framecontainer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local Geom = require("ui/geometry")
|
2018-05-19 12:01:20 +00:00
|
|
|
local GestureRange = require("ui/gesturerange")
|
2017-04-29 08:38:09 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
2021-02-01 22:10:29 +00:00
|
|
|
local RectSpan = require("ui/widget/rectspan")
|
2017-09-13 14:56:20 +00:00
|
|
|
local Size = require("ui/size")
|
2017-04-29 08:38:09 +00:00
|
|
|
local TextWidget = require("ui/widget/textwidget")
|
2013-10-18 20:38:07 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2021-02-01 22:10:29 +00:00
|
|
|
local VerticalGroup = require("ui/widget/verticalgroup")
|
2022-05-05 19:00:22 +00:00
|
|
|
local time = require("ui/time")
|
2022-10-10 16:55:29 +00:00
|
|
|
local _ = require("gettext")
|
2017-04-29 08:38:09 +00:00
|
|
|
local Screen = Device.screen
|
2022-10-10 16:55:29 +00:00
|
|
|
local Input = Device.input
|
2013-01-01 23:08:19 +00:00
|
|
|
|
2021-05-20 21:14:11 +00:00
|
|
|
local band = bit.band
|
|
|
|
|
|
|
|
-- The following constants are positions in a bitfield
|
2023-02-07 00:01:05 +00:00
|
|
|
local SOURCE_BOTTOM_MENU_ICON = 0x0001 -- icons in bottom menu
|
|
|
|
local SOURCE_BOTTOM_MENU_TOGGLE = 0x0002 -- toggles in bottom menu
|
|
|
|
local SOURCE_BOTTOM_MENU_FINE = 0x0004 -- toggles with fine-tuning ("increase", "+" etc)
|
|
|
|
local SOURCE_BOTTOM_MENU_MORE = 0x0008 -- three dots in bottom menu
|
2021-05-20 21:14:11 +00:00
|
|
|
local SOURCE_BOTTOM_MENU_PROGRESS = 0x0010 -- progress indicator on bottom menu
|
2023-02-07 00:01:05 +00:00
|
|
|
local SOURCE_DISPATCHER = 0x0020 -- dispatcher
|
|
|
|
local SOURCE_OTHER = 0x0040 -- all other sources (e.g. keyboard)
|
2024-01-16 17:06:15 +00:00
|
|
|
local SOURCE_ALWAYS_SHOW = 0x8000 -- display this, no matter the display preferences
|
2021-05-20 21:14:11 +00:00
|
|
|
|
|
|
|
-- All bottom menu bits
|
2023-02-07 00:01:05 +00:00
|
|
|
local SOURCE_BOTTOM_MENU = SOURCE_BOTTOM_MENU_ICON +
|
|
|
|
SOURCE_BOTTOM_MENU_TOGGLE +
|
|
|
|
SOURCE_BOTTOM_MENU_FINE +
|
|
|
|
SOURCE_BOTTOM_MENU_MORE +
|
|
|
|
SOURCE_BOTTOM_MENU_PROGRESS
|
2021-05-20 21:14:11 +00:00
|
|
|
|
|
|
|
-- these values can be changed here
|
2023-12-31 19:54:13 +00:00
|
|
|
local SOURCE_SOME = SOURCE_BOTTOM_MENU_FINE
|
|
|
|
local SOURCE_MORE = SOURCE_SOME +
|
|
|
|
SOURCE_BOTTOM_MENU_MORE +
|
|
|
|
SOURCE_BOTTOM_MENU_PROGRESS
|
|
|
|
local SOURCE_DEFAULT = SOURCE_MORE +
|
|
|
|
SOURCE_DISPATCHER
|
2023-02-07 00:01:05 +00:00
|
|
|
local SOURCE_ALL = SOURCE_BOTTOM_MENU +
|
|
|
|
SOURCE_DISPATCHER +
|
|
|
|
SOURCE_OTHER
|
2021-05-20 21:14:11 +00:00
|
|
|
|
2023-03-05 14:21:56 +00:00
|
|
|
-- Maximum number of saved message text
|
|
|
|
local MAX_NB_PAST_MESSAGES = 20
|
|
|
|
|
Clarify our OOP semantics across the codebase (#9586)
Basically:
* Use `extend` for class definitions
* Use `new` for object instantiations
That includes some minor code cleanups along the way:
* Updated `Widget`'s docs to make the semantics clearer.
* Removed `should_restrict_JIT` (it's been dead code since https://github.com/koreader/android-luajit-launcher/pull/283)
* Minor refactoring of LuaSettings/LuaData/LuaDefaults/DocSettings to behave (mostly, they are instantiated via `open` instead of `new`) like everything else and handle inheritance properly (i.e., DocSettings is now a proper LuaSettings subclass).
* Default to `WidgetContainer` instead of `InputContainer` for stuff that doesn't actually setup key/gesture events.
* Ditto for explicit `*Listener` only classes, make sure they're based on `EventListener` instead of something uselessly fancier.
* Unless absolutely necessary, do not store references in class objects, ever; only values. Instead, always store references in instances, to avoid both sneaky inheritance issues, and sneaky GC pinning of stale references.
* ReaderUI: Fix one such issue with its `active_widgets` array, with critical implications, as it essentially pinned *all* of ReaderUI's modules, including their reference to the `Document` instance (i.e., that was a big-ass leak).
* Terminal: Make sure the shell is killed on plugin teardown.
* InputText: Fix Home/End/Del physical keys to behave sensibly.
* InputContainer/WidgetContainer: If necessary, compute self.dimen at paintTo time (previously, only InputContainers did, which might have had something to do with random widgets unconcerned about input using it as a baseclass instead of WidgetContainer...).
* OverlapGroup: Compute self.dimen at *init* time, because for some reason it needs to do that, but do it directly in OverlapGroup instead of going through a weird WidgetContainer method that it was the sole user of.
* ReaderCropping: Under no circumstances should a Document instance member (here, self.bbox) risk being `nil`ed!
* Kobo: Minor code cleanups.
2022-10-06 00:14:48 +00:00
|
|
|
local Notification = InputContainer:extend{
|
2017-04-29 08:38:09 +00:00
|
|
|
face = Font:getFace("x_smallinfofont"),
|
2022-10-10 16:55:29 +00:00
|
|
|
text = _("N/A"),
|
2017-09-13 14:56:20 +00:00
|
|
|
margin = Size.margin.default,
|
|
|
|
padding = Size.padding.default,
|
2021-01-11 17:14:12 +00:00
|
|
|
timeout = 2, -- default to 2 seconds
|
2023-11-22 17:58:31 +00:00
|
|
|
_timeout_func = nil,
|
2021-01-11 17:14:12 +00:00
|
|
|
toast = true, -- closed on any event, and let the event propagate to next top widget
|
2021-02-01 22:10:29 +00:00
|
|
|
|
2022-10-10 16:55:29 +00:00
|
|
|
_shown_list = {}, -- actual static class member, array of stacked notifications (value is show (well, init) time or false).
|
|
|
|
_shown_idx = nil, -- index of this instance in the class's _shown_list array (assumes each Notification object is only shown (well, init) once).
|
2021-05-20 21:14:11 +00:00
|
|
|
|
|
|
|
SOURCE_BOTTOM_MENU_ICON = SOURCE_BOTTOM_MENU_ICON,
|
|
|
|
SOURCE_BOTTOM_MENU_TOGGLE = SOURCE_BOTTOM_MENU_TOGGLE,
|
|
|
|
SOURCE_BOTTOM_MENU_FINE = SOURCE_BOTTOM_MENU_FINE,
|
|
|
|
SOURCE_BOTTOM_MENU_MORE = SOURCE_BOTTOM_MENU_MORE,
|
|
|
|
SOURCE_BOTTOM_MENU_PROGRESS = SOURCE_BOTTOM_MENU_PROGRESS,
|
|
|
|
SOURCE_DISPATCHER = SOURCE_DISPATCHER,
|
|
|
|
SOURCE_OTHER = SOURCE_OTHER,
|
2024-01-16 17:06:15 +00:00
|
|
|
SOURCE_ALWAYS_SHOW = SOURCE_ALWAYS_SHOW,
|
2021-05-20 21:14:11 +00:00
|
|
|
|
|
|
|
SOURCE_BOTTOM_MENU = SOURCE_BOTTOM_MENU,
|
|
|
|
|
|
|
|
SOURCE_NONE = 0,
|
|
|
|
SOURCE_SOME = SOURCE_SOME,
|
2023-12-31 19:54:13 +00:00
|
|
|
SOURCE_MORE = SOURCE_MORE,
|
2021-05-20 21:14:11 +00:00
|
|
|
SOURCE_DEFAULT = SOURCE_DEFAULT,
|
|
|
|
SOURCE_ALL = SOURCE_ALL,
|
2024-01-16 17:06:15 +00:00
|
|
|
|
2023-03-05 14:21:56 +00:00
|
|
|
_past_messages = {}, -- a static class member to store the N last messages text
|
2013-01-01 23:08:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function Notification:init()
|
2021-01-11 17:14:12 +00:00
|
|
|
if not self.toast then
|
|
|
|
-- If not toast, closing is handled in here
|
|
|
|
if Device:hasKeys() then
|
2022-10-27 00:01:51 +00:00
|
|
|
self.key_events.AnyKeyPressed = { { Input.group.Any } }
|
2021-01-11 17:14:12 +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(),
|
|
|
|
}
|
2018-05-19 12:01:20 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-11 17:14:12 +00:00
|
|
|
end
|
2018-05-19 12:01:20 +00:00
|
|
|
end
|
|
|
|
|
2014-05-02 04:47:40 +00:00
|
|
|
local text_widget = TextWidget:new{
|
|
|
|
text = self.text,
|
2018-05-19 12:01:20 +00:00
|
|
|
face = self.face,
|
2021-09-25 08:51:58 +00:00
|
|
|
max_width = Screen:getWidth() - 2 * (self.margin + self.padding)
|
2014-05-02 04:47:40 +00:00
|
|
|
}
|
|
|
|
local widget_size = text_widget:getSize()
|
2021-02-01 22:10:29 +00:00
|
|
|
self.frame = FrameContainer:new{
|
|
|
|
background = Blitbuffer.COLOR_WHITE,
|
|
|
|
radius = 0,
|
|
|
|
margin = self.margin,
|
|
|
|
padding = self.padding,
|
|
|
|
CenterContainer:new{
|
|
|
|
dimen = Geom:new{
|
|
|
|
w = widget_size.w,
|
|
|
|
h = widget_size.h
|
|
|
|
},
|
|
|
|
text_widget,
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
|
|
|
}
|
2021-02-01 22:10:29 +00:00
|
|
|
local notif_height = self.frame:getSize().h
|
|
|
|
|
|
|
|
self:_cleanShownStack()
|
2022-10-10 16:55:29 +00:00
|
|
|
table.insert(Notification._shown_list, UIManager:getTime())
|
|
|
|
self._shown_idx = #Notification._shown_list
|
2021-02-01 22:10:29 +00:00
|
|
|
|
|
|
|
self[1] = VerticalGroup:new{
|
|
|
|
align = "center",
|
|
|
|
-- We use a span to properly position this notification:
|
|
|
|
RectSpan:new{
|
|
|
|
-- have this VerticalGroup full width, to ensure centering
|
|
|
|
width = Screen:getWidth(),
|
2022-10-10 18:25:18 +00:00
|
|
|
-- push this frame at its y=self._shown_idx position
|
|
|
|
height = notif_height * (self._shown_idx - 1) + self.margin,
|
2021-04-02 16:12:08 +00:00
|
|
|
-- (let's add a leading self.margin to get the same distance
|
|
|
|
-- from top of screen to first notification top border as
|
|
|
|
-- between borders of next notifications)
|
2021-02-01 22:10:29 +00:00
|
|
|
},
|
|
|
|
self.frame,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-05-20 21:14:11 +00:00
|
|
|
function Notification:setNotifySource(source)
|
|
|
|
self.notify_source = source
|
|
|
|
end
|
|
|
|
|
|
|
|
function Notification:resetNotifySource()
|
2024-01-16 17:06:15 +00:00
|
|
|
self.notify_source = SOURCE_OTHER
|
2021-05-20 21:14:11 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function Notification:getNotifySource()
|
|
|
|
return self.notify_source
|
|
|
|
end
|
|
|
|
|
2023-02-07 00:01:05 +00:00
|
|
|
-- Display a notification popup if `source` or `self.notify_source` is not masked by the `notification_sources_to_show_mask` setting
|
|
|
|
function Notification:notify(arg, source, refresh_after)
|
|
|
|
source = source or self.notify_source
|
2024-01-16 17:06:15 +00:00
|
|
|
local mask = G_reader_settings:readSetting("notification_sources_to_show_mask") or SOURCE_DEFAULT
|
|
|
|
if source and (source == SOURCE_ALWAYS_SHOW or band(mask, source) ~= 0) then
|
2021-05-20 21:14:11 +00:00
|
|
|
UIManager:show(Notification:new{
|
|
|
|
text = arg,
|
|
|
|
})
|
|
|
|
if refresh_after then
|
|
|
|
UIManager:forceRePaint()
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2023-03-05 14:21:56 +00:00
|
|
|
function Notification:getPastMessages()
|
|
|
|
return self._past_messages
|
|
|
|
end
|
|
|
|
|
2022-10-10 16:55:29 +00:00
|
|
|
function Notification:_cleanShownStack()
|
2021-02-01 22:10:29 +00:00
|
|
|
-- Clean stack of shown notifications
|
2022-10-10 16:55:29 +00:00
|
|
|
if self._shown_idx then
|
|
|
|
-- If this field exists, this is the first time this instance was closed since its init.
|
2021-02-01 22:10:29 +00:00
|
|
|
-- This notification is no longer displayed
|
2022-10-10 16:55:29 +00:00
|
|
|
Notification._shown_list[self._shown_idx] = false
|
2021-02-01 22:10:29 +00:00
|
|
|
end
|
2022-10-10 16:55:29 +00:00
|
|
|
-- We remove from the stack's tail all slots no longer displayed.
|
2021-02-01 22:10:29 +00:00
|
|
|
-- Even if slots at top are available, we'll keep adding new
|
|
|
|
-- notifications only at the tail/bottom (easier for the eyes
|
|
|
|
-- to follow what is happening).
|
|
|
|
-- As a sanity check, we also forget those shown for
|
|
|
|
-- more than 30s in case no close event was received.
|
2022-05-05 19:00:22 +00:00
|
|
|
local expire_time = UIManager:getTime() - time.s(30)
|
2022-10-10 16:55:29 +00:00
|
|
|
for i = #Notification._shown_list, 1, -1 do
|
|
|
|
if Notification._shown_list[i] and Notification._shown_list[i] > expire_time then
|
2021-02-01 22:10:29 +00:00
|
|
|
break -- still shown (or not yet expired)
|
|
|
|
end
|
2022-10-10 16:55:29 +00:00
|
|
|
table.remove(Notification._shown_list, i)
|
2021-02-01 22:10:29 +00:00
|
|
|
end
|
2013-01-01 23:08:19 +00:00
|
|
|
end
|
|
|
|
|
2014-12-01 14:39:41 +00:00
|
|
|
function Notification:onCloseWidget()
|
2022-10-10 16:55:29 +00:00
|
|
|
self:_cleanShownStack()
|
|
|
|
self._shown_idx = nil -- Don't do something stupid if this same instance gets closed multiple times
|
2014-12-01 14:39:41 +00:00
|
|
|
UIManager:setDirty(nil, function()
|
2021-02-01 22:10:29 +00:00
|
|
|
return "ui", self.frame.dimen
|
2014-12-01 14:39:41 +00:00
|
|
|
end)
|
2023-11-22 17:58:31 +00:00
|
|
|
-- If we were closed early, drop the scheduled timeout
|
|
|
|
if self._timeout_func then
|
|
|
|
UIManager:unschedule(self._timeout_func)
|
|
|
|
self._timeout_func = nil
|
|
|
|
end
|
2014-11-30 22:25:23 +00:00
|
|
|
end
|
|
|
|
|
2013-01-01 23:08:19 +00:00
|
|
|
function Notification:onShow()
|
2014-12-01 14:39:41 +00:00
|
|
|
UIManager:setDirty(self, function()
|
2023-08-01 21:53:10 +00:00
|
|
|
return "ui", self.frame.dimen
|
2014-12-01 14:39:41 +00:00
|
|
|
end)
|
2014-03-13 13:52:43 +00:00
|
|
|
if self.timeout then
|
2023-11-22 17:58:31 +00:00
|
|
|
self._timeout_func = function()
|
|
|
|
self._timeout_func = nil
|
|
|
|
UIManager:close(self)
|
|
|
|
end
|
|
|
|
UIManager:scheduleIn(self.timeout, self._timeout_func)
|
2014-03-13 13:52:43 +00:00
|
|
|
end
|
2023-03-05 14:21:56 +00:00
|
|
|
|
|
|
|
if #self._past_messages >= MAX_NB_PAST_MESSAGES then
|
|
|
|
table.remove(self._past_messages)
|
|
|
|
end
|
|
|
|
table.insert(self._past_messages, 1, os.date("%X: ") .. self.text)
|
|
|
|
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
2013-01-01 23:08:19 +00:00
|
|
|
end
|
|
|
|
|
2018-05-19 12:01:20 +00:00
|
|
|
function Notification:onTapClose()
|
2021-01-11 17:14:12 +00:00
|
|
|
if self.toast then return end -- should not happen
|
2018-05-19 12:01:20 +00:00
|
|
|
UIManager:close(self)
|
2021-01-11 17:14:12 +00:00
|
|
|
return true
|
2013-01-01 23:08:19 +00:00
|
|
|
end
|
2022-10-23 20:36:09 +00:00
|
|
|
Notification.onAnyKeyPressed = Notification.onTapClose
|
2013-01-01 23:08:19 +00:00
|
|
|
|
2022-10-10 16:55:29 +00:00
|
|
|
-- Toasts should go bye-bye on user input, without stopping the event's propagation.
|
|
|
|
function Notification:onKeyPress(key)
|
|
|
|
if self.toast then
|
|
|
|
UIManager:close(self)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return InputContainer.onKeyPress(self, key)
|
|
|
|
end
|
|
|
|
function Notification:onKeyRepeat(key)
|
|
|
|
if self.toast then
|
|
|
|
UIManager:close(self)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return InputContainer.onKeyRepeat(self, key)
|
|
|
|
end
|
|
|
|
function Notification:onGesture(ev)
|
|
|
|
if self.toast then
|
|
|
|
UIManager:close(self)
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
return InputContainer.onGesture(self, ev)
|
|
|
|
end
|
|
|
|
|
2023-05-20 23:23:41 +00:00
|
|
|
-- Since toasts do *not* prevent event propagation, if we let this go through to InputContainer, shit happens...
|
|
|
|
function Notification:onIgnoreTouchInput(toggle)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
-- Do the same for other Events caught by our base class
|
|
|
|
Notification.onResume = Notification.onIgnoreTouchInput
|
|
|
|
Notification.onPhysicalKeyboardDisconnected = Notification.onIgnoreTouchInput
|
|
|
|
Notification.onInput = Notification.onIgnoreTouchInput
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return Notification
|