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

8 files changed

+359
-26
lines changed

Examples/Package.resolved

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@
2727
"version" : "1.6.45"
2828
}
2929
},
30+
{
31+
"identity" : "libwebp",
32+
"kind" : "remoteSourceControl",
33+
"location" : "https://github.com/the-swift-collective/libwebp",
34+
"state" : {
35+
"revision" : "5f745a17b9a5c2a4283f17c2cde4517610ab5f99",
36+
"version" : "1.4.1"
37+
}
38+
},
3039
{
3140
"identity" : "pathkit",
3241
"kind" : "remoteSourceControl",
@@ -130,8 +139,8 @@
130139
"kind" : "remoteSourceControl",
131140
"location" : "https://github.com/stackotter/swift-image-formats",
132141
"state" : {
133-
"revision" : "05a0169a2a5e9365a058e9aa13da5937be6e2586",
134-
"version" : "0.3.1"
142+
"revision" : "697bd8aa62bef7d74a1383454e3534a527769e41",
143+
"version" : "0.3.2"
135144
}
136145
},
137146
{
@@ -143,15 +152,6 @@
143152
"version" : "0.4.0"
144153
}
145154
},
146-
{
147-
"identity" : "swift-libwebp",
148-
"kind" : "remoteSourceControl",
149-
"location" : "https://github.com/stackotter/swift-libwebp",
150-
"state" : {
151-
"revision" : "61dc3787c764022ad2f5ab4f9994a569afe86f9f",
152-
"version" : "0.2.0"
153-
}
154-
},
155155
{
156156
"identity" : "swift-log",
157157
"kind" : "remoteSourceControl",

Package.resolved

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"originHash" : "9bdf10c7a21892e3f103174fa966d5a212af3dfca1608849b243319bfbb3d005",
2+
"originHash" : "d7660de3524ddfd94fb5ed75c380bbcda1ec9d99cc0b74dee8d2f70756063c78",
33
"pins" : [
44
{
55
"identity" : "jpeg",
@@ -19,6 +19,15 @@
1919
"version" : "1.6.45"
2020
}
2121
},
22+
{
23+
"identity" : "libwebp",
24+
"kind" : "remoteSourceControl",
25+
"location" : "https://github.com/the-swift-collective/libwebp",
26+
"state" : {
27+
"revision" : "5f745a17b9a5c2a4283f17c2cde4517610ab5f99",
28+
"version" : "1.4.1"
29+
}
30+
},
2231
{
2332
"identity" : "swift-cwinrt",
2433
"kind" : "remoteSourceControl",
@@ -51,17 +60,8 @@
5160
"kind" : "remoteSourceControl",
5261
"location" : "https://github.com/stackotter/swift-image-formats",
5362
"state" : {
54-
"revision" : "05a0169a2a5e9365a058e9aa13da5937be6e2586",
55-
"version" : "0.3.1"
56-
}
57-
},
58-
{
59-
"identity" : "swift-libwebp",
60-
"kind" : "remoteSourceControl",
61-
"location" : "https://github.com/stackotter/swift-libwebp",
62-
"state" : {
63-
"revision" : "61dc3787c764022ad2f5ab4f9994a569afe86f9f",
64-
"version" : "0.2.0"
63+
"revision" : "697bd8aa62bef7d74a1383454e3534a527769e41",
64+
"version" : "0.3.2"
6565
}
6666
},
6767
{

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)