Igor Demin | 0b58e6a | 2020-07-27 13:14:56 +0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2020 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.compose.ui.platform |
| 18 | |
| 19 | import androidx.compose.ui.graphics.Outline |
| 20 | import androidx.compose.ui.graphics.Shape |
| 21 | import androidx.compose.ui.unit.Density |
Andrey Kulikov | eb4ba29 | 2021-01-18 16:36:00 +0300 | [diff] [blame] | 22 | import androidx.compose.ui.unit.LayoutDirection |
Igor Demin | 0b58e6a | 2020-07-27 13:14:56 +0300 | [diff] [blame] | 23 | import androidx.compose.ui.unit.IntSize |
| 24 | import androidx.compose.ui.unit.toSize |
| 25 | |
| 26 | /** |
| 27 | * Class for storing outline. Recalculates outline when [size] or [shape] is changed. |
| 28 | * It' s needed so we don't have to recreate it every time we use it for rendering |
| 29 | * (it can be expensive to create outline every frame). |
| 30 | */ |
| 31 | internal class OutlineCache( |
Roman Sedaikin | af25233 | 2020-10-07 15:05:37 +0300 | [diff] [blame] | 32 | density: Density, |
Igor Demin | 0b58e6a | 2020-07-27 13:14:56 +0300 | [diff] [blame] | 33 | size: IntSize, |
Andrey Kulikov | eb4ba29 | 2021-01-18 16:36:00 +0300 | [diff] [blame] | 34 | shape: Shape, |
| 35 | layoutDirection: LayoutDirection |
Igor Demin | 0b58e6a | 2020-07-27 13:14:56 +0300 | [diff] [blame] | 36 | ) { |
Roman Sedaikin | af25233 | 2020-10-07 15:05:37 +0300 | [diff] [blame] | 37 | var density = density |
| 38 | set(value) { |
| 39 | if (value != field) { |
| 40 | field = value |
| 41 | update() |
| 42 | } |
| 43 | } |
| 44 | |
Igor Demin | 0b58e6a | 2020-07-27 13:14:56 +0300 | [diff] [blame] | 45 | var size = size |
| 46 | set(value) { |
| 47 | if (value != field) { |
| 48 | field = value |
| 49 | update() |
| 50 | } |
| 51 | } |
Roman Sedaikin | af25233 | 2020-10-07 15:05:37 +0300 | [diff] [blame] | 52 | |
Igor Demin | 0b58e6a | 2020-07-27 13:14:56 +0300 | [diff] [blame] | 53 | var shape = shape |
| 54 | set(value) { |
| 55 | if (value != field) { |
| 56 | field = value |
| 57 | update() |
| 58 | } |
| 59 | } |
| 60 | |
Andrey Kulikov | eb4ba29 | 2021-01-18 16:36:00 +0300 | [diff] [blame] | 61 | var layoutDirection = layoutDirection |
| 62 | set(value) { |
| 63 | if (value != field) { |
| 64 | field = value |
| 65 | update() |
| 66 | } |
| 67 | } |
| 68 | |
Igor Demin | 0b58e6a | 2020-07-27 13:14:56 +0300 | [diff] [blame] | 69 | var outline: Outline? = null |
| 70 | private set |
| 71 | |
| 72 | private fun update() { |
| 73 | outline = if (size != IntSize.Zero) { |
| 74 | val floatSize = size.toSize() |
Andrey Kulikov | eb4ba29 | 2021-01-18 16:36:00 +0300 | [diff] [blame] | 75 | shape.createOutline(floatSize, layoutDirection, density) |
Igor Demin | 0b58e6a | 2020-07-27 13:14:56 +0300 | [diff] [blame] | 76 | } else { |
| 77 | null |
| 78 | } |
| 79 | } |
| 80 | } |