forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetPanel.swift
More file actions
86 lines (70 loc) · 2.75 KB
/
Copy pathWidgetPanel.swift
File metadata and controls
86 lines (70 loc) · 2.75 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import AppKit
import ComposableArchitecture
import Foundation
@Reducer
public struct WidgetPanel {
@ObservableState
public struct State {
public var content: SharedPanel.Content {
get { sharedPanelState.content }
set { sharedPanelState.content = newValue }
}
// MARK: SharedPanel
var sharedPanelState = SharedPanel.State()
}
public enum Action {
case presentPromptToCode(PromptToCodePanel.State)
case displayPanelContent
case switchToAnotherEditorAndUpdateContent
case sharedPanel(SharedPanel.Action)
}
@Dependency(\.suggestionWidgetControllerDependency) var suggestionWidgetControllerDependency
@Dependency(\.xcodeInspector) var xcodeInspector
@Dependency(\.activateThisApp) var activateThisApp
var windows: WidgetWindows? { suggestionWidgetControllerDependency.windowsController?.windows }
public var body: some ReducerOf<Self> {
Scope(state: \.sharedPanelState, action: \.sharedPanel) {
SharedPanel()
}
Reduce { state, action in
switch action {
case let .presentPromptToCode(initialState):
return .run { send in
await send(.sharedPanel(.promptToCodeGroup(.createPromptToCode(
initialState,
sendImmediately: true
))))
}
case .displayPanelContent:
if !state.sharedPanelState.isEmpty {
state.sharedPanelState.isPanelDisplayed = true
}
return .none
case .switchToAnotherEditorAndUpdateContent:
return .run { send in
guard let fileURL = await xcodeInspector.safe.realtimeActiveDocumentURL
else { return }
await send(.sharedPanel(
.promptToCodeGroup(
.updateActivePromptToCode(documentURL: fileURL)
)
))
}
case .sharedPanel(.promptToCodeGroup(.activateOrCreatePromptToCode)),
.sharedPanel(.promptToCodeGroup(.createPromptToCode)):
let hasPromptToCode = state.content.promptToCode != nil
return .run { send in
await send(.displayPanelContent)
if hasPromptToCode {
activateThisApp()
await MainActor.run {
windows?.sharedPanelWindow.makeKey()
}
}
}.animation(.easeInOut(duration: 0.2))
case .sharedPanel:
return .none
}
}
}
}