Post

Replies

Boosts

Views

Activity

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 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 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 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 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 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 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