Skip to content

Commit

Permalink
Merge pull request #4972 from wiktor-obrebski/fix/widgets-constants
Browse files Browse the repository at this point in the history
Migrate widgets constants to use getter/setter style
  • Loading branch information
myk002 authored Oct 8, 2024
2 parents 7e779f1 + b647739 commit ea4b174
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 46 deletions.
22 changes: 9 additions & 13 deletions docs/dev/Lua API.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5352,11 +5352,9 @@ 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
user with a command like::

:lua require('gui.widgets').DOUBLE_CLICK_MS=1000
The time duration that a double click can span can be controlled via the
`control-panel` or `gui/control-panel` interfaces (``Mouse double click speed``
option). It defaults to 500 ms.

Window class
------------
Expand Down Expand Up @@ -5558,16 +5556,14 @@ 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_INITIAL_DELAY_MS``: The delay before the second scroll event.
:``SCROLL_DELAY_MS``: The delay between further scroll events.
scroll events when the mouse button is held down can be controlled
via the `control-panel` or `gui/control-panel` interfaces:

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::
1. The delay before the second scroll event is the ``Mouse initial scroll repeat
delay`` setting (default is 300 ms)

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

Label class
-----------
Expand Down
30 changes: 26 additions & 4 deletions library/lua/gui/widgets.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

local _ENV = mkmodule('gui.widgets')

local common = require('gui.widgets.common')

Widget = require('gui.widgets.widget')
Divider = require('gui.widgets.divider')
Panel = require('gui.widgets.containers.panel')
Expand Down Expand Up @@ -29,10 +31,30 @@ DimensionsTooltip = require('gui.widgets.dimensions_tooltip')

Tab = TabBar.Tab
makeButtonLabelText = Label.makeButtonLabelText
STANDARDSCROLL = common.STANDARDSCROLL

---@return boolean
function getDoubleClickMs()
return common.DOUBLE_CLICK_MS
end
function setDoubleClickMs(value)
common.DOUBLE_CLICK_MS = value
end

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

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 getScrollDelayMs()
return common.SCROLL_DELAY_MS
end
function setScrollDelayMs(value)
common.SCROLL_DELAY_MS = value
end

return _ENV
19 changes: 19 additions & 0 deletions library/lua/gui/widgets/common.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
local _ENV = mkmodule('gui.widgets.common')

DOUBLE_CLICK_MS = 500

---@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',
}
SCROLL_INITIAL_DELAY_MS = 300
SCROLL_DELAY_MS = 20

return _ENV
7 changes: 2 additions & 5 deletions library/lua/gui/widgets/containers/panel.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
local gui = require('gui')
local utils = require('utils')
local guidm = require('gui.dwarfmode')
local common = require('gui.widgets.common')
local Widget = require('gui.widgets.widget')

local getval = utils.getval
Expand All @@ -10,8 +11,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 +312,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 <= common.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 +527,4 @@ 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

return Panel
3 changes: 2 additions & 1 deletion library/lua/gui/widgets/labels/label.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local gui = require('gui')
local utils = require('utils')
local common = require('gui.widgets.common')
local Widget = require('gui.widgets.widget')
local Scrollbar = require('gui.widgets.scrollbar')

Expand Down Expand Up @@ -277,7 +278,7 @@ Label.ATTRS{
auto_width = false,
on_click = DEFAULT_NIL,
on_rclick = DEFAULT_NIL,
scroll_keys = STANDARDSCROLL,
scroll_keys = common.STANDARDSCROLL,
}

---@param args widgets.Label.attrs.partial
Expand Down
3 changes: 2 additions & 1 deletion library/lua/gui/widgets/list.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local gui = require('gui')
local utils = require('utils')
local common = require('gui.widgets.common')
local Widget = require('gui.widgets.widget')
local Scrollbar = require('gui.widgets.scrollbar')
local Label = require('gui.widgets.labels.label')
Expand Down Expand Up @@ -60,7 +61,7 @@ List.ATTRS{
on_double_click = DEFAULT_NIL,
on_double_click2 = DEFAULT_NIL,
row_height = 1,
scroll_keys = STANDARDSCROLL,
scroll_keys = common.STANDARDSCROLL,
icon_width = DEFAULT_NIL,
}

Expand Down
22 changes: 2 additions & 20 deletions library/lua/gui/widgets/scrollbar.lua
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
local common = require('gui.widgets.common')
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 +203,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
common.SCROLL_INITIAL_DELAY_MS or common.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 +251,4 @@ 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

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
2 changes: 1 addition & 1 deletion scripts

0 comments on commit ea4b174

Please sign in to comment.