Skip to content

Commit 2a2b5df

Browse files
committed
Update open chat to change it's behavior according to settings
1 parent fff67bf commit 2a2b5df

File tree

5 files changed

+64
-13
lines changed

5 files changed

+64
-13
lines changed

Core/Sources/Service/GUI/GraphicalUserInterfaceController.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -337,10 +337,7 @@ public final class GraphicalUserInterfaceController {
337337
}
338338

339339
public func openGlobalChat() {
340-
Task {
341-
await self.store.send(.createChatGPTChatTabIfNeeded).finish()
342-
store.send(.openChatPanel(forceDetach: true))
343-
}
340+
PseudoCommandHandler().openChat(forceDetach: true)
344341
}
345342
}
346343

Core/Sources/Service/SuggestionCommandHandler/PseudoCommandHandler.swift

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import ActiveApplicationMonitor
22
import AppKit
3+
import Dependencies
34
import Preferences
45
import SuggestionInjector
56
import SuggestionModel
@@ -210,7 +211,13 @@ struct PseudoCommandHandler {
210211
guard let focusElement = application.focusedElement,
211212
focusElement.description == "Source Editor"
212213
else { return }
213-
guard let (content, lines, _, cursorPosition, cursorOffset) = await getFileContent(sourceEditor: nil)
214+
guard let (
215+
content,
216+
lines,
217+
_,
218+
cursorPosition,
219+
cursorOffset
220+
) = await getFileContent(sourceEditor: nil)
214221
else {
215222
PresentInWindowSuggestionPresenter()
216223
.presentErrorMessage("Unable to get file content.")
@@ -266,7 +273,13 @@ struct PseudoCommandHandler {
266273
guard let focusElement = application.focusedElement,
267274
focusElement.description == "Source Editor"
268275
else { return }
269-
guard let (content, lines, _, cursorPosition, cursorOffset) = await getFileContent(sourceEditor: nil)
276+
guard let (
277+
content,
278+
lines,
279+
_,
280+
cursorPosition,
281+
cursorOffset
282+
) = await getFileContent(sourceEditor: nil)
270283
else {
271284
PresentInWindowSuggestionPresenter()
272285
.presentErrorMessage("Unable to get file content.")
@@ -301,12 +314,35 @@ struct PseudoCommandHandler {
301314
await filespace.reset()
302315
PresentInWindowSuggestionPresenter().discardSuggestion(fileURL: documentURL)
303316
}
304-
305-
func openChat() {
306-
Task { @MainActor in
307-
let store = Service.shared.guiController.store
308-
await store.send(.createChatGPTChatTabIfNeeded)
309-
await store.send(.openChatPanel(forceDetach: false))
317+
318+
func openChat(forceDetach: Bool) {
319+
switch UserDefaults.shared.value(for: \.openChatMode) {
320+
case .chatPanel:
321+
Task { @MainActor in
322+
let store = Service.shared.guiController.store
323+
await store.send(.createChatGPTChatTabIfNeeded).finish()
324+
store.send(.openChatPanel(forceDetach: false))
325+
}
326+
case .browser:
327+
let urlString = UserDefaults.shared.value(for: \.openChatInBrowserURL)
328+
let openInApp = UserDefaults.shared.value(for: \.openChatInBrowserInInAppBrowser)
329+
guard let url = URL(string: urlString) else {
330+
let alert = NSAlert()
331+
alert.messageText = "Invalid URL"
332+
alert.informativeText = "The URL provided is not valid."
333+
alert.alertStyle = .warning
334+
alert.runModal()
335+
return
336+
}
337+
338+
if openInApp {
339+
return
340+
} else {
341+
Task {
342+
@Dependency(\.openURL) var openURL
343+
await openURL(url)
344+
}
345+
}
310346
}
311347
}
312348
}

Core/Sources/Service/XPCService.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ public class XPCService: NSObject, XPCServiceProtocol {
145145
withReply reply: @escaping (Data?, Error?) -> Void
146146
) {
147147
let handler = PseudoCommandHandler()
148-
handler.openChat()
148+
handler.openChat(forceDetach: false)
149149
reply(nil, nil)
150150
}
151151

Tool/Sources/Preferences/Keys.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,18 @@ public extension UserDefaultPreferenceKeys {
473473
var keepFloatOnTopIfChatPanelAndXcodeOverlaps: PreferenceKey<Bool> {
474474
.init(defaultValue: true, key: "KeepFloatOnTopIfChatPanelAndXcodeOverlaps")
475475
}
476+
477+
var openChatMode: PreferenceKey<OpenChatMode> {
478+
.init(defaultValue: .chatPanel, key: "OpenChatMode")
479+
}
480+
481+
var openChatInBrowserURL: PreferenceKey<String> {
482+
.init(defaultValue: "", key: "OpenChatInBrowserURL")
483+
}
484+
485+
var openChatInBrowserInInAppBrowser: PreferenceKey<Bool> {
486+
.init(defaultValue: true, key: "OpenChatInBrowserInInAppBrowser")
487+
}
476488
}
477489

478490
// MARK: - Theme
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import Foundation
2+
3+
public enum OpenChatMode: String {
4+
case chatPanel
5+
case browser
6+
}

0 commit comments

Comments
 (0)