forked from androidx/androidx
-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Configure the "material3-window-size-class" module as CMP and include…
… it to the publication (#1466) Configure the `material3-window-size-class` module as CMP and include it to the publication. Added new method to non-android source set to calculate `WindowSizeClass`. Fixes https://youtrack.jetbrains.com/issue/CMP-2404 ## Testing Checked it in the MPP demo app: <img width="400" alt="image" src="https://github.com/user-attachments/assets/4c1b6fab-132d-4d1f-91fb-2c5a14bd9cab"> ## Release Notes ### Features - Multiple Platforms - Commonized `material3-window-size-class` module
1 parent
38c0ae5
commit 74e1caa
Showing
11 changed files
with
472 additions
and
288 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
compose/material3/material3-window-size-class/api/desktop/material3-window-size-class.api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
public abstract interface annotation class androidx/compose/material3/windowsizeclass/ExperimentalMaterial3WindowSizeClassApi : java/lang/annotation/Annotation { | ||
} | ||
|
||
public final class androidx/compose/material3/windowsizeclass/WindowHeightSizeClass : java/lang/Comparable { | ||
public static final field Companion Landroidx/compose/material3/windowsizeclass/WindowHeightSizeClass$Companion; | ||
public static final synthetic fun box-impl (I)Landroidx/compose/material3/windowsizeclass/WindowHeightSizeClass; | ||
public synthetic fun compareTo (Ljava/lang/Object;)I | ||
public fun compareTo-pav6bQQ (I)I | ||
public static fun compareTo-pav6bQQ (II)I | ||
public fun equals (Ljava/lang/Object;)Z | ||
public static fun equals-impl (ILjava/lang/Object;)Z | ||
public static final fun equals-impl0 (II)Z | ||
public fun hashCode ()I | ||
public static fun hashCode-impl (I)I | ||
public fun toString ()Ljava/lang/String; | ||
public static fun toString-impl (I)Ljava/lang/String; | ||
public final synthetic fun unbox-impl ()I | ||
} | ||
|
||
public final class androidx/compose/material3/windowsizeclass/WindowHeightSizeClass$Companion { | ||
public final fun getAllSizeClasses ()Ljava/util/Set; | ||
public final fun getCompact-Pt018CI ()I | ||
public final fun getDefaultSizeClasses ()Ljava/util/Set; | ||
public final fun getExpanded-Pt018CI ()I | ||
public final fun getMedium-Pt018CI ()I | ||
} | ||
|
||
public final class androidx/compose/material3/windowsizeclass/WindowSizeClass { | ||
public static final field $stable I | ||
public static final field Companion Landroidx/compose/material3/windowsizeclass/WindowSizeClass$Companion; | ||
public synthetic fun <init> (IILkotlin/jvm/internal/DefaultConstructorMarker;)V | ||
public fun equals (Ljava/lang/Object;)Z | ||
public final fun getHeightSizeClass-Pt018CI ()I | ||
public final fun getWidthSizeClass-Y0FxcvE ()I | ||
public fun hashCode ()I | ||
public fun toString ()Ljava/lang/String; | ||
} | ||
|
||
public final class androidx/compose/material3/windowsizeclass/WindowSizeClass$Companion { | ||
public final fun calculateFromSize-qzXmJYc (JLjava/util/Set;Ljava/util/Set;)Landroidx/compose/material3/windowsizeclass/WindowSizeClass; | ||
public static synthetic fun calculateFromSize-qzXmJYc$default (Landroidx/compose/material3/windowsizeclass/WindowSizeClass$Companion;JLjava/util/Set;Ljava/util/Set;ILjava/lang/Object;)Landroidx/compose/material3/windowsizeclass/WindowSizeClass; | ||
} | ||
|
||
public final class androidx/compose/material3/windowsizeclass/WindowSizeClass_skikoKt { | ||
public static final fun calculateWindowSizeClass (Landroidx/compose/runtime/Composer;I)Landroidx/compose/material3/windowsizeclass/WindowSizeClass; | ||
} | ||
|
||
public final class androidx/compose/material3/windowsizeclass/WindowWidthSizeClass : java/lang/Comparable { | ||
public static final field Companion Landroidx/compose/material3/windowsizeclass/WindowWidthSizeClass$Companion; | ||
public static final synthetic fun box-impl (I)Landroidx/compose/material3/windowsizeclass/WindowWidthSizeClass; | ||
public synthetic fun compareTo (Ljava/lang/Object;)I | ||
public fun compareTo-GxU_lZo (I)I | ||
public static fun compareTo-GxU_lZo (II)I | ||
public fun equals (Ljava/lang/Object;)Z | ||
public static fun equals-impl (ILjava/lang/Object;)Z | ||
public static final fun equals-impl0 (II)Z | ||
public fun hashCode ()I | ||
public static fun hashCode-impl (I)I | ||
public fun toString ()Ljava/lang/String; | ||
public static fun toString-impl (I)Ljava/lang/String; | ||
public final synthetic fun unbox-impl ()I | ||
} | ||
|
||
public final class androidx/compose/material3/windowsizeclass/WindowWidthSizeClass$Companion { | ||
public final fun getAllSizeClasses ()Ljava/util/Set; | ||
public final fun getCompact-Y0FxcvE ()I | ||
public final fun getDefaultSizeClasses ()Ljava/util/Set; | ||
public final fun getExpanded-Y0FxcvE ()I | ||
public final fun getMedium-Y0FxcvE ()I | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
275 changes: 0 additions & 275 deletions
275
.../androidUnitTest/kotlin/androidx/compose/material3/windowsizeclass/WindowSizeClassTest.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
267 changes: 267 additions & 0 deletions
267
...s/src/commonTest/kotlin/androidx/compose/material3/windowsizeclass/WindowSizeClassTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,267 @@ | ||
/* | ||
* Copyright 2022 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 | ||
* | ||
* http://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 androidx.compose.material3.windowsizeclass | ||
|
||
import androidx.compose.ui.unit.Dp | ||
import androidx.compose.ui.unit.dp | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFailsWith | ||
import kotlin.test.assertFalse | ||
import kotlin.test.assertTrue | ||
|
||
class WindowSizeClassTest { | ||
@Test | ||
fun calculateWidthSizeClass_forNegativeWidth_throws() { | ||
assertFailsWith(IllegalArgumentException::class) { | ||
WindowWidthSizeClass.fromWidth((-10).dp, WindowWidthSizeClass.DefaultSizeClasses) | ||
} | ||
} | ||
|
||
@Test | ||
fun calculateHeightSizeClass_forNegativeHeight_throws() { | ||
assertFailsWith(IllegalArgumentException::class) { | ||
WindowHeightSizeClass.fromHeight((-10).dp, WindowHeightSizeClass.DefaultSizeClasses) | ||
} | ||
} | ||
|
||
@Test | ||
fun calculateWidthSizeClass_noSupportedSizeClass_throws() { | ||
assertFailsWith(IllegalArgumentException::class) { | ||
WindowWidthSizeClass.fromWidth(10.dp, emptySet()) | ||
} | ||
} | ||
|
||
@Test | ||
fun calculateHeightSizeClass_noSupportedSizeClass_throws() { | ||
assertFailsWith(IllegalArgumentException::class) { | ||
WindowHeightSizeClass.fromHeight(10.dp, emptySet()) | ||
} | ||
} | ||
|
||
@Test | ||
fun calculateWidthSizeClass() { | ||
assertWidthClass(WindowWidthSizeClass.Compact, 0.dp) | ||
assertWidthClass(WindowWidthSizeClass.Compact, 200.dp) | ||
|
||
assertWidthClass(WindowWidthSizeClass.Medium, 600.dp) | ||
assertWidthClass(WindowWidthSizeClass.Medium, 700.dp) | ||
|
||
assertWidthClass(WindowWidthSizeClass.Expanded, 840.dp) | ||
assertWidthClass(WindowWidthSizeClass.Expanded, 1000.dp) | ||
} | ||
|
||
@Test | ||
fun calculateHeightSizeClass() { | ||
assertHeightClass(WindowHeightSizeClass.Compact, 0.dp) | ||
assertHeightClass(WindowHeightSizeClass.Compact, 200.dp) | ||
|
||
assertHeightClass(WindowHeightSizeClass.Medium, 480.dp) | ||
assertHeightClass(WindowHeightSizeClass.Medium, 700.dp) | ||
|
||
assertHeightClass(WindowHeightSizeClass.Expanded, 900.dp) | ||
assertHeightClass(WindowHeightSizeClass.Expanded, 1000.dp) | ||
} | ||
|
||
@Test | ||
fun calculateWidthSizeClass_useBestMatchedSupportedSizeClasses() { | ||
assertWidthClass( | ||
WindowWidthSizeClass.Compact, | ||
700.dp, | ||
supportedSizeClasses = | ||
setOf(WindowWidthSizeClass.Compact, WindowWidthSizeClass.Expanded) | ||
) | ||
|
||
assertWidthClass( | ||
WindowWidthSizeClass.Medium, | ||
1000.dp, | ||
supportedSizeClasses = setOf(WindowWidthSizeClass.Compact, WindowWidthSizeClass.Medium) | ||
) | ||
} | ||
|
||
@Test | ||
fun calculateHeightSizeClass_useBestMatchedSupportedSizeClasses() { | ||
assertHeightClass( | ||
WindowHeightSizeClass.Compact, | ||
700.dp, | ||
supportedSizeClasses = | ||
setOf(WindowHeightSizeClass.Compact, WindowHeightSizeClass.Expanded) | ||
) | ||
|
||
assertHeightClass( | ||
WindowHeightSizeClass.Medium, | ||
1000.dp, | ||
supportedSizeClasses = | ||
setOf(WindowHeightSizeClass.Compact, WindowHeightSizeClass.Medium) | ||
) | ||
} | ||
|
||
@Test | ||
fun calculateWidthSizeClass_fallbackToTheSmallestSizeClasses() { | ||
assertWidthClass( | ||
WindowWidthSizeClass.Medium, | ||
200.dp, | ||
supportedSizeClasses = setOf(WindowWidthSizeClass.Medium, WindowWidthSizeClass.Expanded) | ||
) | ||
} | ||
|
||
@Test | ||
fun calculateHeightSizeClass_fallbackToTheSmallestSizeClasses() { | ||
assertHeightClass( | ||
WindowHeightSizeClass.Medium, | ||
200.dp, | ||
supportedSizeClasses = | ||
setOf(WindowHeightSizeClass.Medium, WindowHeightSizeClass.Expanded) | ||
) | ||
} | ||
|
||
@Test | ||
fun widthSizeClassToString() { | ||
assertEquals(WindowWidthSizeClass.Compact.toString(), "WindowWidthSizeClass.Compact") | ||
assertEquals(WindowWidthSizeClass.Medium.toString(), "WindowWidthSizeClass.Medium") | ||
assertEquals(WindowWidthSizeClass.Expanded.toString(), "WindowWidthSizeClass.Expanded") | ||
} | ||
|
||
@Test | ||
fun heightSizeClassToString() { | ||
assertEquals(WindowHeightSizeClass.Compact.toString(), "WindowHeightSizeClass.Compact") | ||
assertEquals(WindowHeightSizeClass.Medium.toString(), "WindowHeightSizeClass.Medium") | ||
assertEquals(WindowHeightSizeClass.Expanded.toString(), "WindowHeightSizeClass.Expanded") | ||
} | ||
|
||
@Test | ||
fun widthSizeClassCompareTo() { | ||
// Less than | ||
assertTrue(WindowWidthSizeClass.Compact < WindowWidthSizeClass.Medium) | ||
assertTrue(WindowWidthSizeClass.Compact < WindowWidthSizeClass.Expanded) | ||
assertTrue(WindowWidthSizeClass.Medium < WindowWidthSizeClass.Expanded) | ||
|
||
assertFalse(WindowWidthSizeClass.Compact < WindowWidthSizeClass.Compact) | ||
assertFalse(WindowWidthSizeClass.Medium < WindowWidthSizeClass.Medium) | ||
assertFalse(WindowWidthSizeClass.Expanded < WindowWidthSizeClass.Expanded) | ||
|
||
assertFalse(WindowWidthSizeClass.Expanded < WindowWidthSizeClass.Medium) | ||
assertFalse(WindowWidthSizeClass.Expanded < WindowWidthSizeClass.Compact) | ||
assertFalse(WindowWidthSizeClass.Medium < WindowWidthSizeClass.Compact) | ||
|
||
// Less than or equal to | ||
assertTrue(WindowWidthSizeClass.Compact <= WindowWidthSizeClass.Compact) | ||
assertTrue(WindowWidthSizeClass.Compact <= WindowWidthSizeClass.Medium) | ||
assertTrue(WindowWidthSizeClass.Compact <= WindowWidthSizeClass.Expanded) | ||
assertTrue(WindowWidthSizeClass.Medium <= WindowWidthSizeClass.Medium) | ||
assertTrue(WindowWidthSizeClass.Medium <= WindowWidthSizeClass.Expanded) | ||
assertTrue(WindowWidthSizeClass.Expanded <= WindowWidthSizeClass.Expanded) | ||
|
||
assertFalse(WindowWidthSizeClass.Expanded <= WindowWidthSizeClass.Medium) | ||
assertFalse(WindowWidthSizeClass.Expanded <= WindowWidthSizeClass.Compact) | ||
assertFalse(WindowWidthSizeClass.Medium <= WindowWidthSizeClass.Compact) | ||
|
||
// Greater than | ||
assertTrue(WindowWidthSizeClass.Expanded > WindowWidthSizeClass.Medium) | ||
assertTrue(WindowWidthSizeClass.Expanded > WindowWidthSizeClass.Compact) | ||
assertTrue(WindowWidthSizeClass.Medium > WindowWidthSizeClass.Compact) | ||
|
||
assertFalse(WindowWidthSizeClass.Expanded > WindowWidthSizeClass.Expanded) | ||
assertFalse(WindowWidthSizeClass.Medium > WindowWidthSizeClass.Medium) | ||
assertFalse(WindowWidthSizeClass.Compact > WindowWidthSizeClass.Compact) | ||
|
||
assertFalse(WindowWidthSizeClass.Compact > WindowWidthSizeClass.Medium) | ||
assertFalse(WindowWidthSizeClass.Compact > WindowWidthSizeClass.Expanded) | ||
assertFalse(WindowWidthSizeClass.Medium > WindowWidthSizeClass.Expanded) | ||
|
||
// Greater than or equal to | ||
assertTrue(WindowWidthSizeClass.Expanded >= WindowWidthSizeClass.Expanded) | ||
assertTrue(WindowWidthSizeClass.Expanded >= WindowWidthSizeClass.Medium) | ||
assertTrue(WindowWidthSizeClass.Expanded >= WindowWidthSizeClass.Compact) | ||
assertTrue(WindowWidthSizeClass.Medium >= WindowWidthSizeClass.Medium) | ||
assertTrue(WindowWidthSizeClass.Medium >= WindowWidthSizeClass.Compact) | ||
assertTrue(WindowWidthSizeClass.Compact >= WindowWidthSizeClass.Compact) | ||
|
||
assertFalse(WindowWidthSizeClass.Compact >= WindowWidthSizeClass.Medium) | ||
assertFalse(WindowWidthSizeClass.Compact >= WindowWidthSizeClass.Expanded) | ||
assertFalse(WindowWidthSizeClass.Medium >= WindowWidthSizeClass.Expanded) | ||
} | ||
|
||
@Test | ||
fun heightSizeClassCompareTo() { | ||
// Less than | ||
assertTrue(WindowHeightSizeClass.Compact < WindowHeightSizeClass.Medium) | ||
assertTrue(WindowHeightSizeClass.Compact < WindowHeightSizeClass.Expanded) | ||
assertTrue(WindowHeightSizeClass.Medium < WindowHeightSizeClass.Expanded) | ||
|
||
assertFalse(WindowHeightSizeClass.Compact < WindowHeightSizeClass.Compact) | ||
assertFalse(WindowHeightSizeClass.Medium < WindowHeightSizeClass.Medium) | ||
assertFalse(WindowHeightSizeClass.Expanded < WindowHeightSizeClass.Expanded) | ||
|
||
assertFalse(WindowHeightSizeClass.Expanded < WindowHeightSizeClass.Medium) | ||
assertFalse(WindowHeightSizeClass.Expanded < WindowHeightSizeClass.Compact) | ||
assertFalse(WindowHeightSizeClass.Medium < WindowHeightSizeClass.Compact) | ||
|
||
// Less than or equal to | ||
assertTrue(WindowHeightSizeClass.Compact <= WindowHeightSizeClass.Compact) | ||
assertTrue(WindowHeightSizeClass.Compact <= WindowHeightSizeClass.Medium) | ||
assertTrue(WindowHeightSizeClass.Compact <= WindowHeightSizeClass.Expanded) | ||
assertTrue(WindowHeightSizeClass.Medium <= WindowHeightSizeClass.Medium) | ||
assertTrue(WindowHeightSizeClass.Medium <= WindowHeightSizeClass.Expanded) | ||
assertTrue(WindowHeightSizeClass.Expanded <= WindowHeightSizeClass.Expanded) | ||
|
||
assertFalse(WindowHeightSizeClass.Expanded <= WindowHeightSizeClass.Medium) | ||
assertFalse(WindowHeightSizeClass.Expanded <= WindowHeightSizeClass.Compact) | ||
assertFalse(WindowHeightSizeClass.Medium <= WindowHeightSizeClass.Compact) | ||
|
||
// Greater than | ||
assertTrue(WindowHeightSizeClass.Expanded > WindowHeightSizeClass.Medium) | ||
assertTrue(WindowHeightSizeClass.Expanded > WindowHeightSizeClass.Compact) | ||
assertTrue(WindowHeightSizeClass.Medium > WindowHeightSizeClass.Compact) | ||
|
||
assertFalse(WindowHeightSizeClass.Expanded > WindowHeightSizeClass.Expanded) | ||
assertFalse(WindowHeightSizeClass.Medium > WindowHeightSizeClass.Medium) | ||
assertFalse(WindowHeightSizeClass.Compact > WindowHeightSizeClass.Compact) | ||
|
||
assertFalse(WindowHeightSizeClass.Compact > WindowHeightSizeClass.Medium) | ||
assertFalse(WindowHeightSizeClass.Compact > WindowHeightSizeClass.Expanded) | ||
assertFalse(WindowHeightSizeClass.Medium > WindowHeightSizeClass.Expanded) | ||
|
||
// Greater than or equal to | ||
assertTrue(WindowHeightSizeClass.Expanded >= WindowHeightSizeClass.Expanded) | ||
assertTrue(WindowHeightSizeClass.Expanded >= WindowHeightSizeClass.Medium) | ||
assertTrue(WindowHeightSizeClass.Expanded >= WindowHeightSizeClass.Compact) | ||
assertTrue(WindowHeightSizeClass.Medium >= WindowHeightSizeClass.Medium) | ||
assertTrue(WindowHeightSizeClass.Medium >= WindowHeightSizeClass.Compact) | ||
assertTrue(WindowHeightSizeClass.Compact >= WindowHeightSizeClass.Compact) | ||
|
||
assertFalse(WindowHeightSizeClass.Compact >= WindowHeightSizeClass.Medium) | ||
assertFalse(WindowHeightSizeClass.Compact >= WindowHeightSizeClass.Expanded) | ||
assertFalse(WindowHeightSizeClass.Medium >= WindowHeightSizeClass.Expanded) | ||
} | ||
|
||
private fun assertWidthClass( | ||
expectedSizeClass: WindowWidthSizeClass, | ||
width: Dp, | ||
supportedSizeClasses: Set<WindowWidthSizeClass> = WindowWidthSizeClass.DefaultSizeClasses | ||
) { | ||
assertEquals(WindowWidthSizeClass.fromWidth(width, supportedSizeClasses), expectedSizeClass) | ||
} | ||
|
||
private fun assertHeightClass( | ||
expectedSizeClass: WindowHeightSizeClass, | ||
height: Dp, | ||
supportedSizeClasses: Set<WindowHeightSizeClass> = WindowHeightSizeClass.DefaultSizeClasses | ||
) { | ||
assertEquals(WindowHeightSizeClass.fromHeight(height, supportedSizeClasses), expectedSizeClass) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...class/src/nonJvmMain/kotlin/androidx/compose/material3/windowsizeclass/TestOnly.nonJvm.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/* | ||
* Copyright 2022 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 | ||
* | ||
* http://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 androidx.compose.material3.windowsizeclass | ||
|
||
actual annotation class TestOnly |
40 changes: 40 additions & 0 deletions
40
.../src/skikoMain/kotlin/androidx/compose/material3/windowsizeclass/WindowSizeClass.skiko.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright 2024 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 | ||
* | ||
* http://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 androidx.compose.material3.windowsizeclass | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.ExperimentalComposeUiApi | ||
import androidx.compose.ui.platform.LocalDensity | ||
import androidx.compose.ui.platform.LocalWindowInfo | ||
import androidx.compose.ui.unit.toSize | ||
|
||
/** | ||
* Calculates the window's [WindowSizeClass]. | ||
* | ||
* A new [WindowSizeClass] will be returned whenever a configuration change causes the width or | ||
* height of the window to cross a breakpoint, such as when the device is rotated or the window is | ||
* resized. | ||
*/ | ||
@OptIn(ExperimentalComposeUiApi::class) | ||
@Composable | ||
@ExperimentalMaterial3WindowSizeClassApi | ||
fun calculateWindowSizeClass(): WindowSizeClass { | ||
val density = LocalDensity.current | ||
val windowInfo = LocalWindowInfo.current | ||
val size = with(density) { windowInfo.containerSize.toSize().toDpSize() } | ||
return WindowSizeClass.calculateFromSize(size) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...ommonMain/kotlin/androidx/compose/mpp/demo/components/material3/WindowSizeClassExample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2024 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 | ||
* | ||
* http://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 androidx.compose.mpp.demo.components.material3 | ||
|
||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.material.Text | ||
import androidx.compose.material3.windowsizeclass.ExperimentalMaterial3WindowSizeClassApi | ||
import androidx.compose.material3.windowsizeclass.calculateWindowSizeClass | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
|
||
@OptIn(ExperimentalMaterial3WindowSizeClassApi::class) | ||
@Composable | ||
fun WindowSizeClassExample() { | ||
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) { | ||
val size = calculateWindowSizeClass() | ||
Text("width = ${size.widthSizeClass}\nheight = ${size.heightSizeClass}") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters