SwiftUI NSViewRepresentable methods called twice when in the Toolbar

I'm creating a custom SwiftUI view using NSViewRepresentable. However, when the view is used inside a ToolbarItem, makeCoordinator and makeNSView get called twice, leading to glitches due to having an extra instance of the coordinator. If the custom view is moved out of the toolbar, those 2 methods only get called once each as expected.

Here is example code to reproduce:

import SwiftUI



struct ContentView: View {

    var body: some View {

        Text("Hello, world!")

            .padding()

            .toolbar(content: {

                ToolbarItem {

                    MyView()

                }

            })

    }

}



struct MyView: NSViewRepresentable {

    init() {

        print("init")

    }

    

    func makeNSView(context: Context) -> NSView {

        print("makeNSView")

        return NSView()

    }

    

    func updateNSView(_ nsView: NSView, context: Context) {

        print("updateNSView")

    }

    

    func makeCoordinator() -> Coordinator {

        print("makeCoordinator")

        return Coordinator()

    }

    typealias NSViewType = NSView

    class Coordinator {}
}

Which results in this log:

init
makeCoordinator
makeNSView
updateNSView
makeCoordinator
makeNSView
updateNSView

Does anybody else experience this bug?

SwiftUI NSViewRepresentable methods called twice when in the Toolbar
 
 
Q