Skip to content

Commit 52ed516

Browse files
Refactor common widgets constant to "gui.widgets.common" module
1 parent 4144b0e commit 52ed516

6 files changed

Lines changed: 36 additions & 36 deletions

File tree

library/lua/gui/widgets.lua

Lines changed: 9 additions & 16 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,39 +31,30 @@ DimensionsTooltip = require('gui.widgets.dimensions_tooltip')
2931

3032
Tab = TabBar.Tab
3133
makeButtonLabelText = Label.makeButtonLabelText
34+
STANDARDSCROLL = common.STANDARDSCROLL
3235

3336
---@return boolean
3437
function getDoubleClickMs()
35-
return Panel.DOUBLE_CLICK_MS
38+
return common.DOUBLE_CLICK_MS
3639
end
3740
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
41+
common.DOUBLE_CLICK_MS = value
4742
end
4843

4944
---@return boolean
5045
function getScrollInitialDelayMs()
51-
return Scrollbar.SCROLL_INITIAL_DELAY_MS
46+
return common.SCROLL_INITIAL_DELAY_MS
5247
end
5348
function setScrollInitialDelayMs(value)
54-
Scrollbar.SCROLL_INITIAL_DELAY_MS = value
49+
common.SCROLL_INITIAL_DELAY_MS = value
5550
end
5651

5752
---@return boolean
5853
function getScrollDelayMs()
59-
return Scrollbar.SCROLL_DELAY_MS
54+
return common.SCROLL_DELAY_MS
6055
end
6156
function setScrollDelayMs(value)
62-
Scrollbar.SCROLL_DELAY_MS = value
57+
common.SCROLL_DELAY_MS = value
6358
end
6459

65-
66-
6760
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 & 3 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
@@ -311,7 +312,7 @@ function Panel:onInput(keys)
311312

312313
if self.resizable and y == 0 then
313314
local now_ms = dfhack.getTickCount()
314-
if now_ms - self.last_title_click_ms <= Panel.DOUBLE_CLICK_MS then
315+
if now_ms - self.last_title_click_ms <= common.DOUBLE_CLICK_MS then
315316
self.last_title_click_ms = 0
316317
if Panel_on_double_click(self) then return true end
317318
else
@@ -526,6 +527,4 @@ function Panel:onResizeEnd(success, new_frame)
526527
if self.on_resize_end then self.on_resize_end(success, new_frame) end
527528
end
528529

529-
Panel.DOUBLE_CLICK_MS = 500
530-
531530
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 = Scrollbar.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 = Scrollbar.STANDARDSCROLL,
64+
scroll_keys = common.STANDARDSCROLL,
6465
icon_width = DEFAULT_NIL,
6566
}
6667

library/lua/gui/widgets/scrollbar.lua

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

34
local to_pen = dfhack.pen.parse
@@ -202,7 +203,7 @@ function Scrollbar:onRenderBody(dc)
202203
if self.last_scroll_ms == 0 then return end
203204
local now = dfhack.getTickCount()
204205
local delay = self.is_first_click and
205-
Scrollbar.SCROLL_INITIAL_DELAY_MS or Scrollbar.SCROLL_DELAY_MS
206+
common.SCROLL_INITIAL_DELAY_MS or common.SCROLL_DELAY_MS
206207
if now - self.last_scroll_ms >= delay then
207208
self.is_first_click = false
208209
self.on_scroll(self.scroll_spec)
@@ -250,18 +251,4 @@ function Scrollbar:onInput(keys)
250251
return true
251252
end
252253

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
266-
267254
return Scrollbar

0 commit comments

Comments
 (0)