From 2316615763d50d24cc304cb5623e11b2ef9d21c0 Mon Sep 17 00:00:00 2001 From: Myk Taylor Date: Thu, 2 Feb 2023 20:24:40 -0800 Subject: [PATCH] react to double clicks on list items --- docs/changelog.txt | 3 ++- docs/dev/Lua API.rst | 4 ++++ library/lua/gui/widgets.lua | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/docs/changelog.txt b/docs/changelog.txt index dc8198c0d..369f0c620 100644 --- a/docs/changelog.txt +++ b/docs/changelog.txt @@ -78,9 +78,10 @@ changelog.txt uses a syntax similar to RST, with a few special sequences: - `helpdb`: new function: ``helpdb.refresh()`` to force a refresh of the database. Call if you are a developer adding new scripts, loading new plugins, or changing help text during play - `helpdb`: changed from auto-refreshing every 60 seconds to only refreshing on explicit call to ``helpdb.refresh()``. docs very rarely change during a play session, and the automatic database refreshes were slowing down the startup of `gui/launcher` and anything else that displays help text. - ``widgets.Label``: ``label.scroll()`` now understands ``home`` and ``end`` keywords for scrolling to the top or bottom +- ``widgets.List``: new callbacks for double click and shift double click - ``dfhack.units.getCitizens()``: gets a list of citizens - ``gui.ZScreen``: new attribute: ``defocusable`` for controlling whether a window loses keyboard focus when the map is clicked -- ``Label``: token ``tile`` properties can now be either pens or numeric texture ids +- ``widgets.Label``: token ``tile`` properties can now be either pens or numeric texture ids - `tiletypes`: now has a Lua API! ``tiletypes_setTile`` ## Removed diff --git a/docs/dev/Lua API.rst b/docs/dev/Lua API.rst index deda0e31b..f0c33eb73 100644 --- a/docs/dev/Lua API.rst +++ b/docs/dev/Lua API.rst @@ -4875,6 +4875,10 @@ It has the following attributes: key/click and calls the callback as ``on_submit(index,choice)``. :on_submit2: Shift-click callback; if specified, the list reacts to the click and calls the callback as ``on_submit2(index,choice)``. +:on_double_click: Mouse double click callback; if specified, the list reacts to the + click and calls the callback as ``on_double_click(index,choice)``. +:on_double_click2: Shift-double click callback; if specified, the list reacts to the click and + calls the callback as ``on_double_click2(index,choice)``. :row_height: Height of every row in text lines. :icon_width: If not *nil*, the specified number of character columns are reserved to the left of the list item for the icons. diff --git a/library/lua/gui/widgets.lua b/library/lua/gui/widgets.lua index 83b586421..5b918a13b 100644 --- a/library/lua/gui/widgets.lua +++ b/library/lua/gui/widgets.lua @@ -1537,6 +1537,8 @@ List.ATTRS{ on_select = DEFAULT_NIL, on_submit = DEFAULT_NIL, on_submit2 = DEFAULT_NIL, + on_double_click = DEFAULT_NIL, + on_double_click2 = DEFAULT_NIL, row_height = 1, scroll_keys = STANDARDSCROLL, icon_width = DEFAULT_NIL, @@ -1557,6 +1559,8 @@ function List:init(info) self.choices = {} self.selected = 1 end + + self.last_select_click_ms = 0 -- used to track double-clicking on an item end function List:setChoices(choices, selected) @@ -1765,6 +1769,16 @@ function List:submit2() end end +function List:double_click() + if #self.choices == 0 then return end + local cb = dfhack.internal.getModifiers().shift and + self.on_double_click2 or self.on_double_click + if cb then + cb(self:getSelected()) + return true + end +end + function List:onInput(keys) if self:inputToSubviews(keys) then return true @@ -1776,6 +1790,18 @@ function List:onInput(keys) elseif keys._MOUSE_L_DOWN then local idx = self:getIdxUnderMouse() if idx then + local now_ms = dfhack.getTickCount() + if idx ~= self:getSelected() then + self.last_select_click_ms = now_ms + else + if now_ms - self.last_select_click_ms <= DOUBLE_CLICK_MS then + self.last_select_click_ms = 0 + if self:double_click() then return true end + else + self.last_select_click_ms = now_ms + end + end + self:setSelected(idx) if dfhack.internal.getModifiers().shift then self:submit2()