Skip to content

Commit 6a3987b

Browse files
committed
Work in progress: ScreenScale handling & Bug Fixes
1 parent aaa5d22 commit 6a3987b

File tree

15 files changed

+438
-200
lines changed

15 files changed

+438
-200
lines changed

doc/Style.md

Lines changed: 80 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -392,12 +392,20 @@ Usually a style object is connected to the application and is used for all widge
392392
```lua
393393
local app = lwtk.Application {
394394
name = "example",
395-
style = { { "*Width", 10 },
396-
{ "*Width@MyWidget2", 20 } }
395+
style = { { "*Columns", 10 },
396+
{ "*Columns@MyWidget2", 20 },
397+
{ "*Width", 11 } }
397398
}
398399
local win = app:newWindow { w1, w2 }
399-
assert(w1:getStyleParam("FooWidth") == 10)
400-
assert(w2:getStyleParam("FooWidth") == 20)
400+
assert(w1:getStyleParam("FooColumns") == 10)
401+
assert(w2:getStyleParam("FooColumns") == 20)
402+
```
403+
404+
For scalable parameters (see [lwtk.BuiltinStyleTypes](../src/lwtk/BuiltinStyleTypes.lua))
405+
the screen scale factor is considered:
406+
```lua
407+
local scale = app:getScreenScale()
408+
assert(w1:getStyleParam("FooWidth") == scale * 11)
401409
```
402410

403411
If no style is specified, the application object gets the default style
@@ -409,16 +417,17 @@ local app = lwtk.Application {
409417
}
410418
local w1, w2, w4 = MyWidget1(), MyWidget2(), MyWidget4()
411419
local win = app:newWindow { w1, w2, w4 }
412-
assert(w1:getStyleParam("TextSize") == 12)
420+
assert(w1:getStyleParam("TextSize") == app:getScreenScale() * 12)
413421
```
414422

415423
Style rules can be added to existing style:
416424
```lua
417425
app:addStyle { { "TextSize@MyWidget1", 10 },
418426
{ "TextSize@MyWidget2", 20 } }
419-
assert(w1:getStyleParam("TextSize") == 10)
420-
assert(w2:getStyleParam("TextSize") == 20)
421-
assert(w4:getStyleParam("TextSize") == 12)
427+
local scale = app:getScreenScale()
428+
assert(w1:getStyleParam("TextSize") == scale * 10)
429+
assert(w2:getStyleParam("TextSize") == scale * 20)
430+
assert(w4:getStyleParam("TextSize") == scale * 12)
422431
```
423432

424433

@@ -430,34 +439,34 @@ Style rules can be specified for individual widgets:
430439
```lua
431440
local app = lwtk.Application {
432441
name = "example",
433-
style = { { "*Width", 10 },
434-
{ "*Height", 15 } }
442+
style = { { "*Seconds", 10 },
443+
{ "*Columns", 15 } }
435444
}
436445
local w1a = MyWidget1 {}
437446
local w1b = MyWidget1 {
438-
style = { { "*Width", 20 } } -- individual style for w1b
447+
style = { { "*Seconds", 20 } } -- individual style for w1b
439448
}
440449
local win = app:newWindow { w1a, w1b }
441-
assert(w1a:getStyleParam("FooWidth") == 10)
442-
assert(w1a:getStyleParam("FooHeight") == 15)
443-
assert(w1b:getStyleParam("FooWidth") == 20)
444-
assert(w1b:getStyleParam("FooHeight") == 15)
450+
assert(w1a:getStyleParam("FooSeconds") == 10)
451+
assert(w1a:getStyleParam("FooColumns") == 15)
452+
assert(w1b:getStyleParam("FooSeconds") == 20)
453+
assert(w1b:getStyleParam("FooColumns") == 15)
445454
```
446455

447456
<!-- ---------------------------------------------------------------------------------------- -->
448457

449458
Style can be replaced:
450459
```lua
451-
app:setStyle { { "*Width", 100 },
452-
{ "*Height", 200 } } -- replaces style for whole ap
453-
assert(w1a:getStyleParam("FooWidth") == 100)
454-
assert(w1a:getStyleParam("FooHeight") == 200)
455-
assert(w1b:getStyleParam("FooWidth") == 20) -- individual style for w1b is still active
456-
assert(w1b:getStyleParam("FooHeight") == 200)
457-
458-
w1b:setStyle { { "*Width", 300 } } -- replaces individual style for w1b
459-
assert(w1b:getStyleParam("FooWidth") == 300)
460-
assert(w1b:getStyleParam("FooHeight") == 200)
460+
app:setStyle { { "*Seconds", 100 },
461+
{ "*Columns", 200 } } -- replaces style for whole ap
462+
assert(w1a:getStyleParam("FooSeconds") == 100)
463+
assert(w1a:getStyleParam("FooColumns") == 200)
464+
assert(w1b:getStyleParam("FooSeconds") == 20) -- individual style for w1b is still active
465+
assert(w1b:getStyleParam("FooColumns") == 200)
466+
467+
w1b:setStyle { { "*Seconds", 300 } } -- replaces individual style for w1b
468+
assert(w1b:getStyleParam("FooSeconds") == 300)
469+
assert(w1b:getStyleParam("FooColumns") == 200)
461470
```
462471

463472
<!-- ---------------------------------------------------------------------------------------- -->
@@ -469,71 +478,71 @@ group's child widgets:
469478
```lua
470479
local app = lwtk.Application {
471480
name = "example",
472-
style = { { "*Width", 10 },
473-
{ "*Height", 15 },
474-
{ "*rHeight", 16 } }
481+
style = { { "*Seconds", 10 },
482+
{ "*Columns", 15 },
483+
{ "*rColumns", 16 } }
475484
}
476485
local w1a = MyWidget1 {}
477486
local w1b = MyWidget1 {
478-
style = { { "*oWidth", 21 } } -- individual style rule for w1b
487+
style = { { "*oSeconds", 21 } } -- individual style rule for w1b
479488
}
480489
local w2a = MyWidget2 {}
481490
local w2b = MyWidget2 {
482-
style = { { "*oWidth", 22 } } -- individual style rule for w2b
491+
style = { { "*oSeconds", 22 } } -- individual style rule for w2b
483492
}
484493
local g1 = lwtk.Group { w1a, w1b }
485494
local g2 = lwtk.Group { w2a, w2b,
486-
style = { { "*oHeight", 33 } } -- individual style rule for widget group g2
495+
style = { { "*oColumns", 33 } } -- individual style rule for widget group g2
487496
}
488497
local win = app:newWindow { g1, g2 }
489498

490-
assert(w1a:getStyleParam("FooWidth") == 10) -- from app
491-
assert(w1a:getStyleParam("FooHeight") == 15) -- from app
499+
assert(w1a:getStyleParam("FooSeconds") == 10) -- from app
500+
assert(w1a:getStyleParam("FooColumns") == 15) -- from app
492501

493-
assert(w1b:getStyleParam("FooWidth") == 21) -- from widget
494-
assert(w1b:getStyleParam("FooHeight") == 15) -- from app
502+
assert(w1b:getStyleParam("FooSeconds") == 21) -- from widget
503+
assert(w1b:getStyleParam("FooColumns") == 15) -- from app
495504

496-
assert(w2a:getStyleParam("FooWidth") == 10) -- from app
497-
assert(w2a:getStyleParam("FooHeight") == 33) -- from group
505+
assert(w2a:getStyleParam("FooSeconds") == 10) -- from app
506+
assert(w2a:getStyleParam("FooColumns") == 33) -- from group
498507

499-
assert(w2b:getStyleParam("FooWidth") == 22) -- from widget
500-
assert(w2b:getStyleParam("FooHeight") == 33) -- from group
501-
assert(w2b:getStyleParam("BarHeight") == 16) -- from app
508+
assert(w2b:getStyleParam("FooSeconds") == 22) -- from widget
509+
assert(w2b:getStyleParam("FooColumns") == 33) -- from group
510+
assert(w2b:getStyleParam("BarColumns") == 16) -- from app
502511
```
503512

504513
<!-- ---------------------------------------------------------------------------------------- -->
505514

506515
Replacing style in widget groups is also possible:
507516
```lua
508-
g1:setStyle { { "*oWidth", 17 } }
517+
g1:setStyle { { "*oSeconds", 17 } }
509518

510-
assert(w1a:getStyleParam("FooWidth") == 17) -- from group
511-
assert(w1a:getStyleParam("FooHeight") == 15) -- from app
519+
assert(w1a:getStyleParam("FooSeconds") == 17) -- from group
520+
assert(w1a:getStyleParam("FooColumns") == 15) -- from app
512521

513-
assert(w1b:getStyleParam("FooWidth") == 21) -- from widget
514-
assert(w1b:getStyleParam("FooHeight") == 15) -- from app
522+
assert(w1b:getStyleParam("FooSeconds") == 21) -- from widget
523+
assert(w1b:getStyleParam("FooColumns") == 15) -- from app
515524

516-
g2:setStyle { { "BarHeight", 34 },
517-
{ "*oHeight", 36 } }
525+
g2:setStyle { { "BarColumns", 34 },
526+
{ "*oColumns", 36 } }
518527

519-
assert(w2a:getStyleParam("FooWidth") == 10) -- from app
520-
assert(w2a:getStyleParam("FooHeight") == 36) -- from group
528+
assert(w2a:getStyleParam("FooSeconds") == 10) -- from app
529+
assert(w2a:getStyleParam("FooColumns") == 36) -- from group
521530

522-
assert(w2b:getStyleParam("FooWidth") == 22) -- from widget
523-
assert(w2b:getStyleParam("FooHeight") == 36) -- from group
524-
assert(w2b:getStyleParam("BarHeight") == 34) -- from group
525-
assert(w2b:getStyleParam("FarHeight") == 16) -- from app
531+
assert(w2b:getStyleParam("FooSeconds") == 22) -- from widget
532+
assert(w2b:getStyleParam("FooColumns") == 36) -- from group
533+
assert(w2b:getStyleParam("BarColumns") == 34) -- from group
534+
assert(w2b:getStyleParam("FarColumns") == 16) -- from app
526535

527-
app:setStyle { { "*Width", 18 },
528-
{ "*rHeight", 19 } }
536+
app:setStyle { { "*Seconds", 18 },
537+
{ "*rColumns", 19 } }
529538

530-
assert(w2a:getStyleParam("FooWidth") == 18) -- from app
531-
assert(w2a:getStyleParam("FooHeight") == 36) -- from group
539+
assert(w2a:getStyleParam("FooSeconds") == 18) -- from app
540+
assert(w2a:getStyleParam("FooColumns") == 36) -- from group
532541

533-
assert(w2b:getStyleParam("FooWidth") == 22) -- from widget
534-
assert(w2b:getStyleParam("FooHeight") == 36) -- from group
535-
assert(w2b:getStyleParam("BarHeight") == 34) -- from group
536-
assert(w2b:getStyleParam("FarHeight") == 19) -- from app
542+
assert(w2b:getStyleParam("FooSeconds") == 22) -- from widget
543+
assert(w2b:getStyleParam("FooColumns") == 36) -- from group
544+
assert(w2b:getStyleParam("BarColumns") == 34) -- from group
545+
assert(w2b:getStyleParam("FarColumns") == 19) -- from app
537546
```
538547

539548
<!-- ---------------------------------------------------------------------------------------- -->
@@ -547,31 +556,31 @@ to the group's child widgets:
547556
```lua
548557
local app = lwtk.Application {
549558
name = "example",
550-
style = { { "*Width", 10 },
551-
{ "*Height", 15 } }
559+
style = { { "*Seconds", 10 },
560+
{ "*Columns", 15 } }
552561
}
553562
local w1a = MyWidget1 {}
554563
local w1b = MyWidget1 {
555-
style = { FooWidth = 22 } -- individual style for w1b
564+
style = { FooSeconds = 22 } -- individual style for w1b
556565
}
557566
local g1 = lwtk.Group {
558567
style = {
559-
FooWidth = 23, -- individual style parameter for g1
560-
{ "*Width", 33 } -- individual style rule for g1
568+
FooSeconds = 23, -- individual style parameter for g1
569+
{ "*Seconds", 33 } -- individual style rule for g1
561570
},
562571
w1a,
563572
w1b
564573
}
565574

566575
local win = app:newWindow { g1 }
567576

568-
assert(g1:getStyleParam("FooWidth") == 23) -- from group
577+
assert(g1:getStyleParam("FooSeconds") == 23) -- from group
569578

570-
assert(w1a:getStyleParam("FooHeight") == 15) -- from app
571-
assert(w1a:getStyleParam("FooWidth") == 33) -- from group
579+
assert(w1a:getStyleParam("FooColumns") == 15) -- from app
580+
assert(w1a:getStyleParam("FooSeconds") == 33) -- from group
572581

573-
assert(w1b:getStyleParam("FooHeight") == 15) -- from app
574-
assert(w1b:getStyleParam("FooWidth") == 22) -- from widget
582+
assert(w1b:getStyleParam("FooColumns") == 15) -- from app
583+
assert(w1b:getStyleParam("FooSeconds") == 22) -- from widget
575584
```
576585

577586
TODO

example/example10.lua

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,20 @@ local app = Application {
6969
{ "Color@MyButton:pressed+hover", Color"b1b1b2" },
7070
}
7171
}
72+
73+
local scale = app.scale
74+
7275
local win = app:newWindow {
7376
title = "example10",
74-
size = { 240, 50 },
77+
size = scale { 240, 50 },
7578
Group {
7679
MyButton {
77-
frame = { 10, 10, 100, 30 },
80+
frame = scale { 10, 10, 100, 30 },
7881
text = "OK",
7982
onClicked = function() print("Button Clicked") end
8083
},
8184
MyButton {
82-
frame = { 120, 10, 100, 30 },
85+
frame = scale { 120, 10, 100, 30 },
8386
text = "Exit",
8487
onClicked = function() app:close() end
8588
}

src/lwtk/Actionable.lua

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,27 @@ end
1515
function Actionable:setInitParams(initParams)
1616
if initParams then
1717
local objectActions
18-
for k, v in ipairs(initParams) do
18+
local hasRemaining = false
19+
for k, v in pairs(initParams) do
1920
if type(k) == "string" and match(k, "^onAction") then
2021
if not objectActions then objectActions = {} end
2122
objectActions[k] = v
2223
initParams[k] = nil
24+
else
25+
hasRemaining = true
2326
end
2427
end
2528
if objectActions then
2629
getActions[self] = objectActions
2730
end
28-
Super.setAttributes(self, initParams)
31+
if hasRemaining then
32+
local handleRemainingInitParams = self.handleRemainingInitParams
33+
if handleRemainingInitParams then
34+
handleRemainingInitParams(self, initParams)
35+
else
36+
Super.setAttributes(self, initParams)
37+
end
38+
end
2939
end
3040
end
3141

src/lwtk/Animatable.lua

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,18 @@ function Animatable:setStyle(style)
262262
Styleable.setStyle(self, style)
263263
end
264264

265+
local function clearCaches(self)
266+
getCurrentValues[self] = {}
267+
for _, c in ipairs(self) do
268+
clearCaches(c)
269+
end
270+
end
271+
272+
function Animatable:getStyle()
273+
clearCaches(self)
274+
return Styleable.getStyle(self)
275+
end
276+
265277
function Animatable:getStyleParam(paramName)
266278
local value = getCurrentValues[self][paramName]
267279
if value == nil then

0 commit comments

Comments
 (0)