Looking at having editable text as the detail view within a master detail interface.
There are various examples out there that show how to build the NSViewRepresentable
wrapper around NSTextView
.
e.g. https://developer.apple.com/forums/thread/125920
However, when I try to embed the NSViewRepresentable
within a NavigationSplitView
the various examples (and my own attempt) don't work properly. There seems to be a race condition competing to update the internal bindings. I also see updateNSView
being called multiple times when only one event, say selection change, is called once.
Using the SwiftUI TextEditor
view actually works. (it is just so limited)
When the master selected item is changed the change isn't automatically propagated through to update the Coordinator. That's because there isn't a delegate for "master did change".
Take any of the TextViews on their own (not within a split view) and they work as you would expect.
The SwiftUI TextEditor
does this without any obvious connection to the master.
So the first question is: has anybody solved this type of problem? I haven't yet found a link to the solution or a discussion on the internal trick to make this work.
In principle the view structure is:
NavigationSplitView {
// master list
List(selection : $selectedItem {
}
}
content: {
TextEditor(text: $selectedItem.text) // works
// or
CustomTextView(text: $selectedItem.text)
// all examples I've found so far fail to properly display changes to the text when edited
}
My current thought is that the internal Coordinator needs to know that there has been a selection change so you can synchronise the significant change in the contents of the text binding. Again TextEditor
doesn't need to know that. makeNSView
is only called once, so there isn't any regeneration of the NSTextView that you could rely on.
Have tried on both macOS and iOS and see the same results.