From 209662f323fad1027b29d6f636872ddb2c87b9cd Mon Sep 17 00:00:00 2001 From: gedoor Date: Thu, 7 Oct 2021 12:40:18 +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/ReadBookActivity.kt | 31 +++------ .../app/ui/book/read/ReadBookBaseActivity.kt | 6 +- .../legado/app/ui/book/read/TextActionMenu.kt | 67 +++++++++++++++++-- 3 files changed, 74 insertions(+), 30 deletions(-) diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index c90f6b7bf..8d5d2b0bc 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -6,7 +6,6 @@ import android.content.Intent import android.content.res.Configuration import android.os.Bundle import android.view.* -import android.view.ViewGroup.LayoutParams.WRAP_CONTENT import androidx.activity.result.contract.ActivityResultContracts import androidx.core.view.get import androidx.core.view.isVisible @@ -475,27 +474,17 @@ class ReadBookActivity : ReadBookBaseActivity(), /** * 显示文本操作菜单 */ - override fun showTextActionMenu() = binding.run { - textActionMenu.contentView.measure( - View.MeasureSpec.UNSPECIFIED, - View.MeasureSpec.UNSPECIFIED + override fun showTextActionMenu() { + val nbh = if (ReadBookConfig.hideNavigationBar) navigationBarHeight else 0 + textActionMenu.show( + binding.textMenuPosition, + binding.root.height + nbh, + binding.textMenuPosition.x.toInt(), + binding.textMenuPosition.y.toInt(), + binding.cursorLeft.y.toInt() + binding.cursorLeft.height, + binding.cursorRight.x.toInt(), + binding.cursorRight.y.toInt() + binding.cursorRight.height ) - val popupHeight = textActionMenu.contentView.measuredHeight - val x = textMenuPosition.x.toInt() - var y = textMenuPosition.y.toInt() - popupHeight - if (y < statusBarHeight) { - y = (cursorLeft.y + cursorLeft.height).toInt() - } - if (cursorRight.y > y && cursorRight.y < y + popupHeight) { - y = (cursorRight.y + cursorRight.height).toInt() - } - if (!textActionMenu.isShowing) { - textActionMenu.showAtLocation( - textMenuPosition, Gravity.TOP or Gravity.START, x, y - ) - } else { - textActionMenu.update(x, y, WRAP_CONTENT, WRAP_CONTENT) - } } /** diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt index a116db5dc..d0db5a313 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookBaseActivity.kt @@ -149,11 +149,7 @@ abstract class ReadBookBaseActivity : binding.navigationBar.run { if (bottomDialog > 0 || binding.readMenu.isVisible) { val navigationBarHeight = - if (ReadBookConfig.hideNavigationBar) { - activity?.navigationBarHeight ?: 0 - } else { - 0 - } + if (ReadBookConfig.hideNavigationBar) navigationBarHeight else 0 when (navigationBarGravity) { Gravity.BOTTOM -> layoutParams = (layoutParams as FrameLayout.LayoutParams).apply { diff --git a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt index ccc3f09cb..fcb969ecb 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt @@ -9,9 +9,7 @@ import android.net.Uri import android.os.Build import android.speech.tts.TextToSpeech import android.speech.tts.UtteranceProgressListener -import android.view.LayoutInflater -import android.view.Menu -import android.view.ViewGroup +import android.view.* import android.widget.PopupWindow import androidx.annotation.RequiresApi import androidx.appcompat.view.SupportMenuInflater @@ -41,6 +39,7 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac private val ttsListener by lazy { TTSUtteranceListener() } + private val expandTextMenu get() = context.getPrefBoolean(PreferKey.expandTextMenu) init { @SuppressLint("InflateParams") @@ -86,7 +85,6 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac } fun upMenu() { - val expandTextMenu = context.getPrefBoolean(PreferKey.expandTextMenu) if (expandTextMenu) { adapter.setItems(menuItems) binding.ivMenuMore.gone() @@ -96,6 +94,67 @@ class TextActionMenu(private val context: Context, private val callBack: CallBac } } + fun show( + view: View, + windowHeight: Int, + startX: Int, + startTopY: Int, + startBottomY: Int, + endX: Int, + endBottomY: Int + ) { + if (expandTextMenu) { + when { + startTopY > 300 -> { + showAtLocation( + view, + Gravity.BOTTOM or Gravity.START, + startX, + windowHeight - startTopY + ) + } + endBottomY - startBottomY > 500 -> { + showAtLocation(view, Gravity.TOP or Gravity.START, startX, startBottomY) + } + else -> { + showAtLocation(view, Gravity.TOP or Gravity.START, endX, endBottomY) + } + } + } else { + contentView.measure( + View.MeasureSpec.UNSPECIFIED, + View.MeasureSpec.UNSPECIFIED, + ) + val popupHeight = contentView.measuredHeight + when { + startBottomY > 300 -> { + showAtLocation( + view, + Gravity.TOP or Gravity.START, + startX, + startTopY - popupHeight + ) + } + endBottomY - startBottomY > 500 -> { + showAtLocation( + view, + Gravity.TOP or Gravity.START, + startX, + startBottomY + ) + } + else -> { + showAtLocation( + view, + Gravity.TOP or Gravity.START, + endX, + endBottomY + ) + } + } + } + } + inner class Adapter(context: Context) : RecyclerAdapter(context) {