Last active
February 11, 2023 08:55
-
-
Save alexvanyo/4328f6cdc993e8add194ddd952424311 to your computer and use it in GitHub Desktop.
Composable method for observing the current window metrics as a 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
// Copyright 2022 Google LLC. | |
// SPDX-License-Identifier: Apache-2.0 | |
/** | |
* Remembers the [Size] in pixels of the window corresponding to the current window | |
* metrics. | |
*/ | |
@Composable | |
fun Activity.rememberWindowSize(): Size { | |
val configuration = LocalConfiguration.current | |
// WindowMetricsCalculator implicitly depends on the configuration | |
// through the activity, so re-calculate it upon changes. | |
val windowMetrics = remember(configuration) { | |
WindowMetricsCalculator.getOrCreate() | |
.computeCurrentWindowMetrics(this) | |
} | |
return windowMetrics.bounds.toComposeRect().size | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment