|
| 1 | +package com.itsaky.androidide.plugins.aiagentgemini.ui |
| 2 | + |
| 3 | +import android.content.res.ColorStateList |
| 4 | +import android.graphics.Color |
| 5 | +import android.view.View |
| 6 | +import android.view.ViewGroup |
| 7 | +import androidx.annotation.ColorRes |
| 8 | +import androidx.core.content.ContextCompat |
| 9 | +import com.google.android.material.button.MaterialButton |
| 10 | +import com.google.android.material.divider.MaterialDivider |
| 11 | +import com.google.android.material.textfield.TextInputLayout |
| 12 | +import com.itsaky.androidide.plugins.aiagentgemini.R |
| 13 | + |
| 14 | +/** How much a pane button stands out: one filled action per section, the rest outlined. */ |
| 15 | +private enum class ButtonEmphasis { FILLED, OUTLINED } |
| 16 | + |
| 17 | +/** |
| 18 | + * Gives every Material button, text field and divider under [this] its Material 3 colours, outline |
| 19 | + * and ripple in code. The styles' `app:` items are dropped inside the host, so XML alone leaves |
| 20 | + * these controls on the host theme's values. |
| 21 | + * |
| 22 | + * @param outlinedButtonIds the buttons that are secondary actions; every other button is filled. |
| 23 | + */ |
| 24 | +internal fun View.applyPaneStyling(outlinedButtonIds: Set<Int>) { |
| 25 | + when (this) { |
| 26 | + is MaterialButton -> applyEmphasis( |
| 27 | + if (id in outlinedButtonIds) ButtonEmphasis.OUTLINED else ButtonEmphasis.FILLED |
| 28 | + ) |
| 29 | + is TextInputLayout -> applyOutline() |
| 30 | + is MaterialDivider -> applyHairline() |
| 31 | + } |
| 32 | + if (this is ViewGroup) { |
| 33 | + for (i in 0 until childCount) getChildAt(i).applyPaneStyling(outlinedButtonIds) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/** Container, label, icon, border and ripple for [emphasis], each with its disabled state. */ |
| 38 | +private fun MaterialButton.applyEmphasis(emphasis: ButtonEmphasis) { |
| 39 | + val filled = emphasis == ButtonEmphasis.FILLED |
| 40 | + val content = colors( |
| 41 | + if (filled) R.color.plugin_button_filled_content else R.color.plugin_button_outlined_content |
| 42 | + ) |
| 43 | + backgroundTintList = if (filled) { |
| 44 | + colors(R.color.plugin_button_filled_container) |
| 45 | + } else { |
| 46 | + ColorStateList.valueOf(Color.TRANSPARENT) |
| 47 | + } |
| 48 | + setTextColor(content) |
| 49 | + iconTint = content |
| 50 | + rippleColor = colors( |
| 51 | + if (filled) R.color.plugin_button_filled_ripple else R.color.plugin_button_outlined_ripple |
| 52 | + ) |
| 53 | + strokeColor = colors(R.color.plugin_button_outlined_stroke) |
| 54 | + strokeWidth = if (filled) 0 else resources.getDimensionPixelSize(R.dimen.button_stroke_width) |
| 55 | + cornerRadius = resources.getDimensionPixelSize(R.dimen.radius_md) |
| 56 | +} |
| 57 | + |
| 58 | +/** Outline, corners, hint and end icon of an outlined-box field. */ |
| 59 | +private fun TextInputLayout.applyOutline() { |
| 60 | + setBoxStrokeColorStateList(colors(R.color.plugin_box_stroke)) |
| 61 | + setBoxStrokeErrorColor(colors(R.color.plugin_error)) |
| 62 | + val radius = resources.getDimension(R.dimen.radius_md) |
| 63 | + setBoxCornerRadii(radius, radius, radius, radius) |
| 64 | + val hint = colors(R.color.plugin_text_muted) |
| 65 | + defaultHintTextColor = hint |
| 66 | + hintTextColor = hint |
| 67 | + setEndIconTintList(colors(R.color.plugin_on_surface_variant)) |
| 68 | +} |
| 69 | + |
| 70 | +private fun MaterialDivider.applyHairline() { |
| 71 | + setDividerColorResource(R.color.plugin_outline_variant) |
| 72 | + setDividerThicknessResource(R.dimen.divider_thickness) |
| 73 | +} |
| 74 | + |
| 75 | +/** Resolved against this view's context, which carries the plugin's resources. */ |
| 76 | +private fun View.colors(@ColorRes id: Int): ColorStateList = |
| 77 | + requireNotNull(ContextCompat.getColorStateList(context, id)) |
0 commit comments