nitro-ui-macos 0.0.0 → 0.0.1

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 +56 -0
  2. package/LICENSE +3 -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 +57 -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,275 @@
1
+ public import AppKit
2
+ internal import QuartzCore
3
+
4
+ /// Adopted by AppKit views that support NitroUI's `isUserInteractionEnabled` semantics.
5
+ public protocol InteractionControllableView: NSView {
6
+ var isUserInteractionEnabled: Bool { get set }
7
+ }
8
+
9
+ /// Independent transform components stored per view so scale/rotation always pivot around
10
+ /// the view's center across resizes and zero-scale recovery.
11
+ public struct NitroTransformState: Equatable {
12
+ public var translation: Point3D = Point3D(x: 0, y: 0, z: 0)
13
+ public var scale: Point3D = Point3D(x: 1, y: 1, z: 1)
14
+ public var rotationRadians: Double = 0
15
+
16
+ public init() {}
17
+
18
+ public var isIdentity: Bool {
19
+ translation.x == 0 && translation.y == 0 && translation.z == 0
20
+ && scale.x == 1 && scale.y == 1 && scale.z == 1
21
+ && rotationRadians == 0
22
+ }
23
+
24
+ public func makeCATransform3D(for size: CGSize) -> CATransform3D {
25
+ if isIdentity {
26
+ return CATransform3DIdentity
27
+ }
28
+ let cx = size.width / 2
29
+ let cy = size.height / 2
30
+ var transform = CATransform3DMakeTranslation(-cx, -cy, 0)
31
+ if scale.x != 1 || scale.y != 1 || (translation.z != 0 && scale.z != 1) {
32
+ let zScale: CGFloat = translation.z != 0 ? CGFloat(scale.z) : 1
33
+ transform = CATransform3DConcat(
34
+ transform,
35
+ CATransform3DMakeScale(CGFloat(scale.x), CGFloat(scale.y), zScale)
36
+ )
37
+ }
38
+ if rotationRadians != 0 {
39
+ transform = CATransform3DConcat(
40
+ transform,
41
+ CATransform3DMakeRotation(CGFloat(rotationRadians), 0, 0, 1)
42
+ )
43
+ }
44
+ transform = CATransform3DConcat(
45
+ transform,
46
+ CATransform3DMakeTranslation(
47
+ cx + CGFloat(translation.x),
48
+ cy + CGFloat(translation.y),
49
+ CGFloat(translation.z)
50
+ )
51
+ )
52
+ return transform
53
+ }
54
+
55
+ /// Reconstructs independent translation, scale, and rotation components from a
56
+ /// center-pivoted `CATransform3D` (for example, when freezing an interrupted animation).
57
+ public static func from(
58
+ caTransform3D matrix: CATransform3D,
59
+ size: CGSize,
60
+ previous: NitroTransformState
61
+ ) -> NitroTransformState {
62
+ let cx = Double(size.width) / 2
63
+ let cy = Double(size.height) / 2
64
+ let m11 = Double(matrix.m11)
65
+ let m12 = Double(matrix.m12)
66
+ let m21 = Double(matrix.m21)
67
+ let m22 = Double(matrix.m22)
68
+
69
+ let tx = (m11 * cx + m21 * cy + Double(matrix.m41)) - cx
70
+ let ty = (m12 * cx + m22 * cy + Double(matrix.m42)) - cy
71
+ let tz = Double(matrix.m43)
72
+
73
+ var sx = hypot(m11, m12)
74
+ var sy = hypot(m21, m22)
75
+ let determinant = m11 * m22 - m12 * m21
76
+ if determinant < 0 {
77
+ if previous.scale.y < 0 && previous.scale.x >= 0 {
78
+ sy = -sy
79
+ } else {
80
+ sx = -sx
81
+ }
82
+ }
83
+
84
+ let rotationRadians: Double
85
+ if abs(sx) > 1e-9 {
86
+ rotationRadians = atan2(m12 / sx, m11 / sx)
87
+ } else if abs(sy) > 1e-9 {
88
+ rotationRadians = atan2(-m21 / sy, m22 / sy)
89
+ } else {
90
+ rotationRadians = previous.rotationRadians
91
+ }
92
+
93
+ let sz = abs(Double(matrix.m33)) > 1e-9 ? Double(matrix.m33) : previous.scale.z
94
+ var state = NitroTransformState()
95
+ state.translation = Point3D(x: tx, y: ty, z: tz)
96
+ state.scale = Point3D(x: sx, y: sy, z: sz)
97
+ state.rotationRadians = rotationRadians
98
+ return state
99
+ }
100
+ }
101
+
102
+ /// Adopted by AppKit views whose backing `CALayer` carries a center-pivoted `NitroTransformState`.
103
+ public protocol TransformableView: NSView {
104
+ var nitroTransform: NitroTransformState { get set }
105
+ func applyNitroTransform()
106
+ }
107
+
108
+ /// Shared transform-aware geometry and hit testing helpers for AppKit views.
109
+ enum NitroViewGeometry {
110
+ private static func activeGeometry(
111
+ of view: NSView
112
+ ) -> (bounds: CGRect, origin: CGPoint, transform: CATransform3D) {
113
+ if let layer = view.layer,
114
+ let keys = layer.animationKeys(),
115
+ !keys.isEmpty,
116
+ let presentation = layer.presentation() {
117
+ return (presentation.bounds, presentation.position, presentation.transform)
118
+ }
119
+ let transform = view.layer?.transform ?? CATransform3DIdentity
120
+ return (view.bounds, view.frame.origin, transform)
121
+ }
122
+
123
+ private static func affineTransform(from transform: CATransform3D) -> CGAffineTransform {
124
+ return CATransform3DGetAffineTransform(transform)
125
+ }
126
+
127
+ private static func invertAffine(_ affine: CGAffineTransform) -> CGAffineTransform? {
128
+ let determinant = affine.a * affine.d - affine.b * affine.c
129
+ guard determinant.isFinite, abs(determinant) > 1e-12 else {
130
+ return nil
131
+ }
132
+ return affine.inverted()
133
+ }
134
+
135
+ /// Converts a point in `view.superview` coordinates into `view.bounds`, accounting for `layer.transform`.
136
+ static func convertFromSuperview(_ point: NSPoint, in view: NSView) -> NSPoint? {
137
+ let preTransform: NSPoint
138
+ if let superview = view.superview {
139
+ preTransform = view.convert(point, from: superview)
140
+ } else {
141
+ preTransform = point
142
+ }
143
+
144
+ let (_, _, transform) = activeGeometry(of: view)
145
+ if CATransform3DIsIdentity(transform) {
146
+ return preTransform
147
+ }
148
+
149
+ // `view.convert` already accounts for `bounds.origin`; undo it before inverting `layer.transform`
150
+ // which operates in layer coordinates `(0..width, 0..height)`.
151
+ let layerPoint = CGPoint(
152
+ x: preTransform.x - view.bounds.origin.x,
153
+ y: preTransform.y - view.bounds.origin.y
154
+ )
155
+ guard let inverse = invertAffine(affineTransform(from: transform)) else {
156
+ return nil
157
+ }
158
+ let unTransformed = layerPoint.applying(inverse)
159
+ return NSPoint(
160
+ x: unTransformed.x + view.bounds.origin.x,
161
+ y: unTransformed.y + view.bounds.origin.y
162
+ )
163
+ }
164
+
165
+ /// Transform-aware hit testing that respects `isUserInteractionEnabled`, `isHidden`, `alphaValue`,
166
+ /// `masksToBounds`, and subview z-order.
167
+ static func hitTest(
168
+ _ view: NSView,
169
+ point: NSPoint,
170
+ isUserInteractionEnabled: Bool
171
+ ) -> NSView? {
172
+ guard isUserInteractionEnabled, !view.isHidden, view.alphaValue > 0.01 else {
173
+ return nil
174
+ }
175
+ guard let localPoint = convertFromSuperview(point, in: view) else {
176
+ return nil
177
+ }
178
+ if view.layer?.masksToBounds == true && !view.bounds.contains(localPoint) {
179
+ return nil
180
+ }
181
+ for subview in view.subviews.reversed() {
182
+ if let hit = subview.hitTest(localPoint) {
183
+ return hit
184
+ }
185
+ }
186
+ return view.bounds.contains(localPoint) ? view : nil
187
+ }
188
+
189
+ /// Converts a window-space `NSEvent.locationInWindow` into `view.bounds`, accounting for all
190
+ /// ancestor/self transforms and scroll offsets.
191
+ static func convertWindowPoint(_ windowPoint: NSPoint, to view: NSView) -> NSPoint? {
192
+ guard let contentView = view.window?.contentView else {
193
+ return nil
194
+ }
195
+ var chain: [NSView] = []
196
+ var cursor: NSView? = view
197
+ while let node = cursor {
198
+ chain.append(node)
199
+ if node === contentView {
200
+ break
201
+ }
202
+ cursor = node.superview
203
+ }
204
+ guard chain.last === contentView else {
205
+ return nil
206
+ }
207
+
208
+ var point = contentView.convert(windowPoint, from: nil)
209
+ // Walk from child of contentView down to `view`.
210
+ for child in chain.dropLast().reversed() {
211
+ let (bounds, origin, transform) = activeGeometry(of: child)
212
+ var relative = CGPoint(x: point.x - origin.x, y: point.y - origin.y)
213
+ if !CATransform3DIsIdentity(transform) {
214
+ guard let inverse = invertAffine(affineTransform(from: transform)) else {
215
+ return nil
216
+ }
217
+ relative = relative.applying(inverse)
218
+ }
219
+ point = NSPoint(x: relative.x + bounds.origin.x, y: relative.y + bounds.origin.y)
220
+ }
221
+ return point
222
+ }
223
+
224
+ /// Computes `view`'s axis-aligned bounding box in window content coordinates, or relative to
225
+ /// its topmost ancestor when detached from a window, including transforms, scroll offsets, and
226
+ /// active presentation layers.
227
+ static func frameInWindow(of view: NSView) -> Frame {
228
+ let targetView: NSView
229
+ if let contentView = view.window?.contentView {
230
+ targetView = contentView
231
+ } else {
232
+ var top: NSView = view
233
+ while let parent = top.superview {
234
+ top = parent
235
+ }
236
+ targetView = top
237
+ }
238
+
239
+ let initialBounds = activeGeometry(of: view).bounds
240
+ var corners = [
241
+ CGPoint(x: initialBounds.minX, y: initialBounds.minY),
242
+ CGPoint(x: initialBounds.maxX, y: initialBounds.minY),
243
+ CGPoint(x: initialBounds.minX, y: initialBounds.maxY),
244
+ CGPoint(x: initialBounds.maxX, y: initialBounds.maxY),
245
+ ]
246
+
247
+ var current: NSView = view
248
+ while current !== targetView {
249
+ let (bounds, origin, transform) = activeGeometry(of: current)
250
+ let affine = CATransform3DIsIdentity(transform) ? nil : affineTransform(from: transform)
251
+ for index in corners.indices {
252
+ var pt = CGPoint(x: corners[index].x - bounds.origin.x, y: corners[index].y - bounds.origin.y)
253
+ if let affine {
254
+ pt = pt.applying(affine)
255
+ }
256
+ corners[index] = CGPoint(x: pt.x + origin.x, y: pt.y + origin.y)
257
+ }
258
+ guard let parent = current.superview else {
259
+ break
260
+ }
261
+ current = parent
262
+ }
263
+
264
+ let minX = corners.map(\.x).min() ?? 0
265
+ let maxX = corners.map(\.x).max() ?? 0
266
+ let minY = corners.map(\.y).min() ?? 0
267
+ let maxY = corners.map(\.y).max() ?? 0
268
+ return Frame(
269
+ x: Double(minX),
270
+ y: Double(minY),
271
+ width: Double(maxX - minX),
272
+ height: Double(maxY - minY)
273
+ )
274
+ }
275
+ }
@@ -0,0 +1,338 @@
1
+ internal import AppKit
2
+ internal import CoreGraphics
3
+ internal import NitroModules
4
+ internal import QuartzCore
5
+ internal import SwiftUI
6
+
7
+ /// Keeps content clipping inside the layer that paints the shadow.
8
+ protocol ShadowContentHost: FlippedView {
9
+ var styledContent: FlippedView { get }
10
+ }
11
+
12
+ func transferShadowContentStyle(from source: FlippedView, to destination: FlippedView) {
13
+ let fromLayer = source.ensureLayer()
14
+ let toLayer = destination.ensureLayer()
15
+ NitroAnimationContext.performWithoutImplicitAnimations {
16
+ toLayer.cornerRadius = fromLayer.cornerRadius
17
+ toLayer.cornerCurve = fromLayer.cornerCurve
18
+ toLayer.maskedCorners = fromLayer.maskedCorners
19
+ toLayer.borderWidth = fromLayer.borderWidth
20
+ destination.nitroBorderColor = source.nitroBorderColor
21
+ toLayer.masksToBounds = fromLayer.masksToBounds
22
+ fromLayer.cornerRadius = 0
23
+ fromLayer.borderWidth = 0
24
+ source.nitroBorderColor = nil
25
+ fromLayer.masksToBounds = false
26
+ }
27
+ }
28
+
29
+ func resolvedShadowColor(_ value: Double) throws -> CGColor {
30
+ guard let rgba = UInt32(exactly: value) else {
31
+ throw RuntimeError("Shadow colors must be unsigned 0xRRGGBBAA integers.")
32
+ }
33
+ return NSColor(rgba: rgba).cgColor
34
+ }
35
+
36
+ struct BoxShadowGeometry: Equatable {
37
+ let bounds: CGRect
38
+ let borderWidth: CGFloat
39
+ let cornerRadius: CGFloat
40
+ let maskedCorners: CACornerMask
41
+ let cornerCurve: CALayerCornerCurve
42
+ let displayScale: CGFloat
43
+ let opaqueFill: Bool
44
+
45
+ init(bounds: CGRect, layer: CALayer, displayScale: CGFloat, opaqueFill: Bool) {
46
+ self.bounds = bounds
47
+ borderWidth = layer.borderWidth
48
+ cornerRadius = layer.cornerRadius
49
+ maskedCorners = layer.maskedCorners
50
+ cornerCurve = layer.cornerCurve
51
+ self.displayScale = max(displayScale, 1)
52
+ self.opaqueFill = opaqueFill
53
+ }
54
+ }
55
+
56
+ private struct RectCornerSet: OptionSet {
57
+ let rawValue: Int
58
+ static let topLeft = RectCornerSet(rawValue: 1 << 0)
59
+ static let topRight = RectCornerSet(rawValue: 1 << 1)
60
+ static let bottomLeft = RectCornerSet(rawValue: 1 << 2)
61
+ static let bottomRight = RectCornerSet(rawValue: 1 << 3)
62
+ }
63
+
64
+ final class NativeBoxShadow {
65
+ let layer = CALayer()
66
+ private(set) var options: BoxShadowOptions
67
+ private var color: CGColor
68
+
69
+ init(options: BoxShadowOptions) throws {
70
+ guard options.offsetX.isFinite, options.offsetY.isFinite, options.spreadDistance.isFinite,
71
+ options.blurRadius.isFinite, options.blurRadius >= 0 else {
72
+ throw RuntimeError("Box shadows require finite lengths and a nonnegative blur radius.")
73
+ }
74
+ self.color = try resolvedShadowColor(options.color)
75
+ self.options = options
76
+ // Non-flipped layer so the rasterized Retina CGImage renders upright.
77
+ layer.isGeometryFlipped = false
78
+ layer.contentsGravity = .resize
79
+ }
80
+
81
+ func updateOptions(_ nextOptions: BoxShadowOptions) throws {
82
+ guard nextOptions.offsetX.isFinite, nextOptions.offsetY.isFinite, nextOptions.spreadDistance.isFinite,
83
+ nextOptions.blurRadius.isFinite, nextOptions.blurRadius >= 0 else {
84
+ throw RuntimeError("Box shadows require finite lengths and a nonnegative blur radius.")
85
+ }
86
+ self.color = try resolvedShadowColor(nextOptions.color)
87
+ self.options = nextOptions
88
+ }
89
+
90
+ func update(_ content: BoxShadowGeometry) {
91
+ let box = content.bounds
92
+ let inset = options.inset
93
+ let border = inset ? content.borderWidth : 0
94
+ let edge = box.insetBy(dx: border, dy: border)
95
+ guard !edge.isNull, !edge.isEmpty else {
96
+ layer.isHidden = true
97
+ layer.contents = nil
98
+ layer.shadowPath = nil
99
+ layer.shadowOpacity = 0
100
+ return
101
+ }
102
+
103
+ let spread = CGFloat(inset ? -options.spreadDistance : options.spreadDistance)
104
+ let expanded = edge.insetBy(dx: -spread, dy: -spread)
105
+ let offset = expanded.offsetBy(dx: CGFloat(options.offsetX), dy: CGFloat(options.offsetY))
106
+ let corners = roundedCorners(content.maskedCorners)
107
+ let edgeRadius = max(content.cornerRadius - border, 0)
108
+ let radius = spreadRadius(edgeRadius, spread: spread)
109
+ let curve = content.cornerCurve
110
+ let silhouette = roundedPath(offset, radius: radius, corners: corners, curve: curve)
111
+ let isHidden = !inset && silhouette.isEmpty
112
+ layer.isHidden = isHidden
113
+ guard !isHidden else {
114
+ layer.contents = nil
115
+ layer.shadowPath = nil
116
+ layer.shadowOpacity = 0
117
+ return
118
+ }
119
+
120
+ let blurRadius = CGFloat(options.blurRadius)
121
+ let shadowRadius = blurRadius / 2
122
+ let padding = ceil(2 * blurRadius) + 2
123
+ let shadowExtent = offset.insetBy(dx: -padding, dy: -padding)
124
+
125
+ // Use GPU-accelerated CALayer.shadowPath when the shadow does not require interior
126
+ // knockout or inset clipping (matching iOS NativeBoxShadow). Keep CPU bitmap
127
+ // rasterization only for inset shadows and outset shadows behind translucent fills
128
+ // whose penumbra overlaps the interior.
129
+ if !inset && (content.opaqueFill || !box.intersects(shadowExtent)) {
130
+ if NitroAnimationContext.isAnimating, let session = NitroAnimationContext.activeSession, layer.shadowOpacity > 0 {
131
+ let fromColor = session.currentPresentedValue(
132
+ for: layer,
133
+ keyPath: "shadowColor",
134
+ fallback: { layer.shadowColor ?? color }
135
+ )
136
+ let fromRadius = session.currentPresentedValue(
137
+ for: layer,
138
+ keyPath: "shadowRadius",
139
+ fallback: { layer.shadowRadius }
140
+ )
141
+ let fromPath = session.currentPresentedValue(
142
+ for: layer,
143
+ keyPath: "shadowPath",
144
+ fallback: { layer.shadowPath ?? silhouette }
145
+ )
146
+ NitroAnimationContext.performApplyingModel {
147
+ self.layer.contents = nil
148
+ self.layer.mask = nil
149
+ self.layer.isGeometryFlipped = false
150
+ self.layer.contentsScale = content.displayScale
151
+ self.layer.frame = box
152
+ self.layer.shadowColor = self.color
153
+ self.layer.shadowOffset = .zero
154
+ self.layer.shadowOpacity = 1
155
+ self.layer.shadowRadius = shadowRadius
156
+ self.layer.shadowPath = silhouette
157
+ }
158
+ let targetColor = color
159
+ session.recordLayerChange(
160
+ layer: layer,
161
+ keyPath: "shadowColor",
162
+ fromValue: fromColor,
163
+ readToValue: { [weak layer] in layer?.shadowColor ?? targetColor },
164
+ syncModel: { [weak layer] value in
165
+ guard let layer, let value else { return }
166
+ let cfObject = value as AnyObject
167
+ if CFGetTypeID(cfObject) == CGColor.typeID {
168
+ layer.shadowColor = (cfObject as! CGColor)
169
+ }
170
+ }
171
+ )
172
+ session.recordLayerChange(
173
+ layer: layer,
174
+ keyPath: "shadowRadius",
175
+ fromValue: fromRadius,
176
+ readToValue: { [weak layer] in layer?.shadowRadius ?? shadowRadius },
177
+ syncModel: { [weak layer] value in
178
+ guard let layer, let number = value as? NSNumber else { return }
179
+ layer.shadowRadius = CGFloat(number.doubleValue)
180
+ }
181
+ )
182
+ session.recordLayerChange(
183
+ layer: layer,
184
+ keyPath: "shadowPath",
185
+ fromValue: fromPath,
186
+ readToValue: { [weak layer] in layer?.shadowPath ?? silhouette },
187
+ syncModel: { [weak layer] value in
188
+ guard let layer, let value else { return }
189
+ let cfObject = value as AnyObject
190
+ if CFGetTypeID(cfObject) == CGPath.typeID {
191
+ layer.shadowPath = (cfObject as! CGPath)
192
+ }
193
+ }
194
+ )
195
+ return
196
+ }
197
+ layer.contents = nil
198
+ layer.mask = nil
199
+ // Host layer is already flipped (FlippedView); keeping `isGeometryFlipped = false`
200
+ // inherits top-down coordinates without double-flipping shadowPath.
201
+ layer.isGeometryFlipped = false
202
+ layer.contentsScale = content.displayScale
203
+ layer.frame = box
204
+ layer.shadowColor = color
205
+ layer.shadowOffset = .zero
206
+ layer.shadowOpacity = 1
207
+ layer.shadowRadius = shadowRadius
208
+ layer.shadowPath = silhouette
209
+ return
210
+ }
211
+
212
+ // Fallback to CPU rasterization for inset shadows and translucent knockout shadows.
213
+ layer.shadowOpacity = 0
214
+ layer.shadowPath = nil
215
+ layer.shadowColor = nil
216
+ layer.isGeometryFlipped = false
217
+ layer.contentsGravity = .resize
218
+
219
+ let canvasRect: CGRect = inset ? edge : shadowExtent
220
+ let scale = content.displayScale
221
+ let pixelWidth = Int(ceil(canvasRect.width * scale))
222
+ let pixelHeight = Int(ceil(canvasRect.height * scale))
223
+ guard pixelWidth > 0, pixelHeight > 0 else {
224
+ layer.isHidden = true
225
+ layer.contents = nil
226
+ return
227
+ }
228
+
229
+ let colorSpace = CGColorSpaceCreateDeviceRGB()
230
+ let bitmapInfo = CGImageAlphaInfo.premultipliedFirst.rawValue | CGBitmapInfo.byteOrder32Little.rawValue
231
+ guard let ctx = CGContext(
232
+ data: nil,
233
+ width: pixelWidth,
234
+ height: pixelHeight,
235
+ bitsPerComponent: 8,
236
+ bytesPerRow: pixelWidth * 4,
237
+ space: colorSpace,
238
+ bitmapInfo: bitmapInfo
239
+ ) else {
240
+ return
241
+ }
242
+
243
+ // Flip into top-down view coordinate space anchored at canvasRect.origin.
244
+ ctx.scaleBy(x: scale, y: scale)
245
+ ctx.translateBy(x: 0, y: canvasRect.height)
246
+ ctx.scaleBy(x: 1, y: -1)
247
+ ctx.translateBy(x: -canvasRect.minX, y: -canvasRect.minY)
248
+
249
+ let clipPath = roundedPath(edge, radius: edgeRadius, corners: corners, curve: curve)
250
+ let shift = max(canvasRect.width, canvasRect.height) + padding * 2 + 1024
251
+
252
+ if inset {
253
+ // Clip strictly inside the box edge, then cast an inward shadow from the inverted silhouette.
254
+ ctx.addPath(clipPath)
255
+ ctx.clip()
256
+
257
+ let exterior = edge.insetBy(dx: -padding - 4, dy: -padding - 4)
258
+ ctx.setShadow(
259
+ offset: CGSize(width: shift * scale, height: 0),
260
+ blur: blurRadius * scale,
261
+ color: color
262
+ )
263
+ ctx.translateBy(x: -shift, y: 0)
264
+ ctx.addRect(exterior)
265
+ if !silhouette.isEmpty {
266
+ ctx.addPath(silhouette)
267
+ }
268
+ ctx.setFillColor(NSColor.black.cgColor)
269
+ ctx.fillPath(using: .evenOdd)
270
+ } else {
271
+ // Knock out the interior so translucent backgrounds do not show outset shadows through them.
272
+ if !content.opaqueFill {
273
+ ctx.addRect(canvasRect)
274
+ ctx.addPath(clipPath)
275
+ ctx.clip(using: .evenOdd)
276
+ }
277
+
278
+ ctx.setShadow(
279
+ offset: CGSize(width: shift * scale, height: 0),
280
+ blur: blurRadius * scale,
281
+ color: color
282
+ )
283
+ ctx.translateBy(x: -shift, y: 0)
284
+ ctx.addPath(silhouette)
285
+ ctx.setFillColor(NSColor.black.cgColor)
286
+ ctx.fillPath()
287
+ }
288
+
289
+ layer.contentsScale = scale
290
+ layer.frame = canvasRect
291
+ layer.contents = ctx.makeImage()
292
+ }
293
+
294
+ private func roundedCorners(_ mask: CACornerMask) -> RectCornerSet {
295
+ var corners: RectCornerSet = []
296
+ if mask.contains(.layerMinXMinYCorner) { corners.insert(.topLeft) }
297
+ if mask.contains(.layerMaxXMinYCorner) { corners.insert(.topRight) }
298
+ if mask.contains(.layerMinXMaxYCorner) { corners.insert(.bottomLeft) }
299
+ if mask.contains(.layerMaxXMaxYCorner) { corners.insert(.bottomRight) }
300
+ return corners
301
+ }
302
+
303
+ private func roundedPath(
304
+ _ rect: CGRect,
305
+ radius: CGFloat,
306
+ corners: RectCornerSet,
307
+ curve: CALayerCornerCurve
308
+ ) -> CGPath {
309
+ guard !rect.isNull, !rect.isEmpty else { return CGMutablePath() }
310
+ let limit = min(rect.width, rect.height) / 2
311
+ let r = min(limit, radius)
312
+ if corners.isEmpty || r == 0 {
313
+ return CGPath(rect: rect, transform: nil)
314
+ }
315
+ let style: RoundedCornerStyle = curve == .circular ? .circular : .continuous
316
+ let shape = RoundedRectangle(cornerRadius: r, style: style)
317
+ var path = shape.path(in: rect).cgPath
318
+ let selected: [RectCornerSet] = [.topLeft, .topRight, .bottomLeft, .bottomRight]
319
+ for corner in selected where !corners.contains(corner) {
320
+ let left = corner == .topLeft || corner == .bottomLeft
321
+ let top = corner == .topLeft || corner == .topRight
322
+ let x = left ? rect.minX : rect.midX
323
+ let y = top ? rect.minY : rect.midY
324
+ let quadrant = CGRect(x: x, y: y, width: rect.width / 2, height: rect.height / 2)
325
+ let square = CGPath(rect: quadrant, transform: nil)
326
+ path = path.union(square)
327
+ }
328
+ return path
329
+ }
330
+
331
+ private func spreadRadius(_ radius: CGFloat, spread: CGFloat) -> CGFloat {
332
+ let distance = abs(spread)
333
+ if radius >= distance { return radius + spread }
334
+ let ratio = radius / distance
335
+ let correction = pow(1 - ratio, 3)
336
+ return max(0, radius + spread * (1 - correction))
337
+ }
338
+ }