Merge pull request #2786 from myk002/myk_list_double_click

[widgets.List] react to double clicks on list items
develop
Myk 2023-02-03 06:45:50 -08:00 committed by GitHub
commit f9376d696e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 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`: 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. - `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.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 - ``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 - ``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`` - `tiletypes`: now has a Lua API! ``tiletypes_setTile``
## Removed ## Removed

@ -4875,6 +4875,10 @@ It has the following attributes:
key/click and calls the callback as ``on_submit(index,choice)``. 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 :on_submit2: Shift-click callback; if specified, the list reacts to the click and
calls the callback as ``on_submit2(index,choice)``. 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. :row_height: Height of every row in text lines.
:icon_width: If not *nil*, the specified number of character columns :icon_width: If not *nil*, the specified number of character columns
are reserved to the left of the list item for the icons. are reserved to the left of the list item for the icons.

@ -1537,6 +1537,8 @@ List.ATTRS{
on_select = DEFAULT_NIL, on_select = DEFAULT_NIL,
on_submit = DEFAULT_NIL, on_submit = DEFAULT_NIL,
on_submit2 = DEFAULT_NIL, on_submit2 = DEFAULT_NIL,
on_double_click = DEFAULT_NIL,
on_double_click2 = DEFAULT_NIL,
row_height = 1, row_height = 1,
scroll_keys = STANDARDSCROLL, scroll_keys = STANDARDSCROLL,
icon_width = DEFAULT_NIL, icon_width = DEFAULT_NIL,
@ -1557,6 +1559,8 @@ function List:init(info)
self.choices = {} self.choices = {}
self.selected = 1 self.selected = 1
end end
self.last_select_click_ms = 0 -- used to track double-clicking on an item
end end
function List:setChoices(choices, selected) function List:setChoices(choices, selected)
@ -1765,6 +1769,16 @@ function List:submit2()
end end
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) function List:onInput(keys)
if self:inputToSubviews(keys) then if self:inputToSubviews(keys) then
return true return true
@ -1776,6 +1790,18 @@ function List:onInput(keys)
elseif keys._MOUSE_L_DOWN then elseif keys._MOUSE_L_DOWN then
local idx = self:getIdxUnderMouse() local idx = self:getIdxUnderMouse()
if idx then 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) self:setSelected(idx)
if dfhack.internal.getModifiers().shift then if dfhack.internal.getModifiers().shift then
self:submit2() self:submit2()
@ -1871,6 +1897,16 @@ function FilteredList:init(info)
return info.on_submit2(self:getSelected()) return info.on_submit2(self:getSelected())
end end
end end
if info.on_double_click then
self.list.on_double_click = function()
return info.on_double_click(self:getSelected())
end
end
if info.on_double_click2 then
self.list.on_double_click2 = function()
return info.on_double_click2(self:getSelected())
end
end
self.not_found = Label{ self.not_found = Label{
visible = true, visible = true,
text = info.not_found_label or 'No matches', text = info.not_found_label or 'No matches',