Skip to content

Commit 1e87a38

Browse files
authored
Merge pull request #35 from NakaokaRei/feature/issue-18-dialog-functions
feat: Add alert(), confirm(), prompt(), password() dialog functions
2 parents 37067b4 + 56c3f3e commit 1e87a38

File tree

5 files changed

+441
-0
lines changed

5 files changed

+441
-0
lines changed

Sample/Sample/Common/DemoTab.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ enum DemoTab: String, CaseIterable {
1515
case imageRecognition
1616
case pixelDetection
1717
case scrolling
18+
case dialog
1819

1920
var title: String {
2021
switch self {
@@ -25,6 +26,7 @@ enum DemoTab: String, CaseIterable {
2526
case .imageRecognition: return "Image Recognition"
2627
case .pixelDetection: return "Pixel Detection"
2728
case .scrolling: return "Scrolling"
29+
case .dialog: return "Dialog"
2830
}
2931
}
3032

@@ -37,6 +39,7 @@ enum DemoTab: String, CaseIterable {
3739
case .imageRecognition: return "eye"
3840
case .pixelDetection: return "eyedropper"
3941
case .scrolling: return "arrow.up.and.down"
42+
case .dialog: return "message"
4043
}
4144
}
4245
}

Sample/Sample/ContentView.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ struct ContentView: View {
9696
PixelDetectionView()
9797
case .scrolling:
9898
ScrollingDemoView()
99+
case .dialog:
100+
DialogDemoView()
99101
}
100102
}
101103
.padding(24)
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
//
2+
// DialogDemoView.swift
3+
// Sample
4+
//
5+
// Created by SwiftAutoGUI on 2025/07/12.
6+
//
7+
8+
import SwiftUI
9+
import SwiftAutoGUI
10+
11+
struct DialogDemoView: View {
12+
@StateObject private var viewModel = DialogDemoViewModel()
13+
14+
var body: some View {
15+
VStack(alignment: .leading, spacing: 24) {
16+
// Title and description
17+
VStack(alignment: .leading, spacing: 8) {
18+
Label("Dialog Boxes", systemImage: "message")
19+
.font(.title2)
20+
.fontWeight(.bold)
21+
22+
Text("Display dialog boxes for user interaction during automation")
23+
.font(.body)
24+
.foregroundColor(.secondary)
25+
}
26+
27+
Divider()
28+
29+
// Alert Demo
30+
DemoSection(
31+
title: "Alert Dialog",
32+
description: "Display a simple alert with a message"
33+
) {
34+
Button("Show Alert") {
35+
Task {
36+
await viewModel.showAlert()
37+
}
38+
}
39+
.buttonStyle(.borderedProminent)
40+
41+
if let result = viewModel.alertResult {
42+
ResultView(label: "Result", value: result)
43+
}
44+
}
45+
46+
// Confirm Demo
47+
DemoSection(
48+
title: "Confirmation Dialog",
49+
description: "Ask for user confirmation with custom buttons"
50+
) {
51+
VStack(alignment: .leading, spacing: 12) {
52+
Button("Show Default Confirm") {
53+
Task {
54+
await viewModel.showDefaultConfirm()
55+
}
56+
}
57+
.buttonStyle(.borderedProminent)
58+
59+
Button("Show Custom Confirm") {
60+
Task {
61+
await viewModel.showCustomConfirm()
62+
}
63+
}
64+
.buttonStyle(.bordered)
65+
66+
if let result = viewModel.confirmResult {
67+
ResultView(label: "Selected", value: result)
68+
}
69+
}
70+
}
71+
72+
// Prompt Demo
73+
DemoSection(
74+
title: "Text Input Dialog",
75+
description: "Get text input from the user"
76+
) {
77+
Button("Show Prompt") {
78+
Task {
79+
await viewModel.showPrompt()
80+
}
81+
}
82+
.buttonStyle(.borderedProminent)
83+
84+
if let result = viewModel.promptResult {
85+
ResultView(label: "Input", value: result)
86+
}
87+
}
88+
89+
// Password Demo
90+
DemoSection(
91+
title: "Password Dialog",
92+
description: "Securely get password input"
93+
) {
94+
Button("Show Password Dialog") {
95+
Task {
96+
await viewModel.showPassword()
97+
}
98+
}
99+
.buttonStyle(.borderedProminent)
100+
101+
if let result = viewModel.passwordResult {
102+
ResultView(label: "Password", value: String(repeating: "", count: result.count))
103+
}
104+
}
105+
106+
Spacer()
107+
}
108+
}
109+
}
110+
111+
struct DemoSection<Content: View>: View {
112+
let title: String
113+
let description: String
114+
@ViewBuilder let content: Content
115+
116+
var body: some View {
117+
VStack(alignment: .leading, spacing: 12) {
118+
VStack(alignment: .leading, spacing: 4) {
119+
Text(title)
120+
.font(.headline)
121+
Text(description)
122+
.font(.caption)
123+
.foregroundColor(.secondary)
124+
}
125+
126+
content
127+
}
128+
.padding()
129+
.frame(maxWidth: .infinity, alignment: .leading)
130+
.background(
131+
RoundedRectangle(cornerRadius: 8)
132+
.fill(Color.gray.opacity(0.1))
133+
)
134+
}
135+
}
136+
137+
struct ResultView: View {
138+
let label: String
139+
let value: String
140+
141+
var body: some View {
142+
HStack {
143+
Text("\(label):")
144+
.font(.caption)
145+
.foregroundColor(.secondary)
146+
Text(value)
147+
.font(.system(.caption, design: .monospaced))
148+
.padding(.horizontal, 8)
149+
.padding(.vertical, 4)
150+
.background(
151+
RoundedRectangle(cornerRadius: 4)
152+
.fill(Color.green.opacity(0.2))
153+
)
154+
}
155+
}
156+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
//
2+
// DialogDemoViewModel.swift
3+
// Sample
4+
//
5+
// Created by SwiftAutoGUI on 2025/07/12.
6+
//
7+
8+
import Foundation
9+
import SwiftAutoGUI
10+
11+
@MainActor
12+
class DialogDemoViewModel: ObservableObject {
13+
@Published var alertResult: String?
14+
@Published var confirmResult: String?
15+
@Published var promptResult: String?
16+
@Published var passwordResult: String?
17+
18+
func showAlert() async {
19+
let result = SwiftAutoGUI.alert(
20+
"This is a sample alert message!",
21+
title: "SwiftAutoGUI Alert",
22+
button: "Got it!"
23+
)
24+
alertResult = result
25+
}
26+
27+
func showDefaultConfirm() async {
28+
let result = SwiftAutoGUI.confirm(
29+
"Do you want to continue with the operation?",
30+
title: "Confirmation Required"
31+
)
32+
confirmResult = result ?? "Cancelled"
33+
}
34+
35+
func showCustomConfirm() async {
36+
let result = SwiftAutoGUI.confirm(
37+
"What would you like to do next?",
38+
title: "Choose Action",
39+
buttons: ["Save", "Don't Save", "Cancel"]
40+
)
41+
confirmResult = result ?? "Cancelled"
42+
}
43+
44+
func showPrompt() async {
45+
let result = SwiftAutoGUI.prompt(
46+
"Please enter your name:",
47+
title: "User Information",
48+
default: "John Doe"
49+
)
50+
promptResult = result ?? "Cancelled"
51+
}
52+
53+
func showPassword() async {
54+
let result = SwiftAutoGUI.password(
55+
"Enter your password to continue:",
56+
title: "Authentication Required"
57+
)
58+
passwordResult = result ?? "Cancelled"
59+
}
60+
}

0 commit comments

Comments
 (0)