From 949cdb3a904eeba3678158dee67391c62b2e568e Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 7 Sep 2022 12:24:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/read/page/ContentTextView.kt | 41 ++++++++----------- .../legado/app/ui/book/read/page/PageView.kt | 5 ++- .../legado/app/ui/book/read/page/ReadView.kt | 36 +++++++++------- .../ui/book/read/page/entities/TextChapter.kt | 3 ++ .../ui/book/read/page/entities/TextChar.kt | 3 ++ .../ui/book/read/page/entities/TextLine.kt | 3 ++ .../ui/book/read/page/entities/TextPage.kt | 3 ++ .../app/ui/book/read/page/entities/TextPos.kt | 3 ++ 8 files changed, 58 insertions(+), 39 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt index e11d5c335..fe1e9e09d 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentTextView.kt @@ -281,16 +281,16 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at fun longPress( x: Float, y: Float, - select: (relativePage: Int, lineIndex: Int, charIndex: Int) -> Unit, + select: (textPos: TextPos) -> Unit, ) { - touch(x, y) { _, relativePos, _, lineIndex, _, charIndex, textChar -> + touch(x, y) { _, textPos, _, _, textChar -> if (textChar.isImage) { callBack.onImageLongPress(x, y, textChar.charData) } else { if (!selectAble) return@touch textChar.selected = true invalidate() - select(relativePos, lineIndex, charIndex) + select(textPos) } } } @@ -300,7 +300,7 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at * @return true:已处理, false:未处理 */ fun click(x: Float, y: Float): Boolean { - touch(x, y) { _, relativePos, textPage, lineIndex, textLine, charIndex, textChar -> + touch(x, y) { _, textPos, textPage, textLine, textChar -> } return false @@ -312,12 +312,12 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at fun selectText( x: Float, y: Float, - select: (relativePage: Int, lineIndex: Int, charIndex: Int) -> Unit, + select: (textPos: TextPos) -> Unit, ) { - touch(x, y) { _, relativePos, _, lineIndex, _, charIndex, textChar -> + touch(x, y) { _, textPos, _, _, textChar -> textChar.selected = true invalidate() - select(relativePos, lineIndex, charIndex) + select(textPos) } } @@ -325,11 +325,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at * 开始选择符移动 */ fun selectStartMove(x: Float, y: Float) { - touch(x, y) { relativeOffset, relativePos, _, lineIndex, textLine, charIndex, textChar -> - val pos = TextPos(relativePos, lineIndex, charIndex) - if (selectStart.compare(pos) != 0) { - if (pos.compare(selectEnd) <= 0) { - selectStart.upData(pos = pos) + touch(x, y) { relativeOffset, textPos, _, textLine, textChar -> + if (selectStart.compare(textPos) != 0) { + if (textPos.compare(selectEnd) <= 0) { + selectStart.upData(pos = textPos) upSelectedStart( textChar.start, textLine.lineBottom + relativeOffset, @@ -345,11 +344,10 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at * 结束选择符移动 */ fun selectEndMove(x: Float, y: Float) { - touch(x, y) { relativeOffset, relativePos, _, lineIndex, textLine, charIndex, textChar -> - val pos = TextPos(relativePos, lineIndex, charIndex) - if (pos.compare(selectEnd) != 0) { - if (pos.compare(selectStart) >= 0) { - selectEnd.upData(pos) + touch(x, y) { relativeOffset, textPos, _, textLine, textChar -> + if (textPos.compare(selectEnd) != 0) { + if (textPos.compare(selectStart) >= 0) { + selectEnd.upData(textPos) upSelectedEnd(textChar.end, textLine.lineBottom + relativeOffset) upSelectChars() } @@ -366,11 +364,9 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at y: Float, touched: ( relativeOffset: Float, - relativePos: Int, + textPos: TextPos, textPage: TextPage, - lineIndex: Int, textLine: TextLine, - charIndex: Int, textChar: TextChar ) -> Unit ) { @@ -390,9 +386,8 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at if (textChar.isTouch(x)) { touched.invoke( relativeOffset, - relativePos, textPage, - lineIndex, textLine, - charIndex, textChar + TextPos(relativePos, lineIndex, charIndex), + textPage, textLine, textChar ) return } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt index 3637438f1..048f9725b 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/PageView.kt @@ -14,6 +14,7 @@ import io.legado.app.help.config.ReadTipConfig import io.legado.app.model.ReadBook import io.legado.app.ui.book.read.ReadBookActivity import io.legado.app.ui.book.read.page.entities.TextPage +import io.legado.app.ui.book.read.page.entities.TextPos import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.ui.widget.BatteryView import io.legado.app.utils.activity @@ -321,7 +322,7 @@ class PageView(context: Context) : FrameLayout(context) { */ fun longPress( x: Float, y: Float, - select: (relativePagePos: Int, lineIndex: Int, charIndex: Int) -> Unit, + select: (textPos: TextPos) -> Unit, ) { return binding.contentTextView.longPress(x, y - headerHeight, select) } @@ -331,7 +332,7 @@ class PageView(context: Context) : FrameLayout(context) { */ fun selectText( x: Float, y: Float, - select: (relativePagePos: Int, lineIndex: Int, charIndex: Int) -> Unit, + select: (textPos: TextPos) -> Unit, ) { return binding.contentTextView.selectText(x, y - headerHeight, select) } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt index eafd9a7bb..3ac543f11 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ReadView.kt @@ -296,18 +296,18 @@ class ReadView(context: Context, attrs: AttributeSet) : */ private fun onLongPress() { kotlin.runCatching { - curPage.longPress(startX, startY) { relativePos, lineIndex, charIndex -> + curPage.longPress(startX, startY) { textPos: TextPos -> isTextSelected = true pressOnTextSelected = true - initialTextPos.upData(relativePos, lineIndex, charIndex) - val startPos = TextPos(relativePos, lineIndex, charIndex) - val endPos = TextPos(relativePos, lineIndex, charIndex) - val page = curPage.relativePage(relativePos) + initialTextPos.upData(textPos) + val startPos = textPos.copy() + val endPos = textPos.copy() + val page = curPage.relativePage(textPos.relativePagePos) val stringBuilder = StringBuilder() - var cIndex = charIndex - var lineStart = lineIndex - var lineEnd = lineIndex - for (index in lineIndex - 1 downTo 0) { + var cIndex = textPos.charIndex + var lineStart = textPos.lineIndex + var lineEnd = textPos.lineIndex + for (index in textPos.lineIndex - 1 downTo 0) { val textLine = page.getLine(index) if (textLine.isParagraphEnd) { break @@ -317,7 +317,7 @@ class ReadView(context: Context, attrs: AttributeSet) : cIndex += textLine.charSize } } - for (index in lineIndex until page.lineSize) { + for (index in textPos.lineIndex until page.lineSize) { val textLine = page.getLine(index) stringBuilder.append(textLine.text) lineEnd += 1 @@ -423,11 +423,15 @@ class ReadView(context: Context, attrs: AttributeSet) : * 选择文本 */ private fun selectText(x: Float, y: Float) { - curPage.selectText(x, y) { relativePagePos, lineIndex, charIndex -> - val compare = initialTextPos.compare(relativePagePos, lineIndex, charIndex) + curPage.selectText(x, y) { textPos -> + val compare = initialTextPos.compare(textPos) when { compare >= 0 -> { - curPage.selectStartMoveIndex(relativePagePos, lineIndex, charIndex) + curPage.selectStartMoveIndex( + textPos.relativePagePos, + textPos.lineIndex, + textPos.charIndex + ) curPage.selectEndMoveIndex( initialTextPos.relativePagePos, initialTextPos.lineIndex, @@ -440,7 +444,11 @@ class ReadView(context: Context, attrs: AttributeSet) : initialTextPos.lineIndex, initialTextPos.charIndex ) - curPage.selectEndMoveIndex(relativePagePos, lineIndex, charIndex) + curPage.selectEndMoveIndex( + textPos.relativePagePos, + textPos.lineIndex, + textPos.charIndex + ) } } } diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt index dfbf4a9cf..50d254e58 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChapter.kt @@ -2,6 +2,9 @@ package io.legado.app.ui.book.read.page.entities import kotlin.math.min +/** + * 章节信息 + */ @Suppress("unused") data class TextChapter( val position: Int, diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt index 518067182..f52525597 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextChar.kt @@ -1,5 +1,8 @@ package io.legado.app.ui.book.read.page.entities +/** + * 字符信息 + */ data class TextChar( val charData: String, var start: Float, diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt index 3ec66b3ca..176c4fea4 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextLine.kt @@ -4,6 +4,9 @@ import android.text.TextPaint import io.legado.app.ui.book.read.page.provider.ChapterProvider import io.legado.app.utils.textHeight +/** + * 行信息 + */ @Suppress("unused", "MemberVisibilityCanBePrivate") data class TextLine( var text: String = "", diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt index 3997b1ead..82966b138 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPage.kt @@ -11,6 +11,9 @@ import splitties.init.appCtx import java.text.DecimalFormat import kotlin.math.min +/** + * 页面信息 + */ @Suppress("unused", "MemberVisibilityCanBePrivate") data class TextPage( var index: Int = 0, diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPos.kt b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPos.kt index cc650c622..35dbfa589 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPos.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/entities/TextPos.kt @@ -1,5 +1,8 @@ package io.legado.app.ui.book.read.page.entities +/** + * 位置信息 + */ data class TextPos( var relativePagePos: Int, var lineIndex: Int,