Skip to content

Commit

Permalink
app: [macOS] send ViewEvents when the NSView is attached to a NSWindow
Browse files Browse the repository at this point in the history
Instead of sending ViewEvents once at construction and once at destruction,
it's better to send them when the underlying NSView changes attachment.

The main advantage is that we're about to move the destruction and
emitting of DestroyEvent to the NSView's dealloc method. However, the
dealloc will not be called if user code has a strong reference to it
through a non-empty ViewEvent. By sending an empty ViewEvent when the
view is detached, well-behaving users will remove the strong reference.

Signed-off-by: Elias Naur <mail@eliasnaur.com>
  • Loading branch information
eliasnaur committed Feb 8, 2024
1 parent caba422 commit 1527e91
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
16 changes: 12 additions & 4 deletions app/os_macos.go
Original file line number Diff line number Diff line change
Expand Up @@ -857,11 +857,21 @@ func configFor(scale float32) unit.Metric {
}
}

//export gio_onAttached
func gio_onAttached(h C.uintptr_t, attached C.int) {
w := windowFor(h)
if attached != 0 {
layer := C.layerForView(w.view)
w.ProcessEvent(ViewEvent{View: uintptr(w.view), Layer: uintptr(layer)})
} else {
w.ProcessEvent(ViewEvent{})
w.setStage(StagePaused)
}
}

//export gio_onClose
func gio_onClose(h C.uintptr_t) {
w := windowFor(h)
w.ProcessEvent(ViewEvent{})
w.setStage(StagePaused)
w.ProcessEvent(DestroyEvent{})
w.displayLink.Close()
w.displayLink = nil
Expand Down Expand Up @@ -927,8 +937,6 @@ func newWindow(win *callbacks, options []Option) {
nextTopLeft = C.cascadeTopLeftFromPoint(window, nextTopLeft)
// makeKeyAndOrderFront assumes ownership of our window reference.
C.makeKeyAndOrderFront(window)
layer := C.layerForView(w.view)
w.ProcessEvent(ViewEvent{View: uintptr(w.view), Layer: uintptr(layer)})
})
<-res
}
Expand Down
1 change: 1 addition & 0 deletions app/os_macos.m
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ - (CALayer *)makeBackingLayer {
return layer;
}
- (void)viewDidMoveToWindow {
gio_onAttached(self.handle, self.window != nil ? 1 : 0);
if (self.window == nil) {
gio_onClose(self.handle);
}
Expand Down

0 comments on commit 1527e91

Please sign in to comment.