Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
Created May 16, 2025 15:19
Show Gist options
  • Save drewolbrich/d72475028bdb7bdc44268afbc0219eab to your computer and use it in GitHub Desktop.
Save drewolbrich/d72475028bdb7bdc44268afbc0219eab to your computer and use it in GitHub Desktop.
A view modifier that wraps `persistentSystemOverlays` and gives it a more memorable and descriptive name on visionOS
//
// View+WindowBarVisibility.swift
//
// Created by Drew Olbrich on 5/15/25.
// Copyright © 2025 Lunar Skydiving LLC. All rights reserved.
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
//
#if os(visionOS)
import SwiftUI
public extension View {
/// A view modifier that wraps `persistentSystemOverlays` and gives it a more
/// memorable and descriptive name on visionOS.
///
/// - Parameter visibility: A value that indicates the visibility of the visionOS window bar.
///
/// ## Usage
///
/// ```
/// // BEFORE
/// // Hide the window bar
/// .persistentSystemOverlays(.hidden)
/// ```
///
/// ```
/// // AFTER
/// .windowBarVisibility(.hidden)
/// ```
///
/// ## Discussion
///
/// This view modifier controls the visibility of the window bar when applied in the
/// context of a `WindowGroup`.
///
/// Because this view modifier calls `persistentSystemOverlays`, it may have a
/// different effect in other contexts. For example, when used with an
/// `ImmersiveSpace`, this view modifier will control the visibility of the visionOS
/// home indicator.
func windowBarVisibility(_ visibility: Visibility) -> some View {
return modifier(WindowBarVisibilityViewModifier(visibility: visibility))
}
}
private struct WindowBarVisibilityViewModifier: ViewModifier {
let visibility: Visibility
func body(content: Content) -> some View {
content.persistentSystemOverlays(visibility)
}
}
#endif // os(visionOS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment