[go: nahoru, domu]

[Text Selection] Add toolbar for text.

-- Add floating toolbar for post-M.
-- Add primary toolbar for pre-M.

Bug: 139311164
Test: ./gradlew test
Test: ./gradlew buildOnServer
Test: ./gradlew :ui:ui-framework:connectedAndroidTest
Test: ./gradlew :ui:ui-platform:connectedAndroidTest
Test: ./gradlew :ui:ui-text:connectedAndroidTest

RelNote: Add Toolbar for text related operations.

Change-Id: I49d533345151058eac9026d1456196331ed0bf45
RelNote: Add toolbar for text.
diff --git a/ui/ui-framework/src/main/java/androidx/ui/core/selection/SelectionManager.kt b/ui/ui-framework/src/main/java/androidx/ui/core/selection/SelectionManager.kt
index af18989..27ed466 100644
--- a/ui/ui-framework/src/main/java/androidx/ui/core/selection/SelectionManager.kt
+++ b/ui/ui-framework/src/main/java/androidx/ui/core/selection/SelectionManager.kt
@@ -27,6 +27,8 @@
 import androidx.ui.core.gesture.LongPressDragObserver
 import androidx.ui.core.hapticfeedback.HapticFeedback
 import androidx.ui.core.hapticfeedback.HapticFeedbackType
+import androidx.ui.core.texttoolbar.TextToolbar
+import androidx.ui.geometry.Rect
 import androidx.ui.text.AnnotatedString
 import androidx.ui.text.length
 import androidx.ui.text.subSequence
@@ -64,6 +66,11 @@
     var clipboardManager: ClipboardManager? = null
 
     /**
+     * [TextToolbar] to show floating toolbar(post-M) or primary toolbar(pre-M).
+     */
+    var textToolbar: TextToolbar? = null
+
+    /**
      * Layout Coordinates of the selection container.
      */
     var containerLayoutCoordinates: LayoutCoordinates? = null
@@ -223,9 +230,20 @@
         return selectedText
     }
 
-    internal fun copy() {
+    /**
+     * This function collects the selected text and the selected region as a Rectangle region,
+     * and pass these information to [TextToolbar] to make the FloatingToolbar show up in the
+     * proper place, and copy the text to the [ClipboardManager].
+     *
+     * @param selectedRegion selected region as [Rect]. The top is the top of the first selected
+     * line, and the bottom is the bottom of the last selected line. The left is the leftmost
+     * handle's horizontal coordinates, and the right is the rightmost handle's coordinates.
+     */
+    internal fun showSelectionToolbar(selectedRegion: Rect) {
         val selectedText = getSelectedText()
-        selectedText?.let { clipboardManager?.setText(it) }
+        selectedText?.let {
+            textToolbar?.showCopyMenu(selectedRegion, it)
+        }
     }
 
     // This is for PressGestureDetector to cancel the selection.