Skip to content

Port XR compose snippets from DAC #500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions xr/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
plugins {
alias(libs.plugins.android.application)
alias(libs.plugins.kotlin.android)
alias(libs.plugins.compose.compiler)
}

android {
Expand All @@ -21,14 +22,45 @@ android {
kotlinOptions {
jvmTarget = "11"
}
buildFeatures {
compose = true
}
}

dependencies {
implementation(libs.androidx.xr.arcore)
implementation(libs.androidx.xr.scenecore)
implementation(libs.androidx.xr.compose)

implementation(libs.androidx.activity.ktx)
implementation(libs.guava)
implementation(libs.kotlinx.coroutines.guava)

val composeBom = platform(libs.androidx.compose.bom)
implementation(composeBom)

implementation(libs.androidx.compose.ui)
implementation(libs.androidx.compose.ui.util)
implementation(libs.androidx.compose.ui.graphics)
implementation(libs.androidx.graphics.shapes)
implementation(libs.androidx.compose.ui.tooling.preview)
implementation(libs.androidx.compose.ui.viewbinding)
implementation(libs.androidx.paging.compose)
implementation(libs.androidx.compose.animation.graphics)

implementation(libs.androidx.compose.material3)
implementation(libs.androidx.compose.material3.adaptive)
implementation(libs.androidx.compose.material3.adaptive.layout)
implementation(libs.androidx.compose.material3.adaptive.navigation)
implementation(libs.androidx.compose.material3.adaptive.navigation.suite)
implementation(libs.androidx.compose.material)

implementation(libs.androidx.compose.runtime)
implementation(libs.androidx.compose.runtime.livedata)
implementation(libs.androidx.compose.material.iconsExtended)
implementation(libs.androidx.compose.material.ripple)
implementation(libs.androidx.constraintlayout.compose)

implementation(libs.androidx.activity.compose)
implementation(libs.androidx.appcompat)
}
160 changes: 160 additions & 0 deletions xr/src/main/java/com/example/xr/compose/Orbiter.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.xr.compose

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.material.MaterialTheme
import androidx.compose.material.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.dimensionResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.xr.compose.spatial.EdgeOffset
import androidx.xr.compose.spatial.Orbiter
import androidx.xr.compose.spatial.OrbiterEdge
import androidx.xr.compose.spatial.Subspace
import androidx.xr.compose.subspace.SpatialPanel
import androidx.xr.compose.subspace.SpatialRow
import androidx.xr.compose.subspace.layout.SpatialRoundedCornerShape
import androidx.xr.compose.subspace.layout.SubspaceModifier
import androidx.xr.compose.subspace.layout.height
import androidx.xr.compose.subspace.layout.movable
import androidx.xr.compose.subspace.layout.resizable
import androidx.xr.compose.subspace.layout.width
import com.example.xr.R

@Composable
private fun OrbiterExampleSubspace() {
// [START androidxr_compose_OrbiterExampleSubspace]
Subspace {
SpatialPanel(
SubspaceModifier
.height(824.dp)
.width(1400.dp)
.movable()
.resizable()
) {
SpatialPanelContent()
OrbiterExample()
}
}
// [END androidxr_compose_OrbiterExampleSubspace]
}

// [START androidxr_compose_OrbiterExample]
@Composable
fun OrbiterExample() {
Orbiter(
position = OrbiterEdge.Bottom,
offset = 96.dp,
alignment = Alignment.CenterHorizontally
) {
Surface(Modifier.clip(CircleShape)) {
Row(
Modifier
.background(color = Color.Black)
.height(100.dp)
.width(600.dp),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically
) {
Text(
text = "Orbiter",
color = Color.White,
fontSize = 50.sp
)
}
}
}
}
// [END androidxr_compose_OrbiterExample]

@Composable
fun OrbiterAnchoringExample() {
// [START androidxr_compose_OrbiterAnchoringExample]
Subspace {
SpatialRow {
Orbiter(
position = OrbiterEdge.Top,
offset = EdgeOffset.inner(8.dp),
shape = SpatialRoundedCornerShape(size = CornerSize(50))
) {
Text(
"Hello World!",
style = MaterialTheme.typography.h2,
modifier = Modifier
.background(Color.White)
.padding(16.dp)
)
}
SpatialPanel(
SubspaceModifier
.height(824.dp)
.width(1400.dp)
) {
Box(
modifier = Modifier
.background(Color.Red)
)
}
SpatialPanel(
SubspaceModifier
.height(824.dp)
.width(1400.dp)
) {
Box(
modifier = Modifier
.background(Color.Blue)
)
}
}
}
// [END androidxr_compose_OrbiterAnchoringExample]
}

@Composable
private fun NavigationRail() {}

@Composable
private fun Ui2DToOribiter() {
// [START androidxr_compose_orbiter_comparison]
// Previous approach
NavigationRail()

// New XR differentiated approach
Orbiter(
position = OrbiterEdge.Start,
offset = dimensionResource(R.dimen.start_orbiter_padding),
alignment = Alignment.Top
) {
NavigationRail()
}
// [END androidxr_compose_orbiter_comparison]
}
40 changes: 40 additions & 0 deletions xr/src/main/java/com/example/xr/compose/SpatialCapabilities.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.xr.compose

import androidx.compose.runtime.Composable
import androidx.xr.compose.platform.LocalSpatialCapabilities

@Composable
private fun SupportingInfoPanel() {}

@Composable
private fun ButtonToPresentInfoModal() {}

@Composable
private fun SpatialCapabilitiesCheck() {
// [START androidxr_compose_checkSpatialCapabilities]
if (LocalSpatialCapabilities.current.isSpatialUiEnabled) {
SupportingInfoPanel()
} else {
ButtonToPresentInfoModal()
}

// Similar check for audio
val spatialAudioEnabled = LocalSpatialCapabilities.current.isSpatialAudioEnabled
// [END androidxr_compose_checkSpatialCapabilities]
}
86 changes: 86 additions & 0 deletions xr/src/main/java/com/example/xr/compose/SpatialDialog.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.xr.compose

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.Dialog
import androidx.xr.compose.spatial.SpatialDialog
import androidx.xr.compose.spatial.SpatialDialogProperties
import kotlinx.coroutines.delay

// [START androidxr_compose_DelayedDialog]
@Composable
fun DelayedDialog() {
var showDialog by remember { mutableStateOf(false) }
LaunchedEffect(Unit) {
delay(3000)
showDialog = true
}
if (showDialog) {
SpatialDialog(
onDismissRequest = { showDialog = false },
SpatialDialogProperties(
dismissOnBackPress = true
)
) {
Box(
Modifier
.height(150.dp)
.width(150.dp)
) {
Button(onClick = { showDialog = false }) {
Text("OK")
}
}
}
}
}
// [END androidxr_compose_DelayedDialog]

@Composable
private fun MyDialogContent() {}
@Composable
private fun SpatialDialogComparison() {
val onDismissRequest: () -> Unit = {}
// [START androidxr_compose_spatialdialog_comparison]
// Previous approach
Dialog(
onDismissRequest = onDismissRequest
) {
MyDialogContent()
}

// New XR differentiated approach
SpatialDialog(
onDismissRequest = onDismissRequest
) {
MyDialogContent()
}
// [END androidxr_compose_spatialdialog_comparison]
}
34 changes: 34 additions & 0 deletions xr/src/main/java/com/example/xr/compose/SpatialElevation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.example.xr.compose

import androidx.compose.runtime.Composable
import androidx.xr.compose.spatial.SpatialElevation
import androidx.xr.compose.spatial.SpatialElevationLevel

@Composable
private fun ComposableThatShouldElevateInXr() {}

@Composable
private fun SpatialElevationExample() {
// [START androidxr_compose_spatialelevation]
// Elevate an otherwise 2D Composable (signified here by ComposableThatShouldElevateInXr).
SpatialElevation(spatialElevationLevel = SpatialElevationLevel.Level4) {
ComposableThatShouldElevateInXr()
}
// [END androidxr_compose_spatialelevation]
}
Loading