Skip to content

Commit 9f42f6d

Browse files
authored
Add NSViewRepresentable for AppKit & support Swift 6 concurrency. (#105)
* AppKit: Implement NSViewRepresentable. Signed-off-by: furby™ <devs@wabi.foundation> * AppKit: specify main actors on NSViewRepresentable. Signed-off-by: furby™ <devs@wabi.foundation> * UIKit: add main actors. Signed-off-by: furby™ <devs@wabi.foundation> * Make UIKit implementation agnostic of iOS/visionOS. Signed-off-by: furby™ <devs@wabi.foundation> * bump swift-image-formats for dependency centralization. * Part of our efforts to condense and streamline all the various C/C++ dependencies under one central repository, this is important because Swift makes it all too easy to have 30 different zlib libraries for example, which leads to a headache of duplicated symbols and what not. * That central repository is https://github.com/the-swift-collective Signed-off-by: furby™ <devs@wabi.foundation> * Remove BaseWidget for AppKit backend. Signed-off-by: furby™ <devs@wabi.foundation> * UIKit: add support for swift 6 concurrency. Signed-off-by: furby™ <devs@wabi.foundation> * AppKit: Add asWidget and update methods back. Signed-off-by: furby™ <devs@wabi.foundation> * appkit: revert to swift 6 compatible but lopsided support. * This ensures scui works with swift 6 strict concurrency - however it is lopsided in that makeCoordinator() is not marked with a main actor. Signed-off-by: furby™ <devs@wabi.foundation> * remove an additional, no longer needed MainActor Signed-off-by: furby™ <devs@wabi.foundation> --------- Signed-off-by: furby™ <devs@wabi.foundation>
1 parent 810cc92 commit 9f42f6d

File tree

8 files changed

+359
-26
lines changed

8 files changed

+359
-26
lines changed

Examples/Package.resolved

Lines changed: 11 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.resolved

Lines changed: 12 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ let package = Package(
9595
),
9696
.package(
9797
url: "https://github.com/stackotter/swift-image-formats",
98-
.upToNextMinor(from: "0.3.1")
98+
.upToNextMinor(from: "0.3.2")
9999
),
100100
.package(
101101
url: "https://github.com/wabiverse/swift-windowsappsdk",
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import AppKit
2+
3+
public class BaseWidget: NSView {
4+
private var leftConstraint: NSLayoutConstraint?
5+
private var topConstraint: NSLayoutConstraint?
6+
private var widthConstraint: NSLayoutConstraint?
7+
private var heightConstraint: NSLayoutConstraint?
8+
9+
var x = 0 {
10+
didSet {
11+
if x != oldValue {
12+
updateLeftConstraint()
13+
}
14+
}
15+
}
16+
17+
var y = 0 {
18+
didSet {
19+
if y != oldValue {
20+
updateTopConstraint()
21+
}
22+
}
23+
}
24+
25+
var width = 0 {
26+
didSet {
27+
if width != oldValue {
28+
updateWidthConstraint()
29+
}
30+
}
31+
}
32+
33+
var height = 0 {
34+
didSet {
35+
if height != oldValue {
36+
updateHeightConstraint()
37+
}
38+
}
39+
}
40+
41+
init() {
42+
super.init(frame: .zero)
43+
44+
self.translatesAutoresizingMaskIntoConstraints = false
45+
}
46+
47+
@available(*, unavailable)
48+
public required init?(coder: NSCoder) {
49+
fatalError("init(coder:) is not used for this view")
50+
}
51+
52+
private func updateLeftConstraint() {
53+
leftConstraint?.isActive = false
54+
guard let superview else { return }
55+
if #available(macOS 11.0, *) {
56+
leftConstraint = self.leftAnchor.constraint(
57+
equalTo: superview.safeAreaLayoutGuide.leftAnchor, constant: CGFloat(x))
58+
} else {
59+
leftConstraint = self.leftAnchor.constraint(
60+
equalTo: superview.leftAnchor, constant: CGFloat(x))
61+
}
62+
leftConstraint!.isActive = true
63+
}
64+
65+
private func updateTopConstraint() {
66+
topConstraint?.isActive = false
67+
guard let superview else { return }
68+
if #available(macOS 11.0, *) {
69+
topConstraint = self.topAnchor.constraint(
70+
equalTo: superview.safeAreaLayoutGuide.topAnchor, constant: CGFloat(x))
71+
} else {
72+
topConstraint = self.topAnchor.constraint(
73+
equalTo: superview.topAnchor, constant: CGFloat(x))
74+
}
75+
topConstraint!.isActive = true
76+
}
77+
78+
private func updateWidthConstraint() {
79+
widthConstraint?.isActive = false
80+
widthConstraint = self.widthAnchor.constraint(equalToConstant: CGFloat(width))
81+
widthConstraint!.isActive = true
82+
}
83+
84+
private func updateHeightConstraint() {
85+
heightConstraint?.isActive = false
86+
heightConstraint = self.heightAnchor.constraint(equalToConstant: CGFloat(height))
87+
heightConstraint!.isActive = true
88+
}
89+
90+
public override func viewDidMoveToSuperview() {
91+
super.viewDidMoveToSuperview()
92+
93+
updateLeftConstraint()
94+
updateTopConstraint()
95+
}
96+
}
97+
98+
class WrapperWidget<View: NSView>: BaseWidget {
99+
init(child: View) {
100+
super.init()
101+
102+
self.addSubview(child)
103+
child.translatesAutoresizingMaskIntoConstraints = false
104+
NSLayoutConstraint.activate([
105+
child.topAnchor.constraint(equalTo: self.topAnchor),
106+
child.leadingAnchor.constraint(equalTo: self.leadingAnchor),
107+
child.bottomAnchor.constraint(equalTo: self.bottomAnchor),
108+
child.trailingAnchor.constraint(equalTo: self.trailingAnchor),
109+
])
110+
}
111+
112+
override convenience init() {
113+
self.init(child: View(frame: .zero))
114+
}
115+
116+
var child: View {
117+
subviews[0] as! View
118+
}
119+
120+
override var intrinsicContentSize: CGSize {
121+
child.intrinsicContentSize
122+
}
123+
}

0 commit comments

Comments
 (0)