Skip to content

Commit

Permalink
add: subsribing to raw events in syntax suger for re-building changab…
Browse files Browse the repository at this point in the history
…le childs
  • Loading branch information
levovix0 committed Nov 8, 2024
1 parent 2321f7d commit 721809a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ elementsObj --- Layout():
text = "+"
```

The `<--- ctor: props[]...` syntax can be used to re-build tree based on property changes
The `<--- ctor: prop[]; event[]; ...` syntax can be used to re-build tree based on property changes
```nim
var elements = ["first", "second"].property
var elementsObj: CustomProperty[Layout]
Expand Down
4 changes: 4 additions & 0 deletions src/sigui/events.nim
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,7 @@ template connectTo*(s: var Event[void], obj: var EventHandler, argname: untyped,
proc hasHandlers*(e: Event): bool =
if e.p == nil: return false
e.p.connected.len > 0


template changed*[T](e: Event[T]): Event[T] =
e
25 changes: 18 additions & 7 deletions tests/t_todoapp.nim
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@ import siwin
import sigui
import t_customComponent

type App = ref object of Uiobj
tasks: seq[tuple[name: string, complete: Property[bool]]]
tasksChanged: Event[void]
type
App = ref object of Uiobj
tasks: seq[tuple[name: string, complete: Property[bool]]]
tasksChanged: Event[void]

layout: CustomProperty[Layout]
layout: CustomProperty[Layout]

DestroyLogger = object
message: string

registerComponent App


proc `=destroy`(x: DestroyLogger) =
if x.message != "":
echo x.message


test "todo app":
let window = newOpenglWindow(size = ivec2(500, 800), title = "todos").newUiWindow

Expand Down Expand Up @@ -84,6 +93,8 @@ test "todo app":
top = taskAdder.bottom + 20

app.layout --- Layout():
<--- Layout(): app.tasksChanged[]

this.binding w: parent.w[]

orientation = vertical
Expand All @@ -92,6 +103,8 @@ test "todo app":
for i in 0..app.tasks.high:
template task: auto = app.tasks[i]

let logger {.used.} = DestroyLogger(message: "destroyed: " & $i)

- Layout():
spacing = 10
align = center
Expand Down Expand Up @@ -119,8 +132,6 @@ test "todo app":
this.fill parent
this.mouseDownAndUpInside.connectTo this:
task.complete[] = not task.complete[]

app.tasksChanged.connectTo app:
app.layout[] = Layout()
echo "made sure ", logger.message, " works"

run window.siwinWindow

0 comments on commit 721809a

Please sign in to comment.