Merge pull request #2378 from myk002/myk_list_mouse

Allow widgets.List to report hover target and respond to shift-click
develop
Myk 2022-11-09 10:32:06 -08:00 committed by GitHub
commit 9fed02435e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 8 deletions

@ -4325,8 +4325,8 @@ It has the following attributes:
with an empty list. with an empty list.
:on_submit: Enter key or mouse click callback; if specified, the list reacts to the :on_submit: Enter key or mouse click callback; if specified, the list reacts to the
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-Enter key callback; if specified, the list reacts to the key :on_submit2: Shift-Enter key or shift-mouse click callback; if specified, the list
and calls it as ``on_submit2(index,choice)``. reacts to the key/click and calls it as ``on_submit2(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.
@ -4363,6 +4363,11 @@ The list supports the following methods:
Returns the selected *index, choice*, or nothing if the list is empty. Returns the selected *index, choice*, or nothing if the list is empty.
* ``list:getIdxUnderMouse()``
Returns the index of the list item under the mouse cursor, or nothing if the
list is empty or the mouse is not over a list item.
* ``list:getContentWidth()`` * ``list:getContentWidth()``
Returns the minimal width to draw all choices without clipping. Returns the minimal width to draw all choices without clipping.

@ -77,6 +77,8 @@ changelog.txt uses a syntax similar to RST, with a few special sequences:
## Lua ## Lua
- ``widgets.Scrollbar``: new scrollbar widget that can be paired with an associated scrollable widget. Integrated with ``widgets.Label`` and ``widgets.List``. - ``widgets.Scrollbar``: new scrollbar widget that can be paired with an associated scrollable widget. Integrated with ``widgets.Label`` and ``widgets.List``.
- ``widgets.List``: new ``getIdxUnderMouse()`` function for detecting the list index under the active mouse cursor
- ``widgets.List``: shift-clicking now triggers the ``submit2`` attribute function if it is defined
- ``dfhack.constructions.findAtTile()``: exposed preexisting function to Lua. - ``dfhack.constructions.findAtTile()``: exposed preexisting function to Lua.
- ``dfhack.constructions.insert()``: exposed new function to Lua. - ``dfhack.constructions.insert()``: exposed new function to Lua.
- ``widgets.EditField`` now allows other widgets to process characters that the ``on_char`` callback rejects. - ``widgets.EditField`` now allows other widgets to process characters that the ``on_char`` callback rejects.

@ -444,7 +444,9 @@ local BAR_BG_CHAR = string.char(179)
function Scrollbar:onRenderBody(dc) function Scrollbar:onRenderBody(dc)
-- don't draw if all elements are visible -- don't draw if all elements are visible
if not scrollbar_is_visible(self) then return end if not scrollbar_is_visible(self) then
return
end
-- render up arrow if we're not at the top -- render up arrow if we're not at the top
dc:seek(0, 0):char( dc:seek(0, 0):char(
self.top_elem == 1 and NO_ARROW_CHAR or UP_ARROW_CHAR, self.fg, self.bg) self.top_elem == 1 and NO_ARROW_CHAR or UP_ARROW_CHAR, self.fg, self.bg)
@ -1216,6 +1218,14 @@ function List:onRenderBody(dc)
end end
end end
function List:getIdxUnderMouse()
local _,mouse_y = self:getMousePos()
if mouse_y and #self.choices > 0 and
mouse_y < (#self.choices-self.page_top+1) * self.row_height then
return self.page_top + math.floor(mouse_y/self.row_height)
end
end
function List:submit() function List:submit()
if self.on_submit and #self.choices > 0 then if self.on_submit and #self.choices > 0 then
self.on_submit(self:getSelected()) self.on_submit(self:getSelected())
@ -1239,12 +1249,14 @@ function List:onInput(keys)
self:submit2() self:submit2()
return true return true
elseif keys._MOUSE_L then elseif keys._MOUSE_L then
local _, mouse_y = self:getMousePos() local idx = self:getIdxUnderMouse()
if mouse_y and #self.choices > 0 and if idx then
mouse_y < (#self.choices-self.page_top+1) * self.row_height then
local idx = self.page_top + math.floor(mouse_y/self.row_height)
self:setSelected(idx) self:setSelected(idx)
self:submit() if dfhack.internal.getModifiers().shift then
self:submit2()
else
self:submit()
end
return true return true
end end
else else