Created
July 27, 2025 19:51
-
-
Save ArthurKun21/477aaf006ef913edcde818b908562fed to your computer and use it in GitHub Desktop.
Wraps the WindowSizeClass into CompositionLocalProvider
This file contains hidden or 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
| import androidx.compose.material3.windowsizeclass.WindowHeightSizeClass | |
| import androidx.compose.material3.windowsizeclass.WindowSizeClass | |
| import androidx.compose.material3.windowsizeclass.WindowWidthSizeClass | |
| import androidx.compose.runtime.Composable | |
| import androidx.compose.runtime.CompositionLocalProvider | |
| import androidx.compose.runtime.ProvidableCompositionLocal | |
| import androidx.compose.runtime.compositionLocalOf | |
| /** | |
| * A utility class that provides convenient properties to determine the current window size class | |
| * based on Material Design 3 breakpoints. | |
| * | |
| * This class wraps the [WindowSizeClass] and offers boolean properties to easily check | |
| * which width and height size class category the current window falls into. | |
| * | |
| * @param windowSizeClass The [WindowSizeClass] instance containing the current window dimensions | |
| * | |
| * @property isCompact True if the window width is in the Compact size class (typically < 600dp) | |
| * @property isMedium True if the window width is in the Medium size class (typically 600dp - 840dp) | |
| * @property isExpanded True if the window width is in the Expanded size class (typically > 840dp) | |
| * @property isHeightCompact True if the window height is in the Compact size class | |
| * @property isHeightMedium True if the window height is in the Medium size class | |
| * @property isHeightExpanded True if the window height is in the Expanded size class | |
| * @property isMobileCompact True if either width or height are in the Compact size class (typically mobile portrait mode) | |
| */ | |
| @Suppress("unused") | |
| class MaterialWindow(private val windowSizeClass: WindowSizeClass) { | |
| val isCompact: Boolean | |
| get() = windowSizeClass.widthSizeClass == WindowWidthSizeClass.Compact | |
| val isMedium: Boolean | |
| get() = windowSizeClass.widthSizeClass == WindowWidthSizeClass.Medium | |
| val isExpanded: Boolean | |
| get() = windowSizeClass.widthSizeClass == WindowWidthSizeClass.Expanded | |
| val isHeightCompact: Boolean | |
| get() = windowSizeClass.heightSizeClass == WindowHeightSizeClass.Compact | |
| val isHeightMedium: Boolean | |
| get() = windowSizeClass.heightSizeClass == WindowHeightSizeClass.Medium | |
| val isHeightExpanded: Boolean | |
| get() = windowSizeClass.heightSizeClass == WindowHeightSizeClass.Expanded | |
| val isMobileCompact: Boolean | |
| get() = isCompact || isHeightCompact | |
| } | |
| /** | |
| * A CompositionLocal that provides access to the MaterialWindow instance throughout the Compose UI tree. | |
| * | |
| * This CompositionLocal must be provided at a higher level in the composition hierarchy before it can be accessed. | |
| * If accessed without being provided, it will throw an error with the message "CompositionLocal MaterialWindow not present". | |
| * | |
| * @see MaterialWindow | |
| * @throws IllegalStateException if accessed without being provided in the composition | |
| */ | |
| val LocalMaterialWindow: ProvidableCompositionLocal<MaterialWindow> = compositionLocalOf { | |
| error("CompositionLocal MaterialWindow not present") | |
| } | |
| @Composable | |
| fun SetMaterialWindow( | |
| windowSizeClass: WindowSizeClass, | |
| content: @Composable () -> Unit | |
| ) { | |
| CompositionLocalProvider( | |
| LocalMaterialWindow provides MaterialWindow(windowSizeClass) | |
| ) { | |
| content() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment