-
-
Notifications
You must be signed in to change notification settings - Fork 65
Add AlertScene
#299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Add AlertScene
#299
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
16046b3
add `AlertScene`
kaascevich 0277782
fix casing of "OK" label for default alert action
kaascevich 98d615c
add `AlertScene` to WindowingExample
kaascevich 97ec266
improve `AlertScene` documentation
kaascevich baf48b2
allow empty `AlertActionsBuilder` block to match SwiftUI
kaascevich 9cb25f3
implement PR requested changes
kaascevich 5a3c886
assign `mainWindow` on initial window creation to fix `AlertScene` crash
kaascevich b351786
Merge branch 'main' into alert-scene
kaascevich File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /// A scene that shows a standalone alert. | ||
| /// | ||
| /// The exact behavior of the alert is backend-dependent, but it typically | ||
| /// shows up as an application modal or standalone window. | ||
kaascevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| public struct AlertScene: Scene { | ||
| public typealias Node = AlertSceneNode | ||
|
|
||
| var title: String | ||
| var isPresented: Binding<Bool> | ||
kaascevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| var actions: [AlertAction] | ||
|
|
||
| public let commands = Commands.empty | ||
|
|
||
| /// Creates an alert scene. | ||
| /// | ||
| /// The exact behavior of the alert is backend-dependent, but it typically | ||
| /// shows up as an application modal or standalone window. | ||
kaascevich marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| /// | ||
| /// - Parameters: | ||
| /// - title: The alert's title. | ||
| /// - isPresented: A binding to a `Bool` that controls whether the alert | ||
| /// is presented. | ||
| /// - actions: The alert's actions. | ||
| public init( | ||
| _ title: String, | ||
| isPresented: Binding<Bool>, | ||
| @AlertActionsBuilder actions: () -> [AlertAction] | ||
| ) { | ||
| self.title = title | ||
| self.isPresented = isPresented | ||
| self.actions = actions() | ||
| } | ||
| } | ||
|
|
||
| /// The scene graph node for ``AlertScene``. | ||
| public final class AlertSceneNode: SceneGraphNode { | ||
| public typealias NodeScene = AlertScene | ||
|
|
||
| private var scene: AlertScene | ||
| private var alert: Any? | ||
|
|
||
| public init<Backend: AppBackend>( | ||
| from scene: AlertScene, | ||
| backend: Backend, | ||
| environment: EnvironmentValues | ||
| ) { | ||
| self.scene = scene | ||
| } | ||
|
|
||
| public func update<Backend: AppBackend>( | ||
| _ newScene: AlertScene?, | ||
| backend: Backend, | ||
| environment: EnvironmentValues | ||
| ) { | ||
| if let newScene { | ||
| self.scene = newScene | ||
| } | ||
|
|
||
| if scene.isPresented.wrappedValue, alert == nil { | ||
| let alert = backend.createAlert() | ||
| backend.updateAlert( | ||
| alert, | ||
| title: scene.title, | ||
| actionLabels: scene.actions.map(\.label), | ||
| environment: environment | ||
| ) | ||
| backend.showAlert(alert, window: nil) { responseId in | ||
| self.alert = nil | ||
| self.scene.isPresented.wrappedValue = false | ||
| self.scene.actions[responseId].action() | ||
| } | ||
|
|
||
| self.alert = alert | ||
| } else if !scene.isPresented.wrappedValue, let alert { | ||
| backend.dismissAlert(alert as! Backend.Alert, window: nil) | ||
| self.alert = nil | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.