yattee/macOS/Windows.swift

76 lines
1.5 KiB
Swift
Raw Normal View History

2022-01-06 15:35:45 +00:00
import AppKit
import Foundation
import SwiftUI
enum Windows: String, CaseIterable {
case player, main
static var mainWindow: NSWindow?
static var playerWindow: NSWindow?
weak var window: NSWindow? {
switch self {
case .player:
return Self.playerWindow
case .main:
return Self.mainWindow
}
}
2022-06-24 22:48:57 +00:00
var isOpen: Bool {
!window.isNil
}
2022-01-06 15:35:45 +00:00
func focus() {
window?.makeKeyAndOrderFront(self)
}
var location: String {
switch self {
case .player:
return rawValue
case .main:
return ""
}
}
func open() {
switch self {
case .player:
2022-03-27 11:42:38 +00:00
if let window = Self.playerWindow {
window.makeKeyAndOrderFront(self)
} else {
NSWorkspace.shared.open(URL(string: "yattee://\(location)")!)
}
2022-01-06 15:35:45 +00:00
case .main:
Self.main.focus()
}
}
2022-04-03 12:23:42 +00:00
2022-08-26 20:17:21 +00:00
func hide() {
window?.close()
}
2022-04-03 12:23:42 +00:00
func toggleFullScreen() {
window?.toggleFullScreen(nil)
}
2022-08-26 20:17:21 +00:00
var visible: Bool {
window?.isVisible ?? false
}
2022-01-06 15:35:45 +00:00
}
struct HostingWindowFinder: NSViewRepresentable {
var callback: (NSWindow?) -> Void
func makeNSView(context _: Self.Context) -> NSView {
let view = NSView()
DispatchQueue.main.async { [weak view] in
self.callback(view?.window)
}
return view
}
func updateNSView(_: NSView, context _: Context) {}
}