WindowSizeClass



WindowSizeClass represents breakpoints for a viewport. The recommended width and height break points are presented through windowWidthSizeClass and windowHeightSizeClass. Designers should design around the different combinations of width and height buckets. Developers should use the different buckets to specify the layouts. Ideally apps will work well in each bucket and by extension work well across multiple devices. If two devices are in similar buckets they should behave similarly.

This class is meant to be a common definition that can be shared across different device types. Application developers can use WindowSizeClass to have standard window buckets and design the UI around those buckets. Library developers can use these buckets to create different UI with respect to each bucket. This will help with consistency across multiple device types.

A library developer use-case can be creating some navigation UI library. For a size class with the WindowWidthSizeClass.EXPANDED width it might be more reasonable to have a side navigation. For a WindowWidthSizeClass.COMPACT width, a bottom navigation might be a better fit.

An application use-case can be applied for apps that use a list-detail pattern. The app can use the WindowWidthSizeClass.MEDIUM to determine if there is enough space to show the list and the detail side by side. If all apps follow this guidance then it will present a very consistent user experience.

In some cases developers or UI systems may decide to create their own break points. A developer might optimize for a window that is smaller than the supported break points or larger. A UI system might find that some break points are better suited than the recommended break points. In these cases developers may wish to specify their own custom break points and match using a when statement.

Summary

Public companion functions

WindowSizeClass
compute(dpWidth: Float, dpHeight: Float)

Computes the recommended WindowSizeClass for the given width and height in DP.

Cmn
WindowSizeClass
@ExperimentalWindowCoreApi
compute(widthPx: Int, heightPx: Int, density: Float)

Computes the WindowSizeClass for the given width and height in pixels with density.

Cmn

Public constructors

WindowSizeClass(widthDp: Int, heightDp: Int)

the primary constructor taking the bounds of the size class.

Cmn

Public functions

open operator Boolean
equals(other: Any?)
Cmn
open Int
Cmn
open String
Cmn

Public properties

Int

the height in DP for the size class.

Cmn
Int

the width in DP for the size class.

Cmn
WindowHeightSizeClass

Returns the WindowHeightSizeClass that corresponds to the heightDp.

Cmn
WindowWidthSizeClass

Returns the WindowWidthSizeClass that corresponds to the widthDp.

Cmn

Extension functions

Int
@ExperimentalWindowCoreApi
WindowSizeClass.scoreWithinAreaBounds(
    windowWidthDp: Int,
    windowHeightDp: Int
)

A scoring function to calculate how close the area of a WindowSizeClass is to the area of a window without exceeding it.

Cmn
Int

A scoring function to calculate how close the height of a WindowSizeClass is to heightDp without exceeding it.

Cmn
Int

A scoring function to calculate how close the width of a WindowSizeClass is to widthDp without exceeding it.

Cmn

Public companion functions

compute

fun compute(dpWidth: Float, dpHeight: Float): WindowSizeClass

Computes the recommended WindowSizeClass for the given width and height in DP.

Parameters
dpWidth: Float

width of a window in DP.

dpHeight: Float

height of a window in DP.

Returns
WindowSizeClass

WindowSizeClass that is recommended for the given dimensions.

See also
widestOrEqualWidthDp

for selecting from a custom set of WindowSizeClass.

compute

@ExperimentalWindowCoreApi
fun compute(widthPx: Int, heightPx: Int, density: Float): WindowSizeClass

Computes the WindowSizeClass for the given width and height in pixels with density.

Parameters
widthPx: Int

width of a window in PX.

heightPx: Int

height of a window in PX.

density: Float

density of the display where the window is shown.

Returns
WindowSizeClass

WindowSizeClass that is recommended for the given dimensions.

Public constructors

WindowSizeClass

WindowSizeClass(widthDp: Int, heightDp: Int)

the primary constructor taking the bounds of the size class.

Public functions

equals

open operator fun equals(other: Any?): Boolean

hashCode

open fun hashCode(): Int

toString

open fun toString(): String

Public properties

heightDp

val heightDpInt

the height in DP for the size class.

widthDp

val widthDpInt

the width in DP for the size class.

windowHeightSizeClass

val windowHeightSizeClassWindowHeightSizeClass

Returns the WindowHeightSizeClass that corresponds to the heightDp.

windowWidthSizeClass

val windowWidthSizeClassWindowWidthSizeClass

Returns the WindowWidthSizeClass that corresponds to the widthDp.

Extension functions

scoreWithinAreaBounds

@ExperimentalWindowCoreApi
fun WindowSizeClass.scoreWithinAreaBounds(
    windowWidthDp: Int,
    windowHeightDp: Int
): Int

A scoring function to calculate how close the area of a WindowSizeClass is to the area of a window without exceeding it.

Parameters
windowWidthDp: Int

the width of a window constraint.

windowHeightDp: Int

the height of a window constraint.

Returns
Int

an integer from -1 to Integer.MAX_VALUE where a larger value indicates a better match.

scoreWithinHeightDp

@ExperimentalWindowCoreApi
fun WindowSizeClass.scoreWithinHeightDp(heightDp: Int): Int

A scoring function to calculate how close the height of a WindowSizeClass is to heightDp without exceeding it.

Parameters
heightDp: Int

the height bound to try to match.

Returns
Int

an integer from -1 to Integer.MAX_VALUE where a larger value indicates a better match.

scoreWithinWidthDp

@ExperimentalWindowCoreApi
fun WindowSizeClass.scoreWithinWidthDp(widthDp: Int): Int

A scoring function to calculate how close the width of a WindowSizeClass is to widthDp without exceeding it.

Parameters
widthDp: Int

the width bound to try to match.

Returns
Int

an integer from -1 to Integer.MAX_VALUE where a larger value indicates a better match.