Skip to content

Commit b7ef214

Browse files
committed
Fix PlatformView layoutSubviews on AppKit
1 parent a4952e3 commit b7ef214

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

Sources/BuddyPlatform/PlatformTypes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public typealias PlatformImage = NSImage
6161
public typealias PlatformColor = NSColor
6262

6363
/// `UIView/NSView` alias for platform-agnostic code.
64-
public typealias PlatformView = NSView
64+
open class PlatformView: NSView { }
6565

6666
/// `UIViewController/NSViewController` alias for platform-agnostic code.
6767
public typealias PlatformViewController = NSViewController

Sources/BuddyPlatform/PlatformView+Layer.swift

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#if !os(watchOS)
2+
import SwiftUI
23

34
#if os(macOS)
45
import AppKit
@@ -31,9 +32,13 @@ import AppKit
3132
/// - note: Check out ``PlatformWindowScene`` for details on how this behaves in AppKit.
3233
open var hostingWindowScene: PlatformWindowScene? { window?.screen }
3334

35+
open override func layout() {
36+
layoutSubviews()
37+
}
38+
3439
/// This offers a place to override `layoutSubviews` for both UIKit and AppKit, since the method name on `NSView` is just `layout`.
3540
open func layoutSubviews() {
36-
layout()
41+
super.layout()
3742
}
3843

3944
/// 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
7681
#endif // os(macOS)
7782

7883
#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

Comments
 (0)