I am considering of shifting my codebase from appkit to SwiftUI entry point. In Appkit, we get control on each NSWindow. So that we can hide/resize window, close window and controll when to present a specific window . Because i have access to NSWindow instance which i can store and perform these actions.
But when i shift to SwiftUI entry point, i declare struct conforming to SwiftUI Scene. And new windows will be created with the instance of this scene.
I am using NSViewControllerRepresentable to add a NSViewController to the hierarchy of these scene. And adding content to this NSViewController's instance to show on screen.
I need help in controlling the size of these windows. How can i close specific window ? Resize specific window ? or Hide specific window?
If i use purely SwiftUI view's , then i can do this by using the Enviorment propery and use DismissWindow to close a window or openWindow with identifier to open a specific window by passing the specificer .
But i am using Appkit's NSViewController where i will add buttons in heirarchy from which i want to trigger these events . And in that case how can i controll a specific window in case of multiwindow application?
Since you're using NSViewControllerRepresentable
, you can pass a closure that performs an action. The implementation of the closure would be defined by the SwiftUI view that renders the representable.
For example:
struct ContentView: View {
@Environment(\.dismissWindow) private var dismissWindow
var body: some View {
ViewRepresentable(
onOpenTapped: {
print("Open window")
},
onCloseTapped: {
print("Close window")
dismissWindow(id: "1")
}
)
}
}
struct ViewRepresentable: UIViewRepresentable {
var onOpenTapped: () -> Void
var onCloseTapped: () -> Void
func makeUIView(context: Context) -> WindowControlView {
let view = WindowControlView()
view.onOpenTapped = onOpenTapped
view.onCloseTapped = onCloseTapped
return view
}
func updateUIView(_ uiView: WindowControlView, context: Context) {
}
}