Add App Shortcuts and improve IntentsExtension

This commit is contained in:
世界
2025-12-07 11:33:30 +08:00
parent 39338ab133
commit bbf0d4945c
3 changed files with 103 additions and 11 deletions
+46 -11
View File
@@ -16,7 +16,7 @@ struct StartServiceIntent: AppIntent {
@Parameter(title: "Profile", optionsProvider: ProfileProvider())
var profile: String
func perform() async throws -> some IntentResult {
func perform() async throws -> some IntentResult & ProvidesDialog {
guard let extensionProfile = try await (ExtensionProfile.load()) else {
throw NSError(domain: "NetworkExtension not installed", code: 0)
}
@@ -34,7 +34,7 @@ struct StartServiceIntent: AppIntent {
}
if extensionProfile.status == .connected {
if !profileChanged {
return .result()
return .result(dialog: "Service is already running")
}
try LibboxNewStandaloneCommandClient()!.serviceReload()
} else if extensionProfile.status.isConnected {
@@ -42,7 +42,7 @@ struct StartServiceIntent: AppIntent {
} else {
try await extensionProfile.start()
}
return .result()
return .result(dialog: "Service started")
}
}
@@ -56,9 +56,9 @@ struct RestartServiceIntent: AppIntent {
Summary("Restart sing-box service")
}
func perform() async throws -> some IntentResult {
func perform() async throws -> some IntentResult & ProvidesDialog {
guard let extensionProfile = try await (ExtensionProfile.load()) else {
return .result()
return .result(dialog: "Service is not installed")
}
if extensionProfile.status == .connected {
try LibboxNewStandaloneCommandClient()!.serviceReload()
@@ -67,7 +67,7 @@ struct RestartServiceIntent: AppIntent {
} else {
try await extensionProfile.start()
}
return .result()
return .result(dialog: "Service restarted")
}
}
@@ -81,12 +81,12 @@ struct StopServiceIntent: AppIntent {
Summary("Stop sing-box service")
}
func perform() async throws -> some IntentResult {
func perform() async throws -> some IntentResult & ProvidesDialog {
guard let extensionProfile = try await (ExtensionProfile.load()) else {
return .result()
return .result(dialog: "Service is not installed")
}
try await extensionProfile.stop()
return .result()
return .result(dialog: "Service stopped")
}
}
@@ -165,7 +165,7 @@ struct UpdateProfileIntent: AppIntent {
var profile: String
init() {}
func perform() async throws -> some IntentResult {
func perform() async throws -> some IntentResult & ProvidesDialog {
guard let profile = try await ProfileManager.get(by: profile) else {
throw NSError(domain: "Specified profile not found: \(profile)", code: 0)
}
@@ -173,7 +173,7 @@ struct UpdateProfileIntent: AppIntent {
throw NSError(domain: "Specified profile is not a remote profile", code: 0)
}
try await profile.updateRemoteProfile()
return .result()
return .result(dialog: "Profile updated")
}
}
@@ -192,3 +192,38 @@ class RemoteProfileProvider: DynamicOptionsProvider {
try await ProfileManager.listRemote().map(\.name)
}
}
struct ServiceShortcuts: AppShortcutsProvider {
static var appShortcuts: [AppShortcut] {
AppShortcut(
intent: StartServiceIntent(),
phrases: ["Start \(.applicationName)"],
shortTitle: "Start",
systemImageName: "power"
)
AppShortcut(
intent: StopServiceIntent(),
phrases: ["Stop \(.applicationName)"],
shortTitle: "Stop",
systemImageName: "stop.fill"
)
AppShortcut(
intent: RestartServiceIntent(),
phrases: ["Restart \(.applicationName)"],
shortTitle: "Restart",
systemImageName: "arrow.clockwise"
)
AppShortcut(
intent: ToggleServiceIntent(),
phrases: ["Toggle \(.applicationName)"],
shortTitle: "Toggle",
systemImageName: "arrow.triangle.2.circlepath"
)
AppShortcut(
intent: UpdateProfileIntent(),
phrases: ["Update \(.applicationName) profile"],
shortTitle: "Update Profile",
systemImageName: "arrow.down.circle"
)
}
}