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 /**
20  * A class used to specify the position of a sized box inside an available space. This is often used
21  * to specify how a parent layout should place its children.
22  */
23 class Alignment(val horizontal: Horizontal, val vertical: Vertical) {
24     /**
25      * Specifies how a parent should lay its children out horizontally, if the child has a width
26      * smaller than the parent.
27      */
28     @JvmInline
29     value class Horizontal private constructor(private val value: Int) {
30         companion object {
31             val Start: Horizontal = Horizontal(0)
32             val CenterHorizontally: Horizontal = Horizontal(1)
33             val End: Horizontal = Horizontal(2)
34         }
35     }
36 
37     /**
38      * Specifies how a parent should lay its children out vertically, if the child has a height
39      * smaller than the parent.
40      */
41     @JvmInline
42     value class Vertical private constructor(private val value: Int) {
43         companion object {
44             val Top: Vertical = Vertical(0)
45             val CenterVertically: Vertical = Vertical(1)
46             val Bottom: Vertical = Vertical(2)
47         }
48     }
49 
50     /** Common [Alignment] options used in layouts. */
51     companion object {
52         val TopStart: Alignment = Alignment(Horizontal.Start, Vertical.Top)
53         val TopCenter: Alignment = Alignment(Horizontal.CenterHorizontally, Vertical.Top)
54         val TopEnd: Alignment = Alignment(Horizontal.End, Vertical.Top)
55 
56         val CenterStart: Alignment = Alignment(Horizontal.Start, Vertical.CenterVertically)
57         val Center: Alignment = Alignment(Horizontal.CenterHorizontally, Vertical.CenterVertically)
58         val CenterEnd: Alignment = Alignment(Horizontal.End, Vertical.CenterVertically)
59 
60         val BottomStart: Alignment = Alignment(Horizontal.Start, Vertical.Bottom)
61         val BottomCenter: Alignment = Alignment(Horizontal.CenterHorizontally, Vertical.Bottom)
62         val BottomEnd: Alignment = Alignment(Horizontal.End, Vertical.Bottom)
63 
64         val Top: Vertical = Vertical.Top
65         val CenterVertically: Vertical = Vertical.CenterVertically
66         val Bottom: Vertical = Vertical.Bottom
67 
68         val Start: Horizontal = Horizontal.Start
69         val CenterHorizontally: Horizontal = Horizontal.CenterHorizontally
70         val End: Horizontal = Horizontal.End
71     }
72 
equalsnull73     override fun equals(other: Any?): Boolean {
74         if (this === other) return true
75         if (javaClass != other?.javaClass) return false
76 
77         other as Alignment
78 
79         if (horizontal != other.horizontal) return false
80         if (vertical != other.vertical) return false
81 
82         return true
83     }
84 
hashCodenull85     override fun hashCode(): Int {
86         var result = horizontal.hashCode()
87         result = 31 * result + vertical.hashCode()
88         return result
89     }
90 
toStringnull91     override fun toString(): String {
92         return "Alignment(horizontal=$horizontal, vertical=$vertical)"
93     }
94 }
95