Skip to content

Commit

Permalink
can-lehmann#115 Reduce nil checks
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippMDoerner committed Jun 22, 2024
1 parent da9ca39 commit 402d35c
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 11 deletions.
12 changes: 2 additions & 10 deletions owlkettle/widgetdef.nim
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ proc redraw*(viewable: Viewable): bool =
viewable.viewed = newWidget
result = true

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

proc newStateRef*(subscribers: varargs[proc(state: WidgetState) {.closure.}]): StateRef =
var observers = initHashSet[proc(state: WidgetState)]()
Expand All @@ -96,15 +96,12 @@ proc newStateRef*(subscribers: varargs[proc(state: WidgetState) {.closure.}]): S
return StateRef(observers: observers)

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

return stateRef.state.unwrapInternalWidget()

proc setRef*(stateRef: StateRef, state: WidgetState) =
if stateRef.isNil():
return

let isStateChange = stateRef.state != state
if not isStateChange:
return
Expand All @@ -114,16 +111,11 @@ proc setRef*(stateRef: StateRef, state: WidgetState) =
observer(stateRef.state)

proc subscribe*(stateRef: StateRef, observer: proc(state: WidgetState)) =
if stateRef.isNil():
return

stateRef.observers.incl(observer)
if stateRef.hasRef():
observer(stateRef.state)

proc unsubscribe*(stateRef: StateRef, observer: proc(state: WidgetState)) =
if stateRef.isNil():
return
stateRef.observers.excl(observer)


Expand Down
5 changes: 4 additions & 1 deletion owlkettle/widgets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2279,8 +2279,11 @@ renderable SearchEntry of BaseWidget:

hooks keyCaptureRef:
build:
if widget.hasKeyCaptureRef:
if widget.hasKeyCaptureRef and widget.valKeyCaptureRef.hasRef():
proc observer(childState: WidgetState) =
if childState.isNil():
return

let childWidget = childState.unwrapInternalWidget()
if not childWidget.isNil():
gtk_search_entry_set_key_capture_widget(state.internalWidget, childWidget)
Expand Down

0 comments on commit 402d35c

Please sign in to comment.