|
@@ -5,6 +5,7 @@ import android.arch.lifecycle.MutableLiveData
|
|
import android.arch.lifecycle.ViewModel
|
|
import android.arch.lifecycle.ViewModel
|
|
import android.content.Context
|
|
import android.content.Context
|
|
import android.graphics.Color
|
|
import android.graphics.Color
|
|
|
|
+import android.graphics.Point
|
|
import android.net.Uri
|
|
import android.net.Uri
|
|
import android.os.Build
|
|
import android.os.Build
|
|
import android.support.v4.content.ContextCompat
|
|
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.StampAttribute
|
|
import com.kdanmobile.reader.screen.data.TextBoxAttribute
|
|
import com.kdanmobile.reader.screen.data.TextBoxAttribute
|
|
import com.kdanmobile.reader.screen.handler.*
|
|
import com.kdanmobile.reader.screen.handler.*
|
|
|
|
+import com.kdanmobile.reader.utils.DensityUtil
|
|
import java.io.File
|
|
import java.io.File
|
|
import java.util.*
|
|
import java.util.*
|
|
import kotlin.collections.ArrayList
|
|
import kotlin.collections.ArrayList
|
|
@@ -464,6 +466,7 @@ class ReaderViewModel(private val readerModelManager: ReaderModelManager, val ur
|
|
override fun onShowContextMenu(type: KMPDFContextMenuCallback.ContextMenuType?, view: View?) {
|
|
override fun onShowContextMenu(type: KMPDFContextMenuCallback.ContextMenuType?, view: View?) {
|
|
if (null != view && null == popupWindow) {
|
|
if (null != view && null == popupWindow) {
|
|
|
|
|
|
|
|
+ // create context menu depended on ContextMenuType
|
|
val context = view.context!!
|
|
val context = view.context!!
|
|
val contentView = when (type) {
|
|
val contentView = when (type) {
|
|
KMPDFContextMenuCallback.ContextMenuType.Markup -> createMarkupContextMenuView(context)
|
|
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)
|
|
contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED)
|
|
val windowWidth = contentView.measuredWidth
|
|
val windowWidth = contentView.measuredWidth
|
|
val windowHeight = contentView.measuredHeight
|
|
val windowHeight = contentView.measuredHeight
|
|
popupWindow = PopupWindow(contentView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true)
|
|
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) {
|
|
val offset = context.resources.getDimensionPixelSize(when (type) {
|
|
KMPDFContextMenuCallback.ContextMenuType.Markup,
|
|
KMPDFContextMenuCallback.ContextMenuType.Markup,
|
|
KMPDFContextMenuCallback.ContextMenuType.INK -> R.dimen.reader_context_menu_window_position_offset_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
|
|
KMPDFContextMenuCallback.ContextMenuType.Stamp -> R.dimen.reader_context_menu_window_position_offset_annotation
|
|
else -> R.dimen.reader_context_menu_window_position_offset
|
|
else -> R.dimen.reader_context_menu_window_position_offset
|
|
})
|
|
})
|
|
|
|
+
|
|
|
|
+ // get location of view
|
|
val anchorLoc = arrayOf(0, 0).toIntArray()
|
|
val anchorLoc = arrayOf(0, 0).toIntArray()
|
|
view.getLocationOnScreen(anchorLoc)
|
|
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) {
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
|
|
popupWindow?.elevation = context.resources.getDimension(R.dimen.reader_context_menu_window_elevation)
|
|
popupWindow?.elevation = context.resources.getDimension(R.dimen.reader_context_menu_window_elevation)
|