From d7d6c5aea66842bb09ff53f2da0502df6ba43f77 Mon Sep 17 00:00:00 2001 From: John Cosker Date: Mon, 6 Feb 2023 08:55:46 -0500 Subject: [PATCH 1/8] Backwards options for cycle hotkey working --- library/lua/gui/widgets.lua | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 342ece201..cc71df9bf 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,7 +1452,8 @@ function CycleHotkeyLabel:init() self:setOption(self.initial_option) self:setText{ - {key=self.key, key_sep=': ', text=self.label, width=self.label_width, + {key=self.key_back, key_sep='', width=0, on_activate=self.key_back and self:callback('cycle', true)}, + {key=self.key, key_sep=self.key_back and '' or ': ', text=self.label, width=self.key_back and 0 or self.label_width, on_activate=self:callback('cycle')}, ' ', {text=self:callback('getOptionLabel'), @@ -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(), From 36b76d709c76e89c690a2f15f533962e2936454b Mon Sep 17 00:00:00 2001 From: John Cosker Date: Mon, 6 Feb 2023 08:59:55 -0500 Subject: [PATCH 2/8] Update documentation for CycleHotkeyLabel --- docs/dev/Lua API.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 5a1b562ee..c63be53f4 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -4816,6 +4816,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 From 4e366790c0c7a91e0a90b70c3f7f368ae2ea033a Mon Sep 17 00:00:00 2001 From: John Cosker Date: Mon, 6 Feb 2023 09:29:31 -0500 Subject: [PATCH 3/8] Changelog/documentation for key_back --- docs/changelog.txt | 1 + docs/dev/Lua API.rst | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index ea140f451..b024271e9 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -46,6 +46,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## API ## Lua +- ``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 c63be53f4..ad1dc6a46 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -4816,7 +4816,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) +: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 From e1f74ab068eaec6a606dcf07a8c95d53a2c78469 Mon Sep 17 00:00:00 2001 From: John Cosker Date: Mon, 6 Feb 2023 14:53:26 -0500 Subject: [PATCH 4/8] Tweak to completely nil the key_back table for setText if not set --- library/lua/gui/widgets.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index cc71df9bf..13c84f3c7 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -1452,7 +1452,7 @@ function CycleHotkeyLabel:init() self:setOption(self.initial_option) self:setText{ - {key=self.key_back, key_sep='', width=0, on_activate=self.key_back and self:callback('cycle', true)}, + self.key_back ~= nil and {key=self.key_back, key_sep='', width=0, on_activate=self.key_back and self:callback('cycle', true)} or {}, {key=self.key, key_sep=self.key_back and '' or ': ', text=self.label, width=self.key_back and 0 or self.label_width, on_activate=self:callback('cycle')}, ' ', From 39dc0ccc8ac2c68c6f0e8d29b3afe8aa189f0d61 Mon Sep 17 00:00:00 2001 From: John Cosker Date: Mon, 6 Feb 2023 15:03:28 -0500 Subject: [PATCH 5/8] Cleanup --- library/lua/gui/widgets.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 13c84f3c7..9d7c62fd2 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -1453,7 +1453,7 @@ function CycleHotkeyLabel:init() self:setText{ self.key_back ~= nil and {key=self.key_back, key_sep='', width=0, on_activate=self.key_back and self:callback('cycle', true)} or {}, - {key=self.key, key_sep=self.key_back and '' or ': ', text=self.label, width=self.key_back and 0 or self.label_width, + {key=self.key, key_sep=': ', text=self.label, width=self.label_width, on_activate=self:callback('cycle')}, ' ', {text=self:callback('getOptionLabel'), From 36391af27ca7f6ba5d973d6b4baf099ff3b075c2 Mon Sep 17 00:00:00 2001 From: John Cosker Date: Mon, 6 Feb 2023 15:12:47 -0500 Subject: [PATCH 6/8] More cleanup --- library/lua/gui/widgets.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 9d7c62fd2..b0893ea52 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -1452,7 +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.key_back and self:callback('cycle', true)} or {}, + 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')}, ' ', From 0ed4a1c5402490fbf456373fb4a59cbe46e7e489 Mon Sep 17 00:00:00 2001 From: John Cosker Date: Mon, 6 Feb 2023 15:44:04 -0500 Subject: [PATCH 7/8] Add documentation to cycle() for new parameter --- docs/dev/Lua API.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index 133093dae..aceb25b5d 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -4835,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)`` From f91555d5e3123b94d65a307cf150eae33723a3ee Mon Sep 17 00:00:00 2001 From: John Cosker Date: Mon, 6 Feb 2023 15:44:50 -0500 Subject: [PATCH 8/8] Add brackets to indicate optional param --- docs/dev/Lua API.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index aceb25b5d..503f4bece 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -4835,7 +4835,7 @@ the ``option_idx`` instance variable. The CycleHotkeyLabel widget implements the following methods: -* ``cyclehotkeylabel:cycle(backwards)`` +* ``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