blob: 2b742dc28e8f1397e8be17335b0836cd3679c1a9 [file] [log] [blame]
Igor Demin0b58e6a2020-07-27 13:14:56 +03001/*
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
17package androidx.compose.ui.platform
18
19import androidx.compose.ui.graphics.Outline
20import androidx.compose.ui.graphics.Shape
21import androidx.compose.ui.unit.Density
Andrey Kulikoveb4ba292021-01-18 16:36:00 +030022import androidx.compose.ui.unit.LayoutDirection
Igor Demin0b58e6a2020-07-27 13:14:56 +030023import androidx.compose.ui.unit.IntSize
24import 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 */
31internal class OutlineCache(
Roman Sedaikinaf252332020-10-07 15:05:37 +030032 density: Density,
Igor Demin0b58e6a2020-07-27 13:14:56 +030033 size: IntSize,
Andrey Kulikoveb4ba292021-01-18 16:36:00 +030034 shape: Shape,
35 layoutDirection: LayoutDirection
Igor Demin0b58e6a2020-07-27 13:14:56 +030036) {
Roman Sedaikinaf252332020-10-07 15:05:37 +030037 var density = density
38 set(value) {
39 if (value != field) {
40 field = value
41 update()
42 }
43 }
44
Igor Demin0b58e6a2020-07-27 13:14:56 +030045 var size = size
46 set(value) {
47 if (value != field) {
48 field = value
49 update()
50 }
51 }
Roman Sedaikinaf252332020-10-07 15:05:37 +030052
Igor Demin0b58e6a2020-07-27 13:14:56 +030053 var shape = shape
54 set(value) {
55 if (value != field) {
56 field = value
57 update()
58 }
59 }
60
Andrey Kulikoveb4ba292021-01-18 16:36:00 +030061 var layoutDirection = layoutDirection
62 set(value) {
63 if (value != field) {
64 field = value
65 update()
66 }
67 }
68
Igor Demin0b58e6a2020-07-27 13:14:56 +030069 var outline: Outline? = null
70 private set
71
72 private fun update() {
73 outline = if (size != IntSize.Zero) {
74 val floatSize = size.toSize()
Andrey Kulikoveb4ba292021-01-18 16:36:00 +030075 shape.createOutline(floatSize, layoutDirection, density)
Igor Demin0b58e6a2020-07-27 13:14:56 +030076 } else {
77 null
78 }
79 }
80}