mirror of
https://github.com/yattee/yattee.git
synced 2025-08-05 02:04:07 +00:00
Improve windows handling on macOS
This commit is contained in:
@@ -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
55
macOS/Windows.swift
Normal 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) {}
|
||||
}
|
Reference in New Issue
Block a user