diff --git a/docs/changelog.txt b/docs/changelog.txt index 5ceb91dd1..14fb4db22 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -38,6 +38,7 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: ## New Tweaks ## Fixes +- ``widgets.CycleHotkeyLabel``: allow initial option values to be specified as an index instead of an option value ## Misc Improvements - `dfhack-examples-guide`: refine food preparation orders and fix conditions for making jugs and pots in the ``basic`` manager orders diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index b8782bd04..6a13d2af5 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -637,6 +637,11 @@ function CycleHotkeyLabel:init() break end end + if not self.option_idx then + if self.options[self.initial_option] then + self.option_idx = self.initial_option + end + end if not self.option_idx then error(('cannot find option with value or index: "%s"') :format(self.initial_option)) diff --git a/test/library/gui/widgets.lua b/test/library/gui/widgets.lua new file mode 100644 index 000000000..1eed30e4f --- /dev/null +++ b/test/library/gui/widgets.lua @@ -0,0 +1,18 @@ +local widgets = require('gui.widgets') + +function test.togglehotkeylabel() + local toggle = widgets.ToggleHotkeyLabel{} + expect.true_(toggle:getOptionValue()) + toggle:cycle() + expect.false_(toggle:getOptionValue()) + toggle:cycle() + expect.true_(toggle:getOptionValue()) +end + +function test.togglehotkeylabel_default_value() + local toggle = widgets.ToggleHotkeyLabel{initial_option=2} + expect.false_(toggle:getOptionValue()) + + toggle = widgets.ToggleHotkeyLabel{initial_option=false} + expect.false_(toggle:getOptionValue()) +end