|
1 | 1 | #if !os(watchOS)
|
| 2 | +import SwiftUI |
2 | 3 |
|
3 | 4 | #if os(macOS)
|
4 | 5 | import AppKit
|
@@ -31,9 +32,13 @@ import AppKit
|
31 | 32 | /// - note: Check out ``PlatformWindowScene`` for details on how this behaves in AppKit.
|
32 | 33 | open var hostingWindowScene: PlatformWindowScene? { window?.screen }
|
33 | 34 |
|
| 35 | + open override func layout() { |
| 36 | + layoutSubviews() |
| 37 | + } |
| 38 | + |
34 | 39 | /// This offers a place to override `layoutSubviews` for both UIKit and AppKit, since the method name on `NSView` is just `layout`.
|
35 | 40 | open func layoutSubviews() {
|
36 |
| - layout() |
| 41 | + super.layout() |
37 | 42 | }
|
38 | 43 |
|
39 | 44 | /// This offers a place to override `didMoveToSuperview` for both UIKit and AppKit, since the method name on `NSView` is different.
|
@@ -76,3 +81,48 @@ import UIKit
|
76 | 81 | #endif // os(macOS)
|
77 | 82 |
|
78 | 83 | #endif // !os(watchOS)
|
| 84 | + |
| 85 | +#if DEBUG |
| 86 | +private final class TestView: PlatformView { |
| 87 | + override func layoutSubviews() { |
| 88 | + super.layoutSubviews() |
| 89 | + |
| 90 | + let testLayer: CALayer |
| 91 | + |
| 92 | + if let layer = platformLayer.sublayers?.first { |
| 93 | + testLayer = layer |
| 94 | + } else { |
| 95 | + testLayer = CALayer() |
| 96 | + testLayer.backgroundColor = PlatformColor.systemGreen.cgColor |
| 97 | + platformLayer.addSublayer(testLayer) |
| 98 | + } |
| 99 | + |
| 100 | + |
| 101 | + CATransaction.begin() |
| 102 | + CATransaction.setDisableActions(true) |
| 103 | + CATransaction.setAnimationDuration(0) |
| 104 | + |
| 105 | + testLayer.bounds = CGRect( |
| 106 | + x: 0, |
| 107 | + y: 0, |
| 108 | + width: platformLayer.bounds.width * 0.5, |
| 109 | + height: platformLayer.bounds.height * 0.5 |
| 110 | + ) |
| 111 | + testLayer.position = CGPoint( |
| 112 | + x: platformLayer.frame.midX, |
| 113 | + y: platformLayer.frame.midY |
| 114 | + ) |
| 115 | + |
| 116 | + platformLayer.backgroundColor = PlatformColor.systemBlue.cgColor |
| 117 | + |
| 118 | + CATransaction.commit() |
| 119 | + |
| 120 | + print(#function, bounds, "testLayer:", testLayer.bounds) |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +@available(macOS 15.0, iOS 18.0, *) |
| 125 | +#Preview { |
| 126 | + TestView(frame: .init(x: 0, y: 0, width: 200, height: 200)) |
| 127 | +} |
| 128 | +#endif |
0 commit comments