Skip to content

Commit 2cd2240

Browse files
authored
Creating new NSWindowDelegateContainer (#6)
1 parent 1d8e402 commit 2cd2240

File tree

3 files changed

+50
-13
lines changed

3 files changed

+50
-13
lines changed

.github/workflows/RadiantKit.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
if: "!contains(github.event.head_commit.message, 'ci skip')"
1414
runs-on: ubuntu-latest
1515
container:
16-
image: swiftlang/swift:nightly-6.0-jammy
16+
image: swift:6.0.1-noble
1717
steps:
1818
- uses: actions/checkout@v4
1919
- name: Cache swift package modules
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//
2+
// NSWindowDelegateContainer.swift
3+
// RadiantKit
4+
//
5+
// Created by Leo Dion.
6+
// Copyright © 2024 BrightDigit.
7+
//
8+
// Permission is hereby granted, free of charge, to any person
9+
// obtaining a copy of this software and associated documentation
10+
// files (the “Software”), to deal in the Software without
11+
// restriction, including without limitation the rights to use,
12+
// copy, modify, merge, publish, distribute, sublicense, and/or
13+
// sell copies of the Software, and to permit persons to whom the
14+
// Software is furnished to do so, subject to the following
15+
// conditions:
16+
//
17+
// The above copyright notice and this permission notice shall be
18+
// included in all copies or substantial portions of the Software.
19+
//
20+
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND,
21+
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
22+
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23+
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
24+
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
25+
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26+
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
27+
// OTHER DEALINGS IN THE SOFTWARE.
28+
//
29+
30+
#if canImport(AppKit) && canImport(SwiftUI)
31+
public import AppKit
32+
@MainActor public protocol NSWindowDelegateContainer: AnyObject {
33+
var windowDelegate: (any NSWindowDelegate)? { get set }
34+
}
35+
#endif

Sources/RadiantKit/AppKit/View+NSWindowDelegate.swift

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,26 @@
2929

3030
#if canImport(AppKit) && canImport(SwiftUI)
3131
import AppKit
32-
3332
public import SwiftUI
3433

3534
fileprivate struct NSWindowDelegateAdaptorModifier: ViewModifier {
36-
@Binding var binding: (any NSWindowDelegate)?
35+
let container: any NSWindowDelegateContainer
3736
let delegate: any NSWindowDelegate
3837

3938
init(
40-
binding: Binding<(any NSWindowDelegate)?>,
39+
container: any NSWindowDelegateContainer,
4140
delegate: @autoclosure () -> any NSWindowDelegate
4241
) {
43-
self._binding = binding
44-
self.delegate = binding.wrappedValue ?? delegate()
45-
46-
#warning(
47-
"Issue 100 - We can't set binding here - Modifying state during view update, this will cause undefined behavior."
48-
)
49-
self.binding = self.delegate
42+
self.container = container
43+
if let windowDelegate = container.windowDelegate {
44+
self.delegate = windowDelegate
45+
}
46+
else {
47+
let newDelegate = delegate()
48+
print("Creating a New Window Delegate")
49+
self.delegate = newDelegate
50+
self.container.windowDelegate = newDelegate
51+
}
5052
}
5153

5254
func body(content: Content) -> some View {
@@ -60,10 +62,10 @@
6062

6163
extension View {
6264
public func nsWindowDelegateAdaptor(
63-
_ binding: Binding<(any NSWindowDelegate)?>,
65+
_ container: any NSWindowDelegateContainer,
6466
_ delegate: @autoclosure () -> any NSWindowDelegate
6567
) -> some View {
66-
self.modifier(NSWindowDelegateAdaptorModifier(binding: binding, delegate: delegate()))
68+
self.modifier(NSWindowDelegateAdaptorModifier(container: container, delegate: delegate()))
6769
}
6870
}
6971
#endif

0 commit comments

Comments
 (0)