Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import com.swmansion.enriched.styles.ParagraphStyles
import com.swmansion.enriched.styles.ParametrizedStyles

class EnrichedStyleManipulator(
val view: EnrichedTextInputView,
private val view: EnrichedTextInputView,
) {
val inlineStyles: InlineStyles = InlineStyles(view)
val paragraphStyles: ParagraphStyles = ParagraphStyles(view)
Expand All @@ -29,26 +29,53 @@ class EnrichedStyleManipulator(
name: TextStyle,
start: Int,
end: Int,
): Boolean =
when (EnrichedSpans.getStyleGroup(name)) {
TextStyleGroup.INLINE -> inlineStyles.removeStyle(name, start, end)
TextStyleGroup.PARAGRAPH -> paragraphStyles.removeStyle(name, start, end)
TextStyleGroup.LIST -> listStyles.removeStyle(name, start, end)
TextStyleGroup.PARAMETRIZED -> parametrizedStyles.removeStyle(name, start, end)
null -> false
): Boolean {
return when (EnrichedSpans.getStyleGroup(name)) {
TextStyleGroup.INLINE -> {
inlineStyles.removeStyle(name, start, end)
}

TextStyleGroup.PARAGRAPH -> {
val removed = paragraphStyles.removeStyle(name, start, end)
view.correctScrollPositionIfNeeded()

return removed
}

TextStyleGroup.LIST -> {
listStyles.removeStyle(name, start, end)
}

TextStyleGroup.PARAMETRIZED -> {
parametrizedStyles.removeStyle(name, start, end)
}

null -> {
false
}
}
}

internal fun toggleStyle(name: TextStyle) {
when (EnrichedSpans.getStyleGroup(name)) {
TextStyleGroup.INLINE -> inlineStyles.toggleStyle(name)
TextStyleGroup.INLINE -> {
inlineStyles.toggleStyle(name)
}

TextStyleGroup.PARAGRAPH -> paragraphStyles.toggleStyle(name)
TextStyleGroup.PARAGRAPH -> {
paragraphStyles.toggleStyle(name)
view.correctScrollPositionIfNeeded()
}

TextStyleGroup.LIST -> listStyles.toggleStyle(name)
TextStyleGroup.LIST -> {
listStyles.toggleStyle(name)
}

TextStyleGroup.PARAMETRIZED,
null,
-> Log.w("EnrichedTextInputView", "Unknown style: $name")
-> {
Log.w("EnrichedTextInputView", "Unknown style: $name")
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,9 @@ class EnrichedTextInputView : AppCompatEditText {
return super.onTouchEvent(event)
}

override fun canScrollVertically(direction: Int): Boolean = scrollEnabled
override fun canScrollVertically(direction: Int): Boolean = scrollEnabled && super.canScrollVertically(direction)

override fun canScrollHorizontally(direction: Int): Boolean = scrollEnabled
override fun canScrollHorizontally(direction: Int): Boolean = scrollEnabled && super.canScrollHorizontally(direction)

override fun onScrollChanged(
horiz: Int,
Expand Down Expand Up @@ -653,7 +653,7 @@ class EnrichedTextInputView : AppCompatEditText {
// we need to override isLayoutRequested to force EditText to scroll to the end of the new text
// immediately.
// Ivan Ihnatsiuk: let android calculate layout to avoid jumping behavior when we insert a new line.
// override fun isLayoutRequested(): Boolean = false
// override fun isLayoutRequested(): Boolean = false

fun afterUpdateTransaction() {
updateTypeface()
Expand Down Expand Up @@ -740,6 +740,14 @@ class EnrichedTextInputView : AppCompatEditText {
dispatcher?.dispatchEvent(event)
}

fun correctScrollPositionIfNeeded() {
if (!scrollEnabled) {
return
}

forceScrollToSelection()
}

private fun forceScrollToSelection() {
val textLayout = layout ?: return
val cursorOffset = selectionStart
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class EnrichedTextWatcher(
}
}
}
view.correctScrollPositionIfNeeded()
}

private fun applyStyles(s: Editable) {
Expand Down