1
1
//
2
- // FormQuickAction .swift
2
+ // ListAction .swift
3
3
// SwiftUIKit
4
4
//
5
5
// Created by Daniel Saidi on 2023-11-21.
6
6
//
7
7
8
8
import SwiftUI
9
9
10
- /**
11
- This enum defines quick list actions, that can be triggered
12
- within a form or list.
13
-
14
- Use the various view builders like ``button(content:)`` and
15
- ``button`` to get a view that triggers the action.
16
- */
10
+ /// This enum defines actions that can be triggered within a
11
+ /// form or a list.
17
12
public enum ListAction {
18
13
19
- /// Call a certain phone number.
14
+ /// Call a phone number.
20
15
case call( phoneNumber: String )
21
16
22
- /// Copy a certain value .
17
+ /// Copy a text .
23
18
case copy( String )
24
19
25
- /// Email a certain address.
20
+ /// Copy an image.
21
+ case copyImage( ImageRepresentable )
22
+
23
+ /// Send an e-mail.
26
24
case email( address: String )
27
25
28
26
/// Open a certain URL.
29
- case open ( url: String )
27
+ case openUrl ( _ url: String )
30
28
}
31
29
32
30
public extension ListAction {
@@ -47,19 +45,21 @@ public extension ListAction {
47
45
link ( url: . init( string: " tel: \( url) " ) , content: content)
48
46
case . copy( let text) :
49
47
button ( action: { copy ( text) } , content: content)
48
+ case . copyImage( let image) :
49
+ button ( action: { copy ( image) } , content: content)
50
50
case . email( let url) :
51
51
link ( url: . init( string: " mailto: \( url) " ) , content: content)
52
- case . open ( let url) :
52
+ case . openUrl ( let url) :
53
53
link ( url: . init( string: url) , content: content)
54
54
}
55
55
}
56
56
57
57
var icon : Image {
58
58
switch self {
59
59
case . call: . init( systemName: " phone " )
60
- case . copy: . init( systemName: " doc.on.doc " )
60
+ case . copy, . copyImage : . init( systemName: " doc.on.doc " )
61
61
case . email: . init( systemName: " envelope " )
62
- case . open : . init( systemName: " safari " )
62
+ case . openUrl : . init( systemName: " safari " )
63
63
}
64
64
}
65
65
}
@@ -91,12 +91,23 @@ private extension ListAction {
91
91
private extension ListAction {
92
92
93
93
func copy( _ value: String ) {
94
- #if os(macOS) || os(iOS)
94
+ #if os(macOS) || os(iOS) || os(visionOS)
95
95
Pasteboard . general. copy ( value)
96
96
#else
97
97
print ( " Unsupported platform " )
98
98
#endif
99
99
}
100
+
101
+ func copy( _ image: ImageRepresentable ) {
102
+ #if os(iOS) || os(visionOS)
103
+ Pasteboard . general. image = image
104
+ #elseif os(macOS)
105
+ Pasteboard . general. clearContents ( )
106
+ Pasteboard . general. writeObjects ( [ image] )
107
+ #else
108
+ print ( " Unsupported platform " )
109
+ #endif
110
+ }
100
111
}
101
112
102
113
#Preview {
@@ -107,10 +118,10 @@ private extension ListAction {
107
118
108
119
return List {
109
120
view ( for: . call( phoneNumber: " abc123 " ) )
110
- #if os(macOS) || os(iOS)
121
+ #if os(macOS) || os(iOS) || os(visionOS)
111
122
view ( for: . copy( " abc123 " ) )
112
123
#endif
113
124
view ( for: . email( address: " abc123 " ) )
114
- view ( for: . open ( url : " https://danielsaidi.com " ) )
125
+ view ( for: . openUrl ( " https://danielsaidi.com " ) )
115
126
}
116
127
}
0 commit comments