Merge pull request #2832 from johncosker/cycle-hotkey-rev

Add 'key_back' optional hotkey for CycleHotkeyLabel to allow cycling backwards.
develop
Myk 2023-02-06 12:55:27 -08:00 committed by GitHub
commit cd9972c442
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 4 deletions

@ -55,6 +55,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## Lua ## Lua
- `overlay`: overlay widgets can now specify focus paths for the viewscreens they attach to so they only appear in specific contexts. see `overlay-dev-guide` for details. - `overlay`: overlay widgets can now specify focus paths for the viewscreens they attach to so they only appear in specific contexts. see `overlay-dev-guide` for details.
- ``widgets.CycleHotkeyLabel``: Added ``key_back`` optional parameter to cycle backwards.
## Removed ## Removed

@ -4817,6 +4817,7 @@ cycle through by pressing a specified hotkey or clicking on the text.
It has the following attributes: It has the following attributes:
:key: The hotkey keycode to display, e.g. ``'CUSTOM_A'``. :key: The hotkey keycode to display, e.g. ``'CUSTOM_A'``.
:key_back: Similar to ``key``, but will cycle backwards (optional)
:label: The string (or a function that returns a string) to display after the :label: The string (or a function that returns a string) to display after the
hotkey. hotkey.
:label_width: The number of spaces to allocate to the ``label`` (for use in :label_width: The number of spaces to allocate to the ``label`` (for use in
@ -4834,9 +4835,10 @@ the ``option_idx`` instance variable.
The CycleHotkeyLabel widget implements the following methods: The CycleHotkeyLabel widget implements the following methods:
* ``cyclehotkeylabel:cycle()`` * ``cyclehotkeylabel:cycle([backwards])``
Cycles the selected option and triggers the ``on_change`` callback. Cycles the selected option and triggers the ``on_change`` callback.
If ``backwards`` is defined and is truthy, the cycle direction will be reversed
* ``cyclehotkeylabel:setOption(value_or_index, call_on_change)`` * ``cyclehotkeylabel:setOption(value_or_index, call_on_change)``

@ -1440,6 +1440,7 @@ CycleHotkeyLabel = defclass(CycleHotkeyLabel, Label)
CycleHotkeyLabel.ATTRS{ CycleHotkeyLabel.ATTRS{
key=DEFAULT_NIL, key=DEFAULT_NIL,
key_back=DEFAULT_NIL,
label=DEFAULT_NIL, label=DEFAULT_NIL,
label_width=DEFAULT_NIL, label_width=DEFAULT_NIL,
options=DEFAULT_NIL, options=DEFAULT_NIL,
@ -1451,6 +1452,7 @@ function CycleHotkeyLabel:init()
self:setOption(self.initial_option) self:setOption(self.initial_option)
self:setText{ self:setText{
self.key_back ~= nil and {key=self.key_back, key_sep='', width=0, on_activate=self:callback('cycle', true)} or {},
{key=self.key, key_sep=': ', text=self.label, width=self.label_width, {key=self.key, key_sep=': ', text=self.label, width=self.label_width,
on_activate=self:callback('cycle')}, on_activate=self:callback('cycle')},
' ', ' ',
@ -1459,12 +1461,14 @@ function CycleHotkeyLabel:init()
} }
end end
function CycleHotkeyLabel:cycle() function CycleHotkeyLabel:cycle(backwards)
local old_option_idx = self.option_idx local old_option_idx = self.option_idx
if self.option_idx == #self.options then if self.option_idx == #self.options and not backwards then
self.option_idx = 1 self.option_idx = 1
elseif self.option_idx == 1 and backwards then
self.option_idx = #self.options
else else
self.option_idx = self.option_idx + 1 self.option_idx = self.option_idx + (not backwards and 1 or -1)
end end
if self.on_change then if self.on_change then
self.on_change(self:getOptionValue(), self.on_change(self:getOptionValue(),