8000 Apple embedding point: ApplePlatformView, kind 28 Platform, sui_platform_set_view (#145) by rleibman · Pull Request #152 · rleibman/thicket · GitHub
[go: up one dir, main page]

Skip to content

Apple embedding point: ApplePlatformView, kind 28 Platform, sui_platform_set_view (#145) - #152

Merged
rleibman merged 4 commits into
mainfrom
issue/145-apple-platform-view
Nov 4, 2026
Merged

rleibman merged 4 commits into
mainfrom
issue/145-apple-platform-view

Conversation

@rleibman
@rleibman rleibman commented Nov 4, 2026
Copy link
Copy Markdown
Owner

Closes #145. This is the Apple half of docs/17 step 1, the embedding point. #148 (camera preview, -apple) builds on it.

What

  • ApplePlatformView (modules/renderer-apple/.../ApplePlatformView.scala):
    trait ApplePlatformView extends PlatformView {
      def create(): Ptr[Byte]                  // NSView* / UIView*, +1 retained, in no superview
      def release(view: Ptr[Byte]): Unit = ()  // about to go: still alive, still in its host
    }
    The trait is Scala-facing. A separate -apple module implements it and calls its own Swift library from create, which returns Unmanaged.passRetained(v).toOpaque(). The README's new section documents this.
  • Ownership: sui_platform_set_view(host, view) takes the caller's +1 over (takeRetainedValue), so the module never releases it. On destroy or replacement, the renderer calls release first, while the view is still alive and in its host. Then sui_platform_set_view(host, NULL) drops the view, which frees it. sui_destroy on a host empties it as well.
  • Shim: kind 28 Platform → PlatformHost on AppKit and UIKit, holding one child pinned to its four edges. I used 28 rather than 26 so that Apple RadioGroup and Stepper: radio NSButtons / an iOS checkmark list; NSStepper / UIStepper #150 (RadioGroup and Stepper) can take 26 and 27. There is one new function, sui_platform_set_view. When a view is not an ApplePlatformView, or create returns null, sui_set_text on the host shows <description>: not available on AppKit (or UIKit).
  • Gallery: the host makes its view in C, outside the renderer: an NSTextField on macOS and a UILabel on iOS (apple-shared/src/main/resources/scala-native/thicket_gallery_native_label.c). build.sbt adds that resource directory. The Platform section has the same checks as GTK and Android, the heading plus 5 checks. In release, the check asks a weak reference whether the view is alive, then reads its superview and its text. After the replacement, the weak reference must go nil.

Measured

  • shimGen/testFull 51/51; shimGen/run --check: current.
  • macOS gallery 228 ok, 0 failed with the screen unlocked, and 224 per run with it locked inside verify-apple.sh. The first label is 1435×16 pt, the same size as its host. It was freed 337–368 ms after the replacement.
  • iOS simulator ("iPhone 17e") gallery 239 ok, 0 failed on the warm simulator. The label is 358×20.3 pt and was freed 63–129 ms after the replacement.
  • bin/verify-apple.sh "iPhone 17e" at dda2bd5: 19/20 steps passed.
    • ios-gallery failed on one check only: "notifications start Denied, not yet asked", on a freshly booted simulator. That is the known trap, and the Platform checks passed in that run.
    • Re-run alone on the warm simulator straight after, the iOS gallery passed with 239 ok and 0 failed.
    • Two earlier lone iOS runs, at load average 85–99, failed in the camera, location and notifications sections' driven steps. The Platform checks passed in both.

Falsified (macOS; each run: 223 ok, 1 failed, on exactly the matching check)

  • release skipped → "released while still alive in its host, then freed" fails (released []).
  • release called after the shim dropped the view → the same check fails (in its host: false).
  • Empty "not available" text → "a view AppKit has nothing for says so" fails 8000 .
  • The shim keeps the caller's retain (takeUnretainedValue) → the same check fails (old view still alive after 2015 ms).

Not done or not measured

🤖 Generated with Claude Code

Roberto Leibman and others added 3 commits November 3, 2026 19:09
…t_view, ApplePlatformView, gallery checks

macOS gallery 228 ok, 0 FAIL (the 6 Platform checks included); shimGen/testFull 51/51.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… freed

Falsified on macOS, each 223 ok / 1 failed on the matching check: release skipped,
release after the shim dropped the view, empty 'not available' label, shim keeping the +1.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…12 and the gallery README

macOS gallery 228 ok / 0 failed (224 with the screen locked); iOS simulator (iPhone 17e) 239 / 0
on the warm simulator. verify-apple.sh 19/20: ios-gallery failed only on the fresh boot's
notifications 'not yet asked' check (known trap), Platform checks passing; alone it passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note

Copilot was unable to complete this review at the selected tier due to model policy or plan restrictions.

🟡 Changes recommended

A moderate-severity active finding reports a memory leak and non-reentrant state in the Gallery platform self-test because weak-reference slots allocated with malloc are never freed and related buffers aren’t cleared.

1 open issue
What changed in this PR

This pull request adds Apple (AppKit/UIKit) support for Thicket’s “embedding point” (WidgetKind.Platform / Prop.View) by introducing an ApplePlatformView contract, a new shim host kind to embed externally-created native views, and gallery self-tests/docs to validate lifecycle and ownership behavior.

Changes:

  • Introduces ApplePlatformView plus renderer-side lifecycle handling for creating/hosting/releasing NSView*/UIView* pointers.
  • Extends the Apple shim ABI with kind 28 (PlatformHost) and a new function sui_platform_set_view(host, view) implemented for both AppKit and UIKit.
  • Updates the Apple gallery to exercise the feature via a native label created in C, adds Platform self-tests, and updates related docs/build wiring.

Review comments to address:

  • GalleryPlatformChecks.scala: weak-reference slots allocated via thk_gallery_weak_new are not freed (thk_gallery_weak_free), and buffers are not cleared, causing leaks and non-reentrant checks (moderate, votes: 3).
  • GalleryPlatformChecks.scala: NativeLabel.made grows unbounded; consider capping to the last 2 entries and freeing dropped weak slots to avoid accumulating state and brittle assertions (moderate, votes: 1).
File Summary
tools/​shim-gen/​src/​main/​scala/​thicket/​tools/​shim/​Abi.scala Adds kind 28 (Platform) and the sui_platform_set_view ABI definition/grouping.
modules/​renderer-apple/​src/​main/​scala/​thicket/​renderer/​apple/​Shim.scala Exposes sui_platform_set_view and the new ShimKind.Platform constant to Scala Native.
modules/​renderer-apple/​src/​main/​scala/​thicket/​renderer/​apple/​AppleRenderer.scala Maps WidgetKind.Platform to kind 28 and wires Prop.View handling/cleanup through the renderer.
modules/​renderer-apple/​src/​main/​scala/​thicket/​renderer/​apple/​ApplePlatformView.scala Introduces ApplePlatformView and renderer-side bookkeeping for hosting/replacement/destroy sequencing.
modules/​renderer-apple/​shim/​Sources/​Shim+UIKit.swift Implements PlatformHost and _cdecl("sui_platform_set_view") for UIKit, including ownership transfer behavior.
modules/​renderer-apple/​shim/​Sources/​Shim+AppKit.swift Implements PlatformHost and _cdecl("sui_platform_set_view") for AppKit, including ownership transfer behavior.
modules/​renderer-apple/​shim/​include/​thicket_apple.h Documents kind 28 and declares sui_platform_set_view in the generated C header.
modules/​renderer-apple/​README.md Documents the Apple embedding contract and ownership/release expectations for modules implementing ApplePlatformView.
examples/​gallery/​README.md Updates gallery documentation to include the new Apple Platform checks and measurements.
examples/​gallery/​macos/​src/​main/​scala/​example/​gallery/​GalleryMac.scala Configures the macOS gallery to supply a native label factory for Platform hosting.
examples/​gallery/​ios/​src/​main/​scala/​example/​gallery/​GalleryIos.scala Configures the iOS gallery to supply a native label factory for Platform hosting.
examples/​gallery/​apple-shared/​src/​main/​scala/​example/​gallery/​GallerySelfTest.scala Inserts the Platform section into the Apple self-test sequence.
examples/​gallery/​apple-shared/​src/​main/​scala/​example/​gallery/​GalleryPlatformChecks.scala Adds Apple-specific Platform embedding checks using weak refs/inspection to validate lifecycle behavior.
examples/​gallery/​apple-shared/​src/​main/​resources/​scala-native/​thicket_gallery_native_label.c Provides the externally-created NSTextField/UILabel and weak-ref helpers used by the self-test.
docs/​decisions.md Records the Apple embedding point decision and related measured/falsified results.
docs/​17-camera-preview.md Updates the camera preview step doc/status to reflect completion of the Apple embedding point.
docs/​12-component-status.md Updates component/prop status to reflect Platform support on Apple.
build.sbt Adds the Apple gallery shared resources directory so the native C source under resources/scala-native is included.

Review effort: Balanced

Added cleanup logic to free weak slots after observing NativeLabels.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@rleibman
rleibman merged commit c64fe58 into main Nov 4, 2026
2 checks passed
rleibman added a commit that referenced this pull request Nov 9, 2026
* Regenerate thicket_apple.h: a merge left it broken, and main's CI red

Merging #152 and #151 left the kind-table comment in the generated Apple header
closed early (`*/` after the Stepper row) with a duplicate `25 Tab` row outside
it, as bare text before `sui_create`. That is not C, and shimGen's consistency
suite has failed on main since 9f2a3bd: 45 passed, 6 failed (Linux and Apple CI
both red on the #151 push). `shimGen/run` regenerates it; this is its output,
and the only file it changes.

Also the two-line comment in AppleRenderer that the same merge left mis-indented,
which `scalafmtAll` reflows.

Measured: `sbt --error checkLinux` exit 0, 356 tests passed, 0 failed (on main,
shimGen alone: 6 failed). Not verified here: the Swift build of the shim, which
needs the Mac.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* POM descriptions for every published module; the release rehearsal in docs/14 (#157)

* POM descriptions for every published module; the release rehearsal in docs/14

Each of the 50 published modules now sets `description` next to its `name`, so
the POM's <description> (what Central's and Maven's search pages show) says what
the module is instead of repeating its name.

docs/14 §14.5 records what the practice release run on 2026-11-09 settled: a clean
clone tagged v0.2.0 locally staged 62 modules (jar, sources, javadoc, POM, all
signed, 56 MB), all 33 external dependencies resolve on Central, and sonaUpload
reached VALIDATED (deployment 86b6f4e7, then dropped; nothing published). Also:
which account owns net.leibman, where sbt 2 reads credentials, why the release
must come from one machine (the Mac), and rehearse-then-Drop.

Measured: `show coreJVM/description` etc. print the new descriptions;
`sbt --error checkLinux` on this branch (stacked on the thicket_apple.h fix):
exit 0, 356 tests passed, 0 failed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Update release instructions for Mac and Linux

Clarified release process for Mac and non-Mac hosts, detailing module staging and potential upload collisions.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Change Platform shim kind to match WidgetKind

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
rleibman added a commit that referenced this pull request Nov 13, 2026
… step (#159)

Two faults left on main after #151's merge and #156.

Prop.View: #151's merge brought back the placeholder that set "<description>:
not available" text, so a Platform host never got its view. Control on the
macOS gallery without this line: 4 Platform checks fail (the NSTextField is
not in the window, the "not available" label, the replacement, the release).

Stepper: StepperView.show() printed NSDecimalNumber's stringValue, which drops
trailing zeros, so a step of 0.25 showed "1.5", not "1.50", on AppKit and
UIKit. It now rounds with NSDecimalNumber and prints exactly `digits`
decimals, as Steps.format does. Before: both galleries failed "showing its
value to its step's precision" and "- moves it back a step" (main's own CI on
9f2a3bd, and #158's run on its merge with main).

Measured: bin/verify-apple.sh "iPhone 17e" on this tree, 20/20 steps, at a
load average of 80-180 (Apple CI ran beside it). macOS gallery 237 ok per
run, iOS 252 ok.

Co-authored-by: Roberto Leibman <rleibman@magrathea.local>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

Apple embedding point: host a platform view (WidgetKind.Platform / Prop.View) in the AppKit and UIKit shims

2 participants

0