|
1 | 1 | import AppKit |
| 2 | +import ChatTab |
| 3 | +import ComposableArchitecture |
2 | 4 | import Environment |
| 5 | +import Preferences |
3 | 6 | import SuggestionWidget |
4 | 7 |
|
5 | | -@MainActor |
6 | | -public final class GraphicalUserInterfaceController { |
7 | | - public nonisolated static let shared = GraphicalUserInterfaceController() |
8 | | - nonisolated let suggestionWidget = SuggestionWidgetController() |
9 | | - private nonisolated init() { |
10 | | - Task { @MainActor in |
11 | | - suggestionWidget.dependency.suggestionWidgetDataSource = WidgetDataSource.shared |
12 | | - suggestionWidget.dependency.onOpenChatClicked = { [weak self] in |
13 | | - Task { |
14 | | - let uri = try await Environment.fetchFocusedElementURI() |
15 | | - let dataSource = WidgetDataSource.shared |
16 | | - await dataSource.createChatIfNeeded(for: uri) |
17 | | - self?.suggestionWidget.presentChatRoom() |
| 8 | +struct GUI: ReducerProtocol { |
| 9 | + struct State: Equatable { |
| 10 | + var suggestionWidgetState = WidgetFeature.State() |
| 11 | + |
| 12 | + var chatTabGroup: ChatPanelFeature.ChatTabGroup { |
| 13 | + get { suggestionWidgetState.chatPanelState.chatTapGroup } |
| 14 | + set { suggestionWidgetState.chatPanelState.chatTapGroup = newValue } |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + enum Action { |
| 19 | + case openChatPanel(forceDetach: Bool) |
| 20 | + case createChatGPTChatTabIfNeeded |
| 21 | + case sendCustomCommandToActiveChat(CustomCommand) |
| 22 | + |
| 23 | + case suggestionWidget(WidgetFeature.Action) |
| 24 | + } |
| 25 | + |
| 26 | + var body: some ReducerProtocol<State, Action> { |
| 27 | + Scope(state: \.suggestionWidgetState, action: /Action.suggestionWidget) { |
| 28 | + WidgetFeature() |
| 29 | + } |
| 30 | + |
| 31 | + Scope( |
| 32 | + state: \.chatTabGroup, |
| 33 | + action: /Action.suggestionWidget .. /WidgetFeature.Action.chatPanel |
| 34 | + ) { |
| 35 | + Reduce { _, action in |
| 36 | + switch action { |
| 37 | + case let .createNewTapButtonClicked(type): |
| 38 | + _ = type // always ChatGPTChatTab at the moment. |
| 39 | + let chatTap = ChatGPTChatTab() |
| 40 | + return .run { send in |
| 41 | + await send(.appendAndSelectTab(chatTap)) |
| 42 | + } |
| 43 | + |
| 44 | + default: |
| 45 | + return .none |
18 | 46 | } |
19 | 47 | } |
20 | | - suggestionWidget.dependency.onCustomCommandClicked = { command in |
21 | | - Task { |
22 | | - let commandHandler = PseudoCommandHandler() |
23 | | - await commandHandler.handleCustomCommand(command) |
| 48 | + } |
| 49 | + |
| 50 | + Reduce { state, action in |
| 51 | + switch action { |
| 52 | + case let .openChatPanel(forceDetach): |
| 53 | + return .run { send in |
| 54 | + await send( |
| 55 | + .suggestionWidget(.chatPanel(.presentChatPanel(forceDetach: forceDetach))) |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + case .createChatGPTChatTabIfNeeded: |
| 60 | + if state.chatTabGroup.tabs.contains(where: { $0 is ChatGPTChatTab }) { |
| 61 | + return .none |
| 62 | + } |
| 63 | + let chatTab = ChatGPTChatTab() |
| 64 | + state.chatTabGroup.tabs.append(chatTab) |
| 65 | + return .none |
| 66 | + |
| 67 | + case let .sendCustomCommandToActiveChat(command): |
| 68 | + @Sendable func stopAndHandleCommand(_ tab: ChatGPTChatTab) async { |
| 69 | + if tab.service.isReceivingMessage { |
| 70 | + await tab.service.stopReceivingMessage() |
| 71 | + } |
| 72 | + try? await tab.service.handleCustomCommand(command) |
| 73 | + } |
| 74 | + |
| 75 | + if let activeTab = state.chatTabGroup.activeChatTab as? ChatGPTChatTab { |
| 76 | + return .run { send in |
| 77 | + await stopAndHandleCommand(activeTab) |
| 78 | + await send(.openChatPanel(forceDetach: false)) |
| 79 | + } |
24 | 80 | } |
| 81 | + |
| 82 | + if let chatTab = state.chatTabGroup.tabs.first(where: { |
| 83 | + guard $0 is ChatGPTChatTab else { return false } |
| 84 | + return true |
| 85 | + }) as? ChatGPTChatTab { |
| 86 | + state.chatTabGroup.selectedTabId = chatTab.id |
| 87 | + return .run { send in |
| 88 | + await stopAndHandleCommand(chatTab) |
| 89 | + await send(.openChatPanel(forceDetach: false)) |
| 90 | + } |
| 91 | + } |
| 92 | + let chatTab = ChatGPTChatTab() |
| 93 | + state.chatTabGroup.tabs.append(chatTab) |
| 94 | + return .run { send in |
| 95 | + await stopAndHandleCommand(chatTab) |
| 96 | + await send(.openChatPanel(forceDetach: false)) |
| 97 | + } |
| 98 | + |
| 99 | + case .suggestionWidget: |
| 100 | + return .none |
25 | 101 | } |
26 | 102 | } |
27 | 103 | } |
28 | | - |
| 104 | +} |
| 105 | + |
| 106 | +@MainActor |
| 107 | +public final class GraphicalUserInterfaceController { |
| 108 | + public static let shared = GraphicalUserInterfaceController() |
| 109 | + private let store: StoreOf<GUI> |
| 110 | + let widgetController: SuggestionWidgetController |
| 111 | + let widgetDataSource: WidgetDataSource |
| 112 | + let viewStore: ViewStoreOf<GUI> |
| 113 | + |
| 114 | + private init() { |
| 115 | + let suggestionDependency = SuggestionWidgetControllerDependency() |
| 116 | + let store = StoreOf<GUI>( |
| 117 | + initialState: .init(), |
| 118 | + reducer: GUI() |
| 119 | + ) { dependencies in |
| 120 | + dependencies.suggestionWidgetControllerDependency = suggestionDependency |
| 121 | + dependencies.suggestionWidgetUserDefaultsObservers = .init() |
| 122 | + } |
| 123 | + self.store = store |
| 124 | + viewStore = ViewStore(store) |
| 125 | + widgetDataSource = .init() |
| 126 | + |
| 127 | + widgetController = SuggestionWidgetController( |
| 128 | + store: store.scope( |
| 129 | + state: \.suggestionWidgetState, |
| 130 | + action: GUI.Action.suggestionWidget |
| 131 | + ), |
| 132 | + dependency: suggestionDependency |
| 133 | + ) |
| 134 | + |
| 135 | + suggestionDependency.suggestionWidgetDataSource = widgetDataSource |
| 136 | + suggestionDependency.onOpenChatClicked = { [weak self] in |
| 137 | + Task { [weak self] in |
| 138 | + await self?.viewStore.send(.createChatGPTChatTabIfNeeded).finish() |
| 139 | + self?.viewStore.send(.openChatPanel(forceDetach: false)) |
| 140 | + } |
| 141 | + } |
| 142 | + suggestionDependency.onCustomCommandClicked = { command in |
| 143 | + Task { |
| 144 | + let commandHandler = PseudoCommandHandler() |
| 145 | + await commandHandler.handleCustomCommand(command) |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + |
29 | 150 | public func openGlobalChat() { |
30 | | - UserDefaults.shared.set(true, for: \.useGlobalChat) |
31 | | - let dataSource = WidgetDataSource.shared |
32 | | - let fakeFileURL = URL(fileURLWithPath: "/") |
33 | 151 | Task { |
34 | | - await dataSource.createChatIfNeeded(for: fakeFileURL) |
35 | | - suggestionWidget.presentDetachedGlobalChat() |
| 152 | + await self.viewStore.send(.createChatGPTChatTabIfNeeded).finish() |
| 153 | + viewStore.send(.openChatPanel(forceDetach: true)) |
36 | 154 | } |
37 | 155 | } |
38 | 156 | } |
| 157 | + |
0 commit comments