react to double clicks on list items

develop
Myk Taylor 2023-02-02 20:24:40 -08:00
parent 76712a533c
commit 2316615763
No known key found for this signature in database
3 changed files with 32 additions and 1 deletions

@ -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

@ -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.

@ -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()