waterui 0.3.0

A modern UI framework for Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
// WuiNavigationStack.swift
// Navigation stack container component with full push/pop support
//
// # Layout Behavior
// NavigationStack stretches to fill available space (greedy).
// Manages a stack of navigation views with native platform navigation.
//
// # Architecture
// Creates a NavigationController (via FFI) that receives push/pop calls from Rust.
// On iOS, uses UINavigationController for native gestures (swipe-back).
// On macOS, uses a custom view stack with titlebar accessories.

import CWaterUI
import OSLog

#if canImport(UIKit)
  import UIKit
#elseif canImport(AppKit)
  import AppKit
#endif

@MainActor
final class WuiNavigationDestinationState {
  private let popEnabled: WuiComputed<Bool>?
  private let popAttempted: Action?
  private let appear: Action?
  private let disappear: Action?
  private let pop: Action?

  init(_ state: CWaterUI.WuiNavigationDestinationState, env: WuiEnvironment) {
    guard let popEnabled = state.pop_enabled else {
      fatalError("Navigation destination pop-enabled signal is null")
    }
    self.popEnabled = WuiComputed<Bool>(popEnabled)
    self.popAttempted = state.pop_attempted.map { Action(inner: $0, env: env) }
    self.appear = state.appear.map { Action(inner: $0, env: env) }
    self.disappear = state.disappear.map { Action(inner: $0, env: env) }
    self.pop = state.pop.map { Action(inner: $0, env: env) }
  }

  init() {
    self.popEnabled = nil
    self.popAttempted = nil
    self.appear = nil
    self.disappear = nil
    self.pop = nil
  }

  func attemptPop() -> Bool {
    popAttempted?.call()
    return popEnabled?.value ?? true
  }

  func appeared() {
    appear?.call()
  }

  func disappeared() {
    disappear?.call()
  }

  func popped() {
    pop?.call()
  }
}

#if canImport(UIKit)
  @MainActor
  final class WuiContentViewController: UIViewController {
    private let contentView: UIView
    private let barState: WuiNavigationBarState?
    private let env: WuiEnvironment
    /// Whether this destination is the stack's root. UIKit shows a back button
    /// for the top item whenever its `backAction` is set — that is how the
    /// browser-style back affordance works — so the root, which has nothing to
    /// pop to, must not install one.
    private let isRoot: Bool
    let destinationState: WuiNavigationDestinationState
    /// The transition this destination arrives and leaves by, already resolved
    /// against the stack's default. UIKit reads `preferredTransition` off the
    /// pushed controller for both directions, so the pop needs it too.
    let transitionKind: WuiNavigationTransitionKind
    private var colorWatcher: WatcherGuard?
    private var hiddenWatcher: WatcherGuard?
    private var titleWatcher: WatcherGuard?
    private var subtitleWatcher: WatcherGuard?
    private var backgroundObservation: WuiComputedObservation<WuiResolvedColor>?
    private var foregroundObservation: WuiComputedObservation<WuiResolvedColor>?
    private var accentObservation: WuiComputedObservation<WuiResolvedColor>?
    private var borderObservation: WuiComputedObservation<WuiResolvedColor>?
    private var searchCoordinator: WuiNavigationSearchCoordinator?
    private let usesThemeBarColor: Bool

    init(
      contentView: UIView,
      barState: WuiNavigationBarState?,
      destinationState: WuiNavigationDestinationState,
      isRoot: Bool,
      transitionKind: WuiNavigationTransitionKind,
      env: WuiEnvironment
    ) {
      self.contentView = contentView
      self.barState = barState
      self.env = env
      self.destinationState = destinationState
      self.isRoot = isRoot
      self.transitionKind = transitionKind
      self.usesThemeBarColor = barState?.color == nil
      super.init(nibName: nil, bundle: nil)

      edgesForExtendedLayout = .all
      extendedLayoutIncludesOpaqueBars = true
    }

    @available(*, unavailable)
    required init?(coder: NSCoder) {
      fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
      super.viewDidLoad()
      // Theme-driven page and navigation backgrounds keep native chrome in
      // sync with WaterUI style overrides and platform color-scheme changes.
      applyThemedBackground()
      contentView.translatesAutoresizingMaskIntoConstraints = true
      view.addSubview(contentView)
      // The bar's scroll-coupled behavior — large-title expansion, scroll-edge
      // appearance, bottom-bar effects — is driven by the content scroll view,
      // which UIKit cannot find on its own behind the WaterUI wrapper views.
      if let scrollView = wuiResolvedPrimaryContent(of: contentView) as? UIScrollView {
        setContentScrollView(scrollView, for: [.top, .bottom])
      }
      applyNavigationChrome()
      startWatching()
    }

    private func applyThemedBackground() {
      backgroundObservation = WuiComputedObservation(
        themeColor: WuiColorSlot_Background,
        env: env
      ) { [weak self] color, _ in
        guard let self else { return }
        self.view.backgroundColor = color.toUIColor()
      }
      if let color = backgroundObservation?.value {
        view.backgroundColor = color.toUIColor()
      }
      foregroundObservation = WuiComputedObservation(
        themeColor: WuiColorSlot_Foreground,
        env: env
      ) { [weak self] _, _ in
        self?.refreshNavigationAppearance()
      }
      accentObservation = WuiComputedObservation(
        themeColor: WuiColorSlot_Accent,
        env: env
      ) { [weak self] color, _ in
        self?.applyNavigationAccent(color)
      }
      borderObservation = WuiComputedObservation(
        themeColor: WuiColorSlot_Border,
        env: env
      ) { [weak self] _, _ in
        self?.refreshNavigationAppearance()
      }
      if let accent = accentObservation?.value {
        applyNavigationAccent(accent)
      }
    }

    override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
      applyBarState(animated: animated)
    }

    override func viewDidAppear(_ animated: Bool) {
      super.viewDidAppear(animated)
      destinationState.appeared()
    }

    override func viewDidDisappear(_ animated: Bool) {
      super.viewDidDisappear(animated)
      destinationState.disappeared()
    }

    override func viewDidLayoutSubviews() {
      super.viewDidLayoutSubviews()
      // A scroll surface owns its insets and gets the full page, so scrolled
      // content passes under the bars. Plain content is inset to the safe
      // area — which includes the bars once this controller is contained —
      // the way a pushed SwiftUI page's content starts below its bar.
      if wuiResolvedPrimaryContent(of: contentView) is WuiSafeAreaManaging {
        contentView.frame = view.bounds
      } else {
        contentView.frame = view.safeAreaLayoutGuide.layoutFrame
      }
    }

    private func applyNavigationChrome() {
      if !isRoot {
        navigationItem.backAction = UIAction { [weak self] _ in
          guard let self, self.destinationState.attemptPop() else { return }
          waterui_navigation_pop(self.env.inner)
        }
      }

      if let title = barState?.title {
        if title.isPlainText {
          navigationItem.title = title.text ?? ""
          navigationItem.titleView = nil
        } else {
          navigationItem.title = title.text
          navigationItem.titleView = title.view
        }
        // A semantic title is a signal; the platform bar shows a string, so
        // the string follows the signal rather than freezing at first render.
        titleWatcher = title.textSignal?.watch { [weak self] value, _ in
          self?.navigationItem.title = value.toString()
        }
      } else {
        navigationItem.title = nil
        navigationItem.titleView = nil
      }

      navigationItem.subtitle = barState?.subtitle.text
      subtitleWatcher = barState?.subtitle.textSignal?.watch { [weak self] value, _ in
        self?.navigationItem.subtitle = value.toString()
      }

      let semanticItems = barState?.toolbar ?? []
      let leadingViews = semanticItems.filter {
        $0.placement == WuiNavigationToolbarPlacement_Cancellation
          || $0.placement == WuiNavigationToolbarPlacement_TopBarLeading
      }.map(\.view)
      let trailingViews = semanticItems.filter {
        $0.placement == WuiNavigationToolbarPlacement_PrimaryAction
          || $0.placement == WuiNavigationToolbarPlacement_SecondaryAction
          || $0.placement == WuiNavigationToolbarPlacement_Confirmation
          || $0.placement == WuiNavigationToolbarPlacement_TopBarTrailing
      }.map(\.view)
      let principal = semanticItems.first {
        $0.placement == WuiNavigationToolbarPlacement_Principal
      }?.view
      let bottomViews = semanticItems.filter {
        $0.placement == WuiNavigationToolbarPlacement_BottomBar
          || $0.placement == WuiNavigationToolbarPlacement_Status
      }.map(\.view)

      if let principal {
        principal.removeFromSuperview()
        principal.frame = CGRect(origin: .zero, size: principal.sizeThatFits(WuiProposalSize()))
        navigationItem.titleView = principal
      }

      if !leadingViews.isEmpty {
        for view in leadingViews {
          view.removeFromSuperview()
          view.frame = CGRect(origin: .zero, size: view.sizeThatFits(WuiProposalSize()))
        }
        navigationItem.leftItemsSupplementBackButton = true
        navigationItem.leftBarButtonItems = leadingViews.map(UIBarButtonItem.init(customView:))
      } else {
        navigationItem.leftBarButtonItems = nil
      }

      if !trailingViews.isEmpty {
        for view in trailingViews {
          view.removeFromSuperview()
          view.frame = CGRect(origin: .zero, size: view.sizeThatFits(WuiProposalSize()))
        }
        navigationItem.rightBarButtonItems = trailingViews.map(UIBarButtonItem.init(customView:))
      } else {
        navigationItem.rightBarButtonItems = nil
      }

      if !bottomViews.isEmpty {
        for view in bottomViews {
          view.removeFromSuperview()
          view.frame = CGRect(origin: .zero, size: view.sizeThatFits(WuiProposalSize()))
        }
        toolbarItems = bottomViews.map(UIBarButtonItem.init(customView:))
        navigationController?.setToolbarHidden(false, animated: false)
      } else {
        toolbarItems = nil
        navigationController?.setToolbarHidden(true, animated: false)
      }

      if let search = barState?.search {
        let (controller, coordinator) = makeNavigationSearchController(search)
        navigationItem.searchController = controller
        navigationItem.hidesSearchBarWhenScrolling = false
        searchCoordinator = coordinator
        definesPresentationContext = true
      } else {
        navigationItem.searchController = nil
        searchCoordinator = nil
      }
    }

    private func startWatching() {
      if let barColor = barState?.color {
        colorWatcher = barColor.watch { [weak self] value, metadata in
          guard let self else { return }
          withPlatformAnimation(metadata) {
            self.applyBarColor(value)
          }
        }
        applyBarColor(barColor.value)
      } else {
        applyDefaultBarAppearance()
      }

      if let barHidden = barState?.hidden {
        hiddenWatcher = barHidden.watch { [weak self] value, metadata in
          guard let self else { return }
          self.applyBarHidden(value, animated: shouldAnimate(metadata.animation ?? .none))
        }
        applyBarHidden(barHidden.value, animated: false)
      }
    }

    private func applyBarState(animated: Bool) {
      if let barColor = barState?.color {
        applyBarColor(barColor.value)
      }
      if let barHidden = barState?.hidden {
        applyBarHidden(barHidden.value, animated: animated)
      }
      if let accent = accentObservation?.value {
        applyNavigationAccent(accent)
      }
      // Re-asserted on every appearance: the toolbar's visibility lives on the
      // navigation controller, not this item, and switching tabs takes the
      // controller's view out of the window and drops it. The platform keeps
      // bottom-bar items through a tab switch (measured against the SwiftUI
      // iOS 26 baseline), so returning to this tab shows them again.
      if toolbarItems?.isEmpty == false {
        navigationController?.setToolbarHidden(false, animated: false)
      }
    }

    private func applyBarColor(_ color: WuiResolvedColor) {
      applyResolvedBarColor(color)
    }

    private func refreshNavigationAppearance() {
      if usesThemeBarColor {
        applyDefaultBarAppearance()
      } else if let color = barState?.color?.value {
        applyResolvedBarColor(color)
      }
    }

    /// SwiftUI's default navigation bar is the system one: a translucent blur
    /// while scrolled under, fully transparent at the scroll edge. Clearing
    /// the item-level appearances restores exactly that; painting the theme
    /// Surface here made every bar an always-opaque gray band.
    private func applyDefaultBarAppearance() {
      navigationItem.standardAppearance = nil
      navigationItem.scrollEdgeAppearance = nil
      navigationItem.compactAppearance = nil
    }

    private func applyResolvedBarColor(_ color: WuiResolvedColor) {
      let appearance = UINavigationBarAppearance()
      appearance.configureWithOpaqueBackground()
      appearance.backgroundColor = color.toUIColor()
      if let foreground = foregroundObservation?.value.toUIColor() {
        appearance.titleTextAttributes = [.foregroundColor: foreground]
        appearance.largeTitleTextAttributes = [.foregroundColor: foreground]
      }
      appearance.shadowColor = borderObservation?.value.toUIColor()
      navigationItem.standardAppearance = appearance
      navigationItem.scrollEdgeAppearance = appearance
      navigationItem.compactAppearance = appearance
    }

    private func applyNavigationAccent(_ color: WuiResolvedColor) {
      let accent = color.toUIColor()
      view.tintColor = accent
      navigationController?.navigationBar.tintColor = accent
    }

    private func applyBarHidden(_ hidden: Bool, animated: Bool) {
      navigationController?.setNavigationBarHidden(hidden, animated: animated)
    }
  }
#endif

@MainActor
final class NavigationControllerWrapper {
  private weak var delegate: WuiNavigationStack?
  private var pendingTransactions: [CWaterUI.WuiNavigationTransaction] = []

  var hasPendingTransactions: Bool {
    !pendingTransactions.isEmpty
  }

  func install(delegate: WuiNavigationStack) {
    precondition(self.delegate == nil, "Navigation controller delegate was installed twice")
    self.delegate = delegate
    let pending = pendingTransactions
    pendingTransactions.removeAll()
    pending.forEach(delegate.handleApply)
  }

  func apply(_ transaction: CWaterUI.WuiNavigationTransaction) {
    if let delegate {
      delegate.handleApply(transaction)
    } else {
      pendingTransactions.append(transaction)
    }
  }
}

@MainActor
final class WuiNavigationStack: PlatformView, WuiComponent {
  static var rawId: CWaterUI.WuiTypeId { waterui_navigation_stack_id() }

  private(set) var stretchAxis: WuiStretchAxis = .both

  private let childEnv: WuiEnvironment
  private let transition: WuiNavigationTransition
  private var wrapper: NavigationControllerWrapper?
  private var pendingTransactionId: UInt64?

  #if canImport(UIKit)
    private var navController: UINavigationController!
    private var viewStack: [UIViewController] = []
    private var expectedNativeStack: [UIViewController]?
    private var pendingRemovedStates: [WuiNavigationDestinationState] = []
  #elseif canImport(AppKit)
    private struct NavigationEntry {
      let view: NSView
      let barState: WuiNavigationBarState?
      let destinationState: WuiNavigationDestinationState
      /// This destination's transition, already resolved against the stack's.
      let transitionKind: WuiNavigationTransitionKind
      var isActive: Bool
    }

    private var viewStack: [NavigationEntry] = []
    private var currentIndex = 0
    private weak var windowToolbar: WuiWindowToolbar?
    /// Starts true so a stack with no tab container above it — the common case —
    /// publishes its chrome without being told to.
    private var chromeIsActive = true
    private var hiddenWatcher: WatcherGuard?
    private var pendingRemovedEntries: [NavigationEntry] = []
    private var pendingAppearanceIndex: Int?
  #endif

  convenience init(anyview: OpaquePointer, env: WuiEnvironment) {
    let ffiStack: CWaterUI.WuiNavigationStack = waterui_force_as_navigation_stack(anyview)

    guard let childEnvPtr = waterui_clone_env(env.inner) else {
      fatalError("Failed to clone environment")
    }
    let childEnv = WuiEnvironment(childEnvPtr)

    let wrapper = NavigationControllerWrapper()
    let wrapperPtr = Unmanaged.passRetained(wrapper).toOpaque()

    waterui_env_install_navigation_controller(
      childEnv.inner,
      wrapperPtr,
      { data, transaction in
        let wrapper = Unmanaged<NavigationControllerWrapper>.fromOpaque(data!).takeUnretainedValue()
        wrapper.apply(transaction)
      },
      { data in
        _ = Unmanaged<NavigationControllerWrapper>.fromOpaque(data!).takeRetainedValue()
      }
    )

    guard let unresolvedRoot = ffiStack.root else {
      fatalError("Navigation stack root is null")
    }
    guard let resolvedRoot = waterui_navigation_stack_root(unresolvedRoot, childEnv.inner) else {
      fatalError("Failed to resolve navigation stack root")
    }
    let rootViewId = WuiViewId(waterui_view_id(resolvedRoot))
    let navigationViewId = WuiViewId(waterui_navigation_view_id())

    let rootView: WuiAnyView
    let rootBarState: WuiNavigationBarState?
    let rootDestinationState: WuiNavigationDestinationState
    let rootDisplayMode: WuiNavigationTitleDisplayMode

    if rootViewId == navigationViewId {
      let rootNav = waterui_force_as_navigation_view(resolvedRoot)
      rootView = WuiAnyView(anyview: rootNav.content, env: childEnv)
      rootBarState = makeNavigationBarState(from: rootNav.bar, env: childEnv)
      rootDestinationState = WuiNavigationDestinationState(rootNav.state, env: childEnv)
      rootDisplayMode = rootNav.bar.display_mode
    } else {
      fatalError("Resolved navigation stack root is not a NavigationView")
    }

    self.init(
      rootView: rootView,
      rootBarState: rootBarState,
      rootDestinationState: rootDestinationState,
      rootDisplayMode: rootDisplayMode,
      transition: ffiStack.transition,
      childEnv: childEnv,
      wrapper: wrapper
    )
  }

  init(
    rootView: WuiAnyView,
    rootBarState: WuiNavigationBarState?,
    rootDestinationState: WuiNavigationDestinationState,
    rootDisplayMode: WuiNavigationTitleDisplayMode,
    transition: WuiNavigationTransition,
    childEnv: WuiEnvironment,
    wrapper: NavigationControllerWrapper
  ) {
    self.transition = transition
    self.childEnv = childEnv
    self.wrapper = wrapper
    super.init(frame: .zero)

    configureNavigation(
      with: rootView,
      barState: rootBarState,
      destinationState: rootDestinationState,
      displayMode: rootDisplayMode,
      activateAppKitRoot: !wrapper.hasPendingTransactions
    )
    wrapper.install(delegate: self)
  }

  @available(*, unavailable)
  required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
  }

  private func configureNavigation(
    with rootView: WuiAnyView,
    barState: WuiNavigationBarState?,
    destinationState: WuiNavigationDestinationState,
    displayMode: WuiNavigationTitleDisplayMode,
    activateAppKitRoot: Bool
  ) {
    #if canImport(UIKit)
      let rootVC = makeViewController(
        for: rootView,
        barState: barState,
        destinationState: destinationState,
        displayMode: wuiLargeTitleDisplayMode(displayMode),
        restorationDepth: 0,
        declared: transition
      )
      navController = UINavigationController(rootViewController: rootVC)
      navController.navigationBar.prefersLargeTitles = true
      navController.delegate = self
      navController.interactivePopGestureRecognizer?.delegate = self
      // The view is attached by `wuiSyncControllerHierarchy` at window time,
      // after the controller has a parent — see that helper for why the order
      // matters.
      viewStack.append(rootVC)
    #elseif canImport(AppKit)
      rootView.identifier = NSUserInterfaceItemIdentifier(
        navigationRestorationIdentifier(depth: 0))
      rootView.translatesAutoresizingMaskIntoConstraints = true
      addSubview(rootView)
      viewStack.append(
        NavigationEntry(
          view: rootView,
          barState: barState,
          destinationState: destinationState,
          transitionKind: transition.kind,
          isActive: activateAppKitRoot
        ))
      if activateAppKitRoot {
        destinationState.appeared()
      }
      currentIndex = 0
    #endif
  }

  func handleApply(_ transaction: CWaterUI.WuiNavigationTransaction) {
    let inserted = WuiArray<CWaterUI.WuiNavigationView>(transaction.inserted).toArray()
    if let pendingTransactionId {
      #if canImport(UIKit)
        settleSupersededUIKitTransaction()
      #elseif canImport(AppKit)
        settleSupersededAppKitTransaction()
      #endif
      _ = waterui_navigation_transition_cancelled(childEnv.inner, pendingTransactionId)
    }
    pendingTransactionId = transaction.id
    #if canImport(UIKit)
      precondition(
        Int(transaction.retained_prefix + transaction.removed) + 1 == viewStack.count,
        "UIKit navigation transaction must replace the current suffix"
      )
      let retained = Array(viewStack.prefix(Int(transaction.retained_prefix) + 1))
      pendingRemovedStates =
        viewStack
        .dropFirst(Int(transaction.retained_prefix) + 1)
        .map { controller in
          guard let controller = controller as? WuiContentViewController else {
            fatalError("UIKit navigation stack contains a non-WaterUI destination")
          }
          return controller.destinationState
        }
      let insertedControllers = inserted.enumerated().map { offset, navView in
        let barState = makeNavigationBarState(from: navView.bar, env: childEnv)
        let destinationState = WuiNavigationDestinationState(navView.state, env: childEnv)
        let contentView = WuiAnyView(anyview: navView.content, env: childEnv)
        return makeViewController(
          for: contentView,
          barState: barState,
          destinationState: destinationState,
          displayMode: wuiLargeTitleDisplayMode(navView.bar.display_mode),
          restorationDepth: Int(transaction.retained_prefix) + offset + 1,
          declared: navView.transition
        )
      }
      let next = retained + insertedControllers
      expectedNativeStack = next
      let movingKind = movingTransitionKind(inserted: insertedControllers)
      if transaction.removed == 0 && insertedControllers.count == 1 {
        pushViewController(insertedControllers[0], kind: movingKind)
      } else if transaction.removed == 1 && insertedControllers.isEmpty {
        popViewController(kind: movingKind)
      } else {
        setViewControllers(next, kind: movingKind)
      }
      viewStack = next
    #elseif canImport(AppKit)
      applyAppKitTransaction(transaction, inserted: inserted)
    #endif
  }

  /// A destination that declared nothing inherits the stack's transition. The
  /// two differ only when a destination names a matched pair — a zoom source
  /// belongs to one push, so the stack cannot name it for every destination.
  private func resolvedTransition(
    _ declared: CWaterUI.WuiNavigationTransition
  ) -> CWaterUI.WuiNavigationTransition {
    declared.kind == WuiNavigationTransitionKind_Inherit ? transition : declared
  }

  private func completeTransaction(_ id: UInt64) {
    guard pendingTransactionId == id else { return }
    _ = waterui_navigation_transition_completed(childEnv.inner, id)
    pendingTransactionId = nil
  }

  #if canImport(UIKit)
    private func pushViewController(_ vc: UIViewController, kind: WuiNavigationTransitionKind) {
      navController.pushViewController(vc, animated: nativeTransitionIsAnimated(kind))
    }

    private func popViewController(kind: WuiNavigationTransitionKind) {
      navController.popViewController(animated: nativeTransitionIsAnimated(kind))
    }

    private func setViewControllers(
      _ viewControllers: [UIViewController],
      kind: WuiNavigationTransitionKind
    ) {
      navController.setViewControllers(viewControllers, animated: nativeTransitionIsAnimated(kind))
    }

    /// A push is animated by the destination arriving, a pop by the one
    /// leaving; `viewStack` still holds the old top when a transaction is
    /// applied, so the leaving destination is its last element.
    private func movingTransitionKind(
      inserted: [UIViewController]
    ) -> WuiNavigationTransitionKind {
      let moving = inserted.last ?? viewStack.last
      return (moving as? WuiContentViewController)?.transitionKind ?? transition.kind
    }

    private func nativeTransitionIsAnimated(_ kind: WuiNavigationTransitionKind) -> Bool {
      switch kind {
      case WuiNavigationTransitionKind_Automatic,
        WuiNavigationTransitionKind_Fade,
        WuiNavigationTransitionKind_Zoom:
        return true
      case WuiNavigationTransitionKind_None:
        return false
      case WuiNavigationTransitionKind_Custom:
        Logger.waterui.warning(
          "Custom navigation transition is unavailable on UIKit; applying without animation")
        return false
      default:
        fatalError("Unsupported WaterUI navigation transition: \(kind.rawValue)")
      }
    }

    private func makeViewController(
      for view: UIView,
      barState: WuiNavigationBarState?,
      destinationState: WuiNavigationDestinationState,
      displayMode: UINavigationItem.LargeTitleDisplayMode = .automatic,
      restorationDepth: Int,
      declared: CWaterUI.WuiNavigationTransition
    ) -> UIViewController {
      let transition = resolvedTransition(declared)
      let vc = WuiContentViewController(
        contentView: view,
        barState: barState,
        destinationState: destinationState,
        isRoot: restorationDepth == 0,
        transitionKind: transition.kind,
        env: childEnv
      )
      vc.restorationIdentifier = navigationRestorationIdentifier(depth: restorationDepth)
      switch transition.kind {
      case WuiNavigationTransitionKind_Automatic,
        WuiNavigationTransitionKind_None,
        WuiNavigationTransitionKind_Custom:
        break
      case WuiNavigationTransitionKind_Fade:
        vc.preferredTransition = .crossDissolve
      case WuiNavigationTransitionKind_Zoom:
        let sourceTag = Int(transition.source_id)
        vc.preferredTransition = .zoom { context in
          guard let source = context.sourceViewController.view.viewWithTag(sourceTag) else {
            fatalError("Navigation zoom source \(sourceTag) is not present in the source page")
          }
          return source
        }
      default:
        fatalError("Unsupported WaterUI navigation transition: \(transition.kind.rawValue)")
      }
      vc.navigationItem.largeTitleDisplayMode = displayMode
      return vc
    }

    func navigationController(
      _ navigationController: UINavigationController,
      didShow viewController: UIViewController,
      animated: Bool
    ) {
      let next = navigationController.viewControllers
      if let expectedNativeStack {
        guard stacksMatch(next, expectedNativeStack) else { return }
        self.expectedNativeStack = nil
        for state in pendingRemovedStates {
          state.popped()
        }
        pendingRemovedStates.removeAll()
        if let pendingTransactionId {
          completeTransaction(pendingTransactionId)
        }
        viewStack = next
        return
      }
      if next.count < viewStack.count {
        precondition(
          stacksMatch(Array(viewStack.prefix(next.count)), next),
          "UIKit native navigation pop must remove a suffix"
        )
        for controller in viewStack.dropFirst(next.count) {
          guard let controller = controller as? WuiContentViewController else {
            fatalError("UIKit navigation stack contains a non-WaterUI destination")
          }
          controller.destinationState.popped()
        }
        waterui_navigation_complete_native_pop(
          childEnv.inner,
          UInt(viewStack.count - next.count)
        )
      }
      viewStack = next
    }

    private func settleSupersededUIKitTransaction() {
      for state in pendingRemovedStates {
        state.popped()
      }
      pendingRemovedStates.removeAll()
      expectedNativeStack = nil
    }

    private func stacksMatch(_ left: [UIViewController], _ right: [UIViewController]) -> Bool {
      left.count == right.count
        && zip(left, right).allSatisfy { pair in
          pair.0 === pair.1
        }
    }
  #elseif canImport(AppKit)
    private func setupTitlebar() {
      guard let window, window.hasTitlebar else { return }
      windowToolbar = WuiWindowToolbar.attached(to: window)
      updateTitlebarState()
    }

    /// Whether this stack is the one the user is looking at.
    ///
    /// A tab container keeps every tab's content in the window and hides all but
    /// one, so being in a window is not the same as being on screen. Only the
    /// stack that is actually visible may publish chrome; see
    /// `NSView.setNavigationChromeActive(_:)`.
    func setChromeActive(_ active: Bool) {
      guard chromeIsActive != active else { return }
      chromeIsActive = active
      if active {
        updateTitlebarState()
      } else {
        windowToolbar?.clearContent(owner: self)
      }
    }

    /// Publishes this stack's chrome to the window toolbar.
    ///
    /// The items go through `NSToolbarItem` rather than titlebar accessory
    /// views, which is what gives them the system's own appearance — the glass
    /// capsule around a toolbar button, the search field's presentation, the
    /// spacing between items. A window has one toolbar, so the stack claims it
    /// while it is on screen and gives it back when it leaves; see
    /// `WuiWindowToolbar`.
    private func updateTitlebarState() {
      guard let windowToolbar, chromeIsActive else { return }

      hiddenWatcher = nil
      let topBarState = viewStack.last?.barState
      if let hidden = topBarState?.hidden {
        hiddenWatcher = hidden.watch { [weak self] _, _ in
          self?.updateTitlebarState()
        }
      }

      guard topBarState?.hidden?.value != true else {
        windowToolbar.clearContent(owner: self)
        return
      }

      var content = WuiWindowToolbar.Content()
      content.showsBack = currentIndex > 0
      content.onBack = { [weak self] in self?.backButtonTapped() }
      if let title = topBarState?.title {
        if title.isPlainText {
          content.title = title.text ?? ""
        } else {
          content.titleView = title.view
        }
      }
      content.leading = topBarState?.leadingItem
      content.trailing = topBarState?.trailingItem
      content.search = topBarState?.search
      windowToolbar.setContent(content, owner: self)
    }

    private func backButtonTapped() {
      guard viewStack.last?.destinationState.attemptPop() != false else { return }
      waterui_navigation_pop(childEnv.inner)
    }

    override func viewDidMoveToWindow() {
      super.viewDidMoveToWindow()
      if window == nil {
        // Several stacks exist at once behind a tab container; one that has been
        // switched away from must not leave its buttons in the toolbar.
        windowToolbar?.clearContent(owner: self)
        windowToolbar = nil
        return
      }
      setupTitlebar()
    }

    private func applyAppKitTransaction(
      _ transaction: CWaterUI.WuiNavigationTransaction,
      inserted: [CWaterUI.WuiNavigationView]
    ) {
      precondition(
        Int(transaction.retained_prefix + transaction.removed) + 1 == viewStack.count,
        "AppKit navigation transaction must replace the current suffix"
      )
      guard let oldTop = viewStack.last else {
        fatalError("AppKit navigation stack is missing its root destination")
      }

      let oldTopIndex = viewStack.count - 1
      if viewStack[oldTopIndex].isActive {
        viewStack[oldTopIndex].destinationState.disappeared()
        viewStack[oldTopIndex].isActive = false
      }

      let retainedCount = Int(transaction.retained_prefix) + 1
      let retained = Array(viewStack.prefix(retainedCount))
      let removed = Array(viewStack.dropFirst(retainedCount))
      let insertedEntries = inserted.enumerated().map { offset, navView in
        let view = WuiAnyView(anyview: navView.content, env: childEnv)
        view.translatesAutoresizingMaskIntoConstraints = true
        view.identifier = NSUserInterfaceItemIdentifier(
          navigationRestorationIdentifier(depth: retainedCount + offset))
        view.frame = bounds
        view.isHidden = true
        view.alphaValue = 1
        addSubview(view)
        return NavigationEntry(
          view: view,
          barState: makeNavigationBarState(from: navView.bar, env: childEnv),
          destinationState: WuiNavigationDestinationState(navView.state, env: childEnv),
          transitionKind: resolvedTransition(navView.transition).kind,
          isActive: false
        )
      }
      let next = retained + insertedEntries
      guard let newTop = next.last else {
        fatalError("AppKit navigation transaction removed the root destination")
      }

      viewStack = next
      currentIndex = viewStack.count - 1
      pendingRemovedEntries = removed
      pendingAppearanceIndex = currentIndex
      updateTitlebarState()

      newTop.view.isHidden = false
      // A push is animated by the destination arriving, a pop by the one
      // leaving.
      let moving = insertedEntries.last ?? removed.last
      let fades = appKitTransitionUsesFade(moving?.transitionKind ?? transition.kind)
      if fades {
        let transactionId = transaction.id
        oldTop.view.isHidden = false
        oldTop.view.alphaValue = 1
        newTop.view.alphaValue = 0
        NSAnimationContext.runAnimationGroup { _ in
          oldTop.view.animator().alphaValue = 0
          newTop.view.animator().alphaValue = 1
        } completionHandler: { [weak self] in
          Task { @MainActor in
            self?.finishAppKitTransaction(transactionId)
          }
        }
      } else {
        finishAppKitTransaction(transaction.id)
      }
    }

    private func finishAppKitTransaction(_ id: UInt64) {
      guard pendingTransactionId == id else { return }
      for entry in pendingRemovedEntries {
        entry.view.removeFromSuperview()
        entry.destinationState.popped()
      }
      pendingRemovedEntries.removeAll()
      markAppKitDestinationAppeared(at: pendingAppearanceIndex)
      pendingAppearanceIndex = nil
      normalizeAppKitStackVisibility()
      completeTransaction(id)
    }

    private func settleSupersededAppKitTransaction() {
      for entry in pendingRemovedEntries {
        entry.view.removeFromSuperview()
        entry.destinationState.popped()
      }
      pendingRemovedEntries.removeAll()
      markAppKitDestinationAppeared(at: pendingAppearanceIndex)
      pendingAppearanceIndex = nil
      normalizeAppKitStackVisibility()
    }

    private func markAppKitDestinationAppeared(at index: Int?) {
      guard let index else { return }
      precondition(
        viewStack.indices.contains(index), "AppKit navigation appearance index is invalid")
      guard !viewStack[index].isActive else { return }
      viewStack[index].destinationState.appeared()
      viewStack[index].isActive = true
    }

    private func normalizeAppKitStackVisibility() {
      for (index, entry) in viewStack.enumerated() {
        entry.view.alphaValue = 1
        entry.view.isHidden = index != viewStack.count - 1
      }
    }

    private func appKitTransitionUsesFade(_ kind: WuiNavigationTransitionKind) -> Bool {
      switch kind {
      case WuiNavigationTransitionKind_Fade:
        return true
      case WuiNavigationTransitionKind_Automatic,
        WuiNavigationTransitionKind_None:
        return false
      case WuiNavigationTransitionKind_Zoom:
        Logger.waterui.warning(
          "Zoom navigation transition is unavailable on AppKit; applying without animation")
        return false
      case WuiNavigationTransitionKind_Custom:
        Logger.waterui.warning(
          "Custom navigation transition is unavailable on AppKit; applying without animation")
        return false
      default:
        fatalError("Unsupported WaterUI navigation transition: \(kind.rawValue)")
      }
    }

  #endif

  func sizeThatFits(_ proposal: WuiProposalSize) -> CGSize {
    let width = proposal.width.map(CGFloat.init) ?? 320
    let height = proposal.height.map(CGFloat.init) ?? 480
    return CGSize(width: width, height: height)
  }

  #if canImport(UIKit)
    override func didMoveToWindow() {
      super.didMoveToWindow()
      wuiSyncControllerHierarchy(of: navController)
    }

    override func layoutSubviews() {
      super.layoutSubviews()
      navController?.view.frame = bounds
    }
  #elseif canImport(AppKit)
    nonisolated override var isFlipped: Bool { true }

    override func layout() {
      super.layout()
      // The pages' chrome lives in the window toolbar, whose height reaches
      // this view as its top safe-area inset when the window supplies
      // full-size content; the pages themselves sit below it.
      let topInset = safeAreaInsets.top
      let pageFrame = CGRect(
        x: 0,
        y: topInset,
        width: bounds.width,
        height: bounds.height - topInset
      )
      for entry in viewStack {
        entry.view.frame = pageFrame
      }
    }
  #endif
}

private func navigationRestorationIdentifier(depth: Int) -> String {
  precondition(depth >= 0, "Navigation restoration depth must be non-negative")
  return "dev.waterui.navigation.destination.\(depth)"
}

#if canImport(UIKit)
  extension WuiNavigationStack: UINavigationControllerDelegate, UIGestureRecognizerDelegate {
    override func gestureRecognizerShouldBegin(_ gestureRecognizer: UIGestureRecognizer) -> Bool {
      guard gestureRecognizer === navController.interactivePopGestureRecognizer else {
        return true
      }
      guard viewStack.count > 1 else { return false }
      guard let destination = viewStack.last as? WuiContentViewController else {
        fatalError("UIKit navigation stack contains a non-WaterUI destination")
      }
      return destination.destinationState.attemptPop()
    }
  }
#endif

#if canImport(AppKit)
  extension NSWindow {
    /// Whether this window has a titlebar to hang accessories off.
    ///
    /// `addTitlebarAccessoryViewController` reaches for `titlebarViewController`,
    /// which raises `NSInternalInconsistencyException` on a window without
    /// `.titled` — the offscreen window preview renders into, among others.
    var hasTitlebar: Bool { styleMask.contains(.titled) }
  }
#endif

/// A navigation stack projects into the platform's own navigation container,
/// which owns its bars and content insets; the window hands it the full bounds.
extension WuiNavigationStack: WuiSafeAreaManaging {}