-
Notifications
You must be signed in to change notification settings - Fork 32
feat: custom selection color #250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hryhoriiK97
merged 10 commits into
software-mansion-labs:main
from
xindixu:feat/custom-selection-color-pr
Apr 24, 2026
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
278035a
feat: custom selection color
xindixu 1f75881
add example color
xindixu 3f9f1c5
feedback: only use selectionHandleColor in android
xindixu 7438221
rename
xindixu 9ff8b6a
clean up jsdoc
xindixu 2752a6e
android: wrap each handle in try catch
xindixu 1a9caa3
ios: handle resetting color
xindixu 90edd78
android: fix
xindixu 79f23a7
feedback: minor changes
xindixu 6972ae2
feedback: refactor applySelectionHandleTint
xindixu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
android/src/main/java/com/swmansion/enriched/markdown/utils/text/view/TextSelectionColors.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| package com.swmansion.enriched.markdown.utils.text.view | ||
|
|
||
| import android.graphics.drawable.Drawable | ||
| import android.os.Build | ||
| import android.util.Log | ||
| import android.widget.TextView | ||
| import androidx.annotation.ColorInt | ||
| import androidx.core.graphics.drawable.DrawableCompat | ||
|
|
||
| private const val TAG = "TextSelectionColors" | ||
|
|
||
| private typealias HandleGetter = (TextView) -> Drawable? | ||
| private typealias HandleSetter = (TextView, Drawable) -> Unit | ||
|
|
||
| /** | ||
| * Applies selection highlight and (where supported) handle tinting to a [TextView]. | ||
| * | ||
| * Handle drawables are only tinted on API 29+ where the framework exposes getters; | ||
| * on older versions the handle theme defaults remain unchanged. | ||
| */ | ||
| fun TextView.applySelectionColors( | ||
| selectionColor: Int?, | ||
|
xindixu marked this conversation as resolved.
|
||
| selectionHandleColor: Int?, | ||
| ) { | ||
| selectionColor?.let { highlightColor = it } | ||
| selectionHandleColor?.let { applySelectionHandleTint(it) } | ||
| } | ||
|
|
||
| private fun TextView.applySelectionHandleTint( | ||
| @ColorInt color: Int, | ||
| ) { | ||
| if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) return | ||
|
|
||
| val handles: List<Pair<HandleGetter, HandleSetter>> = | ||
| listOf( | ||
| TextView::getTextSelectHandleLeft to { tv, d -> tv.setTextSelectHandleLeft(d) }, | ||
| TextView::getTextSelectHandle to { tv, d -> tv.setTextSelectHandle(d) }, | ||
| TextView::getTextSelectHandleRight to { tv, d -> tv.setTextSelectHandleRight(d) }, | ||
| ) | ||
|
|
||
| handles.forEach { (getter, setter) -> | ||
| try { | ||
| getter(this)?.mutate()?.also { DrawableCompat.setTint(it, color) }?.let { setter(this, it) } | ||
| } catch (e: LinkageError) { | ||
| Log.w(TAG, "Selection handle tint skipped: ${e.message}") | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.