diff --git a/app/src/main/java/helium314/keyboard/latin/ClipboardHistoryManager.kt b/app/src/main/java/helium314/keyboard/latin/ClipboardHistoryManager.kt index b3922b62d..283c00935 100644 --- a/app/src/main/java/helium314/keyboard/latin/ClipboardHistoryManager.kt +++ b/app/src/main/java/helium314/keyboard/latin/ClipboardHistoryManager.kt @@ -2,7 +2,6 @@ package helium314.keyboard.latin -import helium314.keyboard.latin.utils.withKeyboardCorners import helium314.keyboard.latin.utils.keyboardCornerRadiusPx import android.content.ClipboardManager import android.content.Context @@ -722,8 +721,6 @@ class ClipboardHistoryManager( latinIME.mSettings.getCustomTypeface()?.let { textView.typeface = it } textView.text = (if (isClipSensitive(inputType)) "*".repeat(content.length) else content) .take(200) // truncate displayed text for performance reasons - val clipIcon = latinIME.mKeyboardSwitcher.keyboard?.mIconsSet?.getIconDrawable(ToolbarKey.PASTE.name.lowercase()) - textView.setCompoundDrawablesRelativeWithIntrinsicBounds(clipIcon, null, null, null) textView.setOnClickListener { dontShowCurrentSuggestion = true latinIME.onTextInput(content.toString()) @@ -743,10 +740,7 @@ class ClipboardHistoryManager( val colors = latinIME.mSettings.current.mColors textView.setTextColor(colors.get(ColorType.KEY_TEXT)) - clipIcon?.let { colors.setColor(it, ColorType.KEY_ICON) } colors.setColor(closeButton, ColorType.REMOVE_SUGGESTION_ICON) - binding.root.background = binding.root.background?.withKeyboardCorners(latinIME.resources) - colors.setBackground(binding.root, ColorType.CLIPBOARD_SUGGESTION_BACKGROUND) return binding.root } @@ -897,8 +891,6 @@ class ClipboardHistoryManager( } val colors = latinIME.mSettings.current.mColors - binding.root.background = binding.root.background?.withKeyboardCorners(latinIME.resources) - colors.setBackground(binding.root, ColorType.CLIPBOARD_SUGGESTION_BACKGROUND) colors.setColor(extractButton, ColorType.REMOVE_SUGGESTION_ICON) colors.setColor(closeButton, ColorType.REMOVE_SUGGESTION_ICON) diff --git a/app/src/main/java/helium314/keyboard/latin/MathSuggestionManager.kt b/app/src/main/java/helium314/keyboard/latin/MathSuggestionManager.kt index 684e4bf69..703625452 100644 --- a/app/src/main/java/helium314/keyboard/latin/MathSuggestionManager.kt +++ b/app/src/main/java/helium314/keyboard/latin/MathSuggestionManager.kt @@ -69,7 +69,6 @@ class MathSuggestionManager(private val latinIME: LatinIME) { val colors = latinIME.mSettings.current.mColors textView.setTextColor(colors.get(ColorType.KEY_TEXT)) colors.setColor(closeButton, ColorType.REMOVE_SUGGESTION_ICON) - colors.setBackground(binding.root, ColorType.CLIPBOARD_SUGGESTION_BACKGROUND) mathSuggestionView = binding.root return mathSuggestionView diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionChip.kt b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionChip.kt new file mode 100644 index 000000000..da468d87a --- /dev/null +++ b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionChip.kt @@ -0,0 +1,114 @@ +// SPDX-License-Identifier: GPL-3.0-only +package helium314.keyboard.latin.suggestions + +import android.content.Context +import android.content.res.ColorStateList +import android.content.res.Resources +import android.graphics.Color +import android.graphics.drawable.Drawable +import android.graphics.drawable.GradientDrawable +import android.graphics.drawable.LayerDrawable +import android.graphics.drawable.RippleDrawable +import android.view.Gravity +import android.view.View +import android.widget.ImageView +import android.widget.LinearLayout +import androidx.core.content.ContextCompat +import androidx.core.graphics.ColorUtils +import androidx.core.view.updateLayoutParams +import helium314.keyboard.latin.R +import helium314.keyboard.latin.common.ColorType +import helium314.keyboard.latin.settings.Settings +import helium314.keyboard.latin.utils.dpToPx +import helium314.keyboard.latin.utils.keyboardCornerRadiusPx +import helium314.keyboard.latin.utils.withKeyboardCorners +import kotlin.math.roundToInt + +/** The shared look of the chips in the suggestion strip: clipboard, screenshot, calculation and text expander. */ +object SuggestionChip { + /** inset of toolbar_key_background, so a chip is as high as the visible background of a toolbar key */ + private const val TOOLBAR_KEY_INSET_DP = 3 + private const val CLOSE_BACKGROUND_MARGIN_DP = 3 + // how far the close button background is moved towards white; dark chips need much less to stand out + private const val CLOSE_LIGHTEN_ON_LIGHT = 0.55f + private const val CLOSE_LIGHTEN_ON_DARK = 0.14f + private const val CLOSE_ICON_FRACTION = 0.5f + + fun height(toolbarKeySize: Int, res: Resources): Int = + (toolbarKeySize - 2 * TOOLBAR_KEY_INSET_DP.dpToPx(res)).coerceAtLeast(1) + + /** The chip keeps [height] and stays vertically centered, no matter how high the view is. */ + fun background(context: Context, height: Int): Drawable? { + val chip = ContextCompat.getDrawable(context, R.drawable.clipboard_suggestion_background) + ?.withKeyboardCorners(context.resources) as? LayerDrawable ?: return null + val side = TOOLBAR_KEY_INSET_DP.dpToPx(context.resources) + chip.setLayerInset(0, side, 0, side, 0) + chip.setLayerHeight(0, height) + chip.setLayerGravity(0, Gravity.CENTER_VERTICAL or Gravity.FILL_HORIZONTAL) + Settings.getValues().mColors.setColor(chip, ColorType.CLIPBOARD_SUGGESTION_BACKGROUND) + return chip + } + + /** Styles the views from clipboard_suggestion, screenshot_suggestion and inline_suggestion, others are not changed. */ + fun apply(root: View, toolbarKeySize: Int) { + val close = root.findViewById(R.id.clipboard_suggestion_close) + ?: root.findViewById(R.id.screenshot_suggestion_close) + ?: root.findViewById(R.id.inline_suggestion_close) + ?: return + val res = root.resources + val height = height(toolbarKeySize, res) + val side = TOOLBAR_KEY_INSET_DP.dpToPx(res) + root.background = background(root.context, height) + // set after the background, which would otherwise define the padding + val thumbnail = root.findViewById(R.id.screenshot_paste_button) + // no extra padding at the end, so the close button sits at the edge of the chip + root.setPaddingRelative(if (thumbnail == null) side else side + 4.dpToPx(res), 0, side, 0) + thumbnail?.updateLayoutParams { + this.height = height - 2 * CLOSE_BACKGROUND_MARGIN_DP.dpToPx(res) + gravity = Gravity.CENTER_VERTICAL + topMargin = 0 + bottomMargin = 0 + } + root.findViewById(R.id.screenshot_extract_text_button)?.updateLayoutParams { + this.height = height + gravity = Gravity.CENTER_VERTICAL + } + applyToCloseButton(close, height) + } + + // the view is a square as high as the chip, so the drawn background has the same margin on all sides + private fun applyToCloseButton(close: ImageView, chipHeight: Int) { + val res = close.resources + close.updateLayoutParams { + width = chipHeight + height = chipHeight + gravity = Gravity.CENTER_VERTICAL + } + val margin = CLOSE_BACKGROUND_MARGIN_DP.dpToPx(res) + val size = (chipHeight - 2 * margin).coerceAtLeast(1) + val shape = GradientDrawable().apply { + // follows the corners of the chip around it + cornerRadius = (keyboardCornerRadiusPx(res).coerceAtMost(chipHeight / 2f) - margin).coerceAtLeast(0f) + setColor(closeBackgroundColor(Settings.getValues().mColors.get(ColorType.CLIPBOARD_SUGGESTION_BACKGROUND))) + } + val layers = LayerDrawable(arrayOf(shape)).apply { + setLayerSize(0, size, size) + setLayerGravity(0, Gravity.CENTER) + } + close.background = RippleDrawable(ColorStateList.valueOf(ColorUtils.setAlphaComponent(Color.GRAY, 0x40)), layers, null) + val iconSize = (size * CLOSE_ICON_FRACTION).roundToInt() + val padding = ((chipHeight - iconSize) / 2).coerceAtLeast(0) + close.setPadding(padding, padding, padding, padding) + } + + /** Always lighter than the chip, except on a (nearly) white chip, where that would be invisible. */ + internal fun closeBackgroundColor(chipColor: Int): Int { + val opaque = ColorUtils.setAlphaComponent(chipColor, 0xFF) + val luminance = ColorUtils.calculateLuminance(opaque) + return when { + luminance > 0.85 -> ColorUtils.blendARGB(opaque, Color.BLACK, 0.06f) + luminance > 0.2 -> ColorUtils.blendARGB(opaque, Color.WHITE, CLOSE_LIGHTEN_ON_LIGHT) + else -> ColorUtils.blendARGB(opaque, Color.WHITE, CLOSE_LIGHTEN_ON_DARK) + } + } +} diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelper.kt b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelper.kt index 8e16df703..4ce8f2507 100644 --- a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelper.kt +++ b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripLayoutHelper.kt @@ -6,7 +6,6 @@ package helium314.keyboard.latin.suggestions -import helium314.keyboard.latin.utils.withKeyboardCorners import android.content.Context import android.content.res.Resources import android.graphics.Bitmap @@ -36,7 +35,6 @@ import android.view.View import android.view.ViewGroup import android.widget.LinearLayout import android.widget.TextView -import androidx.core.content.ContextCompat import androidx.core.graphics.ColorUtils import helium314.keyboard.accessibility.AccessibilityUtils import helium314.keyboard.keyboard.KeyboardSwitcher @@ -66,6 +64,8 @@ internal class SuggestionStripLayoutHelper( val mDividerWidth: Int val mSuggestionsStripHeight: Int private var mSuggestionsCountInStrip: Int + /** visible height of text expander chips, set by the strip view as it depends on the toolbar key size */ + var chipHeight = 0 val mMoreSuggestionsRowHeight: Int private var mMaxMoreSuggestionsRow: Int val mMinMoreSuggestionsWidth: Float @@ -510,11 +510,7 @@ internal class SuggestionStripLayoutHelper( thumbW: Int, thumbH: Int ) { - val colors = Settings.getValues().mColors - val chipBg = ContextCompat.getDrawable(context, R.drawable.clipboard_suggestion_background)?.withKeyboardCorners(context.resources) - if (chipBg != null) { - colors.setColor(chipBg, ColorType.CLIPBOARD_SUGGESTION_BACKGROUND) - } + val chipBg = SuggestionChip.background(context, chipHeight) if (thumb != null && chipBg != null) { val layerDrawable = LayerDrawable(arrayOf(chipBg, thumb)).apply { setLayerGravity(1, Gravity.CENTER) @@ -564,20 +560,20 @@ internal class SuggestionStripLayoutHelper( } wordView.isEnabled = true return wordView - } else if (isImage && hasLabel) { - wordView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null) - wordView.compoundDrawablePadding = 0 + } else if ((isImage || isExpanderPreview(wordInfo)) && hasLabel) { val padH = 10.dpToPx(context.resources) + wordView.background = SuggestionChip.background(context, chipHeight) + // set after the background, which would otherwise define the padding wordView.setPadding(padH, 0, padH, 0) - - val colors = Settings.getValues().mColors - val chipBg = ContextCompat.getDrawable(context, R.drawable.clipboard_suggestion_background)?.withKeyboardCorners(context.resources) - if (chipBg != null) { - colors.setColor(chipBg, ColorType.CLIPBOARD_SUGGESTION_BACKGROUND) + if (positionInStrip == mCenterPositionInStrip && mMoreSuggestionsAvailable) { + wordView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, mMoreSuggestionsHint) + wordView.compoundDrawablePadding = -mMoreSuggestionsHint.intrinsicHeight + } else { + wordView.setCompoundDrawablesWithIntrinsicBounds(null, null, null, null) + wordView.compoundDrawablePadding = 0 } - wordView.background = chipBg - val textWidth = (width - 16.dpToPx(context.resources)).coerceAtLeast(10) + val textWidth = (width - 2 * padH).coerceAtLeast(10) val text = getEllipsizedTextWithSettingScaleX(word, textWidth, wordView.paint) val scaleX = wordView.textScaleX wordView.text = text diff --git a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt index 763415c8a..5cb90e850 100644 --- a/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt +++ b/app/src/main/java/helium314/keyboard/latin/suggestions/SuggestionStripView.kt @@ -579,6 +579,7 @@ class SuggestionStripView(context: Context, attrs: AttributeSet?, defStyle: Int) getMissingDictPromptView(currentLocale, isCompact) } else null + layoutHelper.chipHeight = SuggestionChip.height(getEffectiveKeyDimension(), resources) startIndexOfMoreSuggestions = layoutHelper.layoutAndReturnStartIndexOfMoreSuggestions( context, suggestedWords, suggestionsStrip, this, promptView ) @@ -626,6 +627,7 @@ class SuggestionStripView(context: Context, attrs: AttributeSet?, defStyle: Int) if (isShowingEmojiSuggestions) return clear() isExternalSuggestionVisible = true + SuggestionChip.apply(view, getEffectiveKeyDimension()) val targetView = if (addCloseButton) { val wrapper = LinearLayout(context) diff --git a/app/src/main/res/layout/clipboard_suggestion.xml b/app/src/main/res/layout/clipboard_suggestion.xml index b39f959fa..a5ec912d8 100644 --- a/app/src/main/res/layout/clipboard_suggestion.xml +++ b/app/src/main/res/layout/clipboard_suggestion.xml @@ -12,8 +12,8 @@ android:layout_width="0dp" android:layout_weight="1" android:layout_height="match_parent" - android:drawablePadding="3dp" - android:paddingHorizontal="12dp" + android:paddingStart="12dp" + android:paddingEnd="4dp" android:hapticFeedbackEnabled="false" android:soundEffectsEnabled="false" android:singleLine="true" diff --git a/app/src/main/res/layout/inline_suggestion.xml b/app/src/main/res/layout/inline_suggestion.xml index 6bfd6651d..08131bdc0 100644 --- a/app/src/main/res/layout/inline_suggestion.xml +++ b/app/src/main/res/layout/inline_suggestion.xml @@ -16,7 +16,8 @@ SPDX-License-Identifier: GPL-3.0-only android:layout_weight="1" android:layout_height="match_parent" android:drawablePadding="3dp" - android:paddingHorizontal="12dp" + android:paddingStart="12dp" + android:paddingEnd="4dp" android:hapticFeedbackEnabled="false" android:soundEffectsEnabled="false" android:singleLine="true" diff --git a/app/src/main/res/layout/screenshot_suggestion.xml b/app/src/main/res/layout/screenshot_suggestion.xml index 2e6cd1292..39d4e7252 100644 --- a/app/src/main/res/layout/screenshot_suggestion.xml +++ b/app/src/main/res/layout/screenshot_suggestion.xml @@ -7,7 +7,6 @@ android:layout_width="wrap_content" android:layout_height="match_parent" android:background="@drawable/clipboard_suggestion_background" - android:layout_marginVertical="3dp" android:layout_marginHorizontal="4dp" android:paddingStart="4dp" tools:ignore="RtlSymmetry">