Skip to content

Commit 522f462

Browse files
committed
Added AppIntent based initializers
1 parent e868db5 commit 522f462

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
+131
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
//
2+
// Button+AppIntent.swift
3+
// ButtonKit
4+
//
5+
// MIT License
6+
//
7+
// Copyright (c) 2024 Thomas Durand
8+
//
9+
// Permission is hereby granted, free of charge, to any person obtaining a copy
10+
// of this software and associated documentation files (the "Software"), to deal
11+
// in the Software without restriction, including without limitation the rights
12+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
// copies of the Software, and to permit persons to whom the Software is
14+
// furnished to do so, subject to the following conditions:
15+
//
16+
// The above copyright notice and this permission notice shall be included in all
17+
// copies or substantial portions of the Software.
18+
//
19+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25+
// SOFTWARE.
26+
//
27+
28+
import AppIntents
29+
import SwiftUI
30+
31+
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
32+
extension AsyncButton where P == IndeterminateProgress {
33+
public init(
34+
role: ButtonRole? = nil,
35+
id: AnyHashable? = nil,
36+
intent: some AppIntent,
37+
@ViewBuilder label: @escaping () -> S
38+
) {
39+
self.init(role: role, id: id, action: { _ = try await intent.perform() }, label: label)
40+
}
41+
}
42+
43+
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
44+
extension AsyncButton where P == IndeterminateProgress, S == Text {
45+
public init(
46+
_ titleKey: LocalizedStringKey,
47+
role: ButtonRole? = nil,
48+
id: AnyHashable? = nil,
49+
intent: some AppIntent
50+
) {
51+
self.init(titleKey, role: role, id: id, action: { _ = try await intent.perform() })
52+
}
53+
54+
@_disfavoredOverload
55+
public init(
56+
_ title: some StringProtocol,
57+
role: ButtonRole? = nil,
58+
id: AnyHashable? = nil,
59+
intent: some AppIntent
60+
) {
61+
self.init(title, role: role, id: id, action: { _ = try await intent.perform() })
62+
}
63+
}
64+
65+
@available(iOS 16.0, macOS 13.0, tvOS 16.0, watchOS 9.0, *)
66+
extension AsyncButton where P == IndeterminateProgress, S == Label<Text, Image> {
67+
public init(
68+
_ titleKey: LocalizedStringKey,
69+
image name: String,
70+
role: ButtonRole? = nil,
71+
id: AnyHashable? = nil,
72+
intent: some AppIntent
73+
) {
74+
self.init(titleKey, image: name, role: role, id: id, action: { _ = try await intent.perform() })
75+
}
76+
77+
@_disfavoredOverload
78+
public init(
79+
_ title: some StringProtocol,
80+
image name: String,
81+
role: ButtonRole? = nil,
82+
id: AnyHashable? = nil,
83+
intent: some AppIntent
84+
) {
85+
self.init(title, image: name, role: role, id: id, action: { _ = try await intent.perform() })
86+
}
87+
88+
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *)
89+
public init(
90+
_ titleKey: LocalizedStringKey,
91+
image: ImageResource,
92+
role: ButtonRole? = nil,
93+
id: AnyHashable? = nil,
94+
intent: some AppIntent
95+
) {
96+
self.init(titleKey, image: image, role: role, id: id, action: { _ = try await intent.perform() })
97+
}
98+
99+
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, *)
100+
@_disfavoredOverload
101+
public init(
102+
_ title: some StringProtocol,
103+
image: ImageResource,
104+
role: ButtonRole? = nil,
105+
id: AnyHashable? = nil,
106+
intent: some AppIntent
107+
) {
108+
self.init(title, image: image, role: role, id: id, action: { _ = try await intent.perform() })
109+
}
110+
111+
public init(
112+
_ titleKey: LocalizedStringKey,
113+
systemImage: String,
114+
role: ButtonRole? = nil,
115+
id: AnyHashable? = nil,
116+
intent: some AppIntent
117+
) {
118+
self.init(titleKey, systemImage: systemImage, role: role, id: id, action: { _ = try await intent.perform() })
119+
}
120+
121+
@_disfavoredOverload
122+
public init(
123+
_ title: some StringProtocol,
124+
systemImage: String,
125+
role: ButtonRole? = nil,
126+
id: AnyHashable? = nil,
127+
intent: some AppIntent
128+
) {
129+
self.init(title, systemImage: systemImage, role: role, id: id, action: { _ = try await intent.perform() })
130+
}
131+
}

0 commit comments

Comments
 (0)