Skip to content

Commit 4144b0e

Browse files
Migrate widgets constant to use getter/setter style
1 parent 116e795 commit 4144b0e

7 files changed

Lines changed: 69 additions & 38 deletions

File tree

docs/dev/Lua API.rst

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5348,11 +5348,15 @@ If the panel has already been maximized in this fashion, then it will jump to
53485348
its minimum size. Both jumps respect the resizable edges defined by the
53495349
``resize_anchors`` attribute.
53505350

5351-
The time duration that a double click can span is defined by the global variable
5352-
``DOUBLE_CLICK_MS``. The default value is ``500`` and can be changed by the end
5351+
The time duration that a double click can span can be controlled by widgets API:
5352+
5353+
* ``widgets.getDoubleClickMs()``
5354+
* ``widgets.setDoubleClickMs(value)``
5355+
5356+
The default value is ``500`` and can be changed by the end
53535357
user with a command like::
53545358

5355-
:lua require('gui.widgets').DOUBLE_CLICK_MS=1000
5359+
:lua require('gui.widgets').setDoubleClickMs(1000)
53565360

53575361
Window class
53585362
------------
@@ -5554,16 +5558,21 @@ while scrolling will result in faster movement.
55545558
You can click and drag the scrollbar to scroll to a specific spot, or you can
55555559
click and hold on the end arrows or in the unfilled portion of the scrollbar to
55565560
scroll multiple times, just like in a normal browser scrollbar. The speed of
5557-
scroll events when the mouse button is held down is controlled by two global
5558-
variables:
5561+
scroll events when the mouse button is held down can be controlled by two global
5562+
getter/setter pairs:
5563+
5564+
1. The delay before the second scroll event.
5565+
* ``widgets.getScrollInitialDelayMs()``
5566+
* ``widgets.setScrollInitialDelayMs(value)``
55595567

5560-
:``SCROLL_INITIAL_DELAY_MS``: The delay before the second scroll event.
5561-
:``SCROLL_DELAY_MS``: The delay between further scroll events.
5568+
2. The delay between further scroll events.
5569+
* ``widgets.getScrollDelayMs()``
5570+
* ``widgets.setScrollDelayMs(value)``
55625571

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

5566-
:lua require('gui.widgets').SCROLL_DELAY_MS = 100
5575+
:lua require('gui.widgets').setScrollDelayMs(100)
55675576

55685577
Label class
55695578
-----------

library/lua/gui/widgets.lua

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,38 @@ DimensionsTooltip = require('gui.widgets.dimensions_tooltip')
3030
Tab = TabBar.Tab
3131
makeButtonLabelText = Label.makeButtonLabelText
3232

33-
DOUBLE_CLICK_MS = Panel.DOUBLE_CLICK_MS
34-
STANDARDSCROLL = Scrollbar.STANDARDSCROLL
35-
SCROLL_INITIAL_DELAY_MS = Scrollbar.SCROLL_INITIAL_DELAY_MS
36-
SCROLL_DELAY_MS = Scrollbar.SCROLL_DELAY_MS
33+
---@return boolean
34+
function getDoubleClickMs()
35+
return Panel.DOUBLE_CLICK_MS
36+
end
37+
function setDoubleClickMs(value)
38+
Panel.DOUBLE_CLICK_MS = value
39+
end
40+
41+
---@return boolean
42+
function getStandardScroll()
43+
return Scrollbar.STANDARDSCROLL
44+
end
45+
function setStandardScroll(value)
46+
Scrollbar.STANDARDSCROLL = value
47+
end
48+
49+
---@return boolean
50+
function getScrollInitialDelayMs()
51+
return Scrollbar.SCROLL_INITIAL_DELAY_MS
52+
end
53+
function setScrollInitialDelayMs(value)
54+
Scrollbar.SCROLL_INITIAL_DELAY_MS = value
55+
end
56+
57+
---@return boolean
58+
function getScrollDelayMs()
59+
return Scrollbar.SCROLL_DELAY_MS
60+
end
61+
function setScrollDelayMs(value)
62+
Scrollbar.SCROLL_DELAY_MS = value
63+
end
64+
65+
3766

3867
return _ENV

library/lua/gui/widgets/containers/panel.lua

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ local to_pen = dfhack.pen.parse
1010
-- Panel --
1111
-----------
1212

13-
DOUBLE_CLICK_MS = 500
14-
1513
---@class widgets.Panel.attrs: widgets.Widget.attrs
1614
---@field frame_style? gui.Frame|fun(): gui.Frame
1715
---@field frame_title? string
@@ -313,7 +311,7 @@ function Panel:onInput(keys)
313311

314312
if self.resizable and y == 0 then
315313
local now_ms = dfhack.getTickCount()
316-
if now_ms - self.last_title_click_ms <= DOUBLE_CLICK_MS then
314+
if now_ms - self.last_title_click_ms <= Panel.DOUBLE_CLICK_MS then
317315
self.last_title_click_ms = 0
318316
if Panel_on_double_click(self) then return true end
319317
else
@@ -528,6 +526,6 @@ function Panel:onResizeEnd(success, new_frame)
528526
if self.on_resize_end then self.on_resize_end(success, new_frame) end
529527
end
530528

531-
Panel.DOUBLE_CLICK_MS = DOUBLE_CLICK_MS
529+
Panel.DOUBLE_CLICK_MS = 500
532530

533531
return Panel

library/lua/gui/widgets/labels/label.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ Label.ATTRS{
277277
auto_width = false,
278278
on_click = DEFAULT_NIL,
279279
on_rclick = DEFAULT_NIL,
280-
scroll_keys = STANDARDSCROLL,
280+
scroll_keys = Scrollbar.STANDARDSCROLL,
281281
}
282282

283283
---@param args widgets.Label.attrs.partial

library/lua/gui/widgets/list.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ List.ATTRS{
6060
on_double_click = DEFAULT_NIL,
6161
on_double_click2 = DEFAULT_NIL,
6262
row_height = 1,
63-
scroll_keys = STANDARDSCROLL,
63+
scroll_keys = Scrollbar.STANDARDSCROLL,
6464
icon_width = DEFAULT_NIL,
6565
}
6666

library/lua/gui/widgets/scrollbar.lua

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,10 @@ local Widget = require('gui.widgets.widget')
22

33
local to_pen = dfhack.pen.parse
44

5-
---@enum STANDARDSCROLL
6-
STANDARDSCROLL = {
7-
STANDARDSCROLL_UP = -1,
8-
KEYBOARD_CURSOR_UP = -1,
9-
STANDARDSCROLL_DOWN = 1,
10-
KEYBOARD_CURSOR_DOWN = 1,
11-
STANDARDSCROLL_PAGEUP = '-page',
12-
KEYBOARD_CURSOR_UP_FAST = '-page',
13-
STANDARDSCROLL_PAGEDOWN = '+page',
14-
KEYBOARD_CURSOR_DOWN_FAST = '+page',
15-
}
16-
175
---------------
186
-- Scrollbar --
197
---------------
208

21-
SCROLL_INITIAL_DELAY_MS = 300
22-
SCROLL_DELAY_MS = 20
23-
249
---@class widgets.Scrollbar.attrs: widgets.Widget.attrs
2510
---@field on_scroll? fun(new_top_elem?: integer)
2611

@@ -217,7 +202,7 @@ function Scrollbar:onRenderBody(dc)
217202
if self.last_scroll_ms == 0 then return end
218203
local now = dfhack.getTickCount()
219204
local delay = self.is_first_click and
220-
SCROLL_INITIAL_DELAY_MS or SCROLL_DELAY_MS
205+
Scrollbar.SCROLL_INITIAL_DELAY_MS or Scrollbar.SCROLL_DELAY_MS
221206
if now - self.last_scroll_ms >= delay then
222207
self.is_first_click = false
223208
self.on_scroll(self.scroll_spec)
@@ -265,8 +250,18 @@ function Scrollbar:onInput(keys)
265250
return true
266251
end
267252

268-
Scrollbar.STANDARDSCROLL = STANDARDSCROLL
269-
Scrollbar.SCROLL_INITIAL_DELAY_MS = SCROLL_INITIAL_DELAY_MS
270-
Scrollbar.SCROLL_DELAY_MS = SCROLL_DELAY_MS
253+
---@enum STANDARDSCROLL
254+
Scrollbar.STANDARDSCROLL = {
255+
STANDARDSCROLL_UP = -1,
256+
KEYBOARD_CURSOR_UP = -1,
257+
STANDARDSCROLL_DOWN = 1,
258+
KEYBOARD_CURSOR_DOWN = 1,
259+
STANDARDSCROLL_PAGEUP = '-page',
260+
KEYBOARD_CURSOR_UP_FAST = '-page',
261+
STANDARDSCROLL_PAGEDOWN = '+page',
262+
KEYBOARD_CURSOR_DOWN_FAST = '+page',
263+
}
264+
Scrollbar.SCROLL_INITIAL_DELAY_MS = 300
265+
Scrollbar.SCROLL_DELAY_MS = 20
271266

272267
return Scrollbar

plugins/lua/burrow.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ function BurrowDesignationOverlay:onInput(keys)
105105
self.last_click_ms = now_ms
106106
self.saved_pos = pos
107107
elseif fill ~= 'off' then
108-
if now_ms - self.last_click_ms <= widgets.DOUBLE_CLICK_MS then
108+
if now_ms - self.last_click_ms <= widgets.getDoubleClickMs() then
109109
self.last_click_ms = 0
110110
local do_3d = fill == '3d'
111111
self.pending_fn = curry(flood_fill, pos, if_burrow.erasing, do_3d)

0 commit comments

Comments
 (0)