1 /*
2 * Copyright 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package androidx.glance.layout
18
19 import androidx.annotation.DimenRes
20 import androidx.annotation.RestrictTo
21 import androidx.compose.ui.unit.Dp
22 import androidx.glance.GlanceModifier
23 import androidx.glance.unit.Dimension
24
25 /** Modifier to represent the width of an element. */
26 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
27 class WidthModifier(val width: Dimension) : GlanceModifier.Element
28
29 /** Sets the absolute width of an element, in [Dp]. */
GlanceModifiernull30 fun GlanceModifier.width(width: Dp): GlanceModifier = this.then(WidthModifier(Dimension.Dp(width)))
31
32 /** Set the width of a view from the value of a resource. */
33 fun GlanceModifier.width(@DimenRes width: Int): GlanceModifier =
34 this.then(WidthModifier(Dimension.Resource(width)))
35
36 /** Specifies that the width of the element should wrap its contents. */
37 fun GlanceModifier.wrapContentWidth(): GlanceModifier = this.then(WidthModifier(Dimension.Wrap))
38
39 /**
40 * Specifies that the width of the element should expand to the size of its parent. Note that if
41 * multiple elements within a linear container (e.g. Row or Column) have their width as
42 * [fillMaxWidth], then they will all share the remaining space.
43 */
44 fun GlanceModifier.fillMaxWidth(): GlanceModifier = this.then(WidthModifier(Dimension.Fill))
45
46 /** Modifier to represent the height of an element. */
47 @RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
48 class HeightModifier(val height: Dimension) : GlanceModifier.Element
49
50 /** Sets the absolute height of an element, in [Dp]. */
51 fun GlanceModifier.height(height: Dp): GlanceModifier =
52 this.then(HeightModifier(Dimension.Dp(height)))
53
54 /** Set the height of the view from a resource. */
55 fun GlanceModifier.height(@DimenRes height: Int): GlanceModifier =
56 this.then(HeightModifier(Dimension.Resource(height)))
57
58 /** Specifies that the height of the element should wrap its contents. */
59 fun GlanceModifier.wrapContentHeight(): GlanceModifier = this.then(HeightModifier(Dimension.Wrap))
60
61 /**
62 * Specifies that the height of the element should expand to the size of its parent. Note that if
63 * multiple elements within a linear container (e.g. Row or Column) have their height as
64 * expandHeight, then they will all share the remaining space.
65 */
66 fun GlanceModifier.fillMaxHeight(): GlanceModifier = this.then(HeightModifier(Dimension.Fill))
67
68 /** Sets both the width and height of an element, in [Dp]. */
69 fun GlanceModifier.size(size: Dp): GlanceModifier = this.width(size).height(size)
70
71 /** Sets both width and height of an element from a resource. */
72 fun GlanceModifier.size(@DimenRes size: Int): GlanceModifier = this.width(size).height(size)
73
74 /** Sets both the width and height of an element, in [Dp]. */
75 fun GlanceModifier.size(width: Dp, height: Dp): GlanceModifier = this.width(width).height(height)
76
77 /** Sets both the width and height of an element from resources. */
78 fun GlanceModifier.size(@DimenRes width: Int, @DimenRes height: Int): GlanceModifier =
79 this.width(width).height(height)
80
81 /** Wrap both the width and height's content. */
82 fun GlanceModifier.wrapContentSize(): GlanceModifier = this.wrapContentHeight().wrapContentWidth()
83
84 /** Set both the width and height to the maximum available space. */
85 fun GlanceModifier.fillMaxSize(): GlanceModifier = this.fillMaxWidth().fillMaxHeight()
86