From 402d35c41f51aceb88e0a29a0b5b09095b29cbee Mon Sep 17 00:00:00 2001 From: PhilippMDoerner Date: Sat, 22 Jun 2024 21:07:37 +0200 Subject: [PATCH] #115 Reduce nil checks --- owlkettle/widgetdef.nim | 12 ++---------- owlkettle/widgets.nim | 5 ++++- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/owlkettle/widgetdef.nim b/owlkettle/widgetdef.nim index 41e37a9..e2cff74 100644 --- a/owlkettle/widgetdef.nim +++ b/owlkettle/widgetdef.nim @@ -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)]() @@ -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 @@ -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) diff --git a/owlkettle/widgets.nim b/owlkettle/widgets.nim index 147fcd7..e7de1c8 100644 --- a/owlkettle/widgets.nim +++ b/owlkettle/widgets.nim @@ -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)