Repository navigation
@relextm19/add mention border radius #856
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
Merged
Changes from 15 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
cafe265
feat(web): added border radius
58692ae
feat: add rounded on android
a8d4924
feat(ios): corners only round at the start and end of a wrapped mention
relextm19 785670f
refactor(ios): remove obsolete operation
relextm19 8098958
feat(android): add border radius to mentions
relextm19 b488fb1
refactor(android): remove obsolete code
relextm19 7756fe0
refactor: revert branch unrelated changes
relextm19 11a62f2
refactor: cleanup
relextm19 30bc1b2
refactor(ios): collect mentions based on MentionsParam not bgColor
relextm19 7a13dc6
fix(android): lint fix
relextm19 c22155d
refactor(ios): only use the layout menager to draw mention backgrounds
relextm19 0b8f7ec
refactor(ios): move the style key declaration
relextm19 a7aca8f
fix(ios): dont draw mention background over newline
relextm19 6e967e8
refactor(ios): change the way bgKey is defined
relextm19 a1962b7
fix: revert the podfile.lock
relextm19 7f26478
fix(android): clip border to text bounds
relextm19 ccc5b2e
feat(android): only draw the visible lines
relextm19 4633946
feat: add docs for border radius
relextm19 8601914
Merge branch 'main' into @relextm19/add-mention-border-radius
szydlovsky 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
95 changes: 95 additions & 0 deletions
95
android/src/main/java/com/swmansion/enriched/common/MentionBackgroundDrawer.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,95 @@ | ||
| package com.swmansion.enriched.common | ||
|
|
||
| import android.graphics.Canvas | ||
| import android.graphics.Color | ||
| import android.graphics.Paint | ||
| import android.graphics.Path | ||
| import android.graphics.RectF | ||
| import android.text.Layout | ||
| import android.text.Spanned | ||
| import com.swmansion.enriched.common.spans.EnrichedMentionSpan | ||
|
|
||
| // Mention backgrounds are drawn by the view instead of the span, because spans only get whole-line | ||
| // bounds and cannot round the corners of a single mention. | ||
| object MentionBackgroundDrawer { | ||
| private val paint = Paint(Paint.ANTI_ALIAS_FLAG) | ||
| private val path = Path() | ||
| private val rect = RectF() | ||
| private val radii = FloatArray(8) | ||
|
|
||
| // Must be called with the canvas translated to the layout origin, before the text is drawn. | ||
| fun draw( | ||
| canvas: Canvas, | ||
| layout: Layout, | ||
| text: Spanned, | ||
| style: EnrichedStyle, | ||
| backgroundColorFor: (EnrichedMentionSpan, MentionStyle) -> Int = { _, mentionStyle -> | ||
| mentionStyle.backgroundColor | ||
| }, // lambda to handle press state bg color for enrichedText | ||
| ) { | ||
| val spans = text.getSpans(0, text.length, EnrichedMentionSpan::class.java) | ||
| for (span in spans) { | ||
| val mentionStyle = style.mentionsStyle[span.getIndicator()] ?: continue | ||
| val color = backgroundColorFor(span, mentionStyle) | ||
| if (Color.alpha(color) == 0) continue | ||
|
|
||
| val start = text.getSpanStart(span) | ||
| val end = text.getSpanEnd(span) | ||
|
|
||
| paint.color = color | ||
| drawSpanBackground(canvas, layout, start, end, mentionStyle.borderRadius) | ||
| } | ||
| } | ||
|
|
||
| private fun drawSpanBackground( | ||
| canvas: Canvas, | ||
| layout: Layout, | ||
| start: Int, | ||
| end: Int, | ||
| radius: Float, | ||
| ) { | ||
| val firstLine = layout.getLineForOffset(start) | ||
| val lastLine = layout.getLineForOffset(end - 1) | ||
|
|
||
| for (line in firstLine..lastLine) { | ||
| val isFirst = line == firstLine | ||
| val isLast = line == lastLine | ||
|
|
||
| val segmentStart = | ||
| if (isFirst) layout.getPrimaryHorizontal(start) else layout.getLineLeft(line) | ||
|
relextm19 marked this conversation as resolved.
Resolved
|
||
| val segmentEnd = | ||
| if (isLast) { | ||
| layout.getPrimaryHorizontal(end) | ||
| } else { | ||
| layout.getLineRight(line) | ||
| } | ||
|
|
||
| rect.set( | ||
| segmentStart, | ||
| layout.getLineTop(line).toFloat(), | ||
| segmentEnd, | ||
| layout.getLineBottom(line).toFloat(), | ||
| ) | ||
|
|
||
| // Only round the outer ends of a wrapped mention. | ||
| val leftRadius = if (isFirst) radius else 0f | ||
| val rightRadius = if (isLast) radius else 0f | ||
| // top-left | ||
| radii[0] = leftRadius | ||
| radii[1] = leftRadius | ||
| // top-right | ||
| radii[2] = rightRadius | ||
| radii[3] = rightRadius | ||
| // bottom-right | ||
| radii[4] = rightRadius | ||
| radii[5] = rightRadius | ||
| // bottom-left | ||
| radii[6] = leftRadius | ||
| radii[7] = leftRadius | ||
|
|
||
| path.reset() | ||
| path.addRoundRect(rect, radii, Path.Direction.CW) | ||
| canvas.drawPath(path, paint) | ||
| } | ||
| } | ||
| } | ||
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
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.