diff --git a/docs/changelog.txt b/docs/changelog.txt index ff9ad46a7..d4b23d229 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -55,6 +55,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## 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. +- ``widgets.CycleHotkeyLabel``: Added ``key_back`` optional parameter to cycle backwards. ## Removed diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 8dc448660..503f4bece 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -4817,6 +4817,7 @@ cycle through by pressing a specified hotkey or clicking on the text. It has the following attributes: :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 hotkey. :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: -* ``cyclehotkeylabel:cycle()`` +* ``cyclehotkeylabel:cycle([backwards])`` 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)`` diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index abbd70f7d..ddbeeee6f 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -1440,6 +1440,7 @@ CycleHotkeyLabel = defclass(CycleHotkeyLabel, Label) CycleHotkeyLabel.ATTRS{ key=DEFAULT_NIL, + key_back=DEFAULT_NIL, label=DEFAULT_NIL, label_width=DEFAULT_NIL, options=DEFAULT_NIL, @@ -1451,6 +1452,7 @@ function CycleHotkeyLabel:init() self:setOption(self.initial_option) 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, on_activate=self:callback('cycle')}, ' ', @@ -1459,12 +1461,14 @@ function CycleHotkeyLabel:init() } end -function CycleHotkeyLabel:cycle() +function CycleHotkeyLabel:cycle(backwards) 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 + elseif self.option_idx == 1 and backwards then + self.option_idx = #self.options else - self.option_idx = self.option_idx + 1 + self.option_idx = self.option_idx + (not backwards and 1 or -1) end if self.on_change then self.on_change(self:getOptionValue(),