forked from github/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiffViewWindowController.swift
More file actions
160 lines (129 loc) · 5 KB
/
DiffViewWindowController.swift
File metadata and controls
160 lines (129 loc) · 5 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import SwiftUI
import ChatService
import ComposableArchitecture
import WebKit
import ChatAPIService
enum Style {
/// default diff view frame. Same as the `ChatPanel`
static let diffViewHeight: Double = 560
static let diffViewWidth: Double = 504
}
class DiffViewWindowController: NSObject, NSWindowDelegate {
enum DiffViewerState {
case shown, closed
}
private var diffWindow: NSWindow?
private var hostingView: NSHostingView<DiffView>?
private weak var chat: StoreOf<Chat>?
public private(set) var currentFileEdit: FileEdit? = nil
public private(set) var diffViewerState: DiffViewerState = .closed
public init(chat: StoreOf<Chat>) {
self.chat = chat
}
deinit {
// Break the delegate cycle
diffWindow?.delegate = nil
// Close and release the wi
diffWindow?.close()
diffWindow = nil
// Clear hosting view
hostingView = nil
// Reset state
currentFileEdit = nil
diffViewerState = .closed
}
@MainActor
func showDiffWindow(fileEdit: FileEdit) {
guard let chat else { return }
currentFileEdit = fileEdit
// Create diff view
let newDiffView = DiffView(chat: chat, fileEdit: fileEdit)
if let window = diffWindow, let _ = hostingView {
window.title = "Diff View"
let newHostingView = NSHostingView(rootView: newDiffView)
// Ensure the hosting view fills the window
newHostingView.translatesAutoresizingMaskIntoConstraints = false
self.hostingView = newHostingView
window.contentView = newHostingView
// Set constraints to fill the window
if let contentView = window.contentView {
newHostingView.frame = contentView.bounds
newHostingView.autoresizingMask = [.width, .height]
}
window.makeKeyAndOrderFront(nil)
} else {
let newHostingView = NSHostingView(rootView: newDiffView)
newHostingView.translatesAutoresizingMaskIntoConstraints = false
self.hostingView = newHostingView
let window = NSWindow(
contentRect: getDiffViewFrame(),
styleMask: [.titled, .closable, .miniaturizable, .resizable],
backing: .buffered,
defer: false
)
window.title = "Diff View"
window.contentView = newHostingView
// Set constraints to fill the window
if let contentView = window.contentView {
newHostingView.frame = contentView.bounds
newHostingView.autoresizingMask = [.width, .height]
}
window.center()
window.delegate = self
window.isReleasedWhenClosed = false
self.diffWindow = window
}
NSApp.activate(ignoringOtherApps: true)
diffWindow?.makeKeyAndOrderFront(nil)
diffViewerState = .shown
}
func windowWillClose(_ notification: Notification) {
if let window = notification.object as? NSWindow, window == diffWindow {
DispatchQueue.main.async {
self.diffWindow?.orderOut(nil)
}
}
}
@MainActor
func hideWindow() {
guard diffViewerState != .closed else { return }
diffWindow?.orderOut(nil)
diffViewerState = .closed
}
func getDiffViewFrame() -> NSRect {
guard let mainScreen = NSScreen.screens.first(where: { $0.frame.origin == .zero })
else {
/// default value
return .init(x: 0, y:0, width: Style.diffViewWidth, height: Style.diffViewHeight)
}
let visibleScreenFrame = mainScreen.visibleFrame
// avoid too wide
let width = min(Style.diffViewWidth, visibleScreenFrame.width * 0.3)
let height = visibleScreenFrame.height
return CGRect(x: 0, y: 0, width: width, height: height)
}
func windowDidResize(_ notification: Notification) {
if let window = notification.object as? NSWindow, window == diffWindow {
if let hostingView = self.hostingView,
let webView = findWebView(in: hostingView) {
let script = """
if (window.DiffViewer && window.DiffViewer.handleResize) {
window.DiffViewer.handleResize();
}
"""
webView.evaluateJavaScript(script)
}
}
}
private func findWebView(in view: NSView) -> WKWebView? {
if let webView = view as? WKWebView {
return webView
}
for subview in view.subviews {
if let webView = findWebView(in: subview) {
return webView
}
}
return nil
}
}