|
| 1 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | + |
| 5 | +package mozilla.components.compose.cfr |
| 6 | + |
| 7 | +import android.view.View |
| 8 | +import androidx.annotation.VisibleForTesting |
| 9 | +import androidx.compose.material.Text |
| 10 | +import androidx.compose.runtime.Composable |
| 11 | +import androidx.compose.ui.graphics.Color |
| 12 | +import androidx.compose.ui.graphics.toArgb |
| 13 | +import androidx.compose.ui.unit.Dp |
| 14 | +import androidx.compose.ui.unit.dp |
| 15 | +import mozilla.components.compose.cfr.CFRPopup.IndicatorDirection |
| 16 | +import mozilla.components.compose.cfr.CFRPopup.PopupAlignment |
| 17 | +import java.lang.ref.WeakReference |
| 18 | + |
| 19 | +/** |
| 20 | + * Properties used to customize the behavior of a [CFRPopup]. |
| 21 | + * |
| 22 | + * @property popupWidth Width of the popup. Defaults to [CFRPopup.DEFAULT_WIDTH]. |
| 23 | + * @property popupAlignment Where in relation to it's anchor should the popup be placed. |
| 24 | + * @property popupBodyColors One or more colors serving as the popup background. |
| 25 | + * If more colors are provided they will be used in a gradient. |
| 26 | + * @property popupVerticalOffset Vertical distance between the indicator arrow and the anchor. |
| 27 | + * This only applies if [overlapAnchor] is `false`. |
| 28 | + * @property dismissButtonColor The tint color that should be applied to the dismiss button. |
| 29 | + * @property dismissOnBackPress Whether the popup can be dismissed by pressing the back button. |
| 30 | + * If true, pressing the back button will also call onDismiss(). |
| 31 | + * @property dismissOnClickOutside Whether the popup can be dismissed by clicking outside the |
| 32 | + * popup's bounds. If true, clicking outside the popup will call onDismiss(). |
| 33 | + * @property overlapAnchor How the popup's indicator will be shown in relation to the anchor: |
| 34 | + * - true - indicator will be shown exactly in the middle horizontally and vertically |
| 35 | + * - false - indicator will be shown horizontally in the middle of the anchor but immediately below or above it |
| 36 | + * @property indicatorDirection The direction the indicator arrow is pointing. |
| 37 | + * @property indicatorArrowStartOffset Maximum distance between the popup start and the indicator arrow. |
| 38 | + * If there isn't enough space this could automatically be overridden up to 0 such that |
| 39 | + * the indicator arrow will be pointing to the middle of the anchor. |
| 40 | + */ |
| 41 | +data class CFRPopupProperties( |
| 42 | + val popupWidth: Dp = CFRPopup.DEFAULT_WIDTH.dp, |
| 43 | + val popupAlignment: PopupAlignment = PopupAlignment.BODY_TO_ANCHOR_CENTER, |
| 44 | + val popupBodyColors: List<Int> = listOf(Color.Blue.toArgb()), |
| 45 | + val popupVerticalOffset: Dp = CFRPopup.DEFAULT_VERTICAL_OFFSET.dp, |
| 46 | + val dismissButtonColor: Int = Color.Black.toArgb(), |
| 47 | + val dismissOnBackPress: Boolean = true, |
| 48 | + val dismissOnClickOutside: Boolean = true, |
| 49 | + val overlapAnchor: Boolean = false, |
| 50 | + val indicatorDirection: IndicatorDirection = IndicatorDirection.UP, |
| 51 | + val indicatorArrowStartOffset: Dp = CFRPopup.DEFAULT_INDICATOR_START_OFFSET.dp, |
| 52 | +) |
| 53 | + |
| 54 | +/** |
| 55 | + * CFR - Contextual Feature Recommendation popup. |
| 56 | + * |
| 57 | + * @param anchor [View] that will serve as the anchor of the popup and serve as lifecycle owner |
| 58 | + * for this popup also. |
| 59 | + * @param properties [CFRPopupProperties] allowing to customize the popup appearance and behavior. |
| 60 | + * @param onDismiss Callback for when the popup is dismissed indicating also if the dismissal |
| 61 | + * was explicit - by tapping the "X" button or not. |
| 62 | + * @param text [Text] already styled and ready to be shown in the popup. |
| 63 | + * @param action Optional other composable to show just below the popup text. |
| 64 | + */ |
| 65 | +class CFRPopup( |
| 66 | + @get:VisibleForTesting internal val anchor: View, |
| 67 | + @get:VisibleForTesting internal val properties: CFRPopupProperties, |
| 68 | + @get:VisibleForTesting internal val onDismiss: (Boolean) -> Unit = {}, |
| 69 | + @get:VisibleForTesting internal val text: @Composable (() -> Unit), |
| 70 | + @get:VisibleForTesting internal val action: @Composable (() -> Unit) = {}, |
| 71 | +) { |
| 72 | + // This is just a facade for the CFRPopupFullScreenLayout composable offering a cleaner API. |
| 73 | + |
| 74 | + @VisibleForTesting |
| 75 | + internal var popup: WeakReference<CFRPopupFullscreenLayout>? = null |
| 76 | + |
| 77 | + /** |
| 78 | + * Construct and display a styled CFR popup shown at the coordinates of [anchor]. |
| 79 | + * This popup will be dismissed when the user clicks on the "x" button or based on other user actions |
| 80 | + * with such behavior set in [CFRPopupProperties]. |
| 81 | + */ |
| 82 | + fun show() { |
| 83 | + anchor.post { |
| 84 | + CFRPopupFullscreenLayout(anchor, properties, onDismiss, text, action).apply { |
| 85 | + this.show() |
| 86 | + popup = WeakReference(this) |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Immediately dismiss this CFR popup. |
| 93 | + * The [onDismiss] callback won't be fired. |
| 94 | + */ |
| 95 | + fun dismiss() { |
| 96 | + popup?.get()?.dismiss() |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Possible direction for the arrow indicator of a CFR popup. |
| 101 | + * The direction is expressed in relation with the popup body containing the text. |
| 102 | + */ |
| 103 | + enum class IndicatorDirection { |
| 104 | + UP, |
| 105 | + DOWN, |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Possible alignments of the popup in relation to it's anchor. |
| 110 | + */ |
| 111 | + enum class PopupAlignment { |
| 112 | + /** |
| 113 | + * The popup body will be centered in the space occupied by the anchor. |
| 114 | + * Recommended to be used when the anchor is wider than the popup. |
| 115 | + */ |
| 116 | + BODY_TO_ANCHOR_CENTER, |
| 117 | + |
| 118 | + /** |
| 119 | + * The popup body will be shown aligned to exactly the anchor start. |
| 120 | + */ |
| 121 | + BODY_TO_ANCHOR_START, |
| 122 | + |
| 123 | + /** |
| 124 | + * The popup will be aligned such that the indicator arrow will point to exactly the middle of the anchor. |
| 125 | + * Recommended to be used when there are multiple widgets displayed horizontally so that this will allow |
| 126 | + * to indicate exactly which widget the popup refers to. |
| 127 | + */ |
| 128 | + INDICATOR_CENTERED_IN_ANCHOR, |
| 129 | + } |
| 130 | + |
| 131 | + companion object { |
| 132 | + /** |
| 133 | + * Default width for all CFRs. |
| 134 | + */ |
| 135 | + internal const val DEFAULT_WIDTH = 335 |
| 136 | + |
| 137 | + /** |
| 138 | + * Fixed horizontal padding. |
| 139 | + * Allows the close button to extend with 10dp more to the end and intercept touches to |
| 140 | + * a bit outside of the popup to ensure it respects a11y recommendations of 48dp size while |
| 141 | + * also offer a bit more space to the text. |
| 142 | + */ |
| 143 | + internal const val DEFAULT_EXTRA_HORIZONTAL_PADDING = 10 |
| 144 | + |
| 145 | + /** |
| 146 | + * How tall the indicator arrow should be. |
| 147 | + * This will also affect the width of the indicator's base which is double the height value. |
| 148 | + */ |
| 149 | + internal const val DEFAULT_INDICATOR_HEIGHT = 7 |
| 150 | + |
| 151 | + /** |
| 152 | + * Maximum distance between the popup start and the indicator. |
| 153 | + */ |
| 154 | + internal const val DEFAULT_INDICATOR_START_OFFSET = 30 |
| 155 | + |
| 156 | + /** |
| 157 | + * Corner radius for the popup body. |
| 158 | + */ |
| 159 | + internal const val DEFAULT_CORNER_RADIUS = 12 |
| 160 | + |
| 161 | + /** |
| 162 | + * Vertical distance between the indicator arrow and the anchor. |
| 163 | + */ |
| 164 | + internal const val DEFAULT_VERTICAL_OFFSET = 9 |
| 165 | + } |
| 166 | +} |
0 commit comments