Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
Last active July 20, 2024 00:52
Show Gist options
  • Save drewolbrich/e799d03caa1cb6ad74956f149daa01eb to your computer and use it in GitHub Desktop.
Save drewolbrich/e799d03caa1cb6ad74956f149daa01eb to your computer and use it in GitHub Desktop.
Wrapper around visionOS 2 volumeWorldAlignment that is a no-op in visionOS 1
//
// VolumeWorldAlignment_visionOS2.swift
//
// Created by Drew Olbrich on 6/15/24.
// Copyright © 2024 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
// SOFTWARE.
//
import SwiftUI
enum WorldAlignmentBehavior_visionOS2 {
case adaptive
case automatic
case gravityAligned
}
extension Scene {
/// A wrapper around `volumeWorldAlignment` that applies it only on visionOS 2 and
/// later, and is a no-op on visionOS 1.
///
/// This is necessary because there doesn't appear to be an easy way to
/// conditionally apply SwiftUI view modifiers or scene modifiers for specific OS
/// versions.
func volumeWorldAlignment_visionOS2(_ worldAlignmentBehavior: WorldAlignmentBehavior_visionOS2) -> some Scene {
if #available(visionOS 2.0, *) {
let worldAlignmentBehavior: WorldAlignmentBehavior = switch worldAlignmentBehavior {
case .adaptive:
.adaptive
case .automatic:
.automatic
case .gravityAligned:
.gravityAligned
}
return self.volumeWorldAlignment(worldAlignmentBehavior)
} else {
return self
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment