Is using to SwiftUI entry point and using NSViewControllerRepresentable to add NSViewController to hirarchy right approach ?

In macOS application, we are using SwiftUI as an entry point to our application and attaching appdelegate using NSApplicationDelegateAdaptor. We are using NSViewControllerRepresentable to add a View Controller to the hiracrchy so that we can store intance of viewcontroller and add content to it programatically .

@main
struct TWMainApp: App {
    
   @NSApplicationDelegateAdaptor private var appDelegate: TWAppDelegate
    
    internal var body : some Scene {

        TWInitialScene ()
   
   }
    
}

TWInitialScene :

public struct TWInitialScene : Scene {

    public var body : some Scene {
        WindowGroup {

            TWInitialView ()
                
        }
    }
}

TWInitialView :

struct TWInitialView : View {
    
    @Environment(\.scenePhase) private var scenePhase
    
    
    var body : some View {
        
        TWAppKitToSwiftUIBridge ()
        
        
    }
    
}

TWAppKitToSwiftUIBridge :

struct TWNSKitToSwiftUIBridge : NSViewControllerRepresentable {
    func makeNSViewController(context: Context) -> TWNSViewController {
        let view_hierarchy : TWNSViewController
        
         
        view_hierarchy = TWStaticContext.sViewController
        
        return view_hierarchy
    }
    
    func updateNSViewController(_ nsViewController: TWNSViewController, context: Context) {
        
    }
}


@objc
public class TWStaticContext : NSObject
{
    public static let sViewController = TWNSViewController ()
    
    public override init () {}
    
    @objc
    public static func GetViewController () -> TWNSViewController
    {
        return TWStaticContext.sViewController
    }
}

public class TWNSViewController : NSViewController {
  
    override public func viewDidLoad ()
    {
        super.viewDidLoad ()
        
    }
}

To add content to the hirarchy we are accessing viewcontroller's intance and adding content to it like this :

public func PaintInitialScreen () {
        
        let label = NSTextField(labelWithString: "TW window")
        
        label.frame = NSRect(x: 100, y: 200, width: 200, height: 200)
        
        // Adding content to viewcontroller
        TWStaticContext.sViewController.view.addSubview(label)
    
        
    }

We are using this approach because we have a contraint in our application that we have to update UI programatically and on compile time we dont know what we want to show . We will be adding content on runtime based on how many button we want, what label we want , where to place it etc.

When we were using purely appKit application, doing things programatically was simple but since SwiftUI is a declarative application we have to use above approach.

Rational for shifting to SwiftUI entry point is that we want our application to be future safe and since apple is more inclined to SwiffUI, we want to design our entry flow to use SwiftUI entry point . And SwiftUI being declarative, we are using appKit to add content to hiracrchy programtically.

We have used similar apprach in iOS also , where are using UIApplicationDelegateAdaptor inplace of NSApplicationAdaptor . And UIViewControllerReprestable in place of NSViewControllerRepresentable.

Is this right approach to use ?

You should keep in mind that SwiftUI controls the layout of the UIKit/AppKit view controller’s view using the view’s center, bounds, frame, and transform properties. So don’t directly set these layout-related properties on the view managed by a UIViewControllerRepresentable instance from your own code because that conflicts with SwiftUI and results in undefined behavior.

Is using to SwiftUI entry point and using NSViewControllerRepresentable to add NSViewController to hirarchy right approach ?
 
 
Q