Skip to content

Commit 2541553

Browse files
committed
Add normal title bar to chat window
1 parent ffd258c commit 2541553

4 files changed

Lines changed: 44 additions & 53 deletions

File tree

Core/Sources/SuggestionWidget/ChatWindowView.swift

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ private let r: Double = 8
99

1010
struct ChatWindowView: View {
1111
let store: StoreOf<ChatPanelFeature>
12+
let toggleVisibility: (Bool) -> Void
1213

1314
struct OverallState: Equatable {
1415
var isPanelDisplayed: Bool
@@ -28,10 +29,6 @@ struct ChatWindowView: View {
2829
}
2930
) { viewStore in
3031
VStack(spacing: 0) {
31-
ChatTitleBar(store: store)
32-
33-
Divider()
34-
3532
ChatTabBar(store: store)
3633
.frame(height: 26)
3734

@@ -41,9 +38,9 @@ struct ChatWindowView: View {
4138
.frame(maxWidth: .infinity, maxHeight: .infinity)
4239
}
4340
.background(.regularMaterial)
44-
.xcodeStyleFrame()
45-
.opacity(viewStore.state.isPanelDisplayed ? 1 : 0)
46-
.frame(minWidth: Style.panelWidth, minHeight: Style.panelHeight)
41+
.onChange(of: viewStore.state.isPanelDisplayed) { isDisplayed in
42+
toggleVisibility(isDisplayed)
43+
}
4744
.preferredColorScheme(viewStore.state.colorScheme)
4845
}
4946
}
@@ -453,7 +450,7 @@ struct ChatWindowView_Previews: PreviewProvider {
453450
}
454451

455452
static var previews: some View {
456-
ChatWindowView(store: createStore())
453+
ChatWindowView(store: createStore(), toggleVisibility: { _ in })
457454
.xcodeStyleFrame()
458455
.padding()
459456
.environment(\.chatTabPool, pool)

Core/Sources/SuggestionWidget/FeatureReducers/WidgetFeature.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -581,9 +581,9 @@ public struct WidgetFeature: ReducerProtocol {
581581
windows.toastWindow.alphaValue = noFocus ? 0 : 1
582582

583583
if isChatPanelDetached {
584-
windows.chatPanelWindow.alphaValue = hasChat ? 1 : 0
584+
windows.chatPanelWindow.isWindowHidden = !hasChat
585585
} else {
586-
windows.chatPanelWindow.alphaValue = noFocus ? 0 : 1
586+
windows.chatPanelWindow.isWindowHidden = noFocus
587587
}
588588
} else if let activeApp, activeApp.isExtensionService {
589589
let noFocus = {
@@ -602,18 +602,18 @@ public struct WidgetFeature: ReducerProtocol {
602602
windows.widgetWindow.alphaValue = noFocus ? 0 : 1
603603
windows.toastWindow.alphaValue = noFocus ? 0 : 1
604604
if isChatPanelDetached {
605-
windows.chatPanelWindow.alphaValue = hasChat ? 1 : 0
605+
windows.chatPanelWindow.isWindowHidden = !hasChat
606606
} else {
607-
windows.chatPanelWindow.alphaValue = noFocus && !windows
608-
.chatPanelWindow.isKeyWindow ? 0 : 1
607+
windows.chatPanelWindow.isWindowHidden = noFocus && !windows
608+
.chatPanelWindow.isKeyWindow
609609
}
610610
} else {
611611
windows.sharedPanelWindow.alphaValue = 0
612612
windows.suggestionPanelWindow.alphaValue = 0
613613
windows.widgetWindow.alphaValue = 0
614614
windows.toastWindow.alphaValue = 0
615615
if !isChatPanelDetached {
616-
windows.chatPanelWindow.alphaValue = 0
616+
windows.chatPanelWindow.isWindowHidden = true
617617
}
618618
}
619619
}

Core/Sources/SuggestionWidget/ModuleDependency.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public final class WidgetWindows {
2424
var widgetWindow: NSWindow!
2525
var sharedPanelWindow: NSWindow!
2626
var suggestionPanelWindow: NSWindow!
27-
var chatPanelWindow: NSWindow!
27+
var chatPanelWindow: ChatWindow!
2828
var toastWindow: NSWindow!
2929

3030
nonisolated

Core/Sources/SuggestionWidget/SuggestionWidgetController.swift

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -122,26 +122,40 @@ public final class SuggestionWidgetController: NSObject {
122122
private lazy var chatPanelWindow = {
123123
let it = ChatWindow(
124124
contentRect: .zero,
125-
styleMask: [.resizable],
125+
styleMask: [.resizable, .titled, .miniaturizable],
126126
backing: .buffered,
127127
defer: false
128128
)
129+
it.minimizeWindow = { [weak self] in
130+
self?.store.send(.chatPanel(.hideButtonClicked))
131+
}
129132
it.isReleasedWhenClosed = false
130133
it.isOpaque = false
131134
it.backgroundColor = .clear
132135
it.level = .init(NSWindow.Level.floating.rawValue + 1)
133-
it.collectionBehavior = [.fullScreenAuxiliary, .transient]
136+
it.collectionBehavior = [
137+
.fullScreenAuxiliary,
138+
.transient,
139+
.fullScreenPrimary,
140+
.fullScreenAllowsTiling,
141+
if #available(macOS 13, *) { [.primary] }
142+
]
134143
it.hasShadow = true
135144
it.contentView = NSHostingView(
136145
rootView: ChatWindowView(
137146
store: store.scope(
138147
state: \.chatPanelState,
139148
action: WidgetFeature.Action.chatPanel
140-
)
149+
),
150+
toggleVisibility: { [weak it] isDisplayed in
151+
guard let window = it else { return }
152+
window.isPanelDisplayed = isDisplayed
153+
}
141154
)
142155
.environment(\.chatTabPool, chatTabPool)
143156
)
144157
it.setIsVisible(true)
158+
it.isPanelDisplayed = false
145159
it.delegate = self
146160
return it
147161
}()
@@ -249,31 +263,6 @@ extension SuggestionWidgetController: NSWindowDelegate {
249263
store.send(.chatPanel(.detachChatPanel))
250264
}
251265
}
252-
253-
public func windowDidBecomeKey(_ notification: Notification) {
254-
guard (notification.object as? NSWindow) === chatPanelWindow else { return }
255-
let screenFrame = NSScreen.screens.first(where: { $0.frame.origin == .zero })?
256-
.frame ?? .zero
257-
var mouseLocation = NSEvent.mouseLocation
258-
let windowFrame = chatPanelWindow.frame
259-
if mouseLocation.y > windowFrame.maxY - Style.chatWindowTitleBarHeight,
260-
mouseLocation.y < windowFrame.maxY,
261-
mouseLocation.x > windowFrame.minX,
262-
mouseLocation.x < windowFrame.maxX
263-
{
264-
mouseLocation.y = screenFrame.size.height - mouseLocation.y
265-
if let cgEvent = CGEvent(
266-
mouseEventSource: nil,
267-
mouseType: .leftMouseDown,
268-
mouseCursorPosition: mouseLocation,
269-
mouseButton: .left
270-
),
271-
let event = NSEvent(cgEvent: cgEvent)
272-
{
273-
chatPanelWindow.performDrag(with: event)
274-
}
275-
}
276-
}
277266
}
278267

279268
// MARK: - Window Subclasses
@@ -287,17 +276,22 @@ class CanBecomeKeyWindow: NSWindow {
287276
class ChatWindow: NSWindow {
288277
override var canBecomeKey: Bool { true }
289278
override var canBecomeMain: Bool { true }
290-
291-
override func mouseDown(with event: NSEvent) {
292-
let windowFrame = frame
293-
let currentLocation = event.locationInWindow
294-
if currentLocation.y > windowFrame.size.height - Style.chatWindowTitleBarHeight,
295-
currentLocation.y < windowFrame.size.height,
296-
currentLocation.x > 0,
297-
currentLocation.x < windowFrame.width
298-
{
299-
performDrag(with: event)
279+
280+
var minimizeWindow: () -> Void = {}
281+
282+
var isWindowHidden: Bool = false {
283+
didSet {
284+
alphaValue = isPanelDisplayed && !isWindowHidden ? 1 : 0
300285
}
301286
}
287+
var isPanelDisplayed: Bool = false {
288+
didSet {
289+
alphaValue = isPanelDisplayed && !isWindowHidden ? 1 : 0
290+
}
291+
}
292+
293+
override func miniaturize(_ sender: Any?) {
294+
minimizeWindow()
295+
}
302296
}
303297

0 commit comments

Comments
 (0)