2022-03-04 20:20:00 +00:00
|
|
|
local bit = require("bit")
|
2018-03-30 10:46:36 +00:00
|
|
|
local Device = require("device")
|
2013-10-22 15:11:31 +00:00
|
|
|
local Event = require("ui/event")
|
2018-03-18 10:42:35 +00:00
|
|
|
local InputContainer = require("ui/widget/container/inputcontainer")
|
|
|
|
local logger = require("logger")
|
2013-10-22 15:11:31 +00:00
|
|
|
local UIManager = require("ui/uimanager")
|
2022-03-04 20:20:00 +00:00
|
|
|
local util = require("util")
|
2012-06-10 15:52:09 +00:00
|
|
|
--[[
|
|
|
|
Wrapper Widget that manages focus for a whole dialog
|
|
|
|
|
|
|
|
supports a 2D model of active elements
|
|
|
|
|
|
|
|
e.g.:
|
2014-03-13 13:52:43 +00:00
|
|
|
layout = {
|
2018-03-14 21:16:38 +00:00
|
|
|
{ textinput, textinput, item },
|
|
|
|
{ okbutton, cancelbutton, item },
|
|
|
|
{ nil, item, nil },
|
|
|
|
{ nil, item, nil },
|
|
|
|
{ nil, item, nil },
|
2014-03-13 13:52:43 +00:00
|
|
|
}
|
2018-03-14 21:16:38 +00:00
|
|
|
Navigate the layout by trying to avoid not set or nil value.
|
|
|
|
Provide a simple wrap around in the vertical direction.
|
|
|
|
The first element of the first table must be valid to ensure
|
|
|
|
to not get stuck in an invalid position.
|
2012-06-10 15:52:09 +00:00
|
|
|
|
|
|
|
but notice that this does _not_ do the layout for you,
|
|
|
|
it rather defines an abstract layout.
|
|
|
|
]]
|
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 FocusManager = InputContainer:extend{
|
2014-03-13 13:52:43 +00:00
|
|
|
selected = nil, -- defaults to x=1, y=1
|
|
|
|
layout = nil, -- mandatory
|
2022-01-25 00:32:46 +00:00
|
|
|
movement_allowed = { x = true, y = true },
|
2012-06-10 15:52:09 +00:00
|
|
|
}
|
|
|
|
|
2022-10-29 20:46:35 +00:00
|
|
|
-- Only build the default mappings once on initialization, or when an external keyboard is (dis-)/connected.
|
|
|
|
-- We'll make copies during instantiation.
|
|
|
|
local KEY_EVENTS
|
|
|
|
local BUILTIN_KEY_EVENTS
|
|
|
|
local EXTRA_KEY_EVENTS
|
|
|
|
|
|
|
|
local function populateEventMappings()
|
|
|
|
KEY_EVENTS = {}
|
|
|
|
BUILTIN_KEY_EVENTS = {}
|
|
|
|
EXTRA_KEY_EVENTS = {}
|
|
|
|
|
A few graphics fixes after #4541 (#4554)
* Various FocusManager related tweaks to limit its usage to devices with a DPad, and prevent initial button highlights in Dialogs on devices where it makes no sense (i.e., those without a DPad. And even on DPad devices, I'm not even sure how we'd go about making one of those pop up anyway, because no Touch ;)!).
* One mysterious fix to text-only Buttons so that the flash_ui highlight always works, and always honors `FrameContainer`'s pill shape. (Before that, an unhighlight on a text button with a callback that didn't repaint anything [say, the find first/find last buttons in the Reader's search bar when you're already on the first/last match] would do a square black highlight, and a white pill-shaped unhighlight (leaving the black corners visible)).
The workaround makes *absolutely* no sense to me (as `self[1] -> self.frame`, AFAICT), but it works, and ensures all highlights/unhighlights are pill-shaped, so at least we're not doing maths for rounded corners for nothing ;).
2019-02-07 23:56:32 +00:00
|
|
|
if Device:hasDPad() then
|
2022-03-04 20:20:00 +00:00
|
|
|
local event_keys = {}
|
|
|
|
-- these will all generate the same event, just with different arguments
|
2022-10-27 00:01:51 +00:00
|
|
|
table.insert(event_keys, { "FocusUp", { { "Up" }, event = "FocusMove", args = {0, -1} } })
|
|
|
|
table.insert(event_keys, { "FocusRight", { { "Right" }, event = "FocusMove", args = {1, 0} } })
|
|
|
|
table.insert(event_keys, { "FocusDown", { { "Down" }, event = "FocusMove", args = {0, 1} } })
|
|
|
|
table.insert(event_keys, { "Press", { { "Press" }, event = "Press" } })
|
2022-03-04 20:20:00 +00:00
|
|
|
local FEW_KEYS_END_INDEX = #event_keys -- Few keys device: only setup up, down, right and press
|
|
|
|
|
2022-10-27 00:01:51 +00:00
|
|
|
table.insert(event_keys, { "FocusLeft", { { "Left" }, event = "FocusMove", args = {-1, 0} } })
|
2022-03-04 20:20:00 +00:00
|
|
|
|
2024-05-23 05:36:51 +00:00
|
|
|
-- Advanced features: more event handlers can be enabled via settings.reader.lua in a similar manner
|
|
|
|
table.insert(event_keys, { "HoldContext", { { "ContextMenu" }, event = "Hold" } })
|
|
|
|
table.insert(event_keys, { "HoldShift", { { "Shift", "Press" }, event = "Hold" } })
|
2024-05-25 09:11:53 +00:00
|
|
|
table.insert(event_keys, { "HoldScreenKB", { { "ScreenKB", "Press" }, event = "Hold" } })
|
2024-05-23 05:36:51 +00:00
|
|
|
table.insert(event_keys, { "HoldSymAA", { { "Sym", "AA" }, event = "Hold" } })
|
2022-03-04 20:20:00 +00:00
|
|
|
-- half rows/columns move, it is helpful for slow device like Kindle DX to move quickly
|
2022-10-27 00:01:51 +00:00
|
|
|
table.insert(event_keys, { "HalfFocusUp", { { "Alt", "Up" }, event = "FocusHalfMove", args = {"up"} } })
|
|
|
|
table.insert(event_keys, { "HalfFocusRight", { { "Alt", "Right" }, event = "FocusHalfMove", args = {"right"} } })
|
|
|
|
table.insert(event_keys, { "HalfFocusDown", { { "Alt", "Down" }, event = "FocusHalfMove", args = {"down"} } })
|
|
|
|
table.insert(event_keys, { "HalfFocusLeft", { { "Alt", "Left" }, event = "FocusHalfMove", args = {"left"} } })
|
2022-03-04 20:20:00 +00:00
|
|
|
-- for PC navigation behavior support
|
2022-10-27 00:01:51 +00:00
|
|
|
table.insert(event_keys, { "FocusNext", { { "Tab" }, event = "FocusNext" } })
|
|
|
|
table.insert(event_keys, { "FocusPrevious", { { "Shift", "Tab" }, event = "FocusPrevious" } })
|
2024-05-23 05:36:51 +00:00
|
|
|
local NORMAL_KEYS_END_INDEX = #event_keys
|
2022-03-04 20:20:00 +00:00
|
|
|
|
|
|
|
for i = 1, FEW_KEYS_END_INDEX do
|
|
|
|
local key_name = event_keys[i][1]
|
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
|
|
|
KEY_EVENTS[key_name] = event_keys[i][2]
|
|
|
|
BUILTIN_KEY_EVENTS[key_name] = event_keys[i][2]
|
2022-03-04 20:20:00 +00:00
|
|
|
end
|
|
|
|
if not Device:hasFewKeys() then
|
|
|
|
for i = FEW_KEYS_END_INDEX+1, NORMAL_KEYS_END_INDEX do
|
|
|
|
local key_name = event_keys[i][1]
|
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
|
|
|
KEY_EVENTS[key_name] = event_keys[i][2]
|
|
|
|
BUILTIN_KEY_EVENTS[key_name] = event_keys[i][2]
|
2022-03-04 20:20:00 +00:00
|
|
|
end
|
|
|
|
local focus_manager_setting = G_reader_settings:child("focus_manager")
|
|
|
|
-- Enable advanced feature, like Hold, FocusNext, FocusPrevious
|
|
|
|
-- Can also add extra arrow keys like using A, W, D, S for Left, Up, Right, Down
|
|
|
|
local alternative_keymaps = focus_manager_setting:readSetting("alternative_keymaps")
|
|
|
|
if type(alternative_keymaps) == "table" then
|
|
|
|
for i = 1, #event_keys do
|
|
|
|
local key_name = event_keys[i][1]
|
|
|
|
local alternative_keymap = alternative_keymaps[key_name]
|
|
|
|
if alternative_keymap then
|
|
|
|
local handler_defition = util.tableDeepCopy(event_keys[i][2])
|
|
|
|
handler_defition[1] = alternative_keymap -- replace sample key combinations
|
|
|
|
local new_event_key = "Alternative" .. key_name
|
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
|
|
|
KEY_EVENTS[new_event_key] = handler_defition
|
|
|
|
EXTRA_KEY_EVENTS[new_event_key] = handler_defition
|
2022-03-04 20:20:00 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-29 20:46:35 +00:00
|
|
|
populateEventMappings()
|
|
|
|
|
2022-03-12 11:16:50 +00:00
|
|
|
function FocusManager:_init()
|
|
|
|
InputContainer._init(self)
|
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
|
|
|
|
|
|
|
-- These *need* to be instance-specific, hence the copy
|
2022-03-12 11:16:50 +00:00
|
|
|
if not self.selected then
|
|
|
|
self.selected = { x = 1, y = 1 }
|
|
|
|
else
|
|
|
|
self.selected = {x = self.selected.x, y = self.selected.y }
|
|
|
|
end
|
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
|
|
|
|
|
|
|
-- Ditto, as each widget may choose their own custom key bindings
|
|
|
|
self.key_events = util.tableDeepCopy(KEY_EVENTS)
|
|
|
|
-- We should be fine with a simple ref for those, though
|
|
|
|
self.builtin_key_events = BUILTIN_KEY_EVENTS
|
|
|
|
self.extra_key_events = EXTRA_KEY_EVENTS
|
2022-03-12 11:16:50 +00:00
|
|
|
end
|
|
|
|
|
2022-03-04 20:20:00 +00:00
|
|
|
function FocusManager:isAlternativeKey(key)
|
|
|
|
for _, seq in pairs(self.extra_key_events) do
|
|
|
|
for _, oneseq in ipairs(seq) do
|
|
|
|
if key:match(oneseq) then
|
|
|
|
return true
|
|
|
|
end
|
2020-06-04 11:26:18 +00:00
|
|
|
end
|
2018-03-30 10:46:36 +00:00
|
|
|
end
|
2022-03-04 20:20:00 +00:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function FocusManager:onFocusHalfMove(args)
|
|
|
|
if not self.layout then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local direction = unpack(args)
|
|
|
|
local x, y = self.selected.x, self.selected.y
|
|
|
|
local row = self.layout[self.selected.y]
|
|
|
|
local dx, dy = 0, 0
|
|
|
|
if direction == "up" then
|
|
|
|
dy = - math.floor(#self.layout / 2)
|
|
|
|
if dy == 0 then
|
|
|
|
dy = -1
|
|
|
|
elseif dy + y <= 0 then
|
|
|
|
dy = -y + 1 -- first row
|
|
|
|
end
|
|
|
|
elseif direction == "down" then
|
|
|
|
dy = math.floor(#self.layout / 2)
|
|
|
|
if dy == 0 then
|
|
|
|
dy = 1
|
|
|
|
elseif dy + y > #self.layout then
|
|
|
|
dy = #self.layout - y -- last row
|
|
|
|
end
|
|
|
|
elseif direction == "left" then
|
|
|
|
dx = - math.floor(#row / 2)
|
|
|
|
if dx == 0 then
|
|
|
|
dx = -1
|
|
|
|
elseif dx + x <= 0 then
|
|
|
|
dx = -x + 1 -- first column
|
|
|
|
end
|
|
|
|
elseif direction == "right" then
|
|
|
|
dx = math.floor(#row / 2)
|
|
|
|
if dx == 0 then
|
|
|
|
dx = 1
|
|
|
|
elseif dx + x > #row then
|
|
|
|
dx = #row - y -- last column
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return self:onFocusMove({dx, dy})
|
|
|
|
end
|
|
|
|
|
|
|
|
function FocusManager:onPress()
|
|
|
|
return self:sendTapEventToFocusedWidget()
|
|
|
|
end
|
|
|
|
|
|
|
|
function FocusManager:onHold()
|
|
|
|
return self:sendHoldEventToFocusedWidget()
|
|
|
|
end
|
|
|
|
|
|
|
|
-- for tab key
|
|
|
|
function FocusManager:onFocusNext()
|
|
|
|
if not self.layout then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local x, y = self.selected.x, self.selected.y
|
|
|
|
local row = self.layout[y]
|
|
|
|
local dx, dy = 1, 0
|
|
|
|
if not row[x + dx] then -- beyond end of column, go to next row
|
|
|
|
dx, dy = 0, 1
|
|
|
|
end
|
|
|
|
return self:onFocusMove({dx, dy})
|
|
|
|
end
|
|
|
|
|
|
|
|
-- for backtab key
|
|
|
|
function FocusManager:onFocusPrevious()
|
|
|
|
if not self.layout then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local x, y = self.selected.x, self.selected.y
|
|
|
|
local row = self.layout[y]
|
|
|
|
local dx, dy = -1, 0
|
|
|
|
if not row[x + dx] then -- beyond start of column, go to previous row
|
|
|
|
dx, dy = 0, -1
|
|
|
|
end
|
|
|
|
return self:onFocusMove({dx, dy})
|
2012-06-10 15:52:09 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FocusManager:onFocusMove(args)
|
2022-01-25 00:32:46 +00:00
|
|
|
if not self.layout then -- allow parent focus manger to handle the event
|
|
|
|
return false
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
local dx, dy = unpack(args)
|
|
|
|
|
|
|
|
if (dx ~= 0 and not self.movement_allowed.x)
|
|
|
|
or (dy ~= 0 and not self.movement_allowed.y) then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-01-25 00:32:46 +00:00
|
|
|
if not self.layout[self.selected.y] or not self.layout[self.selected.y][self.selected.x] then
|
2022-05-06 08:44:25 +00:00
|
|
|
logger.dbg("FocusManager: no currently selected widget found")
|
2014-03-13 13:52:43 +00:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
local current_item = self.layout[self.selected.y][self.selected.x]
|
|
|
|
while true do
|
2018-03-14 21:16:38 +00:00
|
|
|
if not self.layout[self.selected.y + dy] then
|
2018-03-18 10:42:35 +00:00
|
|
|
--horizontal border, try to wraparound
|
2020-06-27 06:43:28 +00:00
|
|
|
if not self:_wrapAroundY(dy) then
|
2014-03-13 13:52:43 +00:00
|
|
|
break
|
|
|
|
end
|
2018-03-21 11:21:48 +00:00
|
|
|
elseif not self.layout[self.selected.y + dy][self.selected.x] then
|
|
|
|
--inner horizontal border, trying to be clever and step down
|
2018-03-30 10:46:36 +00:00
|
|
|
if not self:_verticalStep(dy) then
|
|
|
|
break
|
|
|
|
end
|
2018-03-14 21:16:38 +00:00
|
|
|
elseif not self.layout[self.selected.y + dy][self.selected.x + dx] then
|
2020-06-27 06:43:28 +00:00
|
|
|
--vertical border, try to wraparound
|
|
|
|
if not self:_wrapAroundX(dx) then
|
|
|
|
break
|
|
|
|
end
|
2014-03-13 13:52:43 +00:00
|
|
|
else
|
|
|
|
self.selected.y = self.selected.y + dy
|
2018-03-14 21:16:38 +00:00
|
|
|
self.selected.x = self.selected.x + dx
|
2017-10-07 10:11:00 +00:00
|
|
|
end
|
2022-03-12 11:16:50 +00:00
|
|
|
logger.dbg("FocusManager cursor position is:", self.selected.x, ",", self.selected.y)
|
2014-03-13 13:52:43 +00:00
|
|
|
|
|
|
|
if self.layout[self.selected.y][self.selected.x] ~= current_item
|
|
|
|
or not self.layout[self.selected.y][self.selected.x].is_inactive then
|
|
|
|
-- we found a different object to focus
|
|
|
|
current_item:handleEvent(Event:new("Unfocus"))
|
|
|
|
self.layout[self.selected.y][self.selected.x]:handleEvent(Event:new("Focus"))
|
A few graphics fixes after #4541 (#4554)
* Various FocusManager related tweaks to limit its usage to devices with a DPad, and prevent initial button highlights in Dialogs on devices where it makes no sense (i.e., those without a DPad. And even on DPad devices, I'm not even sure how we'd go about making one of those pop up anyway, because no Touch ;)!).
* One mysterious fix to text-only Buttons so that the flash_ui highlight always works, and always honors `FrameContainer`'s pill shape. (Before that, an unhighlight on a text button with a callback that didn't repaint anything [say, the find first/find last buttons in the Reader's search bar when you're already on the first/last match] would do a square black highlight, and a white pill-shaped unhighlight (leaving the black corners visible)).
The workaround makes *absolutely* no sense to me (as `self[1] -> self.frame`, AFAICT), but it works, and ensures all highlights/unhighlights are pill-shaped, so at least we're not doing maths for rounded corners for nothing ;).
2019-02-07 23:56:32 +00:00
|
|
|
-- Trigger a fast repaint, this does not count toward a flashing eink refresh
|
|
|
|
-- NOTE: Ideally, we'd only have to repaint the specific subwidget we're highlighting,
|
|
|
|
-- but we may not know its exact coordinates, so, redraw the parent widget instead.
|
2018-03-14 21:16:38 +00:00
|
|
|
UIManager:setDirty(self.show_parent or self, "fast")
|
2014-03-13 13:52:43 +00:00
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
2012-06-10 18:14:29 +00:00
|
|
|
end
|
|
|
|
|
2022-10-29 20:46:35 +00:00
|
|
|
function FocusManager:onPhysicalKeyboardConnected()
|
|
|
|
-- Re-initialize with new keys info.
|
|
|
|
populateEventMappings()
|
|
|
|
-- We can't just call FocusManager._init because it will *reset* the mappings, losing our widget-specific ones (if any),
|
|
|
|
-- and it'll call InputContainer._init, which *also* resets the touch zones.
|
|
|
|
-- Instead, we'll just do a merge ourselves.
|
|
|
|
util.tableMerge(self.key_events, KEY_EVENTS)
|
|
|
|
-- populateEventMappings replaces these, so, update our refs
|
|
|
|
self.builtin_key_events = BUILTIN_KEY_EVENTS
|
|
|
|
self.extra_key_events = EXTRA_KEY_EVENTS
|
|
|
|
end
|
|
|
|
|
|
|
|
function FocusManager:onPhysicalKeyboardDisconnected()
|
|
|
|
local prev_key_events = KEY_EVENTS
|
|
|
|
populateEventMappings()
|
|
|
|
|
2022-11-01 22:22:07 +00:00
|
|
|
-- If we still have keys, remove what disappeared from KEY_EVENTS from self.key_events (if any).
|
|
|
|
if Device:hasKeys() then
|
|
|
|
-- NOTE: This is slightly overkill, we could very well live with a few unreachable mappings for the rest of this widget's life ;).
|
|
|
|
for k, _ in pairs(prev_key_events) do
|
|
|
|
if not KEY_EVENTS[k] then
|
|
|
|
self.key_events[k] = nil
|
|
|
|
end
|
2022-10-29 20:46:35 +00:00
|
|
|
end
|
2022-11-01 22:22:07 +00:00
|
|
|
else
|
|
|
|
-- If we longer have keys at all, that's easy ;).
|
|
|
|
self.key_events = {}
|
2022-10-29 20:46:35 +00:00
|
|
|
end
|
|
|
|
self.builtin_key_events = BUILTIN_KEY_EVENTS
|
|
|
|
self.extra_key_events = EXTRA_KEY_EVENTS
|
|
|
|
end
|
|
|
|
|
2022-03-04 20:20:00 +00:00
|
|
|
-- constant, used to reset focus widget after layout recreation
|
|
|
|
-- not send Unfocus event
|
|
|
|
FocusManager.NOT_UNFOCUS = 1
|
|
|
|
-- not need to send Focus event
|
|
|
|
FocusManager.NOT_FOCUS = 2
|
|
|
|
|
|
|
|
--- Move focus to specified widget
|
|
|
|
function FocusManager:moveFocusTo(x, y, focus_flags)
|
|
|
|
focus_flags = focus_flags or 0
|
|
|
|
if not self.layout then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
local current_item = nil
|
|
|
|
if self.layout[self.selected.y] then
|
|
|
|
current_item = self.layout[self.selected.y][self.selected.x]
|
|
|
|
end
|
|
|
|
local target_item = nil
|
|
|
|
if self.layout[y] then
|
|
|
|
target_item = self.layout[y][x]
|
|
|
|
end
|
|
|
|
if target_item then
|
2022-05-06 08:44:25 +00:00
|
|
|
logger.dbg("FocusManager: Move focus position to:", x, ",", y)
|
2022-03-04 20:20:00 +00:00
|
|
|
self.selected.x = x
|
|
|
|
self.selected.y = y
|
|
|
|
-- widget create new layout on update, previous may be removed from new layout.
|
|
|
|
if Device:hasDPad() then
|
|
|
|
if not bit.band(focus_flags, FocusManager.NOT_UNFOCUS) and current_item and current_item ~= target_item then
|
|
|
|
current_item:handleEvent(Event:new("Unfocus"))
|
|
|
|
end
|
|
|
|
if not bit.band(focus_flags, FocusManager.NOT_FOCUS) then
|
|
|
|
target_item:handleEvent(Event:new("Focus"))
|
|
|
|
UIManager:setDirty(self.show_parent or self, "fast")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2020-06-27 06:43:28 +00:00
|
|
|
--- Go to the last valid item directly left or right of the current item.
|
|
|
|
-- @return false if none could be found
|
|
|
|
function FocusManager:_wrapAroundX(dx)
|
|
|
|
local x = self.selected.x
|
2022-03-04 20:20:00 +00:00
|
|
|
while self.layout[self.selected.y][x - dx] do
|
2020-06-27 06:43:28 +00:00
|
|
|
x = x - dx
|
|
|
|
end
|
|
|
|
if x ~= self.selected.x then
|
|
|
|
self.selected.x = x
|
|
|
|
if not self.layout[self.selected.y][self.selected.x] then
|
|
|
|
--call verticalStep on the current line to perform the search
|
|
|
|
return self:_verticalStep(0)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
--- Go to the last valid item directly above or below the current item.
|
|
|
|
-- @return false if none could be found
|
|
|
|
function FocusManager:_wrapAroundY(dy)
|
2018-03-14 21:16:38 +00:00
|
|
|
local y = self.selected.y
|
2018-03-21 11:21:48 +00:00
|
|
|
while self.layout[y - dy] do
|
2018-03-14 21:16:38 +00:00
|
|
|
y = y - dy
|
|
|
|
end
|
|
|
|
if y ~= self.selected.y then
|
|
|
|
self.selected.y = y
|
2018-03-21 11:21:48 +00:00
|
|
|
if not self.layout[self.selected.y][self.selected.x] then
|
|
|
|
--call verticalStep on the current line to perform the search
|
2018-03-30 10:46:36 +00:00
|
|
|
return self:_verticalStep(0)
|
2018-03-21 11:21:48 +00:00
|
|
|
end
|
2018-03-14 21:16:38 +00:00
|
|
|
return true
|
|
|
|
else
|
|
|
|
return false
|
|
|
|
end
|
2012-06-10 18:14:29 +00:00
|
|
|
end
|
2013-10-18 20:38:07 +00:00
|
|
|
|
2018-03-21 11:21:48 +00:00
|
|
|
function FocusManager:_verticalStep(dy)
|
|
|
|
local x = self.selected.x
|
2023-10-12 12:52:11 +00:00
|
|
|
if type(self.layout[self.selected.y + dy]) ~= "table" or next(self.layout[self.selected.y + dy]) == nil then
|
2018-03-30 10:46:36 +00:00
|
|
|
logger.err("[FocusManager] : Malformed layout")
|
|
|
|
return false
|
|
|
|
end
|
2018-03-21 11:21:48 +00:00
|
|
|
--looking for the item on the line below, the closest on the left side
|
|
|
|
while not self.layout[self.selected.y + dy][x] do
|
|
|
|
x = x - 1
|
|
|
|
if x == 0 then
|
|
|
|
--if he is not on the left, must be on the right
|
|
|
|
x = self.selected.x
|
|
|
|
while not self.layout[self.selected.y + dy][x] do
|
|
|
|
x = x + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
self.selected.x = x
|
|
|
|
self.selected.y = self.selected.y + dy
|
2018-03-30 10:46:36 +00:00
|
|
|
return true
|
2018-03-21 11:21:48 +00:00
|
|
|
end
|
|
|
|
|
2015-09-13 08:09:00 +00:00
|
|
|
function FocusManager:getFocusItem()
|
2022-01-25 00:32:46 +00:00
|
|
|
if not self.layout then
|
|
|
|
return nil
|
|
|
|
end
|
2015-09-13 08:09:00 +00:00
|
|
|
return self.layout[self.selected.y][self.selected.x]
|
|
|
|
end
|
|
|
|
|
2022-03-04 20:20:00 +00:00
|
|
|
function FocusManager:_sendGestureEventToFocusedWidget(gesture)
|
2022-01-25 00:32:46 +00:00
|
|
|
local focused_widget = self:getFocusItem()
|
|
|
|
if focused_widget then
|
|
|
|
-- center of widget position
|
|
|
|
local point = focused_widget.dimen:copy()
|
|
|
|
point.x = point.x + point.w / 2
|
|
|
|
point.y = point.y + point.h / 2
|
|
|
|
point.w = 0
|
|
|
|
point.h = 0
|
2022-03-12 11:16:50 +00:00
|
|
|
logger.dbg("FocusManager: Send", gesture, "to", point.x , ",", point.y)
|
2022-01-25 00:32:46 +00:00
|
|
|
UIManager:sendEvent(Event:new("Gesture", {
|
2022-03-04 20:20:00 +00:00
|
|
|
ges = gesture,
|
2022-01-25 00:32:46 +00:00
|
|
|
pos = point,
|
|
|
|
}))
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2022-03-04 20:20:00 +00:00
|
|
|
function FocusManager:sendTapEventToFocusedWidget()
|
|
|
|
return self:_sendGestureEventToFocusedWidget("tap")
|
|
|
|
end
|
|
|
|
|
|
|
|
function FocusManager:sendHoldEventToFocusedWidget()
|
|
|
|
return self:_sendGestureEventToFocusedWidget("hold")
|
|
|
|
end
|
|
|
|
|
|
|
|
function FocusManager:mergeLayoutInVertical(child, pos)
|
2022-01-25 00:32:46 +00:00
|
|
|
if not child.layout then
|
|
|
|
return
|
|
|
|
end
|
2022-03-04 20:20:00 +00:00
|
|
|
if not pos then
|
|
|
|
pos = #self.layout + 1 -- end of row
|
|
|
|
end
|
2022-01-25 00:32:46 +00:00
|
|
|
for _, row in ipairs(child.layout) do
|
2022-03-04 20:20:00 +00:00
|
|
|
table.insert(self.layout, pos, row)
|
|
|
|
pos = pos + 1
|
2022-01-25 00:32:46 +00:00
|
|
|
end
|
2022-03-04 20:20:00 +00:00
|
|
|
child:disableFocusManagement(self)
|
2022-01-25 00:32:46 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function FocusManager:mergeLayoutInHorizontal(child)
|
|
|
|
if not child.layout then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
for i, row in ipairs(child.layout) do
|
|
|
|
local prow = self.layout[i]
|
|
|
|
if not prow then
|
|
|
|
prow = {}
|
|
|
|
self.layout[i] = prow
|
|
|
|
end
|
|
|
|
for _, widget in ipairs(row) do
|
|
|
|
table.insert(prow, widget)
|
|
|
|
end
|
|
|
|
end
|
2022-03-04 20:20:00 +00:00
|
|
|
child:disableFocusManagement(self)
|
2022-01-25 00:32:46 +00:00
|
|
|
end
|
|
|
|
|
2022-03-04 20:20:00 +00:00
|
|
|
function FocusManager:disableFocusManagement(parent)
|
|
|
|
self._parent = parent
|
|
|
|
-- unfocus current widget in current child container
|
|
|
|
-- parent container will call refocusWidget to focus another one
|
|
|
|
local row = self.layout[self.selected.y]
|
|
|
|
if row and row[self.selected.x] then
|
|
|
|
row[self.selected.x]:handleEvent(Event:new("Unfocus"))
|
|
|
|
end
|
2022-01-25 00:32:46 +00:00
|
|
|
self.layout = nil -- turn off focus feature
|
|
|
|
end
|
|
|
|
|
2022-03-04 20:20:00 +00:00
|
|
|
-- constant for refocusWidget method to ease code reading
|
|
|
|
FocusManager.RENDER_IN_NEXT_TICK = true
|
|
|
|
|
|
|
|
--- Container calls this method to re-set focus widget style
|
|
|
|
--- Some container regenerate layout on update and lose focus style
|
|
|
|
function FocusManager:refocusWidget(nextTick)
|
|
|
|
if not self._parent then
|
|
|
|
if not nextTick then
|
|
|
|
self:moveFocusTo(self.selected.x, self.selected.y)
|
|
|
|
else
|
|
|
|
-- sometimes refocusWidget called in widget's action callback
|
|
|
|
-- widget may force repaint after callback, like Button with vsync = true
|
|
|
|
-- then focus style will be lost, set focus style to next tick to make sure focus style painted
|
|
|
|
UIManager:nextTick(function()
|
|
|
|
self:moveFocusTo(self.selected.x, self.selected.y)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
self._parent:refocusWidget(nextTick)
|
2022-01-28 21:55:26 +00:00
|
|
|
end
|
2022-01-25 00:32:46 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return FocusManager
|