Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed May 19, 2024
1 parent 0d63180 commit 27571f5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,18 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (textPos.compare(selectEnd) <= 0) {
selectStart.upData(pos = textPos)
upSelectedStart(
if (textPos.isTouch) textColumn.start else textColumn.end,
if (textPos.columnIndex < textLine.columns.lastIndex) textColumn.start else textColumn.end,
textLine.lineBottom + relativeOffset,
textLine.lineTop + relativeOffset
)
} else {
reverseStartCursor = true
reverseEndCursor = false
selectEnd.columnIndex++
selectStartMoveIndex(selectEnd)
selectEnd.upData(textPos)
upSelectedEnd(
if (selectEnd.isTouch || selectEnd.isLast) textColumn.end else textColumn.start,
if (textPos.columnIndex > -1) textColumn.end else textColumn.start,
textLine.lineBottom + relativeOffset
)
}
Expand All @@ -307,16 +308,17 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (textPos.compare(selectStart) >= 0) {
selectEnd.upData(textPos)
upSelectedEnd(
if (selectEnd.isTouch || selectEnd.isLast) textColumn.end else textColumn.start,
if (textPos.columnIndex > -1) textColumn.end else textColumn.start,
textLine.lineBottom + relativeOffset
)
} else {
reverseEndCursor = true
reverseStartCursor = false
selectStart.columnIndex--
selectEndMoveIndex(selectStart)
selectStart.upData(textPos)
upSelectedStart(
if (textPos.isTouch) textColumn.start else textColumn.end,
if (textPos.columnIndex < textLine.columns.lastIndex) textColumn.start else textColumn.end,
textLine.lineBottom + relativeOffset,
textLine.lineTop + relativeOffset
)
Expand Down Expand Up @@ -418,11 +420,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
}
}
val isLast = columns.first().start < x
val charIndex = if (isLast) columns.lastIndex else 0
val charIndex = if (isLast) columns.lastIndex + 1 else -1
val textColumn = if (isLast) columns.last() else columns.first()
touched.invoke(
relativeOffset,
TextPos(relativePos, lineIndex, charIndex, false, isLast),
TextPos(relativePos, lineIndex, charIndex),
textPage, textLine, textColumn
)
return
Expand Down Expand Up @@ -489,26 +491,22 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
relativePagePos: Int,
lineIndex: Int,
charIndex: Int,
isTouch: Boolean,
isLast: Boolean = false
) {
selectStart.relativePagePos = relativePagePos
selectStart.lineIndex = lineIndex
selectStart.columnIndex = charIndex
selectStart.isTouch = isTouch
selectStart.isLast = isLast
val textLine = relativePage(relativePagePos).getLine(lineIndex)
val textColumn = textLine.getColumn(charIndex)
upSelectedStart(
textColumn.start,
if (charIndex < textLine.columns.lastIndex) textColumn.start else textColumn.end,
textLine.lineBottom + relativeOffset(relativePagePos),
textLine.lineTop + relativeOffset(relativePagePos)
)
upSelectChars()
}

fun selectStartMoveIndex(textPos: TextPos) = textPos.run {
selectStartMoveIndex(relativePagePos, lineIndex, columnIndex, isTouch, isLast)
selectStartMoveIndex(relativePagePos, lineIndex, columnIndex)
}

/**
Expand All @@ -518,22 +516,21 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
relativePage: Int,
lineIndex: Int,
charIndex: Int,
isTouch: Boolean,
isLast: Boolean = false
) {
selectEnd.relativePagePos = relativePage
selectEnd.lineIndex = lineIndex
selectEnd.columnIndex = charIndex
selectEnd.isTouch = isTouch
selectEnd.isLast = isLast
val textLine = relativePage(relativePage).getLine(lineIndex)
val textColumn = textLine.getColumn(charIndex)
upSelectedEnd(textColumn.end, textLine.lineBottom + relativeOffset(relativePage))
upSelectedEnd(
if (charIndex > -1) textColumn.end else textColumn.start,
textLine.lineBottom + relativeOffset(relativePage)
)
upSelectChars()
}

fun selectEndMoveIndex(textPos: TextPos) = textPos.run {
selectEndMoveIndex(relativePagePos, lineIndex, columnIndex, isTouch, isLast)
selectEndMoveIndex(relativePagePos, lineIndex, columnIndex)
}

private fun upSelectChars() {
Expand All @@ -553,8 +550,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
val compareStart = textPos.compare(selectStart)
val compareEnd = textPos.compare(selectEnd)
column.selected = when {
compareStart == 0 -> selectStart.isTouch
compareEnd == 0 -> selectEnd.isTouch || selectEnd.isLast
compareStart == 0 -> true
compareEnd == 0 -> true
compareStart > 0 && compareEnd < 0 -> true
else -> false
}
Expand Down Expand Up @@ -624,27 +621,27 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at
if (column is TextColumn) {
when {
compareStart == 0 -> {
if (selectStart.isTouch) {
if (textPos.columnIndex < textLine.columns.lastIndex) {
builder.append(column.charData)
}
if (
textLine.isParagraphEnd
&& charIndex == textLine.charSize - 1
&& charIndex == textLine.columns.lastIndex
&& compareEnd != 0
) {
builder.append("\n")
}
}

compareEnd == 0 -> if (selectEnd.isTouch || selectEnd.isLast) {
compareEnd == 0 -> if (textPos.columnIndex > -1) {
builder.append(column.charData)
}

compareStart > 0 && compareEnd < 0 -> {
builder.append(column.charData)
if (
textLine.isParagraphEnd
&& charIndex == textLine.charSize - 1
&& charIndex == textLine.columns.lastIndex
) {
builder.append("\n")
}
Expand Down
24 changes: 4 additions & 20 deletions app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -404,17 +404,9 @@ class PageView(context: Context) : FrameLayout(context) {
fun selectStartMoveIndex(
relativePagePos: Int,
lineIndex: Int,
charIndex: Int,
isTouch: Boolean = true,
isLast: Boolean = false
charIndex: Int
) {
binding.contentTextView.selectStartMoveIndex(
relativePagePos,
lineIndex,
charIndex,
isTouch,
isLast
)
binding.contentTextView.selectStartMoveIndex(relativePagePos, lineIndex, charIndex)
}

fun selectStartMoveIndex(textPos: TextPos) {
Expand All @@ -428,17 +420,9 @@ class PageView(context: Context) : FrameLayout(context) {
fun selectEndMoveIndex(
relativePagePos: Int,
lineIndex: Int,
charIndex: Int,
isTouch: Boolean = true,
isLast: Boolean = false
charIndex: Int
) {
binding.contentTextView.selectEndMoveIndex(
relativePagePos,
lineIndex,
charIndex,
isTouch,
isLast
)
binding.contentTextView.selectEndMoveIndex(relativePagePos, lineIndex, charIndex)
}

fun selectEndMoveIndex(textPos: TextPos) {
Expand Down
8 changes: 6 additions & 2 deletions app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,13 @@ class ReadView(context: Context, attrs: AttributeSet) :
curPage.selectText(x, y) { textPos ->
val compare = initialTextPos.compare(textPos)
when {
compare >= 0 -> {
compare > 0 -> {
curPage.selectStartMoveIndex(textPos)
curPage.selectEndMoveIndex(initialTextPos)
curPage.selectEndMoveIndex(
initialTextPos.relativePagePos,
initialTextPos.lineIndex,
initialTextPos.columnIndex - 1
)
}

else -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,22 @@ data class TextPos(
var relativePagePos: Int,
var lineIndex: Int,
var columnIndex: Int,
var isTouch: Boolean = true,
var isLast: Boolean = false
) {

fun upData(
relativePos: Int,
lineIndex: Int,
charIndex: Int,
isTouch: Boolean,
isLast: Boolean
) {
this.relativePagePos = relativePos
this.lineIndex = lineIndex
this.columnIndex = charIndex
this.isTouch = isTouch
this.isLast = isLast
}

fun upData(pos: TextPos) {
relativePagePos = pos.relativePagePos
lineIndex = pos.lineIndex
columnIndex = pos.columnIndex
isTouch = pos.isTouch
isLast = pos.isLast
}

fun compare(pos: TextPos): Int {
Expand Down Expand Up @@ -65,8 +57,6 @@ data class TextPos(
relativePagePos = 0
lineIndex = -1
columnIndex = -1
isTouch = true
isLast = false
}

fun isSelected(): Boolean {
Expand Down

0 comments on commit 27571f5

Please sign in to comment.