|
| 1 | +import AppKit |
| 2 | +import ChatTab |
| 3 | +import Combine |
| 4 | +import ComposableArchitecture |
| 5 | +import Foundation |
| 6 | +import SwiftUI |
| 7 | + |
| 8 | +final class ChatPanelWindow: NSWindow { |
| 9 | + override var canBecomeKey: Bool { true } |
| 10 | + override var canBecomeMain: Bool { true } |
| 11 | + |
| 12 | + private var cancellable: Set<AnyCancellable> = [] |
| 13 | + |
| 14 | + var minimizeWindow: () -> Void = {} |
| 15 | + |
| 16 | + init( |
| 17 | + store: StoreOf<ChatPanelFeature>, |
| 18 | + chatTabPool: ChatTabPool, |
| 19 | + minimizeWindow: @escaping () -> Void |
| 20 | + ) { |
| 21 | + self.minimizeWindow = minimizeWindow |
| 22 | + super.init( |
| 23 | + contentRect: .zero, |
| 24 | + styleMask: [.resizable, .titled, .miniaturizable, .fullSizeContentView], |
| 25 | + backing: .buffered, |
| 26 | + defer: false |
| 27 | + ) |
| 28 | + |
| 29 | + titleVisibility = .hidden |
| 30 | + addTitlebarAccessoryViewController({ |
| 31 | + let controller = NSTitlebarAccessoryViewController() |
| 32 | + let view = NSHostingView(rootView: ChatTitleBar(store: store)) |
| 33 | + controller.view = view |
| 34 | + view.frame = .init(x: 0, y: 0, width: 100, height: 40) |
| 35 | + controller.layoutAttribute = .right |
| 36 | + return controller |
| 37 | + }()) |
| 38 | + titlebarAppearsTransparent = true |
| 39 | + isReleasedWhenClosed = false |
| 40 | + isOpaque = false |
| 41 | + backgroundColor = .clear |
| 42 | + level = .init(NSWindow.Level.floating.rawValue + 1) |
| 43 | + collectionBehavior = [ |
| 44 | + .fullScreenAuxiliary, |
| 45 | + .transient, |
| 46 | + .fullScreenPrimary, |
| 47 | + .fullScreenAllowsTiling, |
| 48 | + ] |
| 49 | + hasShadow = true |
| 50 | + contentView = NSHostingView( |
| 51 | + rootView: ChatWindowView( |
| 52 | + store: store, |
| 53 | + toggleVisibility: { [weak self] isDisplayed in |
| 54 | + guard let self else { return } |
| 55 | + self.isPanelDisplayed = isDisplayed |
| 56 | + } |
| 57 | + ) |
| 58 | + .environment(\.chatTabPool, chatTabPool) |
| 59 | + ) |
| 60 | + setIsVisible(true) |
| 61 | + isPanelDisplayed = false |
| 62 | + |
| 63 | + let viewStore = ViewStore(store) |
| 64 | + viewStore.publisher |
| 65 | + .map(\.isDetached) |
| 66 | + .receive(on: DispatchQueue.main) |
| 67 | + .sink { [weak self] isDetached in |
| 68 | + guard let self else { return } |
| 69 | + if UserDefaults.shared.value(for: \.disableFloatOnTopWhenTheChatPanelIsDetached) { |
| 70 | + self.setFloatOnTop(!isDetached) |
| 71 | + } else { |
| 72 | + self.setFloatOnTop(true) |
| 73 | + } |
| 74 | + }.store(in: &cancellable) |
| 75 | + } |
| 76 | + |
| 77 | + func setFloatOnTop(_ isFloatOnTop: Bool) { |
| 78 | + let targetLevel: NSWindow.Level = isFloatOnTop |
| 79 | + ? .init(NSWindow.Level.floating.rawValue + 1) |
| 80 | + : .normal |
| 81 | + |
| 82 | + if targetLevel != level { |
| 83 | + level = targetLevel |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + var isWindowHidden: Bool = false { |
| 88 | + didSet { |
| 89 | + alphaValue = isPanelDisplayed && !isWindowHidden ? 1 : 0 |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + var isPanelDisplayed: Bool = false { |
| 94 | + didSet { |
| 95 | + alphaValue = isPanelDisplayed && !isWindowHidden ? 1 : 0 |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + override var alphaValue: CGFloat { |
| 100 | + didSet { |
| 101 | + ignoresMouseEvents = alphaValue <= 0 |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + override func miniaturize(_: Any?) { |
| 106 | + minimizeWindow() |
| 107 | + } |
| 108 | +} |
| 109 | + |
0 commit comments