Преглед на файлове

Change display position of context menu

cooperku_kdanmobile преди 5 години
родител
ревизия
215811b448
променени са 2 файла, в които са добавени 30 реда и са изтрити 7 реда
  1. 29 7
      src/main/java/com/kdanmobile/reader/ReaderViewModel.kt
  2. 1 0
      src/main/res/values/dimens.xml

+ 29 - 7
src/main/java/com/kdanmobile/reader/ReaderViewModel.kt

@@ -5,6 +5,7 @@ import android.arch.lifecycle.MutableLiveData
 import android.arch.lifecycle.ViewModel
 import android.content.Context
 import android.graphics.Color
+import android.graphics.Point
 import android.net.Uri
 import android.os.Build
 import android.support.v4.content.ContextCompat
@@ -35,6 +36,7 @@ import com.kdanmobile.reader.screen.data.SignatureAttribute
 import com.kdanmobile.reader.screen.data.StampAttribute
 import com.kdanmobile.reader.screen.data.TextBoxAttribute
 import com.kdanmobile.reader.screen.handler.*
+import com.kdanmobile.reader.utils.DensityUtil
 import java.io.File
 import java.util.*
 import kotlin.collections.ArrayList
@@ -464,6 +466,7 @@ class ReaderViewModel(private val readerModelManager: ReaderModelManager, val ur
             override fun onShowContextMenu(type: KMPDFContextMenuCallback.ContextMenuType?, view: View?) {
                 if (null != view && null == popupWindow) {
 
+                    //  create context menu depended on ContextMenuType
                     val context = view.context!!
                     val contentView = when (type) {
                         KMPDFContextMenuCallback.ContextMenuType.Markup -> createMarkupContextMenuView(context)
@@ -486,11 +489,14 @@ class ReaderViewModel(private val readerModelManager: ReaderModelManager, val ur
                         }
                     }
 
+                    //  get the size of context menu
                     contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
                     val windowWidth = contentView.measuredWidth
                     val windowHeight = contentView.measuredHeight
                     popupWindow = PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true)
 
+                    //  get position y offset and margin of context menu
+                    val margin = context.resources.getDimensionPixelSize(R.dimen.reader_context_menu_window_position_margin)
                     val offset = context.resources.getDimensionPixelSize(when (type) {
                         KMPDFContextMenuCallback.ContextMenuType.Markup,
                         KMPDFContextMenuCallback.ContextMenuType.INK -> R.dimen.reader_context_menu_window_position_offset_markup
@@ -501,15 +507,31 @@ class ReaderViewModel(private val readerModelManager: ReaderModelManager, val ur
                         KMPDFContextMenuCallback.ContextMenuType.Stamp -> R.dimen.reader_context_menu_window_position_offset_annotation
                         else -> R.dimen.reader_context_menu_window_position_offset
                     })
+
+                    //  get location of view
                     val anchorLoc = arrayOf(0, 0).toIntArray()
                     view.getLocationOnScreen(anchorLoc)
-                    val isNeedShowDown = (anchorLoc[1] - (view.height) * 0.5 - offset < windowHeight)
-                    val positionX = anchorLoc[0] + (view.width - windowWidth) / 2
-                    val positionY = if (isNeedShowDown) {
-                        anchorLoc[1] + view.height + offset
-                    } else {
-                        anchorLoc[1] - windowHeight - offset
-                    }.toInt()
+
+                    //  compute the position of context menu
+                    val screenHeight = DensityUtil.getScreenHeightPx(context)
+                    val screenWidth = DensityUtil.getScreenWidthPx(context)
+                    val topPositionY = anchorLoc[1] - windowHeight - offset
+                    val bottomPositionY = anchorLoc[1] + view.height + offset
+                    val leftPositionX = anchorLoc[0] - windowWidth - offset
+                    val rightPositionX = anchorLoc[0] + view.width + offset
+
+                    //  determine the final position of context menu
+                    val positionY = when {
+                        topPositionY - margin > 0 -> topPositionY                                                               //  top
+                        screenHeight - windowHeight - bottomPositionY - margin > 0 -> bottomPositionY                           //  bottom
+                        else -> (topPositionY + bottomPositionY) / 2                                                            //  center
+                    }
+                    val positionX = when {
+                        positionY == topPositionY || positionY == bottomPositionY -> (leftPositionX + rightPositionX) / 2       //  center
+                        leftPositionX - margin > 0 -> leftPositionX                                                             //  left
+                        screenWidth - windowWidth - rightPositionX - margin > 0 -> rightPositionX                               //  right
+                        else -> (leftPositionX + rightPositionX) / 2                                                            //  center
+                    }
 
                     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                         popupWindow?.elevation = context.resources.getDimension(R.dimen.reader_context_menu_window_elevation)

+ 1 - 0
src/main/res/values/dimens.xml

@@ -28,5 +28,6 @@
     <dimen name="reader_context_menu_window_position_offset_markup">20dp</dimen>
     <dimen name="reader_context_menu_window_position_offset_select_text">-20dp</dimen>
     <dimen name="reader_context_menu_window_position_offset">10dp</dimen>
+    <dimen name="reader_context_menu_window_position_margin">40dp</dimen>
     <dimen name="reader_context_menu_window_elevation">4dp</dimen>
 </resources>