Skip to content

Commit

Permalink
can-lehmann#115 improve semantics and add utility for fetching widget
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippMDoerner committed Jun 21, 2024
1 parent eb6e66f commit 3cc80ee
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion owlkettle/widgetdef.nim
Original file line number Diff line number Diff line change
Expand Up @@ -88,14 +88,20 @@ proc redraw*(viewable: Viewable): bool =
viewable.viewed = newWidget
result = true

proc hasRef*(stateRef: StateRef): bool = not stateRef.state.isNil()
proc hasRef*(stateRef: StateRef): bool = not stateRef.isNil() and not stateRef.state.isNil()

proc newRef*(subscribers: varargs[proc(state: WidgetState) {.closure.}]): StateRef =
var observers = initHashSet[proc(state: WidgetState)]()
for subscriber in subscribers:
observers.incl(subscriber)
return StateRef(observers: observers)

proc unwrapInternalWidget*(stateRef: StateRef): GtkWidget =
if stateRef.isNil() or stateRef.state.isNil():
return nil.GtkWidget

return stateRef.state.unwrapInternalWidget()

proc setRef*(stateRef: StateRef, state: WidgetState) =
if stateRef.isNil():
return
Expand All @@ -110,6 +116,8 @@ proc setRef*(stateRef: StateRef, state: WidgetState) =

proc subscribe*(stateRef: StateRef, observer: proc(state: WidgetState)) =
stateRef.observers.incl(observer)
if stateRef.hasRef():
observer(stateRef.state)

proc unsubscribe*(stateRef: StateRef, observer: proc(state: WidgetState)) =
stateRef.observers.excl(observer)
Expand Down

0 comments on commit 3cc80ee

Please sign in to comment.