Skip to content

Commit a254960

Browse files
committed
Complete the missing part of Link support
1 parent fb29a73 commit a254960

File tree

4 files changed

+29
-20
lines changed

4 files changed

+29
-20
lines changed

Sources/OpenSwiftUI/DataAndStorage/EnvironmentValues/EnvironmentValues+openURL.swift

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -78,17 +78,32 @@ extension EnvironmentValues {
7878
/// For example, a view that uses the action declared above
7979
/// receives `true` when calling the action, because the
8080
/// handler always returns ``OpenURLAction/Result/handled``.
81-
// MARK: - TODO
8281
public var openURL: OpenURLAction {
8382
get { _openURL }
8483
set { _openURL = newValue }
8584
}
8685

8786
public var _openURL: OpenURLAction {
88-
get { self[OpenURLActionKey.self] }
87+
get {
88+
let action = self[OpenURLActionKey.self]
89+
let hostingAction = self[HostingViewOpenURLActionKey.self]
90+
guard let hostingAction else {
91+
return action
92+
}
93+
guard !action.isDefault else {
94+
return hostingAction
95+
}
96+
guard case let .custom(customResultBlock, _) = action.handler,
97+
case let .system(systemHandler) = hostingAction.handler else {
98+
return action
99+
}
100+
return OpenURLAction(
101+
handler: .custom(customResultBlock, fallback: systemHandler),
102+
isDefault: false
103+
)
104+
}
89105
set { self[OpenURLActionKey.self] = newValue }
90106
}
91-
// MARK: TODO -
92107

93108
public var _openSensitiveURL: OpenURLAction {
94109
get { self[OpenSensitiveURLActionKey.self] }
@@ -102,14 +117,14 @@ struct OpenURLActionKey: EnvironmentKey {
102117
#if os(iOS) || os(tvOS)
103118
UIApplication.shared.open(url, options: [:], completionHandler: completion)
104119
#elseif os(macOS)
105-
NSWorkspace.shared.open(url, configuration: .init()) { application, error in
120+
NSWorkspace.shared.open(url, configuration: .init()) { _, error in
106121
completion(error != nil)
107122
}
108123
#else
109124
fatalError("Unimplemented")
110125
#endif
111126
},
112-
isDefault: false
127+
isDefault: true
113128
)
114129
}
115130

@@ -129,13 +144,10 @@ struct OpenSensitiveURLActionKey: EnvironmentKey {
129144
fatalError("Unimplemented")
130145
#endif
131146
},
132-
isDefault: false
147+
isDefault: true
133148
)
134149
}
135150

136151
struct HostingViewOpenURLActionKey: EnvironmentKey {
137-
static let defaultValue = OpenURLAction(
138-
handler: .custom({ .systemAction($0)}, fallback: nil),
139-
isDefault: true
140-
)
152+
static let defaultValue: OpenURLAction? = nil
141153
}

Sources/OpenSwiftUI/EventHandling/SystemEvents/OpenURLAction.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ import Foundation
7171
public struct OpenURLAction {
7272
enum Handler {
7373
case system((URL, @escaping (Bool) -> Void) -> Void)
74-
case custom((URL) -> Result, fallback: ((URL, (Bool) -> Void) -> Void)?)
74+
case custom((URL) -> Result, fallback: ((URL, @escaping (Bool) -> Void) -> Void)?)
7575
}
7676

7777
let handler: Handler
@@ -257,15 +257,12 @@ extension OpenURLAction {
257257
} else {
258258
systemHandler(url, completion)
259259
}
260-
case let .custom(customHandler, fallback: fallbackHandler):
261-
let result = customHandler(url)
260+
case let .custom(customResultBlock, fallback: fallbackHandler):
261+
let result = customResultBlock(url)
262262
switch result.actionResult {
263263
case let .systemAction(optionalURL):
264-
if let fallbackHandler {
265-
fallbackHandler(optionalURL ?? url, completion)
266-
} else {
267-
OpenURLActionKey.defaultValue._open(optionalURL ?? url, completion: completion)
268-
}
264+
let handler = fallbackHandler ?? OpenURLActionKey.defaultValue._open(_:completion:)
265+
handler(optionalURL ?? url, completion)
269266
case .handled:
270267
completion(true)
271268
case .discarded:

Sources/OpenSwiftUI/Views/Controls/Link/Link.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// OpenSwiftUI
44
//
55
// Created by Kyle on 2023/11/28.
6-
// Lastest Version: iOS 15.0
6+
// Lastest Version: iOS 15.5
77
// Status: Blocked by Text
88

99
import Foundation

Sources/OpenSwiftUI/Views/Controls/Link/internal/LinkDestination.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// OpenSwiftUI
44
//
55
// Created by Kyle on 2023/11/28.
6-
// Lastest Version: iOS 15.0
6+
// Lastest Version: iOS 15.5
77
// Status: Complete
88
// ID: 85AAA54365DC4DDF1DC8F2FECEF4501A
99

0 commit comments

Comments
 (0)