Skip to content

Commit 850cab3

Browse files
committed
Make detach chat window no persistent
1 parent 32c50f2 commit 850cab3

6 files changed

Lines changed: 23 additions & 154 deletions

File tree

Core/Sources/Preferences/Keys.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -175,17 +175,6 @@ public struct UserDefaultPreferenceKeys {
175175
public var preferWidgetToStayInsideEditorWhenWidthGreaterThan: PreferWidgetToStayInsideEditorWhenWidthGreaterThan {
176176
.init()
177177
}
178-
179-
// MARK: - Chat Panel in a Separate Window
180-
181-
public struct ChatPanelInASeparateWindow: UserDefaultPreferenceKey {
182-
public let defaultValue = true
183-
public let key = "ChatPanelInASeparateWindow"
184-
}
185-
186-
public var chatPanelInASeparateWindow: ChatPanelInASeparateWindow {
187-
.init()
188-
}
189178
}
190179

191180
// MARK: - OpenAI Account Settings

Core/Sources/SuggestionWidget/ChatWindowView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ final class ChatWindowViewModel: ObservableObject {
88
@Published var chat: ChatProvider?
99
@Published var colorScheme: ColorScheme
1010
@Published var isPanelDisplayed = false
11-
@AppStorage(\.chatPanelInASeparateWindow) var chatPanelInASeparateWindow
11+
@Published var chatPanelInASeparateWindow = false
1212

1313
public init(chat: ChatProvider? = nil, colorScheme: ColorScheme = .dark) {
1414
self.chat = chat

Core/Sources/SuggestionWidget/SuggestionPanelView.swift

Lines changed: 2 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,14 @@ final class SuggestionPanelViewModel: ObservableObject {
2222

2323
enum ActiveTab {
2424
case suggestion
25-
case chat
2625
}
27-
28-
@AppStorage(\.chatPanelInASeparateWindow) var chatPanelInASeparateWindow
2926

3027
@Published var content: Content? {
3128
didSet {
32-
adjustActiveTabAndShowHideIfNeeded(tab: .suggestion)
3329
requestApplicationPolicyUpdate?(self)
3430
}
3531
}
3632

37-
@Published var chat: ChatProvider? {
38-
didSet {
39-
adjustActiveTabAndShowHideIfNeeded(tab: .chat)
40-
}
41-
}
42-
4333
@Published var activeTab: ActiveTab {
4434
didSet {
4535
requestApplicationPolicyUpdate?(self)
@@ -54,51 +44,17 @@ final class SuggestionPanelViewModel: ObservableObject {
5444

5545
public init(
5646
content: Content? = nil,
57-
chat: ChatProvider? = nil,
5847
isPanelDisplayed: Bool = false,
5948
activeTab: ActiveTab = .suggestion,
6049
colorScheme: ColorScheme = .dark,
6150
requestApplicationPolicyUpdate: ((SuggestionPanelViewModel) -> Void)? = nil
6251
) {
6352
self.content = content
64-
self.chat = chat
6553
self.isPanelDisplayed = isPanelDisplayed
6654
self.activeTab = activeTab
6755
self.colorScheme = colorScheme
6856
self.requestApplicationPolicyUpdate = requestApplicationPolicyUpdate
6957
}
70-
71-
func adjustActiveTabAndShowHideIfNeeded(tab: ActiveTab) {
72-
if chatPanelInASeparateWindow {
73-
activeTab = .suggestion
74-
return
75-
}
76-
77-
switch tab {
78-
case .suggestion:
79-
if content != nil {
80-
activeTab = .suggestion
81-
return
82-
}
83-
case .chat:
84-
if chat != nil {
85-
activeTab = .chat
86-
return
87-
}
88-
}
89-
90-
if content != nil {
91-
activeTab = .suggestion
92-
return
93-
}
94-
95-
if chat != nil {
96-
activeTab = .chat
97-
return
98-
}
99-
100-
activeTab = .suggestion
101-
}
10258
}
10359

10460
struct SuggestionPanelView: View {
@@ -130,15 +86,6 @@ struct SuggestionPanelView: View {
13086
.allowsHitTesting(viewModel.isPanelDisplayed)
13187
}
13288
}
133-
134-
// if let chat = viewModel.chat {
135-
// if case .chat = viewModel.activeTab {
136-
// ChatPanel(chat: chat)
137-
// .frame(maxWidth: .infinity, maxHeight: Style.panelHeight)
138-
// .fixedSize(horizontal: false, vertical: true)
139-
// .allowsHitTesting(viewModel.isPanelDisplayed)
140-
// }
141-
// }
14289
}
14390
.frame(maxWidth: .infinity)
14491

@@ -151,7 +98,7 @@ struct SuggestionPanelView: View {
15198
.preferredColorScheme(viewModel.colorScheme)
15299
.opacity({
153100
guard viewModel.isPanelDisplayed else { return 0 }
154-
guard viewModel.content != nil || viewModel.chat != nil else { return 0 }
101+
guard viewModel.content != nil else { return 0 }
155102
return 1
156103
}())
157104
.animation(.easeInOut(duration: 0.2), value: viewModel.content?.contentHash)
@@ -193,59 +140,6 @@ struct SuggestionPanelView_Error_Preview: PreviewProvider {
193140
}
194141
}
195142

196-
struct SuggestionPanelView_Chat_Preview: PreviewProvider {
197-
static var previews: some View {
198-
SuggestionPanelView(viewModel: .init(
199-
chat: .init(
200-
history: [
201-
.init(id: "1", isUser: true, text: "Hello"),
202-
.init(id: "2", isUser: false, text: "Hi"),
203-
.init(id: "3", isUser: true, text: "What's up?"),
204-
]
205-
),
206-
isPanelDisplayed: true,
207-
activeTab: .chat
208-
))
209-
.frame(width: 450, height: 300)
210-
}
211-
}
212-
213-
struct SuggestionPanelView_Both_DisplayingChat_Preview: PreviewProvider {
214-
static var previews: some View {
215-
SuggestionPanelView(viewModel: .init(
216-
content: .suggestion(SuggestionProvider(
217-
code: """
218-
- (void)addSubview:(UIView *)view {
219-
[self addSubview:view];
220-
}
221-
""",
222-
language: "objective-c",
223-
startLineIndex: 8,
224-
suggestionCount: 2,
225-
currentSuggestionIndex: 0
226-
)),
227-
chat: .init(
228-
history: [
229-
.init(id: "1", isUser: true, text: "Hello"),
230-
.init(id: "2", isUser: false, text: "Hi"),
231-
.init(id: "3", isUser: true, text: "What's up?"),
232-
]
233-
),
234-
isPanelDisplayed: true,
235-
activeTab: .chat,
236-
colorScheme: .light
237-
))
238-
.frame(width: 450, height: 500)
239-
.background {
240-
HStack {
241-
Color.red
242-
Color.green
243-
Color.blue
244-
}
245-
}
246-
}
247-
}
248-
249143
struct SuggestionPanelView_Both_DisplayingSuggestion_Preview: PreviewProvider {
250144
static var previews: some View {
251145
SuggestionPanelView(viewModel: .init(
@@ -260,13 +154,6 @@ struct SuggestionPanelView_Both_DisplayingSuggestion_Preview: PreviewProvider {
260154
suggestionCount: 2,
261155
currentSuggestionIndex: 0
262156
)),
263-
chat: .init(
264-
history: [
265-
.init(id: "1", isUser: true, text: "Hello"),
266-
.init(id: "2", isUser: false, text: "Hi"),
267-
.init(id: "3", isUser: true, text: "What's up?"),
268-
]
269-
),
270157
isPanelDisplayed: true,
271158
activeTab: .suggestion,
272159
colorScheme: .dark
@@ -281,3 +168,4 @@ struct SuggestionPanelView_Both_DisplayingSuggestion_Preview: PreviewProvider {
281168
}
282169
}
283170
}
171+

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import ActiveApplicationMonitor
22
import AppKit
33
import AsyncAlgorithms
44
import AXNotificationStream
5+
import Combine
56
import Environment
67
import Preferences
78
import SwiftUI
@@ -120,12 +121,12 @@ public final class SuggestionWidgetController: NSObject {
120121

121122
private var presentationModeChangeObserver = UserDefaultsObserver()
122123
private var colorSchemeChangeObserver = UserDefaultsObserver()
123-
private var detachChatPanelObserver = UserDefaultsObserver()
124124
private var windowChangeObservationTask: Task<Void, Error>?
125125
private var activeApplicationMonitorTask: Task<Void, Error>?
126126
private var sourceEditorMonitorTask: Task<Void, Error>?
127127
private var currentFileURL: URL?
128128
private var colorScheme: ColorScheme = .light
129+
private var cancellable = Set<AnyCancellable>()
129130

130131
public var onOpenChatClicked: () -> Void = {}
131132
public var onCustomCommandClicked: (CustomCommand) -> Void = { _ in }
@@ -160,7 +161,7 @@ public final class SuggestionWidgetController: NSObject {
160161
self.widgetWindow.alphaValue = 0
161162
self.panelWindow.alphaValue = 0
162163
self.tabWindow.alphaValue = 0
163-
if !UserDefaults.shared.value(for: \.chatPanelInASeparateWindow) {
164+
if !chatWindowViewModel.chatPanelInASeparateWindow {
164165
self.chatWindow.alphaValue = 0
165166
}
166167
}
@@ -184,16 +185,13 @@ public final class SuggestionWidgetController: NSObject {
184185
}
185186

186187
Task { @MainActor in
187-
detachChatPanelObserver.onChange = { [weak self] in
188-
guard let self else { return }
189-
self.updateWindowLocation(animated: true)
190-
}
191-
UserDefaults.shared.addObserver(
192-
detachChatPanelObserver,
193-
forKeyPath: UserDefaultPreferenceKeys().chatPanelInASeparateWindow.key,
194-
options: .new,
195-
context: nil
196-
)
188+
chatWindowViewModel.$chatPanelInASeparateWindow.dropFirst().removeDuplicates()
189+
.sink { [weak self] _ in
190+
guard let self else { return }
191+
Task { @MainActor in
192+
self.updateWindowLocation(animated: true)
193+
}
194+
}.store(in: &cancellable)
197195
}
198196

199197
Task { @MainActor in
@@ -280,9 +278,8 @@ public extension SuggestionWidgetController {
280278
if let chat = await dataSource?.chatForFile(at: fileURL) {
281279
chatWindowViewModel.chat = chat
282280
chatWindowViewModel.isPanelDisplayed = true
283-
suggestionPanelViewModel.chat = chat
284281

285-
if UserDefaults.shared.value(for: \.chatPanelInASeparateWindow) {
282+
if chatWindowViewModel.chatPanelInASeparateWindow {
286283
self.updateWindowLocation()
287284
}
288285

@@ -444,7 +441,7 @@ extension SuggestionWidgetController {
444441
return
445442
}
446443

447-
let detachChat = UserDefaults.shared.value(for: \.chatPanelInASeparateWindow)
444+
let detachChat = chatWindowViewModel.chatPanelInASeparateWindow
448445

449446
if let widgetFrames = {
450447
if let xcode = ActiveApplicationMonitor.latestXcode {
@@ -571,19 +568,14 @@ extension SuggestionWidgetController {
571568
}() else {
572569
suggestionPanelViewModel.content = nil
573570
chatWindowViewModel.chat = nil
574-
suggestionPanelViewModel.chat = nil
575571
return
576572
}
577573

578574
if let chat = await dataSource?.chatForFile(at: fileURL) {
579-
if suggestionPanelViewModel.chat?.id != chat.id {
580-
suggestionPanelViewModel.chat = chat
581-
}
582575
if chatWindowViewModel.chat?.id != chat.id {
583576
chatWindowViewModel.chat = chat
584577
}
585578
} else {
586-
suggestionPanelViewModel.chat = nil
587579
chatWindowViewModel.chat = nil
588580
}
589581

@@ -604,7 +596,7 @@ extension SuggestionWidgetController: NSWindowDelegate {
604596
guard (notification.object as? NSWindow) === chatWindow else { return }
605597
Task { @MainActor in
606598
await Task.yield()
607-
UserDefaults.shared.set(true, for: \.chatPanelInASeparateWindow)
599+
chatWindowViewModel.chatPanelInASeparateWindow = true
608600
}
609601
}
610602

Core/Sources/SuggestionWidget/TabView.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import SwiftUI
22

33
struct TabView: View {
44
@ObservedObject var chatWindowViewModel: ChatWindowViewModel
5-
@AppStorage(\.chatPanelInASeparateWindow) var chatPanelInASeparateWindow
65

76
var body: some View {
87
Button(action: {
@@ -16,7 +15,7 @@ struct TabView: View {
1615
)
1716
})
1817
.buttonStyle(.plain)
19-
.opacity(chatPanelInASeparateWindow ? 1 : 0)
18+
.opacity(chatWindowViewModel.chatPanelInASeparateWindow ? 1 : 0)
2019
.preferredColorScheme(chatWindowViewModel.colorScheme)
2120
.frame(maxWidth: Style.widgetWidth, maxHeight: Style.widgetHeight)
2221
}

Core/Sources/SuggestionWidget/WidgetView.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct WidgetView: View {
3535
let minimumLineWidth: Double = 4
3636
let lineWidth = (1 - processingProgress) * 28 + minimumLineWidth
3737
let scale = max(processingProgress * 1, 0.0001)
38-
let empty = panelViewModel.content == nil && panelViewModel.chat == nil
38+
let empty = panelViewModel.content == nil && chatWindowViewModel.chat == nil
3939

4040
ZStack {
4141
Circle()
@@ -81,6 +81,7 @@ struct WidgetView: View {
8181
}
8282
}.contextMenu {
8383
WidgetContextMenu(
84+
chatWindowViewModel: chatWindowViewModel,
8485
widgetViewModel: viewModel,
8586
isChatOpen: chatWindowViewModel.isPanelDisplayed
8687
&& chatWindowViewModel.chat != nil,
@@ -96,7 +97,7 @@ struct WidgetView: View {
9697
if viewModel.isProcessing {
9798
processingProgress = 1 - processingProgress
9899
} else {
99-
let empty = panelViewModel.content == nil && panelViewModel.chat == nil
100+
let empty = panelViewModel.content == nil && chatWindowViewModel.chat == nil
100101
processingProgress = empty ? 0 : 1
101102
}
102103
}
@@ -112,7 +113,7 @@ struct WidgetContextMenu: View {
112113
@AppStorage(\.disableSuggestionFeatureGlobally) var disableSuggestionFeatureGlobally
113114
@AppStorage(\.suggestionFeatureEnabledProjectList) var suggestionFeatureEnabledProjectList
114115
@AppStorage(\.customCommands) var customCommands
115-
@AppStorage(\.chatPanelInASeparateWindow) var chatPanelInASeparateWindow
116+
@ObservedObject var chatWindowViewModel: ChatWindowViewModel
116117
@ObservedObject var widgetViewModel: WidgetViewModel
117118
@State var projectPath: String?
118119
var isChatOpen: Bool
@@ -137,10 +138,10 @@ struct WidgetContextMenu: View {
137138

138139
Group { // Settings
139140
Button(action: {
140-
chatPanelInASeparateWindow.toggle()
141+
chatWindowViewModel.chatPanelInASeparateWindow.toggle()
141142
}) {
142143
Text("Detach Chat Panel")
143-
if chatPanelInASeparateWindow {
144+
if chatWindowViewModel.chatPanelInASeparateWindow {
144145
Image(systemName: "checkmark")
145146
}
146147
}

0 commit comments

Comments
 (0)