Skip to content

Commit dd7051a

Browse files
TimMenu/Main: improved doebase abit
1 parent ece3564 commit dd7051a

File tree

1 file changed

+34
-39
lines changed

1 file changed

+34
-39
lines changed

TimMenu/Main.lua

Lines changed: 34 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ end
5151

5252
local _currentWindow = nil
5353

54+
local function withCurrentWindow(func, ...)
55+
local win = TimMenu.GetCurrentWindow()
56+
if not win then
57+
return
58+
end
59+
return func(win, ...)
60+
end
61+
5462
local Common = require("TimMenu.Common")
5563
local Globals = require("TimMenu.Globals")
5664
local Utils = require("TimMenu.Utils")
@@ -170,24 +178,21 @@ function TimMenu.ShowDebug()
170178
end
171179

172180
function TimMenu.NextLine(spacing)
173-
local win = TimMenu.GetCurrentWindow()
174-
if win then
181+
return withCurrentWindow(function(win, spacing)
175182
win:NextLine(spacing)
176-
end
183+
end, spacing)
177184
end
178185

179186
function TimMenu.SameLine(spacing)
180-
local win = TimMenu.GetCurrentWindow()
181-
if win then
187+
return withCurrentWindow(function(win, spacing)
182188
win:SameLine(spacing)
183-
end
189+
end, spacing)
184190
end
185191

186192
function TimMenu.Spacing(verticalSpacing)
187-
local win = TimMenu.GetCurrentWindow()
188-
if win then
193+
return withCurrentWindow(function(win, verticalSpacing)
189194
win:Spacing(verticalSpacing)
190-
end
195+
end, verticalSpacing)
191196
end
192197

193198
function TimMenu.Slider(label, value, min, max, step)
@@ -199,11 +204,9 @@ function TimMenu.Slider(label, value, min, max, step)
199204
end
200205

201206
function TimMenu.Separator(label)
202-
local win = TimMenu.GetCurrentWindow()
203-
if not win then
204-
return
205-
end
206-
return SeparatorLayout.Draw(win, label)
207+
return withCurrentWindow(function(win, label)
208+
return SeparatorLayout.Draw(win, label)
209+
end, label)
207210
end
208211

209212
function TimMenu.TextInput(label, text)
@@ -255,11 +258,9 @@ function TimMenu.TabControl(id, tabs, defaultSelection)
255258
end
256259

257260
function TimMenu.BeginSector(label)
258-
local win = TimMenu.GetCurrentWindow()
259-
if not win then
260-
return
261-
end
262-
SectorWidget.Begin(win, label)
261+
return withCurrentWindow(function(win, label)
262+
SectorWidget.Begin(win, label)
263+
end, label)
263264
end
264265

265266
function TimMenu.EndSector()
@@ -368,29 +369,25 @@ function TimMenu.Keybind(label, currentKey)
368369
return withFontReset(Widgets.Keybind, win, label, currentKey)
369370
end
370371

371-
function TimMenu.FontSet(name, size, weight)
372-
Globals.Style.FontName = name
373-
Globals.Style.FontSize = size
374-
Globals.Style.FontWeight = weight
372+
local function setFontStyle(style, name, size, weight)
373+
Globals.Style[style .. "Name"] = name
374+
Globals.Style[style .. "Size"] = size
375+
Globals.Style[style .. "Weight"] = weight
375376
Globals.ReloadFonts()
376377
end
377378

379+
function TimMenu.FontSet(name, size, weight)
380+
setFontStyle("Font", name, size, weight)
381+
end
382+
378383
function TimMenu.FontSetBold(name, size, weight)
379-
Globals.Style.FontBoldName = name
380-
Globals.Style.FontBoldSize = size
381-
Globals.Style.FontBoldWeight = weight
382-
Globals.ReloadFonts()
384+
setFontStyle("FontBold", name, size, weight)
383385
end
384386

385387
function TimMenu.FontReset()
386388
local d = Globals.DefaultFontSettings
387-
Globals.Style.FontName = d.FontName
388-
Globals.Style.FontSize = d.FontSize
389-
Globals.Style.FontWeight = d.FontWeight
390-
Globals.Style.FontBoldName = d.FontBoldName
391-
Globals.Style.FontBoldSize = d.FontBoldSize
392-
Globals.Style.FontBoldWeight = d.FontBoldWeight
393-
Globals.ReloadFonts()
389+
setFontStyle("Font", d.FontName, d.FontSize, d.FontWeight)
390+
setFontStyle("FontBold", d.FontBoldName, d.FontBoldSize, d.FontBoldWeight)
394391
end
395392

396393
function TimMenu.ColorPicker(label, color)
@@ -402,11 +399,9 @@ function TimMenu.ColorPicker(label, color)
402399
end
403400

404401
function TimMenu.Tooltip(text)
405-
local win = TimMenu.GetCurrentWindow()
406-
if not win then
407-
return
408-
end
409-
Widgets.Tooltip.AttachToLastWidget(win, text)
402+
return withCurrentWindow(function(win, text)
403+
Widgets.Tooltip.AttachToLastWidget(win, text)
404+
end, text)
410405
end
411406

412407
_G.TimMenu = TimMenu

0 commit comments

Comments
 (0)