Created
February 26, 2024 18:31
-
-
Save phranck/24dcf45d2f5ff0543ed679d8a7841687 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#if os(macOS) | |
import SwiftUI | |
public extension View { | |
func hostingWindow(_ callback: @escaping (NSWindow?) -> Void) -> some View { | |
background(HostingWindowFinder(callback: callback)) | |
} | |
func windowTitle(_ title: String) -> some View { | |
hostingWindow { $0?.title = title } | |
} | |
func windowTabbingMode(_ tabbingMode: NSWindow.TabbingMode) -> some View { | |
hostingWindow { $0?.tabbingMode = tabbingMode } | |
} | |
func windowIsExcludedFromWindowsMenu(_ value: Bool) -> some View { | |
hostingWindow { $0?.isExcludedFromWindowsMenu = value } | |
} | |
func windowStandardWindowButton(_ buttonType: NSWindow.ButtonType, callback: @escaping (NSButton?) -> Void) -> some View { | |
hostingWindow { callback($0?.standardWindowButton(buttonType)) } | |
} | |
func windowStandardWindowButtons(_ buttonTypes: [NSWindow.ButtonType], enabled: Bool) -> some View { | |
hostingWindow { window in | |
buttonTypes.forEach { type in | |
window?.standardWindowButton(type)?.isEnabled = enabled | |
} | |
} | |
} | |
func windowStandardWindowButton(_ buttonType: NSWindow.ButtonType, isEnabled: Bool) -> some View { | |
windowStandardWindowButton(buttonType) { $0?.isEnabled = isEnabled } | |
} | |
func windowIsMovableByWindowBackground(_ value: Bool) -> some View { | |
hostingWindow { $0?.isMovableByWindowBackground = value } | |
} | |
func windowIsReleasedWhenClosed(_ value: Bool) -> some View { | |
hostingWindow { $0?.isReleasedWhenClosed = value } | |
} | |
} | |
private struct HostingWindowFinder: NSViewRepresentable { | |
private let callback: (NSWindow?) -> Void | |
init(callback: @escaping (NSWindow?) -> Void) { | |
self.callback = callback | |
} | |
func makeNSView(context: Self.Context) -> NSView { | |
let view = NSView() | |
DispatchQueue.main.async { [weak view] in | |
self.callback(view?.window) | |
} | |
return view | |
} | |
func updateNSView(_ nsView: NSView, context: Context) {} | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment