Mihai Popa | b09b65c | 2019-10-14 13:04:21 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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.ui.layout.samples |
| 18 | |
| 19 | import androidx.compose.Composable |
Mihai Popa | b09b65c | 2019-10-14 13:04:21 +0100 | [diff] [blame] | 20 | import androidx.ui.core.Layout |
| 21 | import androidx.ui.core.Modifier |
Anastasia Soboleva | 2983fc4 | 2019-11-01 18:15:23 +0000 | [diff] [blame] | 22 | import androidx.ui.core.VerticalAlignmentLine |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame] | 23 | import androidx.ui.foundation.DrawBackground |
Mihai Popa | b09b65c | 2019-10-14 13:04:21 +0100 | [diff] [blame] | 24 | import androidx.ui.graphics.Color |
George Mount | 842c8c1 | 2020-01-08 16:03:42 -0800 | [diff] [blame] | 25 | import androidx.ui.unit.Dp |
| 26 | import androidx.ui.unit.ipx |
| 27 | import androidx.ui.unit.max |
| 28 | import androidx.ui.unit.min |
Mihai Popa | b09b65c | 2019-10-14 13:04:21 +0100 | [diff] [blame] | 29 | |
| 30 | /** |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame] | 31 | * Rect with two alignment lines [Start] and [End]. |
Mihai Popa | b09b65c | 2019-10-14 13:04:21 +0100 | [diff] [blame] | 32 | */ |
Matvei Malkov | 59bac36 | 2020-02-13 20:23:13 +0000 | [diff] [blame] | 33 | // TODO (popam): remove this and make FlexSamples where it's being used copy-pastable |
Anastasia Soboleva | 2983fc4 | 2019-11-01 18:15:23 +0000 | [diff] [blame] | 34 | @Composable |
| 35 | fun SizedRectangleWithLines( |
| 36 | modifier: Modifier = Modifier.None, |
| 37 | color: Color, |
| 38 | width: Dp? = null, |
| 39 | height: Dp? = null |
| 40 | ) { |
Anastasia Soboleva | 9474ff8 | 2020-02-19 19:02:15 +0000 | [diff] [blame] | 41 | Layout( |
| 42 | children = { }, |
| 43 | modifier = modifier + DrawBackground(color = color) |
| 44 | ) { _, constraints, _ -> |
Anastasia Soboleva | 2983fc4 | 2019-11-01 18:15:23 +0000 | [diff] [blame] | 45 | val widthPx = max(width?.toIntPx() ?: constraints.maxWidth, constraints.minWidth) |
| 46 | val heightPx = max(height?.toIntPx() ?: constraints.maxHeight, constraints.minHeight) |
| 47 | layout(widthPx, heightPx, mapOf(Start to 0.ipx, End to widthPx)) {} |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | /** |
Anastasia Soboleva | 2983fc4 | 2019-11-01 18:15:23 +0000 | [diff] [blame] | 52 | * Alignment lines for [SizedRectangleWithLines]. |
| 53 | */ |
| 54 | internal val Start = VerticalAlignmentLine(::min) |
| 55 | internal val End = VerticalAlignmentLine(::min) |