|
| 1 | +/* |
| 2 | + * Copyright 2022 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 com.example.feedcompose.ui.components |
| 18 | + |
| 19 | +import androidx.compose.foundation.Indication |
| 20 | +import androidx.compose.foundation.IndicationInstance |
| 21 | +import androidx.compose.foundation.interaction.InteractionSource |
| 22 | +import androidx.compose.foundation.interaction.collectIsFocusedAsState |
| 23 | +import androidx.compose.foundation.interaction.collectIsHoveredAsState |
| 24 | +import androidx.compose.runtime.Composable |
| 25 | +import androidx.compose.runtime.State |
| 26 | +import androidx.compose.runtime.getValue |
| 27 | +import androidx.compose.runtime.remember |
| 28 | +import androidx.compose.ui.graphics.Color |
| 29 | +import androidx.compose.ui.graphics.Shape |
| 30 | +import androidx.compose.ui.graphics.SolidColor |
| 31 | +import androidx.compose.ui.graphics.drawOutline |
| 32 | +import androidx.compose.ui.graphics.drawscope.ContentDrawScope |
| 33 | +import androidx.compose.ui.graphics.drawscope.Stroke |
| 34 | +import androidx.compose.ui.unit.Dp |
| 35 | + |
| 36 | +class OutlinedFocusIndication( |
| 37 | + private val shape: Shape, |
| 38 | + private val outlineWidth: Dp, |
| 39 | + private val outlineColor: Color |
| 40 | +) : Indication { |
| 41 | + |
| 42 | + @Composable |
| 43 | + override fun rememberUpdatedInstance(interactionSource: InteractionSource): IndicationInstance { |
| 44 | + val isEnabledState = interactionSource.collectIsFocusedAsState() |
| 45 | + |
| 46 | + return remember(interactionSource) { |
| 47 | + OutlineIndicationInstance( |
| 48 | + shape = shape, |
| 49 | + outlineWidth = outlineWidth, |
| 50 | + outlineColor = outlineColor, |
| 51 | + isEnabledState = isEnabledState |
| 52 | + ) |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +private class OutlineIndicationInstance( |
| 58 | + private val shape: Shape, |
| 59 | + private val outlineWidth: Dp, |
| 60 | + private val outlineColor: Color, |
| 61 | + isEnabledState: State<Boolean> |
| 62 | +) : IndicationInstance { |
| 63 | + private val isEnabled by isEnabledState |
| 64 | + |
| 65 | + override fun ContentDrawScope.drawIndication() { |
| 66 | + drawContent() |
| 67 | + if (isEnabled) { |
| 68 | + drawOutline( |
| 69 | + outline = shape.createOutline( |
| 70 | + size = size, |
| 71 | + layoutDirection = layoutDirection, |
| 72 | + density = this |
| 73 | + ), |
| 74 | + brush = SolidColor(outlineColor), |
| 75 | + style = Stroke(width = outlineWidth.toPx()) |
| 76 | + ) |
| 77 | + } |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +class HighlightIndication( |
| 82 | + private val highlightColor: Color = Color.White, |
| 83 | + private val alpha: Float = 0.2f, |
| 84 | + private val isEnabled: (isFocused: Boolean, isHovered: Boolean) -> Boolean = { isFocused, isHovered -> |
| 85 | + isFocused || isHovered |
| 86 | + } |
| 87 | +) : Indication { |
| 88 | + |
| 89 | + @Composable |
| 90 | + override fun rememberUpdatedInstance(interactionSource: InteractionSource): IndicationInstance { |
| 91 | + val isFocusedState = interactionSource.collectIsFocusedAsState() |
| 92 | + val isHoveredState = interactionSource.collectIsHoveredAsState() |
| 93 | + return remember(interactionSource) { |
| 94 | + HighlightIndicationInstance( |
| 95 | + isFocusedState = isFocusedState, |
| 96 | + isHoveredState = isHoveredState, |
| 97 | + isEnabled = isEnabled, |
| 98 | + highlightColor = highlightColor, |
| 99 | + alpha = alpha |
| 100 | + ) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +private class HighlightIndicationInstance( |
| 106 | + val highlightColor: Color = Color.White, |
| 107 | + val alpha: Float = 0.2f, |
| 108 | + isFocusedState: State<Boolean>, |
| 109 | + isHoveredState: State<Boolean>, |
| 110 | + val isEnabled: (isFocused: Boolean, isHovered: Boolean) -> Boolean = { isFocused, isHovered -> |
| 111 | + isFocused || isHovered |
| 112 | + } |
| 113 | +) : IndicationInstance { |
| 114 | + private val isFocused by isFocusedState |
| 115 | + private val isHovered by isHoveredState |
| 116 | + |
| 117 | + override fun ContentDrawScope.drawIndication() { |
| 118 | + drawContent() |
| 119 | + if (isEnabled(isFocused, isHovered)) { |
| 120 | + drawRect( |
| 121 | + size = size, |
| 122 | + color = highlightColor, |
| 123 | + alpha = alpha |
| 124 | + ) |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments