From e29c9d8ad8666ecc80ad1a786fab2b5110bf3269 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sat, 22 Feb 2020 12:29:30 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../app/ui/book/read/ReadBookActivity.kt | 15 ++++++- .../legado/app/ui/book/read/TextActionMenu.kt | 27 +++++++++++++ .../read/page/ContentSelectActionCallback.kt | 39 ------------------- .../app/ui/book/read/page/ContentTextView.kt | 5 +++ .../app/ui/book/read/page/ContentView.kt | 2 + .../legado/app/ui/book/read/page/PageView.kt | 2 + .../legado/app/ui/widget/KeyboardToolPop.kt | 2 +- app/src/main/res/layout/popup_action_menu.xml | 8 ++++ 8 files changed, 58 insertions(+), 42 deletions(-) create mode 100644 app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt delete mode 100644 app/src/main/java/io/legado/app/ui/book/read/page/ContentSelectActionCallback.kt create mode 100644 app/src/main/res/layout/popup_action_menu.xml 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 50333f1fd..0ded2b2db 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 @@ -55,6 +55,7 @@ import org.jetbrains.anko.toast class ReadBookActivity : VMBaseActivity(R.layout.activity_book_read), View.OnTouchListener, PageView.CallBack, + TextActionMenu.CallBack, ContentTextView.CallBack, ReadMenu.CallBack, ReadAloudDialog.CallBack, @@ -65,6 +66,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo private val requestCodeEditSource = 111 private val requestCodeReplace = 312 private var menu: Menu? = null + private var textActionMenu: TextActionMenu? = null override val viewModel: ReadBookViewModel get() = getViewModel(ReadBookViewModel::class.java) @@ -340,6 +342,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo cursor_left.x = x - cursor_left.width cursor_left.y = y cursor_left.visible(true) + showTextActionMenu() } /** @@ -349,6 +352,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo cursor_right.x = x cursor_right.y = y cursor_right.visible(true) + showTextActionMenu() } /** @@ -359,8 +363,14 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo cursor_right.invisible() } - private fun showTextContextMenu(x: Float, y: Float) { - + /** + * 显示文本操作菜单 + */ + private fun showTextActionMenu() { + textActionMenu ?: let { + textActionMenu = TextActionMenu(this) + } + page_view.selectedText } /** @@ -556,6 +566,7 @@ class ReadBookActivity : VMBaseActivity(R.layout.activity_boo override fun onDestroy() { super.onDestroy() mHandler.removeCallbacks(keepScreenRunnable) + textActionMenu?.dismiss() } override fun observeLiveBus() { 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 new file mode 100644 index 000000000..6dfc30952 --- /dev/null +++ b/app/src/main/java/io/legado/app/ui/book/read/TextActionMenu.kt @@ -0,0 +1,27 @@ +package io.legado.app.ui.book.read + +import android.annotation.SuppressLint +import android.content.Context +import android.view.LayoutInflater +import android.view.ViewGroup +import android.widget.PopupWindow +import io.legado.app.R + + +class TextActionMenu(context: Context, callBack: CallBack) : + PopupWindow(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT) { + + init { + @SuppressLint("InflateParams") + contentView = LayoutInflater.from(context).inflate(R.layout.popup_action_menu, null) + + isTouchable = true + isOutsideTouchable = false + isFocusable = false + } + + + interface CallBack { + + } +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentSelectActionCallback.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentSelectActionCallback.kt deleted file mode 100644 index f274b2744..000000000 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentSelectActionCallback.kt +++ /dev/null @@ -1,39 +0,0 @@ -package io.legado.app.ui.book.read.page - -import android.view.ActionMode -import android.view.Menu -import android.view.MenuItem -import android.widget.TextView - -import io.legado.app.R -import io.legado.app.constant.EventBus -import io.legado.app.utils.postEvent - -class ContentSelectActionCallback(private val textView: TextView) : ActionMode.Callback { - - override fun onActionItemClicked(mode: ActionMode?, item: MenuItem?): Boolean { - when (item?.itemId) { - R.id.menu_replace -> { - val text = textView.text.substring(textView.selectionStart, textView.selectionEnd) - postEvent(EventBus.REPLACE, text) - mode?.finish() - return true - } - } - return false - } - - override fun onCreateActionMode(mode: ActionMode?, menu: Menu?): Boolean { - mode?.menuInflater?.inflate(R.menu.content_select_action, menu) - return true - } - - override fun onPrepareActionMode(mode: ActionMode?, menu: Menu?): Boolean { - return false - } - - override fun onDestroyActionMode(mode: ActionMode?) { - - } - -} \ No newline at end of file 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 0e2001f8e..4aceff37f 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 @@ -200,6 +200,11 @@ class ContentTextView(context: Context, attrs: AttributeSet?) : View(context, at activityCallBack?.onCancelSelect() } + val selectedText: String + get() { + return "" + } + interface CallBack { fun upSelectedStart(x: Float, y: Float) fun upSelectedEnd(x: Float, y: Float) diff --git a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt index be24f5714..3e2c9fc74 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/page/ContentView.kt @@ -121,6 +121,8 @@ class ContentView(context: Context) : FrameLayout(context) { content_text_view.cancelSelect() } + val selectedText: String get() = content_text_view.selectedText + fun scrollTo(pos: Int?) { if (pos != null) { content_text_view.post { 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 dbb7167f0..8832b3828 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 @@ -191,6 +191,8 @@ class PageView(context: Context, attrs: AttributeSet) : curPage.selectEndMove(x, y) } + val selectedText: String get() = curPage.selectedText + override val isScrollDelegate: Boolean get() = pageDelegate is ScrollPageDelegate diff --git a/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt b/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt index 01d36ca4a..4c61917bc 100644 --- a/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt +++ b/app/src/main/java/io/legado/app/ui/widget/KeyboardToolPop.kt @@ -23,7 +23,7 @@ class KeyboardToolPop( init { @SuppressLint("InflateParams") - this.contentView = LayoutInflater.from(context).inflate(R.layout.popup_keyboard_tool, null) + contentView = LayoutInflater.from(context).inflate(R.layout.popup_keyboard_tool, null) isTouchable = true isOutsideTouchable = false diff --git a/app/src/main/res/layout/popup_action_menu.xml b/app/src/main/res/layout/popup_action_menu.xml new file mode 100644 index 000000000..649534d76 --- /dev/null +++ b/app/src/main/res/layout/popup_action_menu.xml @@ -0,0 +1,8 @@ + +