-
Notifications
You must be signed in to change notification settings - Fork 39
feat: checkbox list - android #363
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 all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3576e94
feat: wip - android checklist
exploIF f55649c
Merge remote-tracking branch 'origin/main' into @exploif/checklist
exploIF 2893386
chore: cleanup
exploIF 364118f
fix: layout calculation for bigger checkbox size
exploIF 383f3f6
feat: customize checbox color
exploIF 6a3b0d0
Merge remote-tracking branch 'origin/main' into @exploif/checklist
exploIF 345edcd
chore: cleanup
exploIF 7790f7a
chore: resolve conflicts
exploIF b7c1abc
chore: document checkbox list
exploIF 6300d60
feat: improve cursor handling for checkboxes
exploIF 571813a
Merge remote-tracking branch 'origin/main' into @exploif/checklist
exploIF 4332f9e
chore: resolve conflicts
exploIF 0fc4e8b
chore: resolve conflicts
exploIF f9ea2c6
chore: resolve cr comments
exploIF 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
94 changes: 94 additions & 0 deletions
94
android/src/main/java/com/swmansion/enriched/textinput/spans/EnrichedCheckboxListSpan.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,94 @@ | ||
| package com.swmansion.enriched.textinput.spans | ||
|
|
||
| import android.graphics.Canvas | ||
| import android.graphics.Paint | ||
| import android.text.Layout | ||
| import android.text.Spanned | ||
| import android.text.TextPaint | ||
| import android.text.style.LeadingMarginSpan | ||
| import android.text.style.LineHeightSpan | ||
| import android.text.style.MetricAffectingSpan | ||
| import androidx.core.graphics.withTranslation | ||
| import com.swmansion.enriched.textinput.spans.interfaces.EnrichedParagraphSpan | ||
| import com.swmansion.enriched.textinput.styles.HtmlStyle | ||
| import com.swmansion.enriched.utils.CheckboxDrawable | ||
|
|
||
| class EnrichedCheckboxListSpan( | ||
| var isChecked: Boolean, | ||
| private val htmlStyle: HtmlStyle, | ||
| ) : MetricAffectingSpan(), | ||
| LineHeightSpan, | ||
| LeadingMarginSpan, | ||
| EnrichedParagraphSpan { | ||
| override val dependsOnHtmlStyle: Boolean = true | ||
|
|
||
| private val checkboxDrawable = | ||
| CheckboxDrawable(htmlStyle.ulCheckboxBoxSize, htmlStyle.ulCheckboxBoxColor, isChecked).apply { | ||
| setBounds(0, 0, htmlStyle.ulCheckboxBoxSize, htmlStyle.ulCheckboxBoxSize) | ||
| } | ||
|
|
||
| override fun updateMeasureState(tp: TextPaint) { | ||
| // Do nothing, but inform layout that this span affects text metrics | ||
| } | ||
|
|
||
| override fun updateDrawState(tp: TextPaint) { | ||
| // Do nothing, but inform layout that this span affects text metrics | ||
| } | ||
|
|
||
| // Include checkbox size in text measurements to avoid clipping | ||
| override fun chooseHeight( | ||
| text: CharSequence, | ||
| start: Int, | ||
| end: Int, | ||
| spanstartv: Int, | ||
| v: Int, | ||
| fm: Paint.FontMetricsInt, | ||
| ) { | ||
| val checkboxSize = htmlStyle.ulCheckboxBoxSize | ||
| val currentLineHeight = fm.descent - fm.ascent | ||
|
|
||
| if (checkboxSize > currentLineHeight) { | ||
| val extraSpace = checkboxSize - currentLineHeight | ||
| val halfExtra = extraSpace / 2 | ||
|
|
||
| fm.ascent -= halfExtra | ||
| fm.descent += (extraSpace - halfExtra) | ||
|
|
||
| fm.top -= halfExtra | ||
| fm.bottom += (extraSpace - halfExtra) | ||
| } | ||
| } | ||
|
|
||
| override fun getLeadingMargin(first: Boolean): Int = | ||
| htmlStyle.ulCheckboxBoxSize + htmlStyle.ulCheckboxMarginLeft + htmlStyle.ulCheckboxGapWidth | ||
|
|
||
| override fun drawLeadingMargin( | ||
| canvas: Canvas, | ||
| paint: Paint, | ||
| x: Int, | ||
| dir: Int, | ||
| top: Int, | ||
| baseline: Int, | ||
| bottom: Int, | ||
| text: CharSequence, | ||
| start: Int, | ||
| end: Int, | ||
| first: Boolean, | ||
| layout: Layout?, | ||
| ) { | ||
| val spannedText = text as Spanned | ||
|
|
||
| if (spannedText.getSpanStart(this) == start) { | ||
| checkboxDrawable.update(isChecked) | ||
|
|
||
| val lineCenter = (top + bottom) / 2f | ||
| val drawableTop = lineCenter - (htmlStyle.ulCheckboxBoxSize / 2f) | ||
|
|
||
| canvas.withTranslation(x.toFloat() + htmlStyle.ulCheckboxMarginLeft, drawableTop) { | ||
| checkboxDrawable.draw(this) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| override fun rebuildWithStyle(htmlStyle: HtmlStyle): EnrichedCheckboxListSpan = EnrichedCheckboxListSpan(isChecked, htmlStyle) | ||
| } | ||
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.