Skip to content

Instantly share code, notes, and snippets.

@drewolbrich
Last active August 11, 2024 03:35
Show Gist options
  • Save drewolbrich/15de5f1cac0163ad80150ad81a291776 to your computer and use it in GitHub Desktop.
Save drewolbrich/15de5f1cac0163ad80150ad81a291776 to your computer and use it in GitHub Desktop.
A wrapper around SwiftUI defaultWindowPlacement that is a no-op on visionOS 1
//
// DefaultWindowPlacement_visionOS2.swift
//
// Created by Drew Olbrich on 6/11/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 WindowPlacementPosition {
case utilityPanel
case leading(_ id: String)
case trailing(_ id: String)
case above(_ id: String)
case below(_ id: String)
}
extension SwiftUI.Scene {
/// A wrapper around `defaultWindowPlacement` 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 defaultWindowPlacement_visionOS2(_ windowPlacementPosition: WindowPlacementPosition) -> some SwiftUI.Scene {
if #available(visionOS 2.0, *) {
return self.defaultWindowPlacement { content, context in
func firstWindowProxy(withID id: String) -> WindowProxy? {
if let windowProxy = context.windows.first(where: { $0.id == id }) {
return windowProxy
} else {
assertionFailure("windowProxy is undefined")
return nil
}
}
switch windowPlacementPosition {
case .utilityPanel:
return WindowPlacement(.utilityPanel)
case .leading(let id):
if let windowProxy = firstWindowProxy(withID: id) {
return WindowPlacement(.leading(windowProxy))
} else {
return WindowPlacement(.none)
}
case .trailing(let id):
if let windowProxy = firstWindowProxy(withID: id) {
return WindowPlacement(.trailing(windowProxy))
} else {
return WindowPlacement(.none)
}
case .above(let id):
if let windowProxy = firstWindowProxy(withID: id) {
return WindowPlacement(.above(windowProxy))
} else {
return WindowPlacement(.none)
}
case .below(let id):
if let windowProxy = firstWindowProxy(withID: id) {
return WindowPlacement(.below(windowProxy))
} else {
return WindowPlacement(.none)
}
}
}
} else {
return self
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment