import AppKit import PlakServices /// The one window: paper up to the top edge, no title, hidden instead of closed. public final class PageWindowController: NSWindowController, NSWindowDelegate { public static let frameName = "PlakPage" public let page: PageViewController /// Hides Plak on Esc, ⌘W and the close button; without it the window only orders out. public var onHide: (() -> Void)? public init(store: PageStore, now: @escaping () -> Date = Date.init) { page = PageViewController(store: store, now: now) let window = NSWindow( contentRect: NSRect(x: 0, y: 0, width: 920, height: 760), styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], backing: .buffered, defer: false ) window.title = "Plak" window.titleVisibility = .hidden window.titlebarAppearsTransparent = true window.appearance = NSAppearance(named: .aqua) window.backgroundColor = Theme.paper window.isReleasedWhenClosed = false window.minSize = NSSize(width: 420, height: 320) window.collectionBehavior = [.moveToActiveSpace, .fullScreenAuxiliary] window.contentViewController = page if !window.setFrameUsingName(Self.frameName) { window.setContentSize(NSSize(width: 920, height: 760)) window.center() } window.setFrameAutosaveName(Self.frameName) super.init(window: window) window.delegate = self page.textView.onHide = { [weak self] in self?.hide() } } @available(*, unavailable) required init?(coder: NSCoder) { fatalError("init(coder:) is not supported") } /// Shows the window scrolled to the end of the page: at launch and on a reopen from Spotlight or Finder. public func showAtEnd() { showWindow(nil) bringToFront() page.showEnd() } /// Shows the window again where it was, without scrolling. public func reveal() { bringToFront() } /// orderFrontRegardless puts the window in front even while the activation of the app still lags. private func bringToFront() { window?.makeKeyAndOrderFront(nil) window?.orderFrontRegardless() } public func windowShouldClose(_ sender: NSWindow) -> Bool { hide() return false } private func hide() { if let onHide { onHide() } else { window?.orderOut(nil) } } }