Skip to content

Commit 429cfe9

Browse files
committed
allow overriding all parameters in DefaultStyle
1 parent 2d716f3 commit 429cfe9

File tree

2 files changed

+102
-75
lines changed

2 files changed

+102
-75
lines changed

src/lwtk/DefaultStyle.lua

Lines changed: 90 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
local lwtk = require"lwtk"
22

3+
local lower = string.lower
4+
local upper = string.upper
35
local Color = lwtk.Color
46
local get = lwtk.StyleRef.get
57
local scale = lwtk.StyleRef.scale
@@ -9,102 +11,115 @@ local DefaultStyle = lwtk.newClass("lwtk.DefaultStyle", Super)
911

1012
function DefaultStyle.override:new(initParams)
1113

12-
local function par(name)
13-
local rslt = initParams and initParams[name]
14-
if rslt ~= nil then
15-
initParams[name] = nil
16-
return rslt
14+
local function par(name, defaultValue)
15+
if initParams then
16+
local rslt = initParams[name]
17+
if rslt ~= nil then
18+
initParams[name] = nil
19+
return rslt
20+
end
21+
local name2 = lower(name:sub(1,1))..name:sub(2)
22+
rslt = initParams[name2]
23+
if rslt ~= nil then
24+
initParams[name2] = nil
25+
return rslt
26+
end
27+
name2 = upper(name:sub(1,1))..name:sub(2)
28+
rslt = initParams[name2]
29+
if rslt ~= nil then
30+
initParams[name2] = nil
31+
return rslt
32+
end
1733
end
34+
return defaultValue
1835
end
1936

20-
local scaleFactor = par"scaleFactor" or 1
21-
local screenScale = par"screenScale" or 1
22-
local backgroundColor = par"backgroundColor" or Color"f9f9fa"
23-
local textColor = par"textColor" or Color"000000"
24-
local accentColor = par"accentColor" or Color"006fc7" --"0078d7"
25-
37+
local function parRule(name, defaultValue)
38+
return { name, par(name, defaultValue) }
39+
end
40+
2641
local ruleList = {
2742

28-
scaleFactor = scaleFactor * screenScale,
43+
scaleFactor = par("ScaleFactor", 1) * par("ScreenScale", 1),
2944

30-
{ "*TransitionSeconds", 0.05 },
31-
{ "VisibilityTransitionSeconds", 0.05 },
32-
{ "VisibilityTransitionSeconds:invisible", 0.10 },
33-
{ "FrameTransitionSeconds", 0.05 },
34-
{ "HoverTransitionSeconds", 0.20 },
35-
{ "PressedTransitionSeconds", 0.05 },
36-
{ "SimulateButtonClickSeconds", 0.10 },
45+
parRule( "*TransitionSeconds", 0.05 ),
46+
parRule( "VisibilityTransitionSeconds", 0.05 ),
47+
parRule( "VisibilityTransitionSeconds:invisible", 0.10 ),
48+
parRule( "FrameTransitionSeconds", 0.05 ),
49+
parRule( "HoverTransitionSeconds", 0.20 ),
50+
parRule( "PressedTransitionSeconds", 0.05 ),
51+
parRule( "SimulateButtonClickSeconds", 0.10 ),
3752

38-
{ "TextSize", 12 },
39-
{ "FontFamily", "sans-serif" },
40-
{ "ScrollBarSize", 12 },
53+
parRule( "TextSize", 12 ),
54+
parRule( "FontFamily", "sans-serif" ),
55+
parRule( "ScrollBarSize", 12 ),
4156

42-
{ "BackgroundColor", backgroundColor },
43-
{ "TextColor", textColor },
44-
{ "AccentColor", accentColor },
57+
parRule( "BackgroundColor", Color"f9f9fa" ),
58+
parRule( "TextColor", Color"000000" ),
59+
parRule( "AccentColor", Color"006fc7" ), --"0078d7"
4560

46-
{ "TextOffset", 0 },
61+
parRule( "TextOffset", 0 ),
4762

48-
{ "CursorColor", Color"00000000" }, -- Color"adadad" },
49-
{ "CursorColor:focused", Color"000000" },
50-
{ "CursorWidth", 2 },
63+
parRule( "CursorColor", Color"00000000" ), -- Color"adadad" ),
64+
parRule( "CursorColor:focused", Color"000000" ),
65+
parRule( "CursorWidth", 2 ),
5166

52-
{ "Margin@Control", 8 },
53-
{ "*Margin@Control", get"Margin" },
54-
{ "Height@Control", 24 },
55-
{ "BorderPadding@Control", 3 },
56-
{ "TextFullVisible@Control", true },
67+
parRule( "Margin@Control", 8 ),
68+
parRule( "*Margin@Control", get"Margin" ),
69+
parRule( "Height@Control", 24 ),
70+
parRule( "BorderPadding@Control", 3 ),
71+
parRule( "TextFullVisible@Control", true ),
5772

58-
{ "BorderSize@Box", 1 },
59-
{ "BorderPadding@Box", 3 },
60-
{ "BorderColor@Box", Color"adadad" },
73+
parRule( "BorderSize@Box", 1 ),
74+
parRule( "BorderPadding@Box", 3 ),
75+
parRule( "BorderColor@Box", Color"adadad" ),
6176

62-
{ "BorderPadding@TextLabel", 0 },
63-
{ "BorderSize@TextLabel", 0 },
64-
{ "BorderColor@TextLabel", nil },
77+
parRule( "BorderPadding@TextLabel", 0 ),
78+
parRule( "BorderSize@TextLabel", 0 ),
79+
parRule( "BorderColor@TextLabel", nil ),
6580

66-
{ "Width@PushButton", 80 },
67-
{ "BorderSize@PushButton", 1 },
68-
{ "BorderSize@PushButton:focused", 2 },
81+
parRule( "Width@PushButton", 80 ),
82+
parRule( "BorderSize@PushButton", 1 ),
83+
parRule( "BorderSize@PushButton:focused", 2 ),
6984

70-
{ "LeftPadding@PushButton",
71-
"RightPadding@PushButton", 10 },
72-
{ "TextOffset@PushButton:pressed+hover", 0.3 },
85+
parRule( "LeftPadding@PushButton", 10),
86+
parRule( "RightPadding@PushButton", 10),
87+
parRule( "TextOffset@PushButton:pressed+hover", 0.3 ),
7388

74-
{ "BackgroundColor@PushButton", Color"e1e1e1" },
75-
{ "BackgroundColor@PushButton:hover", Color"c9c9ca" },
76-
{ "BackgroundColor@PushButton:pressed", Color"c9c9ca" },
77-
{ "BackgroundColor@PushButton:pressed+hover", Color"b1b1b2" },
78-
{ "BackgroundColor@PushButton:default", Color"e5f1fb" },
79-
{ "BackgroundColor@PushButton:default+hover", Color"d5e1eb" },
80-
{ "BackgroundColor@PushButton:default+pressed", Color"c5d1db" },
81-
{ "BackgroundColor@PushButton:focused", Color"e5f1fb" },
82-
{ "BackgroundColor@PushButton:focused+hover", Color"d5e1eb" },
83-
{ "BackgroundColor@PushButton:focused+pressed", Color"c5d1db" },
89+
parRule( "BackgroundColor@PushButton", Color"e1e1e1" ),
90+
parRule( "BackgroundColor@PushButton:hover", Color"c9c9ca" ),
91+
parRule( "BackgroundColor@PushButton:pressed", Color"c9c9ca" ),
92+
parRule( "BackgroundColor@PushButton:pressed+hover", Color"b1b1b2" ),
93+
parRule( "BackgroundColor@PushButton:default", Color"e5f1fb" ),
94+
parRule( "BackgroundColor@PushButton:default+hover", Color"d5e1eb" ),
95+
parRule( "BackgroundColor@PushButton:default+pressed", Color"c5d1db" ),
96+
parRule( "BackgroundColor@PushButton:focused", Color"e5f1fb" ),
97+
parRule( "BackgroundColor@PushButton:focused+hover", Color"d5e1eb" ),
98+
parRule( "BackgroundColor@PushButton:focused+pressed", Color"c5d1db" ),
8499

85-
{ "TextColor@PushButton:disabled", Color"adadad" },
100+
parRule( "TextColor@PushButton:disabled", Color"adadad" ),
86101

87-
{ "BorderColor@PushButton", Color"adadad" },
88-
{ "BorderColor@PushButton:focused", get"AccentColor" },
89-
{ "BorderColor@PushButton:default", get"AccentColor" },
102+
parRule( "BorderColor@PushButton", Color"adadad" ),
103+
parRule( "BorderColor@PushButton:focused", get"AccentColor" ),
104+
parRule( "BorderColor@PushButton:default", get"AccentColor" ),
90105

91-
{ "MinColumns@TextInput", 10 },
92-
{ "Columns@TextInput", 20 },
93-
{ "MaxColumns@TextInput", -1 },
94-
{ "BorderColor@TextInput", Color"adadad" },
95-
{ "BorderColor@TextInput:focused", get"AccentColor" },
96-
{ "BorderSize@TextInput", 1 },
97-
{ "BorderSize@TextInput:focused", 2 },
98-
{ "FontFamily@TextInput", "monospace" },
99-
{ "TextFullVisible@TextInput", false },
106+
parRule( "MinColumns@TextInput", 10 ),
107+
parRule( "Columns@TextInput", 20 ),
108+
parRule( "MaxColumns@TextInput", -1 ),
109+
parRule( "BorderColor@TextInput", Color"adadad" ),
110+
parRule( "BorderColor@TextInput:focused", get"AccentColor" ),
111+
parRule( "BorderSize@TextInput", 1 ),
112+
parRule( "BorderSize@TextInput:focused", 2 ),
113+
parRule( "FontFamily@TextInput", "monospace" ),
114+
parRule( "TextFullVisible@TextInput", false ),
100115

101-
{ "TextSize@TitleText", scale(2, get"TextSize")},
116+
parRule( "TextSize@TitleText", scale(2, get"TextSize")),
102117

103-
{ "BorderColor@FocusGroup", Color"adadad" },
104-
{ "BorderColor@FocusGroup:focused", get"AccentColor" },
105-
{ "BorderSize@FocusGroup", 1 },
106-
{ "BorderSize@FocusGroup:focused", 2 },
107-
{ "BorderSize@FocusGroup:focused+entered", 1 },
118+
parRule( "BorderColor@FocusGroup", Color"adadad" ),
119+
parRule( "BorderColor@FocusGroup:focused", get"AccentColor" ),
120+
parRule( "BorderSize@FocusGroup", 1 ),
121+
parRule( "BorderSize@FocusGroup:focused", 2 ),
122+
parRule( "BorderSize@FocusGroup:focused+entered", 1 ),
108123
}
109124

110125
Super.new(self, ruleList)

src/tests/test02.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,4 +454,16 @@ do
454454
assertEq(g:childById("g2"):getStyleParam("yWidth"), 2000)
455455
end
456456
PRINT("----------------------------------------------------------------------------------")
457+
do
458+
local g = lwtk.Group {
459+
style = lwtk.DefaultStyle { textSize = 13 },
460+
lwtk.Group {
461+
id = "g1",
462+
},
463+
lwtk.TitleText {id = "tt"},
464+
}
465+
assertEq(g:getStyleParam("TextSize"), 13)
466+
assertEq(g:childById("tt"):getStyleParam("TextSize"), 2 * 13)
467+
end
468+
PRINT("----------------------------------------------------------------------------------")
457469
print("OK.")

0 commit comments

Comments
 (0)