Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Jul 14, 2024
1 parent a875d50 commit c0f4fd3
Showing 1 changed file with 19 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import io.legado.app.utils.getCompatColor
import io.legado.app.utils.showDialogFragment
import io.legado.app.utils.toastOnUi
import java.util.concurrent.Executors
import kotlin.math.max
import kotlin.math.min

/**
Expand Down Expand Up @@ -271,59 +272,39 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
* 开始选择符移动
*/
fun selectStartMove(x: Float, y: Float) {
touchRough(x, y) { relativeOffset, textPos, _, textLine, textColumn ->
touchRough(x, y) { _, textPos, _, _, _ ->
if (selectStart.compare(textPos) == 0) {
return@touchRough
}
if (textPos.compare(selectEnd) <= 0) {
selectStart.upData(pos = textPos)
upSelectedStart(
if (textPos.columnIndex < textLine.columns.size) textColumn.start else textColumn.end,
textLine.lineBottom + relativeOffset,
textLine.lineTop + relativeOffset
)
selectStartMoveIndex(textPos)
} else {
reverseStartCursor = true
reverseEndCursor = false
selectEnd.columnIndex++
selectStartMoveIndex(selectEnd)
selectEnd.upData(textPos)
upSelectedEnd(
if (textPos.columnIndex > -1) textColumn.end else textColumn.start,
textLine.lineBottom + relativeOffset
)
selectEndMoveIndex(textPos)
}
upSelectChars()
}
}

/**
* 结束选择符移动
*/
fun selectEndMove(x: Float, y: Float) {
touchRough(x, y) { relativeOffset, textPos, _, textLine, textColumn ->
touchRough(x, y) { _, textPos, _, _, _ ->
if (textPos.compare(selectEnd) == 0) {
return@touchRough
}
if (textPos.compare(selectStart) >= 0) {
selectEnd.upData(textPos)
upSelectedEnd(
if (textPos.columnIndex > -1) textColumn.end else textColumn.start,
textLine.lineBottom + relativeOffset
)
selectEndMoveIndex(textPos)
} else {
reverseEndCursor = true
reverseStartCursor = false
selectStart.columnIndex--
selectEndMoveIndex(selectStart)
selectStart.upData(textPos)
upSelectedStart(
if (textPos.columnIndex < textLine.columns.size) textColumn.start else textColumn.end,
textLine.lineBottom + relativeOffset,
textLine.lineTop + relativeOffset
)
selectStartMoveIndex(textPos)
}
upSelectChars()
}
}

Expand Down Expand Up @@ -494,7 +475,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
) {
selectStart.relativePagePos = relativePagePos
selectStart.lineIndex = lineIndex
selectStart.columnIndex = charIndex
selectStart.columnIndex = max(0, charIndex)
val textLine = relativePage(relativePagePos).getLine(lineIndex)
val textColumn = textLine.getColumn(charIndex)
upSelectedStart(
Expand All @@ -519,8 +500,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
) {
selectEnd.relativePagePos = relativePage
selectEnd.lineIndex = lineIndex
selectEnd.columnIndex = charIndex
val textLine = relativePage(relativePage).getLine(lineIndex)
selectEnd.columnIndex = min(charIndex, textLine.columns.lastIndex)
val textColumn = textLine.getColumn(charIndex)
upSelectedEnd(
if (charIndex > -1) textColumn.end else textColumn.start,
Expand Down Expand Up @@ -549,12 +530,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (column is TextColumn) {
val compareStart = textPos.compare(selectStart)
val compareEnd = textPos.compare(selectEnd)
column.selected = when {
compareStart == 0 -> true
compareEnd == 0 -> true
compareStart > 0 && compareEnd < 0 -> true
else -> false
}
column.selected = compareStart >= 0 && compareEnd <= 0
column.isSearchResult =
column.selected && callBack.isSelectingSearchResult
if (column.isSearchResult) {
Expand Down Expand Up @@ -620,28 +596,23 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
val compareEnd = textPos.compare(selectEnd)
if (column is TextColumn) {
when {
compareStart == 0 -> {
if (textPos.columnIndex < textLine.columns.size) {
builder.append(column.charData)
}
if (
textLine.isParagraphEnd
&& charIndex == textLine.columns.lastIndex
&& compareEnd != 0
) {
builder.append("\n")
}
compareStart == -1 -> if (
selectStart.columnIndex == textLine.columns.size
&& charIndex == textLine.columns.lastIndex
) {
builder.append("\n")
}

compareEnd == 0 -> if (textPos.columnIndex > -1) {
builder.append(column.charData)
compareEnd == 1 -> if (selectEnd.columnIndex == -1 && charIndex == 0) {
builder.append("\n")
}

compareStart > 0 && compareEnd < 0 -> {
compareStart >= 0 && compareEnd <= 0 -> {
builder.append(column.charData)
if (
textLine.isParagraphEnd
&& charIndex == textLine.columns.lastIndex
&& compareEnd != 0
) {
builder.append("\n")
}
Expand Down

0 comments on commit c0f4fd3

Please sign in to comment.