Android: Fix wrong position of EditPopupMenu

When Edit Popup Menu was not shown, the size of EditContextView was
wrongly returned. That is why the popup appeared in the wrong position
and then "jumped" to the right one.

To get correct values even when Popup is invisible, size should be
calculated manually by getting height/width of all visible buttons (with
getMeasuredHeight/getMeasuredWidth[0]) and paddings of EditContextView.

[0]https://developer.android.com/reference/android/view/View#getMeasuredWidth()

Fixes: QTBUG-115632
Change-Id: I5f6abffab1a1e836c59a922ffa727d893ca9820f
Reviewed-by: Assam Boudjelthia <assam.boudjelthia@qt.io>
(cherry picked from commit 51d331b13bc1b922797eb81b2a73f78e1c07bf22)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Bartlomiej Moskal 2023-08-14 13:09:49 +02:00 committed by Qt Cherry-pick Bot
parent 4781de5859
commit b32f16b169
2 changed files with 25 additions and 4 deletions

View File

@ -5,6 +5,7 @@ package org.qtproject.qt.android;
import android.content.Context;
import android.graphics.Point;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
@ -74,6 +75,23 @@ public class EditContextView extends LinearLayout implements View.OnClickListene
m_buttons.get(R.string.selectAll).setVisibility((buttonsLayout & SALL_BUTTON) != 0 ? View.VISIBLE : View.GONE);
}
public Point getCalculatedSize()
{
Point size = new Point(0, 0);
for (ContextButton b : m_buttons.values()) {
if (b.getVisibility() == View.VISIBLE) {
b.measure(0, 0);
size.x += b.getMeasuredWidth();
size.y = Math.max(size.y, b.getMeasuredHeight());
}
}
size.x += getPaddingLeft() + getPaddingRight();
size.y += getPaddingTop() + getPaddingBottom();
return size;
}
public EditContextView(Context context, OnClickListener onClickListener) {
super(context);
m_onClickListener = onClickListener;

View File

@ -5,6 +5,7 @@
package org.qtproject.qt.android;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
@ -69,6 +70,8 @@ public class EditPopupMenu implements ViewTreeObserver.OnPreDrawListener, View.O
initOverlay();
m_view.updateButtons(buttons);
Point viewSize = m_view.getCalculatedSize();
final int[] layoutLocation = new int[2];
m_layout.getLocationOnScreen(layoutLocation);
@ -81,9 +84,9 @@ public class EditPopupMenu implements ViewTreeObserver.OnPreDrawListener, View.O
int x2 = x + layoutLocation[0] - activityLocation[0];
int y2 = y + layoutLocation[1] + (activityLocationInWindow[1] - activityLocation[1]);
x2 -= m_view.getWidth() / 2 ;
x2 -= viewSize.x / 2 ;
y2 -= m_view.getHeight();
y2 -= viewSize.y;
if (y2 < 0) {
if (cursorHandle != null) {
y2 = cursorHandle.bottom();
@ -94,8 +97,8 @@ public class EditPopupMenu implements ViewTreeObserver.OnPreDrawListener, View.O
}
}
if (m_layout.getWidth() < x + m_view.getWidth() / 2)
x2 = m_layout.getWidth() - m_view.getWidth();
if (m_layout.getWidth() < x + viewSize.x / 2)
x2 = m_layout.getWidth() - viewSize.x;
if (x2 < 0)
x2 = 0;