Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate widgets constants to use getter/setter style #4972

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5348,11 +5348,15 @@ If the panel has already been maximized in this fashion, then it will jump to
its minimum size. Both jumps respect the resizable edges defined by the
``resize_anchors`` attribute.

The time duration that a double click can span is defined by the global variable
``DOUBLE_CLICK_MS``. The default value is ``500`` and can be changed by the end
The time duration that a double click can span can be controlled by widgets API:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you update this to indicate that the control-panel or gui/control-panel interfaces should be used to set these properties? That goes for the others as well. You can remove the widget.* function references. These docs were written before the control panel was a thing.

The gui/control-panel interface looks like this:
image

and the control-panel interface looks like this:
image

the functions should no longer be called by anything other than the control panel interfaces


* ``widgets.getDoubleClickMs()``
* ``widgets.setDoubleClickMs(value)``

The default value is ``500`` and can be changed by the end
user with a command like::

:lua require('gui.widgets').DOUBLE_CLICK_MS=1000
:lua require('gui.widgets').setDoubleClickMs(1000)

Window class
------------
Expand Down Expand Up @@ -5554,16 +5558,21 @@ while scrolling will result in faster movement.
You can click and drag the scrollbar to scroll to a specific spot, or you can
click and hold on the end arrows or in the unfilled portion of the scrollbar to
scroll multiple times, just like in a normal browser scrollbar. The speed of
scroll events when the mouse button is held down is controlled by two global
variables:
scroll events when the mouse button is held down can be controlled by two global
getter/setter pairs:

1. The delay before the second scroll event.
* ``widgets.getScrollInitialDelayMs()``
* ``widgets.setScrollInitialDelayMs(value)``

:``SCROLL_INITIAL_DELAY_MS``: The delay before the second scroll event.
:``SCROLL_DELAY_MS``: The delay between further scroll events.
2. The delay between further scroll events.
* ``widgets.getScrollDelayMs()``
* ``widgets.setScrollDelayMs(value)``

The defaults are 300 and 20, respectively, but they can be overridden by the
user in their :file:`dfhack-config/init/dfhack.init` file, for example::

:lua require('gui.widgets').SCROLL_DELAY_MS = 100
:lua require('gui.widgets').setScrollDelayMs(100)

Label class
-----------
Expand Down
37 changes: 33 additions & 4 deletions library/lua/gui/widgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,38 @@ DimensionsTooltip = require('gui.widgets.dimensions_tooltip')
Tab = TabBar.Tab
makeButtonLabelText = Label.makeButtonLabelText

DOUBLE_CLICK_MS = Panel.DOUBLE_CLICK_MS
STANDARDSCROLL = Scrollbar.STANDARDSCROLL
SCROLL_INITIAL_DELAY_MS = Scrollbar.SCROLL_INITIAL_DELAY_MS
SCROLL_DELAY_MS = Scrollbar.SCROLL_DELAY_MS
---@return boolean
function getDoubleClickMs()
return Panel.DOUBLE_CLICK_MS
end
function setDoubleClickMs(value)
Panel.DOUBLE_CLICK_MS = value
end

---@return boolean
function getStandardScroll()
return Scrollbar.STANDARDSCROLL
end
function setStandardScroll(value)
Scrollbar.STANDARDSCROLL = value
end
Comment on lines +41 to +47
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

STANDARDSCROLL is a constant and doesn't need a function wrapper. we could keep getStandardScroll() if you think it makes the API clearer, but there should not be a setStandardScroll()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(my vote is that it doesn't make the API clearer)


---@return boolean
function getScrollInitialDelayMs()
return Scrollbar.SCROLL_INITIAL_DELAY_MS
end
function setScrollInitialDelayMs(value)
Scrollbar.SCROLL_INITIAL_DELAY_MS = value
end

---@return boolean
function getScrollDelayMs()
return Scrollbar.SCROLL_DELAY_MS
end
function setScrollDelayMs(value)
Scrollbar.SCROLL_DELAY_MS = value
end



return _ENV
6 changes: 2 additions & 4 deletions library/lua/gui/widgets/containers/panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ local to_pen = dfhack.pen.parse
-- Panel --
-----------

DOUBLE_CLICK_MS = 500

---@class widgets.Panel.attrs: widgets.Widget.attrs
---@field frame_style? gui.Frame|fun(): gui.Frame
---@field frame_title? string
Expand Down Expand Up @@ -313,7 +311,7 @@ function Panel:onInput(keys)

if self.resizable and y == 0 then
local now_ms = dfhack.getTickCount()
if now_ms - self.last_title_click_ms <= DOUBLE_CLICK_MS then
if now_ms - self.last_title_click_ms <= Panel.DOUBLE_CLICK_MS then
self.last_title_click_ms = 0
if Panel_on_double_click(self) then return true end
else
Expand Down Expand Up @@ -528,6 +526,6 @@ function Panel:onResizeEnd(success, new_frame)
if self.on_resize_end then self.on_resize_end(success, new_frame) end
end

Panel.DOUBLE_CLICK_MS = DOUBLE_CLICK_MS
Panel.DOUBLE_CLICK_MS = 500

return Panel
2 changes: 1 addition & 1 deletion library/lua/gui/widgets/labels/label.lua
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ Label.ATTRS{
auto_width = false,
on_click = DEFAULT_NIL,
on_rclick = DEFAULT_NIL,
scroll_keys = STANDARDSCROLL,
scroll_keys = Scrollbar.STANDARDSCROLL,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we have multiple other widgets referencing STANDARDSCROLL, maybe it should move back to being a top-level constant (like I think it was before, in widgets.lua)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue here, I think, is that widgets can't include gui.widgets since then it would be a circular dependency

another option is to move all constants (and settable preferences) into a separate, common file

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, maybe that would be a circular dependency. In that case, I think we do need a separate constants file. Making Label depend on Scrollbar for a shared constant feels very arbitrary to me, and could result in cyclical dependencies down the road if we add more constants that multiple widgets need.

}

---@param args widgets.Label.attrs.partial
Expand Down
2 changes: 1 addition & 1 deletion library/lua/gui/widgets/list.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ List.ATTRS{
on_double_click = DEFAULT_NIL,
on_double_click2 = DEFAULT_NIL,
row_height = 1,
scroll_keys = STANDARDSCROLL,
scroll_keys = Scrollbar.STANDARDSCROLL,
icon_width = DEFAULT_NIL,
}

Expand Down
33 changes: 14 additions & 19 deletions library/lua/gui/widgets/scrollbar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,10 @@ local Widget = require('gui.widgets.widget')

local to_pen = dfhack.pen.parse

---@enum STANDARDSCROLL
STANDARDSCROLL = {
STANDARDSCROLL_UP = -1,
KEYBOARD_CURSOR_UP = -1,
STANDARDSCROLL_DOWN = 1,
KEYBOARD_CURSOR_DOWN = 1,
STANDARDSCROLL_PAGEUP = '-page',
KEYBOARD_CURSOR_UP_FAST = '-page',
STANDARDSCROLL_PAGEDOWN = '+page',
KEYBOARD_CURSOR_DOWN_FAST = '+page',
}

---------------
-- Scrollbar --
---------------

SCROLL_INITIAL_DELAY_MS = 300
SCROLL_DELAY_MS = 20

---@class widgets.Scrollbar.attrs: widgets.Widget.attrs
---@field on_scroll? fun(new_top_elem?: integer)

Expand Down Expand Up @@ -217,7 +202,7 @@ function Scrollbar:onRenderBody(dc)
if self.last_scroll_ms == 0 then return end
local now = dfhack.getTickCount()
local delay = self.is_first_click and
SCROLL_INITIAL_DELAY_MS or SCROLL_DELAY_MS
Scrollbar.SCROLL_INITIAL_DELAY_MS or Scrollbar.SCROLL_DELAY_MS
if now - self.last_scroll_ms >= delay then
self.is_first_click = false
self.on_scroll(self.scroll_spec)
Expand Down Expand Up @@ -265,8 +250,18 @@ function Scrollbar:onInput(keys)
return true
end

Scrollbar.STANDARDSCROLL = STANDARDSCROLL
Scrollbar.SCROLL_INITIAL_DELAY_MS = SCROLL_INITIAL_DELAY_MS
Scrollbar.SCROLL_DELAY_MS = SCROLL_DELAY_MS
---@enum STANDARDSCROLL
Scrollbar.STANDARDSCROLL = {
STANDARDSCROLL_UP = -1,
KEYBOARD_CURSOR_UP = -1,
STANDARDSCROLL_DOWN = 1,
KEYBOARD_CURSOR_DOWN = 1,
STANDARDSCROLL_PAGEUP = '-page',
KEYBOARD_CURSOR_UP_FAST = '-page',
STANDARDSCROLL_PAGEDOWN = '+page',
KEYBOARD_CURSOR_DOWN_FAST = '+page',
}
Scrollbar.SCROLL_INITIAL_DELAY_MS = 300
Scrollbar.SCROLL_DELAY_MS = 20

return Scrollbar
2 changes: 1 addition & 1 deletion plugins/lua/burrow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ function BurrowDesignationOverlay:onInput(keys)
self.last_click_ms = now_ms
self.saved_pos = pos
elseif fill ~= 'off' then
if now_ms - self.last_click_ms <= widgets.DOUBLE_CLICK_MS then
if now_ms - self.last_click_ms <= widgets.getDoubleClickMs() then
self.last_click_ms = 0
local do_3d = fill == '3d'
self.pending_fn = curry(flood_fill, pos, if_burrow.erasing, do_3d)
Expand Down
Loading