Skip to content

Commit

Permalink
can-lehmann#115 Improve handling of stateRef changes
Browse files Browse the repository at this point in the history
Previously the stateRef got the observer unsubscribed and resubscribed every update.
That shouldn't happen, we only want to do that when the stateRef actually change.
Therefore we now check first for changes in the ref.

That uncovered that we need to also do the initial subscription.
This is now handled in the build hook for this field.
  • Loading branch information
PhilippMDoerner committed Jun 21, 2024
1 parent 988dd29 commit faa291f
Showing 1 changed file with 32 additions and 18 deletions.
50 changes: 32 additions & 18 deletions owlkettle/widgets.nim
Original file line number Diff line number Diff line change
Expand Up @@ -2278,25 +2278,39 @@ renderable SearchEntry of BaseWidget:
state.internalWidget.disconnect(state.stopSearch)

hooks keyCaptureRef:
build:
if widget.hasKeyCaptureRef:
proc observer(childState: WidgetState) =
let childWidget = childState.unwrapInternalWidget()
if not childWidget.isNil():
gtk_search_entry_set_key_capture_widget(state.internalWidget, childWidget)

widget.valKeyCaptureRef.subscribe(observer)
state.keyCaptureRef = widget.valKeyCaptureRef

update:
proc observer(childState: WidgetState) =
let childWidget = childState.unwrapInternalWidget()
if not childWidget.isNil():
gtk_search_entry_set_key_capture_widget(state.internalWidget, childWidget)

# Remove observer from old keyCaptureRef
let oldKeyCaptureRef = state.keyCaptureRef
oldKeyCaptureRef.unsubscribe(observer)

# Add observer to new keyCaptureRef
let hasNewKeyCaptureRef = widget.hasKeyCaptureRef
let newKeyCaptureRef = if hasNewKeyCaptureRef:
widget.valKeyCaptureRef.subscribe(observer)
widget.valKeyCaptureRef
else:
nil

state.keyCaptureRef = newKeyCaptureRef
let isChangeFromNoneToSome = state.keyCaptureRef.isNil() and widget.hasKeyCaptureRef
let isChangeFromSomeToNone = not state.keyCaptureRef.isNil() and not widget.hasKeyCaptureRef
let isRefChange = isChangeFromNoneToSome or isChangeFromSomeToNone or state.keyCaptureRef != widget.valKeyCaptureRef
if isRefChange:
proc observer(childState: WidgetState) =
let childWidget = childState.unwrapInternalWidget()
if not childWidget.isNil():
gtk_search_entry_set_key_capture_widget(state.internalWidget, childWidget)

# Remove observer from old keyCaptureRef
let oldKeyCaptureRef = state.keyCaptureRef
oldKeyCaptureRef.unsubscribe(observer)

# Add observer to new keyCaptureRef
let hasNewKeyCaptureRef = widget.hasKeyCaptureRef
let newKeyCaptureRef = if hasNewKeyCaptureRef:
widget.valKeyCaptureRef.subscribe(observer)
widget.valKeyCaptureRef
else:
nil

state.keyCaptureRef = newKeyCaptureRef

hooks text:
property:
Expand Down

0 comments on commit faa291f

Please sign in to comment.