nitro-ui-macos 0.0.0 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (128) hide show
  1. package/CONTRIBUTING.md +49 -0
  2. package/LICENSE +4 -3
  3. package/PARITY.md +373 -0
  4. package/README.md +144 -5
  5. package/native/nitro-core/Package.swift +78 -0
  6. package/native/nitro-ui-core/Package.swift +112 -0
  7. package/native/nitro-ui-core/Sources/NitroUICoreMac/AnimationFrameDisplayLink.swift +127 -0
  8. package/native/nitro-ui-core/Sources/NitroUICoreMac/HybridAnimationFrameScheduler.swift +91 -0
  9. package/native/nitro-ui-core/Sources/NitroUICoreMac/HybridLinking.swift +18 -0
  10. package/native/nitro-ui-dev/Package.swift +102 -0
  11. package/native/nitro-ui-dev/Sources/NitroUIDevMac/DevBundleLoad.swift +14 -0
  12. package/native/nitro-ui-dev/Sources/NitroUIDevMac/DevBundleLoader.swift +85 -0
  13. package/native/nitro-ui-dev/Sources/NitroUIDevMac/DevLauncher.swift +192 -0
  14. package/native/nitro-ui-dev/Sources/NitroUIDevMac/DevMenu.swift +172 -0
  15. package/native/nitro-ui-dev/Sources/NitroUIDevMac/DevServerBrowser.swift +86 -0
  16. package/native/nitro-ui-dev/Sources/NitroUIDevMac/DevSession.swift +151 -0
  17. package/native/nitro-ui-dev/Sources/NitroUIDevMac/DevShortcuts.swift +85 -0
  18. package/native/nitro-ui-dev/Sources/NitroUIDevMac/DevToast.swift +85 -0
  19. package/native/nitro-ui-dev/Sources/NitroUIDevMac/DevTools.swift +141 -0
  20. package/native/nitro-ui-dev/Sources/NitroUIDevMac/ErrorModels.swift +97 -0
  21. package/native/nitro-ui-dev/Sources/NitroUIDevMac/ErrorOverlay.swift +698 -0
  22. package/native/nitro-ui-dev/Sources/NitroUIDevMac/HybridDevBundles.swift +64 -0
  23. package/native/nitro-ui-dev/Sources/NitroUIDevMac/HybridDevErrors.swift +56 -0
  24. package/native/nitro-ui-dev/Sources/NitroUIDevMac/HybridDevLoadingView.swift +27 -0
  25. package/native/nitro-ui-dev/Sources/NitroUIDevMac/HybridDevMenu.swift +40 -0
  26. package/native/nitro-ui-dev/Sources/NitroUIDevMac/HybridDevToast.swift +19 -0
  27. package/native/nitro-ui-dev/Sources/NitroUIDevMac/LauncherWindowController.swift +217 -0
  28. package/native/nitro-ui-dev/Sources/NitroUIDevMac/LoadingOverlay.swift +104 -0
  29. package/native/nitro-ui-dev/Sources/NitroUIDevMac/RegisterDevLoader.swift +22 -0
  30. package/native/nitro-ui-dev/Sources/NitroUIDevMac/SavedBundles.swift +70 -0
  31. package/native/nitro-ui-glass/Package.swift +103 -0
  32. package/native/nitro-ui-glass/macos/HybridLiquidGlass.swift +5 -0
  33. package/native/nitro-ui-glass/macos/HybridNativeLiquidGlassContainerView.swift +40 -0
  34. package/native/nitro-ui-glass/macos/HybridNativeLiquidGlassView.swift +80 -0
  35. package/native/nitro-ui-glass/macos/LiquidGlassEffectView.swift +181 -0
  36. package/native/nitro-ui-glass/macos/LiquidGlassSupport.swift +17 -0
  37. package/native/nitro-ui-navigation/Package.swift +105 -0
  38. package/native/nitro-ui-navigation/Sources/NitroUINavigationMac/HybridNavigationScreen.swift +199 -0
  39. package/native/nitro-ui-navigation/Sources/NitroUINavigationMac/HybridNavigationSharedElement.swift +17 -0
  40. package/native/nitro-ui-navigation/Sources/NitroUINavigationMac/HybridNavigationSplit.swift +889 -0
  41. package/native/nitro-ui-navigation/Sources/NitroUINavigationMac/HybridNavigationStack.swift +404 -0
  42. package/native/nitro-ui-navigation/Sources/NitroUINavigationMac/HybridNavigationTabs.swift +186 -0
  43. package/native/nitro-ui-navigation/Sources/NitroUINavigationMac/MacStackContainerController.swift +260 -0
  44. package/native/nitro-ui-navigation/Sources/NitroUINavigationMac/NavigationSharedElementView.swift +28 -0
  45. package/native/nitro-ui-renderer/Package.swift +105 -0
  46. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Animations/HybridAnimator.swift +502 -0
  47. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Animations/NitroAnimationContext.swift +54 -0
  48. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Animations/View+Animations.swift +35 -0
  49. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/AssetFonts.swift +36 -0
  50. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/FlippedView.swift +360 -0
  51. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/HybridContainerView.swift +28 -0
  52. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/HybridFonts.swift +7 -0
  53. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/HybridLabel.swift +127 -0
  54. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/HybridMaskedView.swift +185 -0
  55. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/HybridScrollView.swift +255 -0
  56. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/HybridScrollViewContent.swift +22 -0
  57. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/HybridView.swift +18 -0
  58. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/Image/HybridImage.swift +281 -0
  59. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/Image/HybridImageFactory.swift +132 -0
  60. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/Image/HybridImageLoader.swift +149 -0
  61. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/Image/HybridImageView.swift +601 -0
  62. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/Image/ImagePixelSupport.swift +418 -0
  63. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/LabelView.swift +198 -0
  64. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/List/HybridListView.swift +177 -0
  65. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/List/NativeListCell.swift +98 -0
  66. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/List/NativeListContentView.swift +12 -0
  67. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/List/NativeListFenwickTree.swift +94 -0
  68. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/List/NativeListScrollView.swift +1100 -0
  69. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/List/NativeListSnapshot.swift +24 -0
  70. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/List/NativeListUpdate.swift +11 -0
  71. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/List/NativeListView.swift +260 -0
  72. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/NativeContainerView.swift +443 -0
  73. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/NitroViewProtocols.swift +275 -0
  74. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/ShadowLayers.swift +338 -0
  75. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/TextInput/HybridTextInput.swift +252 -0
  76. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/TextInput/NitroTextInputView.swift +447 -0
  77. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Components/View.swift +660 -0
  78. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/CustomViewController.swift +254 -0
  79. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/DesktopHostDelegate.swift +14 -0
  80. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Extensions.swift +102 -0
  81. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Gestures/GestureRecognizerDelegate.swift +24 -0
  82. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Gestures/HybridGestureRecognizer.swift +41 -0
  83. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Gestures/HybridLongPressGestureRecognizer.swift +172 -0
  84. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Gestures/HybridPanGestureRecognizer.swift +193 -0
  85. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Gestures/HybridTapGestureRecognizer.swift +165 -0
  86. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/HybridAppearance.swift +75 -0
  87. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/HybridApplication.swift +29 -0
  88. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/HybridKeyboard.swift +26 -0
  89. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/HybridScreen.swift +29 -0
  90. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/HybridViewController.swift +232 -0
  91. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/HybridWindow.swift +61 -0
  92. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/HybridWindowScene.swift +17 -0
  93. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/LayoutNode.swift +73 -0
  94. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/NSColor+rgba.swift +29 -0
  95. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/RegisterRootCallback.swift +27 -0
  96. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/RuntimeReloadableAppDelegate.swift +7 -0
  97. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/SafeAreaInsetsListeners.swift +26 -0
  98. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/SizeListeners.swift +27 -0
  99. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/UIErrorHandler.swift +15 -0
  100. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/ViewController.swift +6 -0
  101. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/Window.swift +156 -0
  102. package/native/nitro-ui-renderer/Sources/NitroUIRendererMac/WindowScene.swift +23 -0
  103. package/native/nitro-ui-runtime/Package.swift +67 -0
  104. package/native/nitro-ui-runtime/Sources/NitroUIRuntimeMac/DevLoader.swift +16 -0
  105. package/native/nitro-ui-runtime/Sources/NitroUIRuntimeMac/NitroUIAppDelegate.swift +253 -0
  106. package/package.json +59 -8
  107. package/src/Platform.ts +43 -0
  108. package/src/bin.js +18 -0
  109. package/src/build.ts +453 -0
  110. package/src/bundleWorker.ts +80 -0
  111. package/src/cli.ts +157 -0
  112. package/src/config.ts +233 -0
  113. package/src/create.ts +67 -0
  114. package/src/files.ts +70 -0
  115. package/src/index.ts +1 -0
  116. package/src/init.ts +301 -0
  117. package/src/inlinePlatform.ts +140 -0
  118. package/src/modules.ts +385 -0
  119. package/src/packageApp.ts +103 -0
  120. package/src/packageDirectory.ts +61 -0
  121. package/src/preload.ts +40 -0
  122. package/src/prepare.ts +433 -0
  123. package/src/run.ts +41 -0
  124. package/src/runApp.ts +81 -0
  125. package/src/start.ts +152 -0
  126. package/src/toolchain.ts +83 -0
  127. package/src/versions.ts +103 -0
  128. package/templates/AppIcon.icns +0 -0
@@ -0,0 +1,502 @@
1
+ internal import AppKit
2
+ internal import Foundation
3
+ internal import NitroModules
4
+ internal import QuartzCore
5
+
6
+ extension AnimationCurve {
7
+ func toCAMediaTimingFunctionName() -> CAMediaTimingFunctionName {
8
+ switch self {
9
+ case .linear: return .linear
10
+ case .easein: return .easeIn
11
+ case .easeout: return .easeOut
12
+ case .easeinout: return .easeInEaseOut
13
+ }
14
+ }
15
+ }
16
+
17
+ private struct AnimationTargetKey: Hashable {
18
+ let layerID: ObjectIdentifier
19
+ let keyPath: String
20
+ }
21
+
22
+ fileprivate final class ActiveLayerAnimation {
23
+ weak var animator: HybridAnimator?
24
+ weak var layer: CALayer?
25
+ let layerID: ObjectIdentifier
26
+ let keyPath: String
27
+ let animationKey: String
28
+ let fromValue: Any?
29
+ let toValue: Any?
30
+ let syncModel: (Any?) -> Void
31
+ var pausedPresentationValue: Any?
32
+
33
+ init(
34
+ animator: HybridAnimator,
35
+ layer: CALayer,
36
+ keyPath: String,
37
+ animationKey: String,
38
+ fromValue: Any?,
39
+ toValue: Any?,
40
+ syncModel: @escaping (Any?) -> Void
41
+ ) {
42
+ self.animator = animator
43
+ self.layer = layer
44
+ self.layerID = ObjectIdentifier(layer)
45
+ self.keyPath = keyPath
46
+ self.animationKey = animationKey
47
+ self.fromValue = fromValue
48
+ self.toValue = toValue
49
+ self.syncModel = syncModel
50
+ }
51
+
52
+ var targetKey: AnimationTargetKey {
53
+ AnimationTargetKey(layerID: layerID, keyPath: keyPath)
54
+ }
55
+ }
56
+
57
+ /// Tracks the currently owning animation per `(CALayer, keyPath)` on the main thread so
58
+ /// interrupting animations start from the exact on-screen value and supersede cleanly.
59
+ enum NitroAnimationRegistry {
60
+ nonisolated(unsafe) private static var activeByTarget: [AnimationTargetKey: ActiveLayerAnimation] = [:]
61
+
62
+ static var activeCount: Int {
63
+ pruneDeallocated()
64
+ return activeByTarget.count
65
+ }
66
+
67
+ fileprivate static func activeEntry(for layer: CALayer, keyPath: String) -> ActiveLayerAnimation? {
68
+ let key = AnimationTargetKey(layerID: ObjectIdentifier(layer), keyPath: keyPath)
69
+ guard let entry = activeByTarget[key] else { return nil }
70
+ if entry.layer == nil || entry.animator == nil {
71
+ activeByTarget.removeValue(forKey: key)
72
+ return nil
73
+ }
74
+ return entry
75
+ }
76
+
77
+ static func hasActiveAnimation(on layer: CALayer, keyPath: String) -> Bool {
78
+ return activeEntry(for: layer, keyPath: keyPath) != nil
79
+ }
80
+
81
+ fileprivate static func register(_ entry: ActiveLayerAnimation) {
82
+ let key = entry.targetKey
83
+ if let existing = activeByTarget[key], existing !== entry {
84
+ if let layer = existing.layer {
85
+ layer.removeAnimation(forKey: existing.animationKey)
86
+ }
87
+ existing.animator?.detachSupersededEntry(existing)
88
+ }
89
+ activeByTarget[key] = entry
90
+ }
91
+
92
+ fileprivate static func unregister(_ entry: ActiveLayerAnimation) {
93
+ let key = entry.targetKey
94
+ if let current = activeByTarget[key], current === entry {
95
+ activeByTarget.removeValue(forKey: key)
96
+ }
97
+ }
98
+
99
+ static func currentPresentedValue(
100
+ for layer: CALayer,
101
+ keyPath: String,
102
+ fallback: () -> Any?
103
+ ) -> Any? {
104
+ if let existing = activeEntry(for: layer, keyPath: keyPath) {
105
+ if let presentation = layer.presentation() {
106
+ return presentation.value(forKeyPath: keyPath)
107
+ }
108
+ if let paused = existing.pausedPresentationValue {
109
+ return paused
110
+ }
111
+ return existing.fromValue ?? fallback()
112
+ }
113
+ if let keys = layer.animationKeys(), !keys.isEmpty, let presentation = layer.presentation() {
114
+ return presentation.value(forKeyPath: keyPath)
115
+ }
116
+ return fallback()
117
+ }
118
+
119
+ private static func pruneDeallocated() {
120
+ activeByTarget = activeByTarget.filter { _, entry in
121
+ entry.layer != nil && entry.animator != nil
122
+ }
123
+ }
124
+ }
125
+
126
+ private struct RecordedLayerAnimation {
127
+ weak var layer: CALayer?
128
+ let keyPath: String
129
+ let fromValue: Any?
130
+ let readToValue: () -> Any?
131
+ let syncModel: (Any?) -> Void
132
+ }
133
+
134
+ private final class NitroAnimationDelegate: NSObject, CAAnimationDelegate {
135
+ private weak var animator: HybridAnimator?
136
+ private let token: UInt64
137
+ private weak var entry: ActiveLayerAnimation?
138
+
139
+ init(animator: HybridAnimator, token: UInt64, entry: ActiveLayerAnimation) {
140
+ self.animator = animator
141
+ self.token = token
142
+ self.entry = entry
143
+ super.init()
144
+ }
145
+
146
+ func animationDidStop(_ anim: CAAnimation, finished flag: Bool) {
147
+ guard let entry else { return }
148
+ animator?.handleAnimationDidStop(token: token, entry: entry)
149
+ }
150
+ }
151
+
152
+ enum NitroAnimatorTiming {
153
+ case curve(duration: Double, curve: AnimationCurve)
154
+ case spring(duration: Double, dampingRatio: Double)
155
+
156
+ var requestedDuration: Double {
157
+ switch self {
158
+ case .curve(let duration, _): return duration
159
+ case .spring(let duration, _): return duration
160
+ }
161
+ }
162
+ }
163
+
164
+ final class HybridAnimator: HybridAnimatorSpec, NitroAnimationSession {
165
+ nonisolated(unsafe) private static var nextAnimatorID: UInt64 = 1
166
+
167
+ private let animatorID: UInt64
168
+ private let timing: NitroAnimatorTiming
169
+ private var animationsBlock: (any Callback<(), Void>)?
170
+ private var recorded: [RecordedLayerAnimation] = []
171
+ private var activeEntries: [ActiveLayerAnimation] = []
172
+ private var pendingStartWorkItem: DispatchWorkItem?
173
+ private var isStarted = false
174
+ private var isPaused = false
175
+ private var isStopped = false
176
+ private var runToken: UInt64 = 0
177
+ private var elapsedBeforeCurrentRun: CFTimeInterval = 0
178
+ private var currentRunStartTime: CFTimeInterval = 0
179
+
180
+ init(timing: NitroAnimatorTiming, animations: any Callback<(), Void>) throws {
181
+ self.animatorID = Self.nextAnimatorID
182
+ Self.nextAnimatorID &+= 1
183
+ self.timing = timing
184
+ self.animationsBlock = animations
185
+ try super.init()
186
+ }
187
+
188
+ deinit {
189
+ pendingStartWorkItem?.cancel()
190
+ for entry in activeEntries {
191
+ NitroAnimationRegistry.unregister(entry)
192
+ }
193
+ }
194
+
195
+ func currentPresentedValue(
196
+ for layer: CALayer,
197
+ keyPath: String,
198
+ fallback: () -> Any?
199
+ ) -> Any? {
200
+ // If this same (layer, keyPath) was already recorded earlier in this block,
201
+ // keep the original starting presentation value.
202
+ if let existing = recorded.first(where: { $0.layer === layer && $0.keyPath == keyPath }) {
203
+ return existing.fromValue
204
+ }
205
+ return NitroAnimationRegistry.currentPresentedValue(for: layer, keyPath: keyPath, fallback: fallback)
206
+ }
207
+
208
+ func recordLayerChange(
209
+ layer: CALayer,
210
+ keyPath: String,
211
+ fromValue: Any?,
212
+ readToValue: @escaping () -> Any?,
213
+ syncModel: @escaping (Any?) -> Void
214
+ ) {
215
+ // Coalesce multiple updates to the same (layer, keyPath) inside one animations block,
216
+ // keeping the original fromValue and latest readToValue/syncModel.
217
+ if let existingIndex = recorded.firstIndex(where: { $0.layer === layer && $0.keyPath == keyPath }) {
218
+ let originalFrom = recorded[existingIndex].fromValue
219
+ recorded[existingIndex] = RecordedLayerAnimation(
220
+ layer: layer,
221
+ keyPath: keyPath,
222
+ fromValue: originalFrom,
223
+ readToValue: readToValue,
224
+ syncModel: syncModel
225
+ )
226
+ return
227
+ }
228
+ recorded.append(
229
+ RecordedLayerAnimation(
230
+ layer: layer,
231
+ keyPath: keyPath,
232
+ fromValue: fromValue,
233
+ readToValue: readToValue,
234
+ syncModel: syncModel
235
+ )
236
+ )
237
+ }
238
+
239
+ func startAnimation(afterDelay: Double?) throws {
240
+ guard !isStopped else { return }
241
+
242
+ if isPaused {
243
+ resumePausedAnimations()
244
+ return
245
+ }
246
+
247
+ guard !isStarted else { return }
248
+
249
+ if let delay = afterDelay, delay > 0 {
250
+ guard delay.isFinite else {
251
+ throw RuntimeError("Animation delay must be a finite number.")
252
+ }
253
+ pendingStartWorkItem?.cancel()
254
+ let item = DispatchWorkItem { [weak self] in
255
+ self?.pendingStartWorkItem = nil
256
+ self?.executeAndStart()
257
+ }
258
+ pendingStartWorkItem = item
259
+ DispatchQueue.main.asyncAfter(deadline: .now() + delay, execute: item)
260
+ return
261
+ }
262
+
263
+ executeAndStart()
264
+ }
265
+
266
+ func pauseAnimation() throws {
267
+ guard isStarted, !isPaused, !isStopped else { return }
268
+ isPaused = true
269
+ let now = CACurrentMediaTime()
270
+ elapsedBeforeCurrentRun += max(0, now - currentRunStartTime)
271
+ runToken &+= 1
272
+
273
+ CATransaction.begin()
274
+ CATransaction.setDisableActions(true)
275
+ var surviving: [ActiveLayerAnimation] = []
276
+ for entry in activeEntries {
277
+ guard let layer = entry.layer else {
278
+ NitroAnimationRegistry.unregister(entry)
279
+ continue
280
+ }
281
+ let frozenValue = layer.presentation()?.value(forKeyPath: entry.keyPath) ?? entry.fromValue
282
+ entry.pausedPresentationValue = frozenValue
283
+ // Hold ONLY this keyPath at its exact paused presentation value without touching `layer.speed`,
284
+ // so other animators on the same layer continue running undisturbed.
285
+ let hold = CABasicAnimation(keyPath: entry.keyPath)
286
+ hold.fromValue = frozenValue
287
+ hold.toValue = frozenValue
288
+ hold.duration = 0.001
289
+ hold.fillMode = .both
290
+ hold.isRemovedOnCompletion = false
291
+ hold.delegate = nil
292
+ layer.add(hold, forKey: entry.animationKey)
293
+ surviving.append(entry)
294
+ }
295
+ activeEntries = surviving
296
+ CATransaction.commit()
297
+ }
298
+
299
+ func stopAnimation(withoutFinishing: Bool?) throws {
300
+ pendingStartWorkItem?.cancel()
301
+ pendingStartWorkItem = nil
302
+ guard !isStopped else { return }
303
+ isStopped = true
304
+ isPaused = false
305
+ runToken &+= 1
306
+ let stopInPlace = withoutFinishing ?? false
307
+
308
+ let entries = activeEntries
309
+ activeEntries.removeAll()
310
+ recorded.removeAll()
311
+ animationsBlock = nil
312
+
313
+ // Snapshot presentation values BEFORE removing any animations so related keyPaths
314
+ // (e.g. position, bounds, transform) sample the same frame.
315
+ let sampledValues: [(entry: ActiveLayerAnimation, value: Any?)] = entries.map { entry in
316
+ guard stopInPlace, let layer = entry.layer else {
317
+ return (entry, nil)
318
+ }
319
+ let value = layer.presentation()?.value(forKeyPath: entry.keyPath)
320
+ ?? entry.pausedPresentationValue
321
+ ?? entry.fromValue
322
+ return (entry, value)
323
+ }
324
+
325
+ // Apply position/bounds before transform so transform decomposition uses the frozen bounds size.
326
+ let ordered = sampledValues.sorted { lhs, rhs in
327
+ Self.syncPriority(for: lhs.entry.keyPath) < Self.syncPriority(for: rhs.entry.keyPath)
328
+ }
329
+
330
+ NitroAnimationContext.performApplyingModel {
331
+ for pair in ordered {
332
+ if stopInPlace, let value = pair.value {
333
+ pair.entry.syncModel(value)
334
+ }
335
+ if let layer = pair.entry.layer {
336
+ layer.removeAnimation(forKey: pair.entry.animationKey)
337
+ }
338
+ NitroAnimationRegistry.unregister(pair.entry)
339
+ }
340
+ }
341
+ }
342
+
343
+ private static func syncPriority(for keyPath: String) -> Int {
344
+ switch keyPath {
345
+ case "position": return 0
346
+ case "bounds": return 1
347
+ case "transform": return 2
348
+ default: return 3
349
+ }
350
+ }
351
+
352
+ private func resumePausedAnimations() {
353
+ isPaused = false
354
+ runToken &+= 1
355
+ let token = runToken
356
+ let now = CACurrentMediaTime()
357
+ currentRunStartTime = now
358
+
359
+ CATransaction.begin()
360
+ CATransaction.setDisableActions(true)
361
+ var stillActive: [ActiveLayerAnimation] = []
362
+ for entry in activeEntries {
363
+ guard let layer = entry.layer else {
364
+ NitroAnimationRegistry.unregister(entry)
365
+ continue
366
+ }
367
+ entry.pausedPresentationValue = nil
368
+ let resumed = makeAnimation(
369
+ keyPath: entry.keyPath,
370
+ fromValue: entry.fromValue,
371
+ toValue: entry.toValue
372
+ )
373
+ if elapsedBeforeCurrentRun >= resumed.duration {
374
+ layer.removeAnimation(forKey: entry.animationKey)
375
+ NitroAnimationRegistry.unregister(entry)
376
+ continue
377
+ }
378
+ let layerNow = layer.convertTime(now, from: nil)
379
+ resumed.speed = 1
380
+ resumed.timeOffset = 0
381
+ resumed.beginTime = layerNow - elapsedBeforeCurrentRun
382
+ resumed.fillMode = .backwards
383
+ resumed.isRemovedOnCompletion = true
384
+ resumed.delegate = NitroAnimationDelegate(animator: self, token: token, entry: entry)
385
+ layer.add(resumed, forKey: entry.animationKey)
386
+ stillActive.append(entry)
387
+ }
388
+ activeEntries = stillActive
389
+ CATransaction.commit()
390
+ }
391
+
392
+ private func executeAndStart() {
393
+ guard !isStopped, !isStarted, let animationsBlock else { return }
394
+ isStarted = true
395
+ self.animationsBlock = nil
396
+ recorded.removeAll()
397
+
398
+ let previousSession = NitroAnimationContext.activeSession
399
+ NitroAnimationContext.activeSession = self
400
+ do {
401
+ defer {
402
+ NitroAnimationContext.activeSession = previousSession
403
+ }
404
+ try animationsBlock.callSync()
405
+ } catch {
406
+ UIErrorHandler.report(error)
407
+ }
408
+
409
+ guard !recorded.isEmpty else { return }
410
+
411
+ let items = recorded
412
+ recorded.removeAll()
413
+
414
+ let duration = timing.requestedDuration
415
+ runToken &+= 1
416
+ let token = runToken
417
+ elapsedBeforeCurrentRun = 0
418
+ currentRunStartTime = CACurrentMediaTime()
419
+
420
+ CATransaction.begin()
421
+ CATransaction.setDisableActions(true)
422
+ for item in items {
423
+ guard let layer = item.layer else { continue }
424
+ let toValue = item.readToValue()
425
+ let animationKey = "nitro.anim.\(animatorID).\(item.keyPath)"
426
+
427
+ if duration <= 0 {
428
+ if let existing = NitroAnimationRegistry.activeEntry(for: layer, keyPath: item.keyPath) {
429
+ layer.removeAnimation(forKey: existing.animationKey)
430
+ NitroAnimationRegistry.unregister(existing)
431
+ existing.animator?.detachSupersededEntry(existing)
432
+ }
433
+ continue
434
+ }
435
+
436
+ let entry = ActiveLayerAnimation(
437
+ animator: self,
438
+ layer: layer,
439
+ keyPath: item.keyPath,
440
+ animationKey: animationKey,
441
+ fromValue: item.fromValue,
442
+ toValue: toValue,
443
+ syncModel: item.syncModel
444
+ )
445
+ NitroAnimationRegistry.register(entry)
446
+ let animation = makeAnimation(keyPath: item.keyPath, fromValue: item.fromValue, toValue: toValue)
447
+ animation.isRemovedOnCompletion = true
448
+ animation.delegate = NitroAnimationDelegate(animator: self, token: token, entry: entry)
449
+ layer.add(animation, forKey: animationKey)
450
+ activeEntries.append(entry)
451
+ }
452
+ CATransaction.commit()
453
+ }
454
+
455
+ fileprivate func detachSupersededEntry(_ superseded: ActiveLayerAnimation) {
456
+ activeEntries.removeAll { $0 === superseded || $0.layer == nil }
457
+ }
458
+
459
+ fileprivate func handleAnimationDidStop(token: UInt64, entry: ActiveLayerAnimation) {
460
+ guard token == runToken, !isPaused else { return }
461
+ NitroAnimationRegistry.unregister(entry)
462
+ activeEntries.removeAll { $0 === entry || $0.layer == nil }
463
+ }
464
+
465
+ private func makeAnimation(
466
+ keyPath: String,
467
+ fromValue: Any?,
468
+ toValue: Any?
469
+ ) -> CAAnimation {
470
+ switch timing {
471
+ case .curve(let duration, let curve):
472
+ let animation = CABasicAnimation(keyPath: keyPath)
473
+ animation.fromValue = fromValue
474
+ animation.toValue = toValue
475
+ animation.duration = max(0, duration)
476
+ animation.timingFunction = CAMediaTimingFunction(name: curve.toCAMediaTimingFunctionName())
477
+ return animation
478
+
479
+ case .spring(let duration, let dampingRatio):
480
+ let animation = CASpringAnimation(keyPath: keyPath)
481
+ animation.fromValue = fromValue
482
+ animation.toValue = toValue
483
+ // Match UIViewPropertyAnimator(duration:dampingRatio:):
484
+ // UIKit clamps dampingRatio to (0, 1] and chooses natural frequency so the spring
485
+ // settles to within ~0.1% at `duration`, with animation.duration equal to `duration`.
486
+ let safeDuration = max(0.001, duration)
487
+ let zeta = min(1.0, max(0.01, dampingRatio))
488
+ let omega: Double
489
+ if zeta < 1.0 {
490
+ omega = 6.907755 / (zeta * safeDuration)
491
+ } else {
492
+ omega = 9.233 / safeDuration
493
+ }
494
+ animation.mass = 1.0
495
+ animation.stiffness = CGFloat(omega * omega)
496
+ animation.damping = CGFloat(2.0 * zeta * omega)
497
+ animation.initialVelocity = 0
498
+ animation.duration = safeDuration
499
+ return animation
500
+ }
501
+ }
502
+ }
@@ -0,0 +1,54 @@
1
+ internal import AppKit
2
+ internal import QuartzCore
3
+
4
+ /// Coordinates implicit Core Animation suppression outside `View.animate` / `View.animateSpring`
5
+ /// and tracks active animation blocks. All renderer mutations execute on the main thread.
6
+ enum NitroAnimationContext {
7
+ nonisolated(unsafe) static var activeSession: (any NitroAnimationSession)?
8
+ nonisolated(unsafe) private static var suppressRecordingDepth: Int = 0
9
+
10
+ static var isAnimating: Bool {
11
+ activeSession != nil && suppressRecordingDepth == 0
12
+ }
13
+
14
+ /// Disables Core Animation implicit actions while applying direct model changes.
15
+ static func performWithoutImplicitAnimations(_ body: () -> Void) {
16
+ CATransaction.begin()
17
+ CATransaction.setDisableActions(true)
18
+ body()
19
+ CATransaction.commit()
20
+ }
21
+
22
+ /// Applies model state while suppressing both implicit Core Animation actions and
23
+ /// re-entrant animation session recording.
24
+ static func performApplyingModel(_ body: () -> Void) {
25
+ suppressRecordingDepth += 1
26
+ defer { suppressRecordingDepth -= 1 }
27
+ performWithoutImplicitAnimations(body)
28
+ }
29
+ }
30
+
31
+ /// Captures layer animations recorded inside `View.animate` / `View.animateSpring`.
32
+ protocol NitroAnimationSession: AnyObject {
33
+ func currentPresentedValue(
34
+ for layer: CALayer,
35
+ keyPath: String,
36
+ fallback: () -> Any?
37
+ ) -> Any?
38
+
39
+ func recordLayerChange(
40
+ layer: CALayer,
41
+ keyPath: String,
42
+ fromValue: Any?,
43
+ readToValue: @escaping () -> Any?,
44
+ syncModel: @escaping (Any?) -> Void
45
+ )
46
+ }
47
+
48
+ /// Public diagnostics surface for inspecting active animations in tests.
49
+ public enum NitroAnimationDiagnostics {
50
+ @MainActor
51
+ public static var activeAnimationCount: Int {
52
+ NitroAnimationRegistry.activeCount
53
+ }
54
+ }
@@ -0,0 +1,35 @@
1
+ internal import AppKit
2
+ public import NitroModules
3
+
4
+ public extension View where Self: HybridViewSpec {
5
+ func animate(
6
+ duration: Double,
7
+ curve: AnimationCurve,
8
+ animations: any Callback<(), Void>
9
+ ) throws -> any HybridAnimatorSpec {
10
+ guard duration.isFinite && duration >= 0 else {
11
+ throw RuntimeError("Animation duration must be a nonnegative finite number.")
12
+ }
13
+ return try HybridAnimator(
14
+ timing: .curve(duration: duration, curve: curve),
15
+ animations: animations
16
+ )
17
+ }
18
+
19
+ func animateSpring(
20
+ duration: Double,
21
+ dampingRatio: Double,
22
+ animations: any Callback<(), Void>
23
+ ) throws -> any HybridAnimatorSpec {
24
+ guard duration.isFinite && duration >= 0 else {
25
+ throw RuntimeError("Spring animation duration must be a nonnegative finite number.")
26
+ }
27
+ guard dampingRatio.isFinite && dampingRatio > 0 else {
28
+ throw RuntimeError("Spring dampingRatio must be a positive finite number.")
29
+ }
30
+ return try HybridAnimator(
31
+ timing: .spring(duration: duration, dampingRatio: dampingRatio),
32
+ animations: animations
33
+ )
34
+ }
35
+ }
@@ -0,0 +1,36 @@
1
+ internal import AppKit
2
+ internal import CoreText
3
+ internal import Foundation
4
+ internal import NitroModules
5
+
6
+ enum AssetFonts {
7
+ // The AppKit renderer accesses the font cache on the main thread.
8
+ nonisolated(unsafe) private static var descriptors: [String: CTFontDescriptor] = [:]
9
+
10
+ static func registerFont(name: String, data: Data) throws {
11
+ guard let descriptor = CTFontManagerCreateFontDescriptorFromData(data as CFData) else {
12
+ throw RuntimeError("Font \(name) could not be decoded.")
13
+ }
14
+ descriptors[name] = descriptor
15
+ }
16
+
17
+ static func load(name: String, size: CGFloat) throws -> NSFont {
18
+ let descriptor: CTFontDescriptor
19
+ if let cached = descriptors[name] {
20
+ descriptor = cached
21
+ } else if let asset = NSDataAsset(name: name) {
22
+ let data = asset.data as CFData
23
+ guard let loaded = CTFontManagerCreateFontDescriptorFromData(data) else {
24
+ throw RuntimeError("Font asset \(name) could not be decoded.")
25
+ }
26
+ descriptors[name] = loaded
27
+ descriptor = loaded
28
+ } else {
29
+ throw RuntimeError(
30
+ "Font asset \(name) is missing from the app's asset catalog. Import the font file and rebuild the app."
31
+ )
32
+ }
33
+ let font = CTFontCreateWithFontDescriptor(descriptor, size, nil)
34
+ return font as NSFont
35
+ }
36
+ }