Skip to content

Commit b1b4ef6

Browse files
committed
Port compose snippets from DAC
1 parent 4493417 commit b1b4ef6

File tree

7 files changed

+553
-0
lines changed

7 files changed

+553
-0
lines changed

Diff for: xr/build.gradle.kts

+32
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
plugins {
22
alias(libs.plugins.android.application)
33
alias(libs.plugins.kotlin.android)
4+
alias(libs.plugins.compose.compiler)
45
}
56

67
android {
@@ -21,14 +22,45 @@ android {
2122
kotlinOptions {
2223
jvmTarget = "11"
2324
}
25+
buildFeatures {
26+
compose = true
27+
}
2428
}
2529

2630
dependencies {
2731
implementation(libs.androidx.xr.arcore)
2832
implementation(libs.androidx.xr.scenecore)
2933
implementation(libs.androidx.xr.compose)
34+
3035
implementation(libs.androidx.activity.ktx)
3136
implementation(libs.guava)
3237
implementation(libs.kotlinx.coroutines.guava)
3338

39+
val composeBom = platform(libs.androidx.compose.bom)
40+
implementation(composeBom)
41+
42+
implementation(libs.androidx.compose.ui)
43+
implementation(libs.androidx.compose.ui.util)
44+
implementation(libs.androidx.compose.ui.graphics)
45+
implementation(libs.androidx.graphics.shapes)
46+
implementation(libs.androidx.compose.ui.tooling.preview)
47+
implementation(libs.androidx.compose.ui.viewbinding)
48+
implementation(libs.androidx.paging.compose)
49+
implementation(libs.androidx.compose.animation.graphics)
50+
51+
implementation(libs.androidx.compose.material3)
52+
implementation(libs.androidx.compose.material3.adaptive)
53+
implementation(libs.androidx.compose.material3.adaptive.layout)
54+
implementation(libs.androidx.compose.material3.adaptive.navigation)
55+
implementation(libs.androidx.compose.material3.adaptive.navigation.suite)
56+
implementation(libs.androidx.compose.material)
57+
58+
implementation(libs.androidx.compose.runtime)
59+
implementation(libs.androidx.compose.runtime.livedata)
60+
implementation(libs.androidx.compose.material.iconsExtended)
61+
implementation(libs.androidx.compose.material.ripple)
62+
implementation(libs.androidx.constraintlayout.compose)
63+
64+
implementation(libs.androidx.activity.compose)
65+
implementation(libs.androidx.appcompat)
3466
}

Diff for: xr/src/main/java/com/example/xr/compose/Orbiter.kt

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.compose
18+
19+
import androidx.compose.foundation.background
20+
import androidx.compose.foundation.layout.Arrangement
21+
import androidx.compose.foundation.layout.Box
22+
import androidx.compose.foundation.layout.Row
23+
import androidx.compose.foundation.layout.height
24+
import androidx.compose.foundation.layout.padding
25+
import androidx.compose.foundation.layout.width
26+
import androidx.compose.foundation.shape.CircleShape
27+
import androidx.compose.foundation.shape.CornerSize
28+
import androidx.compose.material.MaterialTheme
29+
import androidx.compose.material.Surface
30+
import androidx.compose.material3.Text
31+
import androidx.compose.runtime.Composable
32+
import androidx.compose.ui.Alignment
33+
import androidx.compose.ui.Modifier
34+
import androidx.compose.ui.draw.clip
35+
import androidx.compose.ui.graphics.Color
36+
import androidx.compose.ui.unit.dp
37+
import androidx.compose.ui.unit.sp
38+
import androidx.xr.compose.spatial.EdgeOffset
39+
import androidx.xr.compose.spatial.Orbiter
40+
import androidx.xr.compose.spatial.OrbiterEdge
41+
import androidx.xr.compose.spatial.Subspace
42+
import androidx.xr.compose.subspace.SpatialPanel
43+
import androidx.xr.compose.subspace.SpatialRow
44+
import androidx.xr.compose.subspace.layout.SpatialRoundedCornerShape
45+
import androidx.xr.compose.subspace.layout.SubspaceModifier
46+
import androidx.xr.compose.subspace.layout.height
47+
import androidx.xr.compose.subspace.layout.movable
48+
import androidx.xr.compose.subspace.layout.resizable
49+
import androidx.xr.compose.subspace.layout.width
50+
51+
@Composable
52+
private fun OrbiterExampleSubspace() {
53+
// [START androidxr_compose_OrbiterExampleSubspace]
54+
Subspace {
55+
SpatialPanel(
56+
SubspaceModifier
57+
.height(824.dp)
58+
.width(1400.dp)
59+
.movable()
60+
.resizable()
61+
) {
62+
SpatialPanelContent()
63+
OrbiterExample()
64+
}
65+
}
66+
// [END androidxr_compose_OrbiterExampleSubspace]
67+
}
68+
69+
// [START androidxr_compose_OrbiterExample]
70+
@Composable
71+
fun OrbiterExample() {
72+
Orbiter(
73+
position = OrbiterEdge.Bottom,
74+
offset = 96.dp,
75+
alignment = Alignment.CenterHorizontally
76+
) {
77+
Surface(Modifier.clip(CircleShape)) {
78+
Row(
79+
Modifier
80+
.background(color = Color.Black)
81+
.height(100.dp)
82+
.width(600.dp),
83+
horizontalArrangement = Arrangement.Center,
84+
verticalAlignment = Alignment.CenterVertically
85+
) {
86+
Text(
87+
text = "Orbiter",
88+
color = Color.White,
89+
fontSize = 50.sp
90+
)
91+
}
92+
}
93+
}
94+
}
95+
// [END androidxr_compose_OrbiterExample]
96+
97+
@Composable
98+
fun OrbiterAnchoringExample() {
99+
// [START androidxr_compose_OrbiterAnchoringExample]
100+
Subspace {
101+
SpatialRow {
102+
Orbiter(
103+
position = OrbiterEdge.Top,
104+
offset = EdgeOffset.inner(8.dp),
105+
shape = SpatialRoundedCornerShape(size = CornerSize(50))
106+
) {
107+
Text(
108+
"Hello World!",
109+
style = MaterialTheme.typography.h2,
110+
modifier = Modifier
111+
.background(Color.White)
112+
.padding(16.dp)
113+
)
114+
}
115+
SpatialPanel(
116+
SubspaceModifier
117+
.height(824.dp)
118+
.width(1400.dp)
119+
) {
120+
Box(
121+
modifier = Modifier
122+
.background(Color.Red)
123+
)
124+
}
125+
SpatialPanel(
126+
SubspaceModifier
127+
.height(824.dp)
128+
.width(1400.dp)
129+
) {
130+
Box(
131+
modifier = Modifier
132+
.background(Color.Blue)
133+
)
134+
}
135+
}
136+
}
137+
// [END androidxr_compose_OrbiterAnchoringExample]
138+
}
+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.compose
18+
19+
import androidx.compose.foundation.layout.Box
20+
import androidx.compose.foundation.layout.height
21+
import androidx.compose.foundation.layout.width
22+
import androidx.compose.material3.Button
23+
import androidx.compose.material3.Text
24+
import androidx.compose.runtime.Composable
25+
import androidx.compose.runtime.LaunchedEffect
26+
import androidx.compose.runtime.getValue
27+
import androidx.compose.runtime.mutableStateOf
28+
import androidx.compose.runtime.remember
29+
import androidx.compose.runtime.setValue
30+
import androidx.compose.ui.Modifier
31+
import androidx.compose.ui.unit.dp
32+
import androidx.xr.compose.spatial.SpatialDialog
33+
import androidx.xr.compose.spatial.SpatialDialogProperties
34+
import kotlinx.coroutines.delay
35+
36+
// [START androidxr_compose_DelayedDialog]
37+
@Composable
38+
fun DelayedDialog() {
39+
var showDialog by remember { mutableStateOf(false) }
40+
LaunchedEffect(Unit) {
41+
delay(3000)
42+
showDialog = true
43+
}
44+
if (showDialog) {
45+
SpatialDialog(
46+
onDismissRequest = { showDialog = false },
47+
SpatialDialogProperties(
48+
dismissOnBackPress = true
49+
)
50+
) {
51+
Box(
52+
Modifier
53+
.height(150.dp)
54+
.width(150.dp)
55+
) {
56+
Button(onClick = { showDialog = false }) {
57+
Text("OK")
58+
}
59+
}
60+
}
61+
}
62+
}
63+
// [END androidxr_compose_DelayedDialog]
+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright 2025 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.xr.compose
18+
19+
import androidx.compose.foundation.background
20+
import androidx.compose.foundation.layout.Arrangement
21+
import androidx.compose.foundation.layout.Column
22+
import androidx.compose.foundation.layout.fillMaxSize
23+
import androidx.compose.material3.Text
24+
import androidx.compose.runtime.Composable
25+
import androidx.compose.ui.Alignment
26+
import androidx.compose.ui.Modifier
27+
import androidx.compose.ui.graphics.Color
28+
import androidx.compose.ui.text.font.FontWeight
29+
import androidx.compose.ui.unit.dp
30+
import androidx.compose.ui.unit.sp
31+
import androidx.xr.compose.spatial.Subspace
32+
import androidx.xr.compose.subspace.SpatialColumn
33+
import androidx.xr.compose.subspace.SpatialPanel
34+
import androidx.xr.compose.subspace.SpatialRow
35+
import androidx.xr.compose.subspace.layout.SubspaceModifier
36+
import androidx.xr.compose.subspace.layout.height
37+
import androidx.xr.compose.subspace.layout.width
38+
39+
@Composable
40+
private fun SpatialLayoutExampleSubspace() {
41+
// [START androidxr_compose_SpatialLayoutExampleSubspace]
42+
Subspace {
43+
SpatialRow {
44+
SpatialColumn {
45+
SpatialPanel(SubspaceModifier.height(250.dp).width(400.dp)) {
46+
SpatialPanelContent("Top Left")
47+
}
48+
SpatialPanel(SubspaceModifier.height(200.dp).width(400.dp)) {
49+
SpatialPanelContent("Middle Left")
50+
}
51+
SpatialPanel(SubspaceModifier.height(250.dp).width(400.dp)) {
52+
SpatialPanelContent("Bottom Left")
53+
}
54+
}
55+
SpatialColumn {
56+
SpatialPanel(SubspaceModifier.height(250.dp).width(400.dp)) {
57+
SpatialPanelContent("Top Right")
58+
}
59+
SpatialPanel(SubspaceModifier.height(200.dp).width(400.dp)) {
60+
SpatialPanelContent("Middle Right")
61+
}
62+
SpatialPanel(SubspaceModifier.height(250.dp).width(400.dp)) {
63+
SpatialPanelContent("Bottom Right")
64+
}
65+
}
66+
}
67+
}
68+
// [END androidxr_compose_SpatialLayoutExampleSubspace]
69+
}
70+
71+
// [START androidxr_compose_SpatialLayoutExampleSpatialPanelContent]
72+
@Composable
73+
fun SpatialPanelContent(text: String) {
74+
Column(
75+
Modifier
76+
.background(color = Color.Black)
77+
.fillMaxSize(),
78+
horizontalAlignment = Alignment.CenterHorizontally,
79+
verticalArrangement = Arrangement.Center
80+
) {
81+
Text(
82+
text = "Panel",
83+
color = Color.White,
84+
fontSize = 15.sp
85+
)
86+
Text(
87+
text = text,
88+
color = Color.White,
89+
fontSize = 25.sp,
90+
fontWeight = FontWeight.Bold
91+
)
92+
}
93+
}
94+
// [END androidxr_compose_SpatialLayoutExampleSpatialPanelContent]

0 commit comments

Comments
 (0)