Skip to content

Commit e2bbfdd

Browse files
committed
Add file dialogs, alerts, and menu items to WindowingExample
1 parent 5069fc4 commit e2bbfdd

File tree

1 file changed

+80
-1
lines changed

1 file changed

+80
-1
lines changed

Examples/Sources/WindowingExample/WindowingApp.swift

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,70 @@ import SwiftCrossUI
66
import SwiftBundlerRuntime
77
#endif
88

9+
struct FileDialogDemo: View {
10+
@State var selectedFile: URL? = nil
11+
@State var saveDestination: URL? = nil
12+
13+
@Environment(\.chooseFile) var chooseFile
14+
@Environment(\.chooseFileSaveDestination) var chooseFileSaveDestination
15+
16+
var body: some View {
17+
Text("File dialog demo")
18+
.font(.system(size: 18))
19+
20+
if let selectedFile {
21+
Text("Selected file: \(selectedFile.path)")
22+
} else {
23+
Text("No file selected")
24+
}
25+
26+
if let saveDestination {
27+
Text("Save destination: \(saveDestination.path)")
28+
}
29+
30+
HStack {
31+
Button("Open") {
32+
Task {
33+
guard let file = await chooseFile() else {
34+
return
35+
}
36+
selectedFile = file
37+
}
38+
}
39+
40+
Button("Save") {
41+
Task {
42+
guard let file = await chooseFileSaveDestination() else {
43+
return
44+
}
45+
saveDestination = file
46+
}
47+
}
48+
}
49+
}
50+
}
51+
52+
struct AlertDemo: View {
53+
@Environment(\.presentAlert) var presentAlert
54+
55+
var body: some View {
56+
Text("Alert demo")
57+
.font(.system(size: 18))
58+
59+
Button("Present error") {
60+
Task {
61+
await presentAlert("Failed to succeed")
62+
}
63+
}
64+
}
65+
}
66+
967
@main
1068
@HotReloadable
1169
struct WindowingApp: App {
1270
@State var title = "My window"
1371
@State var resizable = false
72+
@State var errorMessage: String? = nil
1473

1574
var body: some Scene {
1675
WindowGroup(title) {
@@ -20,16 +79,36 @@ struct WindowingApp: App {
2079
Text("Window title:")
2180
TextField("My window", $title)
2281
}
82+
2383
Button(resizable ? "Disable resizing" : "Enable resizing") {
2484
resizable = !resizable
2585
}
86+
2687
Image(Bundle.module.bundleURL.appendingPathComponent("Banner.png"))
88+
89+
Divider()
90+
91+
FileDialogDemo()
92+
93+
Divider()
94+
95+
AlertDemo()
2796
}
28-
.padding(10)
97+
.padding(20)
2998
}
3099
}
31100
.defaultSize(width: 500, height: 500)
32101
.windowResizability(resizable ? .contentMinSize : .contentSize)
102+
.commands {
103+
CommandMenu("Demo menu") {
104+
Button("Menu item") {}
105+
106+
Menu("Submenu") {
107+
Button("Item 1") {}
108+
Button("Item 2") {}
109+
}
110+
}
111+
}
33112

34113
WindowGroup("Secondary window") {
35114
#hotReloadable {

0 commit comments

Comments
 (0)