Skip to content

Commit ea4b174

Browse files
authored
Merge pull request #4972 from wiktor-obrebski/fix/widgets-constants
Migrate widgets constants to use getter/setter style
2 parents 7e779f1 + b647739 commit ea4b174

File tree

9 files changed

+64
-46
lines changed

9 files changed

+64
-46
lines changed

docs/dev/Lua API.rst

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5352,11 +5352,9 @@ If the panel has already been maximized in this fashion, then it will jump to
53525352
its minimum size. Both jumps respect the resizable edges defined by the
53535353
``resize_anchors`` attribute.
53545354

5355-
The time duration that a double click can span is defined by the global variable
5356-
``DOUBLE_CLICK_MS``. The default value is ``500`` and can be changed by the end
5357-
user with a command like::
5358-
5359-
:lua require('gui.widgets').DOUBLE_CLICK_MS=1000
5355+
The time duration that a double click can span can be controlled via the
5356+
`control-panel` or `gui/control-panel` interfaces (``Mouse double click speed``
5357+
option). It defaults to 500 ms.
53605358

53615359
Window class
53625360
------------
@@ -5558,16 +5556,14 @@ while scrolling will result in faster movement.
55585556
You can click and drag the scrollbar to scroll to a specific spot, or you can
55595557
click and hold on the end arrows or in the unfilled portion of the scrollbar to
55605558
scroll multiple times, just like in a normal browser scrollbar. The speed of
5561-
scroll events when the mouse button is held down is controlled by two global
5562-
variables:
5563-
5564-
:``SCROLL_INITIAL_DELAY_MS``: The delay before the second scroll event.
5565-
:``SCROLL_DELAY_MS``: The delay between further scroll events.
5559+
scroll events when the mouse button is held down can be controlled
5560+
via the `control-panel` or `gui/control-panel` interfaces:
55665561

5567-
The defaults are 300 and 20, respectively, but they can be overridden by the
5568-
user in their :file:`dfhack-config/init/dfhack.init` file, for example::
5562+
1. The delay before the second scroll event is the ``Mouse initial scroll repeat
5563+
delay`` setting (default is 300 ms)
55695564

5570-
:lua require('gui.widgets').SCROLL_DELAY_MS = 100
5565+
2. The delay between further scroll events is the ``Mouse scroll repeat delay`` option
5566+
(default is 20 ms)
55715567

55725568
Label class
55735569
-----------

library/lua/gui/widgets.lua

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
local _ENV = mkmodule('gui.widgets')
44

5+
local common = require('gui.widgets.common')
6+
57
Widget = require('gui.widgets.widget')
68
Divider = require('gui.widgets.divider')
79
Panel = require('gui.widgets.containers.panel')
@@ -29,10 +31,30 @@ DimensionsTooltip = require('gui.widgets.dimensions_tooltip')
2931

3032
Tab = TabBar.Tab
3133
makeButtonLabelText = Label.makeButtonLabelText
34+
STANDARDSCROLL = common.STANDARDSCROLL
35+
36+
---@return boolean
37+
function getDoubleClickMs()
38+
return common.DOUBLE_CLICK_MS
39+
end
40+
function setDoubleClickMs(value)
41+
common.DOUBLE_CLICK_MS = value
42+
end
43+
44+
---@return boolean
45+
function getScrollInitialDelayMs()
46+
return common.SCROLL_INITIAL_DELAY_MS
47+
end
48+
function setScrollInitialDelayMs(value)
49+
common.SCROLL_INITIAL_DELAY_MS = value
50+
end
3251

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
52+
---@return boolean
53+
function getScrollDelayMs()
54+
return common.SCROLL_DELAY_MS
55+
end
56+
function setScrollDelayMs(value)
57+
common.SCROLL_DELAY_MS = value
58+
end
3759

3860
return _ENV

library/lua/gui/widgets/common.lua

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
local _ENV = mkmodule('gui.widgets.common')
2+
3+
DOUBLE_CLICK_MS = 500
4+
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+
SCROLL_INITIAL_DELAY_MS = 300
17+
SCROLL_DELAY_MS = 20
18+
19+
return _ENV

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
local gui = require('gui')
22
local utils = require('utils')
33
local guidm = require('gui.dwarfmode')
4+
local common = require('gui.widgets.common')
45
local Widget = require('gui.widgets.widget')
56

67
local getval = utils.getval
@@ -10,8 +11,6 @@ local to_pen = dfhack.pen.parse
1011
-- Panel --
1112
-----------
1213

13-
DOUBLE_CLICK_MS = 500
14-
1514
---@class widgets.Panel.attrs: widgets.Widget.attrs
1615
---@field frame_style? gui.Frame|fun(): gui.Frame
1716
---@field frame_title? string
@@ -313,7 +312,7 @@ function Panel:onInput(keys)
313312

314313
if self.resizable and y == 0 then
315314
local now_ms = dfhack.getTickCount()
316-
if now_ms - self.last_title_click_ms <= DOUBLE_CLICK_MS then
315+
if now_ms - self.last_title_click_ms <= common.DOUBLE_CLICK_MS then
317316
self.last_title_click_ms = 0
318317
if Panel_on_double_click(self) then return true end
319318
else
@@ -528,6 +527,4 @@ function Panel:onResizeEnd(success, new_frame)
528527
if self.on_resize_end then self.on_resize_end(success, new_frame) end
529528
end
530529

531-
Panel.DOUBLE_CLICK_MS = DOUBLE_CLICK_MS
532-
533530
return Panel

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local gui = require('gui')
22
local utils = require('utils')
3+
local common = require('gui.widgets.common')
34
local Widget = require('gui.widgets.widget')
45
local Scrollbar = require('gui.widgets.scrollbar')
56

@@ -277,7 +278,7 @@ Label.ATTRS{
277278
auto_width = false,
278279
on_click = DEFAULT_NIL,
279280
on_rclick = DEFAULT_NIL,
280-
scroll_keys = STANDARDSCROLL,
281+
scroll_keys = common.STANDARDSCROLL,
281282
}
282283

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

library/lua/gui/widgets/list.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local gui = require('gui')
22
local utils = require('utils')
3+
local common = require('gui.widgets.common')
34
local Widget = require('gui.widgets.widget')
45
local Scrollbar = require('gui.widgets.scrollbar')
56
local Label = require('gui.widgets.labels.label')
@@ -60,7 +61,7 @@ List.ATTRS{
6061
on_double_click = DEFAULT_NIL,
6162
on_double_click2 = DEFAULT_NIL,
6263
row_height = 1,
63-
scroll_keys = STANDARDSCROLL,
64+
scroll_keys = common.STANDARDSCROLL,
6465
icon_width = DEFAULT_NIL,
6566
}
6667

library/lua/gui/widgets/scrollbar.lua

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,12 @@
1+
local common = require('gui.widgets.common')
12
local Widget = require('gui.widgets.widget')
23

34
local to_pen = dfhack.pen.parse
45

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-
176
---------------
187
-- Scrollbar --
198
---------------
209

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

@@ -217,7 +203,7 @@ function Scrollbar:onRenderBody(dc)
217203
if self.last_scroll_ms == 0 then return end
218204
local now = dfhack.getTickCount()
219205
local delay = self.is_first_click and
220-
SCROLL_INITIAL_DELAY_MS or SCROLL_DELAY_MS
206+
common.SCROLL_INITIAL_DELAY_MS or common.SCROLL_DELAY_MS
221207
if now - self.last_scroll_ms >= delay then
222208
self.is_first_click = false
223209
self.on_scroll(self.scroll_spec)
@@ -265,8 +251,4 @@ function Scrollbar:onInput(keys)
265251
return true
266252
end
267253

268-
Scrollbar.STANDARDSCROLL = STANDARDSCROLL
269-
Scrollbar.SCROLL_INITIAL_DELAY_MS = SCROLL_INITIAL_DELAY_MS
270-
Scrollbar.SCROLL_DELAY_MS = SCROLL_DELAY_MS
271-
272254
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)