Improve windows handling on macOS

This commit is contained in:
Arkadiusz Fal
2022-01-06 16:35:45 +01:00
parent 3baa7a6893
commit 8a74938b98
7 changed files with 91 additions and 59 deletions

View File

@@ -1,40 +0,0 @@
import AppKit
import Foundation
enum OpenWindow: String, CaseIterable {
case player, main
var window: NSWindow? {
// this is not solid but works as long as there is only two windows in the app
// needs to be changed in case we ever have more windows to handle
switch self {
case .player:
return NSApplication.shared.windows.last
case .main:
return NSApplication.shared.windows.first
}
}
func focus() {
window?.makeKeyAndOrderFront(self)
}
var location: String {
switch self {
case .player:
return rawValue
case .main:
return ""
}
}
func open() {
switch self {
case .player:
NSWorkspace.shared.open(URL(string: "yattee://\(location)")!)
case .main:
Self.main.focus()
}
}
}

55
macOS/Windows.swift Normal file
View File

@@ -0,0 +1,55 @@
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
}
}
func focus() {
window?.makeKeyAndOrderFront(self)
}
var location: String {
switch self {
case .player:
return rawValue
case .main:
return ""
}
}
func open() {
switch self {
case .player:
NSWorkspace.shared.open(URL(string: "yattee://\(location)")!)
case .main:
Self.main.focus()
}
}
}
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) {}
}