-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathCommandsModifier.swift
More file actions
66 lines (57 loc) · 1.82 KB
/
CommandsModifier.swift
File metadata and controls
66 lines (57 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
extension Scene {
/// Adds menu commands to this scene.
///
/// The commands will typically be displayed in the system's global menu bar
/// if it has one, or in individual windows' menu bars otherwise.
///
/// - Parameter commands: The commands to add.
public func commands(@CommandsBuilder _ commands: () -> Commands) -> some Scene {
CommandsModifier(content: self, commands: commands())
}
}
struct CommandsModifier<Content: Scene>: Scene {
typealias Node = CommandsModifierNode<Content>
var content: Content
var commands: Commands
init(content: Content, commands: Commands) {
self.content = content
self.commands = commands
}
}
final class CommandsModifierNode<Content: Scene>: SceneGraphNode {
typealias NodeScene = CommandsModifier<Content>
var commands: Commands
var contentNode: Content.Node
init<Backend: AppBackend>(
from scene: NodeScene,
backend: Backend,
environment: EnvironmentValues
) {
self.commands = scene.commands
self.contentNode = Content.Node(
from: scene.content,
backend: backend,
environment: environment
)
}
func updateNode(
_ newScene: NodeScene?,
environment: EnvironmentValues
) -> SceneNodeUpdateResult {
if let newScene {
self.commands = newScene.commands
}
var result = contentNode.updateNode(newScene?.content, environment: environment)
result.preferences.commands = result.preferences.commands.overlayed(with: commands)
return result
}
func update<Backend: AppBackend>(
backend: Backend,
environment: EnvironmentValues
) {
contentNode.update(
backend: backend,
environment: environment
)
}
}