Post

Replies

Boosts

Views

Activity

Reply to Clarification on SwiftUI Environment Write Performance
[quote='865440022, Engineer, /thread/806702?answerId=865440022#865440022'] However, in your example, since those environment variables wouldn't likely update very frequently (they're not really in a hot path) the cost of putting them in the environment is probably negligible. [/quote] Indeed, and now that I know that every write to any environment variable is not as egregious as I initially thought, then I'm a bit less concerned about this issue. That said, I do want to revisit this a bit. In your demo, you were worried about writes in the hot path during a critical operation like scrolling. What I'm finding on macOS isn't that I have "hot paths" but rather I have a lot of views being updated because of a very small change. These might be infrequent, the cost is high and noticeable. An example would be keeping track of a selected objects on a canvas such that a bunch of inspector panels and other panes need to update their state based as the selection changes. Selection changes are relatively infrequent (compared to scrolling) but a when they occur, a lot of views need to be updated. In my app, such a scenario is giving the application an overall "sluggish" feel as the user clicks around. I wouldn't say that you need to go back to all your uses of environment values and change them to use @Observable classes, but if you're seeing performance issues, or building something new, it's worth considering. One big issue I have with @Observable and @Environment is that I have to use a concrete type in @Environment if I want to be able to make an @Bindable reference. Ideally, I want the environment value to be generic. I can define it as being a protocol, but then I cannot convert the protocol to an @Bindable, or at least I don't know how. Specifically, the following doesn't work: struct Item { var frame: CGRect } protocol SelectionProvider: Observable { var selectedItem: Item? { get } } @Observable final class Document: SelectionProvider { var selectedItem: Item? } extension EnvironmentValues { @Entry var selectionProvider: (any SelectionProvider)? } struct SampleView: View { @Environment(\.selectionProvider) private var selectionProvider var body: some View { // Error: 'init(wrappedValue:)' is unavailable: // The wrapped value must be an object that conforms to Observable @Bindable var selectionProvider = selectionProvider } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’25
Reply to Clarification on SwiftUI Environment Write Performance
@StevenPeterson Thanks for chiming in and thanks for the video itself, much appreciated. No, the view bodies are only run if the body uses the environment value for the key(s) used in that view and the value changes. That makes a lot more sense to me and aligns with the simple demo app I made to test out my original question. Consider: struct ContentView: View { @Environment(\.name) var name @Environment(\.age) var age var body: some View { Text(name) } } If \.name is updated anywhere, then ContentView.body will be run but if \.age is updated anywhere, then body is not run because \.age is not referenced in the body. What worried me was that if somewhere else a view wrote to \.address, then ContentView would be re-evaluated even though it doesn't refer to \.address in any way. That's how I (incorrectly) interpreted the slide above. But in SwiftUI, an update doesn't always cause the view body to run again, but there is still a cost associated with these updates. That's the real clarifying statement, for me. There's a SwiftUI concept of "updating a view" that doesn't require calling body and there's a concept of "updating a view" that does require calling body. You're saying that the cost of "updating a view even if it doesn't call body" is something that we should still consider in performance sensitive areas like scrolling. (ie: The cost is less than calling .body but it's not "zero".) Yes, look for EnvironmentWriter in Instruments, in both the lists of updates and the Cause & Effect graph. This is shown in the demos in the talk you linked. I'm quite thankful for the new SwiftUI Instrument, but I'm still learning how to correctly interpret the data it produces. With your comments and video, I'm going to revisit some of my @Environment usage and likely replace writes an @Observable object of properties I formerly wrote directly into the environment. My example above is contrived, but rather than having environment values for \.name, \.age and \.address it sounds like I should just have \.person, which is an @Observable object containing the three properties. That way, writes to any of those properties don't trigger the issue you were talking about in the video. Cheers
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’25
Reply to NSOutlineView incorrectly draws disclosure indicator when item views are SwiftUI views.
Apparently if you just wrap the NSHostingView inside an NSTableCellView without doing anything else then it works and the disclosure indicator is correctly aligned. 🤷‍♂️ final class WrappedHostingView<Content: View> : NSTableCellView { let hostingView: NSHostingView<Content> init(rootView: Content) { self.hostingView = NSHostingView(rootView: rootView) self.hostingView.translatesAutoresizingMaskIntoConstraints = false super.init(frame: .zero) self.identifier = .wrappedHostingViewIdentifier self.addSubview(hostingView) NSLayoutConstraint.activate([ hostingView.leadingAnchor.constraint(equalTo: leadingAnchor), hostingView.trailingAnchor.constraint(equalTo: trailingAnchor), hostingView.topAnchor.constraint(equalTo: topAnchor), hostingView.bottomAnchor.constraint(equalTo: bottomAnchor) ]) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Nov ’25
Reply to Fous, FocusState and Architecture
I’ve run into a similar issue in a macOS app and ended up rethinking how to use SwiftUI’s focus system. The app has the usual triple-panel layout: sidebar, content, inspector. The content is two tables, either of which can be focused. Each table has a TableController, exposed via .focused(). The inspector reads the focused controller to update its display, and the sidebar shows its selection—both via @FocusedValue. On paper this works, but there’s a catch: when the sidebar or inspector itself gains focus (e.g. a text field), the focused table controller becomes nil. I tried various .focus modifiers but could never get the behaviour I wanted. What I really needed was a property influenced by focus but not identical to it. For example: Use the focused table controller if one exists. Otherwise fall back to the last focused table controller. Add other rules as needed. The solution was to introduce an active table controller managed by an @Observable object in the environment. The sidebar and inspector observe this active controller instead of the raw focused value. Whenever focus changes, the observable updates the active controller according to my rules. This also allows changing the active controller through other means (picker, button, etc.). In your case, you might similarly combine focused values with some key state to derive an “active” entity. If focus remains your primary driver, you still get tab-navigation, keyboard control, and accessibility “for free.” Finally, don’t forget about `.focused(_:equals:). If you need to drive focus from your controller (mapping active → focused), this modifier helps align SwiftUI focus with your active entity. Hope that helps. Good luck!
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’25
Reply to On macOS, what is the appropriate way to disable the sidebar material in a NavigationSplitView?
@DTS Engineer Unfortunately, .scrollContentBackground doesn't seem to have any affect regardless of where I place it. We don't actually use a List in our Sidebar but I tried this in a demo app and wasn't able to make it work. In our SidebarView, we use a ScrollView as the top-element, then a VStack, followed by a bunch of text fields and other standard controls. What's interesting (and desirable) with the .background(.windowColor) implementation is that it disables vibrancy for all these controls, which is actually what we want. (We're trying to avoid vibrant textfields in the sidebar with non-vibrant textfields in the inspector.) As for the "custom tab view" I alluded to, it's not a SwiftUI TabView but rather just a custom View that conditionally shows a child view. We wanted to use TabView but we couldn't figure out how to hide the native tab bar, which is possible in AppKit. (NSTabViewController.TabStyle.unspecified)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’25
Reply to On macOS, what is the appropriate way to disable the sidebar material in a NavigationSplitView?
@szymczyk The contents of my SidebarView is a custom tab view where each tab hosts views containing different content. Refer to the sidebar or inspector panels in Xcode for a good idea. In Xcode's case, they allow vibrancy in the sidebar even for the panels that are not lists. In the Inspector, there is no vibrancy. We're hoping to disable the vibrancy in the sidebar so that it matches the look-and-feel of the inspector panel.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’25
Reply to How do you compute backing pixel alignment in SwiftUI's `Layout`?
Stumbled across: EnvironmentValues - displayScale EnvironmentValues - pixelLength I suppose I can pass these into the custom Layout object for use during subview placement. Still curious if there's any way to tell SwiftUI or Layout to always use backing aligned coordinates instead of having to calculate them all by hand (which I've oddly never seen mentioned in any documentation or videos).
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’25
Reply to In SwiftUI for macOS, how can you detect if a view or any ancestor is "hidden"?
@pilotcoder Yeah, I would normally agree to remove the expensive view. On macOS, I find that some "state" can be lost when that happens. (ex: the scroll position in a table, the viewport in a map, the current position in a video player, etc...) You could create @State variables for as many of those properties as you can (assuming they are accessible), but at least in AppKit where it's possible to hold on to views that have already been created, it's a nice feature to also know if the view is hidden or not.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’25
Reply to How can I connect NSTableCellView.textField to a SwiftUI view?
You can't directly assign NSHostingView to the textField property of NSTableCellView because it expects an NSTextField, and a hosting view is a NSView object. I'm aware of that. The pseudo code above was trying to ask how I could connect a property or view from rootView to either the cell's textField or the imageView. Instead, you could use your hosting view as the cell and include a text field and image either as a Label or LabeledContent If I create a SwiftUI View who's body consists of a Label and assign that SwiftUI view to the rootView of an NSHostingView which I return from the NSOutlineView's delegate (in place of returning an NSTableCellView), the NSOutlineView does not recognize the Label as the textField in the same way it does in AppKit. Specifically, changes to the "Sidebar Icon Size" in the Settings app are not reflected in this SwiftUI view. The size of the label's text and image do not automatically update, as they do in AppKit. Creating a SwiftUI cell view and then populating it with an NSTextField and an NSImageView kind of defeats the purpose of using SwiftUI here. I might as well just use an NSTableCellView, no?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’25
Reply to On macOS, how do you place a toolbar item on the trailing edge of the window's toolbar when an Inspector view is open?
macOS Tahoe 26 Beta 4 does, somewhat, fix the problem but it also introduces a new set of problems. Namely: The Sidebar icon is now trailing-aligned instead of leading align, which means the Sidebar icon now moves as the sidebar is toggled. The tracking separator for the inspector view is now ignored. Toolbar items that are leading of the separator under Sequoia are now flush-trailing with the Inspector toolbar item on Tahoe. See the attached screenshots comparing the two. The Sequoia screenshot was compiled under Xcode 16 running on Sequois. The Tahoe screenshot was compiled under Xcode 26b4 running on Tahoe. In both cases the Minimum Deployment is set to macOS 15.5. Note the difference in positions between the toolbar items. The toolbar code is the "original" implementation, whereby .toolbar and all ToolbarItems are attached to the NavigationSplitView.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’25
Reply to On macOS, how do you place a toolbar item on the trailing edge of the window's toolbar when an Inspector view is open?
Turns out if you accidentally move the toolbar from being on the NavigationSplitView to being on the InspectorContentView then you get the behaviour I was looking for. .inspector(isPresented: $isInspectorPresented) { InspectorContentView() .toolbar { ToolbarItem { Spacer() } ToolbarItem(placement: .primaryAction) { Button { isInspectorPresented.toggle() } label: { Label("Toggle Inspector", systemImage: "sidebar.right") } } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Aug ’25
Reply to Application crashes when using TextEditor(text, selection)
Similar experience on macOS. Using TextSelection with a TextEditor is very unstable and crashes quite easily, especially if the selection is used to mutate the string bound to the TextEditor. (Which is common for operations that involve replacing the selected text.) In Xcode, the following error is reporting in the console: Swift/StringUTF16View.swift:372: Fatal error: String index is out of bounds The full call stack is pasted below For any SwiftUI developers reading this: Unscientifically, it feels like this happens much more frequently in an AppKit application hosting a TextEditor than it does inside a SwiftUI application. If I make a demo SwiftUI app with nothing more than a ContentView and a TextEditor, then it's harder to trigger this bug (but not impossible, as I just did it). On the other hand, when used inside a much larger AppKit app, it happens quite easily even though the actual SwiftUI code isn't that different. 🤷‍♂️ Xcode Version 16.3 (16E140), macOS 15.4.1 (24E263)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to How to effectively use task(id:) when multiple properties are involved?
Follow Up Awhile back there was a discussion on Swift Forum that proposed creating an EquatableBox to solve this problem. That solution leverages parameters packs. I've since adapted that and created a View modifier along the following lines. So far it seems to be working as expected, though I can't help but think I've overlooked something. extension View { /// task(ids:) /// func task<each T: Equatable>( ids: repeat each T, priority: TaskPriority = .userInitiated, _ action: @escaping @Sendable () async -> Void ) -> some View { task( id: EquatablePack(repeat each ids), priority: priority, action ) } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to How to hide the tab bar in SwiftUI's TabView for macOS?
[quote='839309022, DTS Engineer, /thread/784248?answerId=839309022#839309022'] We suggest using native UI elements [/quote] Are you suggesting that I forgo using SwiftUI's TabView and instead use NSTabView from AppKit? I'm currently using NSTabViewController but was hoping to reduce my dependencies on AppKit and adopt SwiftUI as much as possible. We suggest using native UI elements, but you could hide the Toolbar with .toolbar(.hidden). That seems to hide the window's toolbar. I'm trying to hide the tab bar as presented by TabView on macOS, which I presume is using NSTabView. As noted above, NSTabViewController allows you to hide the tab bar and NSTabView itself offers NSTabPosition.None. However, I'm unable to configure those withTabView and thus, on macOS, I'm left with the default "Aqua-style" tab bar, which I don't want.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to How do you restore a Sheet's window frame in SwiftUI for macOS
[quote='838899022, DTS Engineer, /thread/783804?answerId=838899022#838899022'] Are you referring to Sheet or windows? [/quote] Well, in AppKit I can present an NSViewController as a sheet via NSViewController.presentViewControllerAsSheet or I can present an NSWindow via NSWindow.beginSheet... When using NSWindow, I have easy access to NSWindow.frameAutosaveName which, if set before the NSWindow is presented as a sheet, will restore it's size. If I use NSViewController, I have to grab the window in something like viewWillAppear, but that does work. In the end, I ended up a bridged solution that does the following: Embeds the SwiftUI view that should be presented as a sheet inside an NSHostingController. Create a new NSWindow with the hosting controller as the contentViewController. Configure the NSWindow as appropriate and then present as a sheet through AppKit. It took quite awhile to get it right, but in the end the secret sauce was setting NSHostingController.sizingOptions to .intrinsicContentSize. There's still an issue with NSWindow resizing when its contentViewController is set, but I think that's been an AppKit issue for awhile. It's more reliable to just add the view to the NSWindow's contentView instead, otherwise the NSWindow resizes to the initial size of the hosting controller. But for now, it appears to be working. I suspect I'm pushing my luck a little bit because the actual view hierarchy is: NSWindow.contentViewController -> NSHostingController NSHostingController -> NSViewControllerRepresentable NSViewControllerRepresentable -> NSSplitViewController NSSplitViewItems -> SwiftUI Views VSplitView and HSplitView don't appear to remember their positions at all. NSSplitViewController does, hence the need to use it instead. But NSSplitViewController's splitViewItems were wreaking havoc with a resizable sheet. Intrinsic content size appears to have resolved that issue. Ultimately, it's three SwiftUI views hosted inside an NSSplitViewController which is presented as a resizable sheet.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25