1 /*
2  * Copyright 2024 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  *      http://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 androidx.compose.ui.test.samples
18 
19 import androidx.annotation.Sampled
20 import androidx.compose.foundation.background
21 import androidx.compose.foundation.isSystemInDarkTheme
22 import androidx.compose.foundation.layout.Box
23 import androidx.compose.foundation.layout.safeDrawingPadding
24 import androidx.compose.material.Text
25 import androidx.compose.runtime.Composable
26 import androidx.compose.ui.Modifier
27 import androidx.compose.ui.graphics.Color
28 import androidx.compose.ui.platform.LocalConfiguration
29 import androidx.compose.ui.platform.LocalDensity
30 import androidx.compose.ui.test.DarkMode
31 import androidx.compose.ui.test.DeviceConfigurationOverride
32 import androidx.compose.ui.test.FontScale
33 import androidx.compose.ui.test.FontWeightAdjustment
34 import androidx.compose.ui.test.ForcedSize
35 import androidx.compose.ui.test.LayoutDirection
36 import androidx.compose.ui.test.Locales
37 import androidx.compose.ui.test.RoundScreen
38 import androidx.compose.ui.test.WindowInsets
39 import androidx.compose.ui.test.then
40 import androidx.compose.ui.text.intl.LocaleList
41 import androidx.compose.ui.unit.DpRect
42 import androidx.compose.ui.unit.DpSize
43 import androidx.compose.ui.unit.IntRect
44 import androidx.compose.ui.unit.LayoutDirection
45 import androidx.compose.ui.unit.dp
46 import androidx.compose.ui.unit.roundToIntRect
47 import androidx.core.view.WindowInsetsCompat
48 
49 @Sampled
50 @Composable
DeviceConfigurationOverrideThenSamplenull51 fun DeviceConfigurationOverrideThenSample() {
52     DeviceConfigurationOverride(
53         DeviceConfigurationOverride.FontScale(1.5f) then
54             DeviceConfigurationOverride.FontWeightAdjustment(200)
55     ) {
56         Text(text = "text with increased scale and weight")
57     }
58 }
59 
60 @Sampled
61 @Composable
DeviceConfigurationOverrideFontScaleSamplenull62 fun DeviceConfigurationOverrideFontScaleSample() {
63     DeviceConfigurationOverride(DeviceConfigurationOverride.FontScale(1.5f)) {
64         MyScreen() // will be rendered with a larger than default font scale
65     }
66 }
67 
68 @Sampled
69 @Composable
DeviceConfigurationOverrideForcedSizeSamplenull70 fun DeviceConfigurationOverrideForcedSizeSample() {
71     DeviceConfigurationOverride(DeviceConfigurationOverride.ForcedSize(DpSize(1280.dp, 800.dp))) {
72         MyScreen() // will be rendered in the space for 1280dp by 800dp without clipping
73     }
74 }
75 
76 @Sampled
77 @Composable
DeviceConfigurationOverrideLayoutDirectionSamplenull78 fun DeviceConfigurationOverrideLayoutDirectionSample() {
79     DeviceConfigurationOverride(DeviceConfigurationOverride.LayoutDirection(LayoutDirection.Rtl)) {
80         MyComponent() // will be rendered with a right-to-left layout direction
81     }
82 }
83 
84 @Sampled
85 @Composable
DeviceConfigurationOverrideLocalesSamplenull86 fun DeviceConfigurationOverrideLocalesSample() {
87     DeviceConfigurationOverride(DeviceConfigurationOverride.Locales(LocaleList("es-ES"))) {
88         MyScreen() // will be rendered with overridden locale
89     }
90 }
91 
92 @Sampled
93 @Composable
DeviceConfigurationOverrideDarkModeSamplenull94 fun DeviceConfigurationOverrideDarkModeSample() {
95     DeviceConfigurationOverride(DeviceConfigurationOverride.DarkMode(true)) {
96         isSystemInDarkTheme() // will be true
97     }
98 }
99 
100 @Sampled
101 @Composable
DeviceConfigurationOverrideFontWeightAdjustmentSamplenull102 fun DeviceConfigurationOverrideFontWeightAdjustmentSample() {
103     DeviceConfigurationOverride(DeviceConfigurationOverride.FontWeightAdjustment(200)) {
104         MyComponent() // will be rendered with adjusted font weight
105     }
106 }
107 
108 @Sampled
109 @Composable
DeviceConfigurationOverrideRoundScreenSamplenull110 fun DeviceConfigurationOverrideRoundScreenSample() {
111     DeviceConfigurationOverride(DeviceConfigurationOverride.RoundScreen(true)) {
112         LocalConfiguration.current.isScreenRound // will be true
113     }
114 }
115 
116 @Sampled
117 @Composable
DeviceConfigurationOverrideWindowInsetsSamplenull118 fun DeviceConfigurationOverrideWindowInsetsSample() {
119     fun IntRect.toAndroidXInsets() = androidx.core.graphics.Insets.of(left, top, right, bottom)
120 
121     DeviceConfigurationOverride(
122         DeviceConfigurationOverride.WindowInsets(
123             WindowInsetsCompat.Builder()
124                 .setInsets(
125                     WindowInsetsCompat.Type.captionBar(),
126                     with(LocalDensity.current) { DpRect(0.dp, 64.dp, 0.dp, 0.dp).toRect() }
127                         .roundToIntRect()
128                         .toAndroidXInsets()
129                 )
130                 .setInsets(
131                     WindowInsetsCompat.Type.navigationBars(),
132                     with(LocalDensity.current) { DpRect(24.dp, 0.dp, 48.dp, 24.dp).toRect() }
133                         .roundToIntRect()
134                         .toAndroidXInsets()
135                 )
136                 .build()
137         )
138     ) {
139         Box(
140             Modifier.background(Color.Blue)
141                 // Will apply 64dp padding on the top, 24dp padding on the sides, and 48dp on the
142                 // bottom
143                 .safeDrawingPadding()
144                 .background(Color.Red)
145         )
146     }
147 }
148 
MyScreennull149 @Composable private fun MyScreen() = Unit
150 
151 @Composable private fun MyComponent() = Unit
152