Skip to content

macOS: Refactor NSView subclass implementation#262

Open
prokopyl wants to merge 14 commits into
masterfrom
macos-subclass-refactor
Open

macOS: Refactor NSView subclass implementation#262
prokopyl wants to merge 14 commits into
masterfrom
macos-subclass-refactor

Conversation

@prokopyl
Copy link
Copy Markdown
Member

@prokopyl prokopyl commented May 28, 2026

This PR introduces a safe wrapper around the AppKit NSView creation and lifecycle.

While a large part of objc2's API surface is safe, managing our custom-class' instance variables is not. And because our custom class is dynamically generated, there is no check for correctness of the subclass' methods signatures.

These two parts are what this wrapper handles, the rest is delegated to objc2's existing APIs.

This PR also introduces safe wrappers around the NSView's timer and notification observers, making them correctly de-register themselves when the NSView is deallocated, while not holding the NSView itself alive for too long.

This PR also completely overhauls the NSView ownership structure. It previously had a clunky WindowInner > NSView > WindowState > WindowInner > NSView ownership cycle that had to be manually broken.

Now the NSView properly and directly owns all of its internal state and object, except for the tiny bit of WindowSharedState that's needed by the WindowHandle (and now doesn't reference the NSView at all anymore).

Doing this also allowed to properly initialize the WindowHandler only after the NSView was fully initialized and parented, fixing #248.

@prokopyl prokopyl force-pushed the macos-subclass-refactor branch from 4d7a9cb to 0bc5754 Compare May 31, 2026 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant