import AppKit

class MenulessWindow: NSWindow {
    init(viewController: NSViewController) {
        super.init(
            contentRect: CGRect(x: 0, y: 0, width: 100, height: 100),
            styleMask: [.titled, .closable],
            backing: .buffered,
            defer: false
        )
        super.contentViewController = viewController
        
        title = ""
        titlebarAppearsTransparent = true
        collectionBehavior = [.managed, .fullScreenNone]
        isReleasedWhenClosed = false
        hidesOnDeactivate = false
    }
    
    override func performKeyEquivalent(with event: NSEvent) -> Bool {
        let commandKey = NSEvent.ModifierFlags.command.rawValue
        
        if event.type == NSEvent.EventType.keyDown {
            if (event.modifierFlags.rawValue & NSEvent.ModifierFlags.deviceIndependentFlagsMask.rawValue) == commandKey,
               event.charactersIgnoringModifiers! == "w" {
                performClose(nil)
            }
        }
        return super.performKeyEquivalent(with: event)
    }
}
