1
0
mirror of https://github.com/yattee/yattee.git synced 2024-12-15 14:50:32 +05:30
yattee/macOS/Windows.swift

68 lines
1.4 KiB
Swift
Raw Normal View History

2022-01-06 21:05:45 +05:30
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-25 04:18:57 +05:30
var isOpen: Bool {
!window.isNil
}
2022-01-06 21:05:45 +05:30
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 17:12:38 +05:30
if let window = Self.playerWindow {
window.makeKeyAndOrderFront(self)
} else {
NSWorkspace.shared.open(URL(string: "yattee://\(location)")!)
}
2022-01-06 21:05:45 +05:30
case .main:
Self.main.focus()
}
}
2022-04-03 17:53:42 +05:30
func toggleFullScreen() {
window?.toggleFullScreen(nil)
}
2022-01-06 21:05:45 +05:30
}
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) {}
}