-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
637 additions
and
73 deletions.
There are no files selected for viewing
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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// NSWindowKey.swift | ||
// | ||
// | ||
// Created by Tatsuya Tanaka on 2023/02/14. | ||
// | ||
|
||
import SwiftUI | ||
|
||
private struct NSWindowKey: EnvironmentKey { | ||
static let defaultValue: NSWindow? = nil | ||
} | ||
|
||
extension EnvironmentValues { | ||
var nsWindow: NSWindow? { | ||
get { self[NSWindowKey.self] } | ||
set { self[NSWindowKey.self] = newValue } | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// | ||
// MacWindowManager.swift | ||
// | ||
// | ||
// Created by Tatsuya Tanaka on 2023/02/14. | ||
// | ||
|
||
import AppKit | ||
import SwiftUI | ||
import VCamEntity | ||
|
||
public protocol MacWindow: View { | ||
static var windowTitle: String { get } | ||
} | ||
|
||
public final class MacWindowManager { | ||
public static let shared = MacWindowManager() | ||
|
||
private var openWindows: [String: NSWindow] = [:] | ||
|
||
public func open<T: MacWindow>(_ windowView: T) { | ||
let id = String(describing: T.self) | ||
if let window = openWindows[id] { | ||
window.makeKeyAndOrderFront(nil) | ||
return | ||
} | ||
|
||
let window = NSWindow( | ||
contentRect: .init(origin: .zero, size: .init(width: 1, height: 400)), | ||
styleMask: [.titled, .closable, .fullSizeContentView], | ||
backing: .buffered, | ||
defer: false | ||
) | ||
window.isReleasedWhenClosed = false | ||
window.contentView = NSHostingView( | ||
rootView: WindowContainer(content: windowView) | ||
.environment(\.nsWindow, window) | ||
) | ||
window.title = T.windowTitle | ||
window.makeKeyAndOrderFront(nil) | ||
window.center() | ||
openWindows[id] = window | ||
|
||
var observation: Any? | ||
observation = NotificationCenter.default.addObserver(forName: NSWindow.willCloseNotification, object: nil, queue: .main) { notification in | ||
guard notification.object as? NSWindow == window else { return } | ||
Self.shared.openWindows.removeValue(forKey: id) | ||
if let observation { | ||
NotificationCenter.default.removeObserver(observation) | ||
} | ||
} | ||
} | ||
} | ||
|
||
private struct WindowContainer<Content: View>: View { | ||
let content: Content | ||
|
||
@AppStorage(key: .locale) var locale | ||
|
||
var body: some View { | ||
content | ||
.environment(\.locale, Locale(identifier: locale)) | ||
} | ||
} |
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
Oops, something went wrong.