Skip to content

Commit

Permalink
add: reapply style when it's changed
Browse files Browse the repository at this point in the history
  • Loading branch information
levovix0 committed Sep 8, 2024
1 parent 65746eb commit e2035c5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 15 deletions.
28 changes: 26 additions & 2 deletions src/sigui/styles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,42 @@ type
Styler* = ref object of UiObj
style*: Property[proc(obj: UiObj)]

objsCreatedUsingStyle: seq[UiObj]
runningStyle: bool

registerComponent Styler


method recieve*(this: Styler, signal: Signal) =
if signal of ChildAdded:
if this.style[] != nil:
this.style[](signal.ChildAdded.child)
if this.runningStyle:
this.objsCreatedUsingStyle.add signal.ChildAdded.child
this.style[](signal.ChildAdded.child)
else:
this.runningStyle = true
this.style[](signal.ChildAdded.child)
this.runningStyle = false

procCall this.super.recieve(signal)


# todo: re-apply styles when Styler.style changes
method init*(this: Styler) =
procCall this.super.init

this.style.changed.connectTo this:
for x in this.objsCreatedUsingStyle:
delete x
this.objsCreatedUsingStyle = @[]

if this.style[] != nil:
proc impl(n: UiObj) =
if n == nil: return
this.style[](n)
for x in n.childs:
impl(x)

impl(this)


macro makeStyle*(body: untyped): proc(obj: UiObj) =
Expand Down
7 changes: 7 additions & 0 deletions src/sigui/uibase.nim
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,13 @@ method deteach*(this: UiObj) {.base.} =
deteachStatic(this)


proc delete*(this: UiObj) =
deteach this
if this.parent != nil:
this.parent.childs.del this.parent.childs.find(this)
this.parent = nil


method addChild*(parent: Uiobj, child: Uiobj) {.base.} =
assert child.parent == nil
if parent.newChildsObject != nil:
Expand Down
45 changes: 32 additions & 13 deletions tests/t_styles.nim
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,43 @@ test "styles":
const typefaceFile = staticRead "Roboto-Regular.ttf"
let typeface = parseTtf(typefaceFile)

let darkTheme = makeStyle:
UiText:
font = typeface.withSize(24)
color = "ffffff"

UiRect:
color = "303030"
radius = 5

- UiText():
this.centerIn parent
text = "rect"
color = "808080"

let lightTheme = makeStyle:
apply darkTheme

UiText:
color = "000000"

UiRect:
color = "e0e0e0"
radius = 5

- UiText():
this.centerIn parent
text = "this should not be visible"
color = "808080"


window.makeLayout:
this.clearColor = "202020"

- Styler():
this.fill parent
style = makeStyle:
UiText:
font = typeface.withSize(24)
color = "ffffff"

UiRect:
color = "303030"
radius = 5

- UiText():
this.centerIn parent
text = "rect"
color = "808080"
style = lightTheme
style = darkTheme


- UiRect():
Expand Down

0 comments on commit e2035c5

Please sign in to comment.