-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatGPTForXcodeApp.swift
More file actions
110 lines (95 loc) · 2.99 KB
/
ChatGPTForXcodeApp.swift
File metadata and controls
110 lines (95 loc) · 2.99 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
//
// ChatGPTForXcodeApp.swift
// ChatGPTForXcode
//
// Created by TAISHIN MIYAMOTO on 2023/03/08
//
//
import SwiftUI
@main
struct ChatGPTForXcodeApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate : AppDelegate
var body: some Scene {
MenuBarExtra {
Button {
NSApplication.shared.terminate(self)
} label: {
Text("Quit")
}
} label: {
Image(systemName: "bubble.left.fill")
}
}
}
// chat-gpt-for-xcode://
class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
openConfigurationView()
}
func applicationShouldHandleReopen(_ sender: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
if !flag {
openConfigurationView()
}
return true
}
func application(_ application: NSApplication, open urls: [URL]) {
print(urls)
if let url = urls.first {
openChatPanel(url: url)
}
}
private func openConfigurationView() {
if NSApplication.shared.windows.filter({$0.identifier == .init("configurationWindow")}).first != nil {
return
}
let view = ConfigurationView()
let controller = NSHostingController(rootView: view)
let window = NSPanel(
contentRect: .zero,
styleMask: [
.closable,
.miniaturizable,
.titled,
],
backing: .buffered,
defer: false)
window.identifier = .init("configurationWindow")
window.contentViewController = controller
window.toolbarStyle = .unified
window.toolbar = .init()
window.center()
window.makeKeyAndOrderFront(nil)
}
let chatViewModel = ChatViewModel()
private func openChatPanel(url: URL) {
guard let query = url.query,
let text = query.removingPercentEncoding
else { return }
chatViewModel.messages.append(.init(text: text))
if NSApplication.shared.windows.filter({$0.identifier == .init("chatPanel")}).first != nil {
return
}
let view = ChatView(viewModel: self.chatViewModel)
let controller = NSHostingController(rootView: view)
let panel = NSPanel(
contentRect: .zero,
styleMask: [
.closable,
.miniaturizable,
.nonactivatingPanel,
.titled,
.resizable
],
backing: .buffered,
defer: false)
panel.identifier = .init("chatPanel")
panel.contentViewController = controller
panel.level = .floating
panel.collectionBehavior = [
.canJoinAllSpaces,
.fullScreenAuxiliary
]
panel.center()
panel.makeKeyAndOrderFront(nil)
}
}