Skip to content

Commit

Permalink
Refactor common widgets constant to "gui.widgets.common" module
Browse files Browse the repository at this point in the history
  • Loading branch information
wiktor-obrebski committed Oct 8, 2024
1 parent 4144b0e commit 52ed516
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 36 deletions.
25 changes: 9 additions & 16 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,39 +31,30 @@ DimensionsTooltip = require('gui.widgets.dimensions_tooltip')

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

---@return boolean
function getDoubleClickMs()
return Panel.DOUBLE_CLICK_MS
return common.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
common.DOUBLE_CLICK_MS = value
end

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

---@return boolean
function getScrollDelayMs()
return Scrollbar.SCROLL_DELAY_MS
return common.SCROLL_DELAY_MS
end
function setScrollDelayMs(value)
Scrollbar.SCROLL_DELAY_MS = 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
5 changes: 2 additions & 3 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 Down Expand Up @@ -311,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 <= Panel.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 @@ -526,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 = 500

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 = Scrollbar.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 = Scrollbar.STANDARDSCROLL,
scroll_keys = common.STANDARDSCROLL,
icon_width = DEFAULT_NIL,
}

Expand Down
17 changes: 2 additions & 15 deletions library/lua/gui/widgets/scrollbar.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
local common = require('gui.widgets.common')
local Widget = require('gui.widgets.widget')

local to_pen = dfhack.pen.parse
Expand Down Expand Up @@ -202,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
Scrollbar.SCROLL_INITIAL_DELAY_MS or Scrollbar.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 @@ -250,18 +251,4 @@ function Scrollbar:onInput(keys)
return true
end

---@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

0 comments on commit 52ed516

Please sign in to comment.