Skip to content

Instantly share code, notes, and snippets.

@alexvanyo
Last active February 11, 2023 08:55
Show Gist options
  • Save alexvanyo/4328f6cdc993e8add194ddd952424311 to your computer and use it in GitHub Desktop.
Save alexvanyo/4328f6cdc993e8add194ddd952424311 to your computer and use it in GitHub Desktop.
Composable method for observing the current window metrics as a Size
// 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