Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
EditPopupMenu.java
Go to the documentation of this file.
1// Copyright (C) 2018 BogDan Vatra <bogdan@kde.org>
2// Copyright (C) 2016 Olivier Goffart <ogoffart@woboq.com>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
4
5package org.qtproject.qt.android;
6
7import android.app.Activity;
8import android.content.Context;
9import android.graphics.Point;
10import android.view.View;
11import android.view.ViewGroup;
12import android.view.ViewTreeObserver;
13import android.widget.PopupWindow;
14
15// Helper class that manages a cursor or selection handle
16class EditPopupMenu implements ViewTreeObserver.OnPreDrawListener, View.OnLayoutChangeListener,
17 EditContextView.OnClickListener
18{
19 private final View m_layout;
20 private final EditContextView m_view;
21 private PopupWindow m_popup = null;
22 private final Activity m_activity;
23 private int m_posX;
24 private int m_posY;
25 private int m_buttons;
26 private CursorHandle m_cursorHandle;
27 private CursorHandle m_leftSelectionHandle;
28 private CursorHandle m_rightSelectionHandle;
29
30 public EditPopupMenu(Activity activity, View layout)
31 {
32 m_activity = activity;
33 m_view = new EditContextView(activity, this);
34 m_view.addOnLayoutChangeListener(this);
35
36 m_layout = layout;
37 }
38
39 private void initOverlay()
40 {
41 if (m_popup != null)
42 return;
43
44 Context context = m_layout.getContext();
45 m_popup = new PopupWindow(context, null, android.R.attr.textSelectHandleWindowStyle);
46 m_popup.setSplitTouchEnabled(true);
47 m_popup.setClippingEnabled(false);
48 m_popup.setContentView(m_view);
49 m_popup.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
50 m_popup.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
51
52 m_layout.getViewTreeObserver().addOnPreDrawListener(this);
53 }
54
55 // Show the handle at a given position (or move it if it is already shown)
56 public void setPosition(final int x, final int y, final int buttons,
57 CursorHandle cursorHandle, CursorHandle leftSelectionHandle, CursorHandle rightSelectionHandle)
58 {
59 initOverlay();
60
61 m_view.updateButtons(buttons);
62 Point viewSize = m_view.getCalculatedSize();
63
64 final int[] layoutLocation = new int[2];
65 m_layout.getLocationOnScreen(layoutLocation);
66
67 // These values are used for handling split screen case
68 final int[] activityLocation = new int[2];
69 final int[] activityLocationInWindow = new int[2];
70 m_activity.getWindow().getDecorView().getLocationOnScreen(activityLocation);
71 m_activity.getWindow().getDecorView().getLocationInWindow(activityLocationInWindow);
72
73 int x2 = x + layoutLocation[0] - activityLocation[0];
74 int y2 = y + layoutLocation[1] + (activityLocationInWindow[1] - activityLocation[1]);
75
76 x2 -= viewSize.x / 2 ;
77
78 y2 -= viewSize.y;
79 if (y2 < 0) {
80 if (cursorHandle != null) {
81 y2 = cursorHandle.bottom();
82 } else if (leftSelectionHandle != null && rightSelectionHandle != null) {
83 y2 = Math.max(leftSelectionHandle.bottom(), rightSelectionHandle.bottom());
84 if (y2 <= 0)
85 m_layout.requestLayout();
86 }
87 }
88
89 if (m_layout.getWidth() < x + viewSize.x / 2)
90 x2 = m_layout.getWidth() - viewSize.x;
91
92 if (x2 < 0)
93 x2 = 0;
94
95 if (m_popup.isShowing())
96 m_popup.update(x2, y2, -1, -1);
97 else
98 m_popup.showAtLocation(m_layout, 0, x2, y2);
99
100 m_posX = x;
101 m_posY = y;
102 m_buttons = buttons;
103 m_cursorHandle = cursorHandle;
104 m_leftSelectionHandle = leftSelectionHandle;
105 m_rightSelectionHandle = rightSelectionHandle;
106 }
107
108 public void hide() {
109 if (m_popup != null) {
110 m_popup.dismiss();
111 m_popup = null;
112 }
113 }
114
115 @Override
116 public boolean onPreDraw() {
117 // This hook is called when the view location is changed
118 // For example if the keyboard appears.
119 // Adjust the position of the handle accordingly
120 if (m_popup != null && m_popup.isShowing())
121 setPosition(m_posX, m_posY, m_buttons, m_cursorHandle, m_leftSelectionHandle, m_rightSelectionHandle);
122
123 return true;
124 }
125
126 @Override
127 public void onLayoutChange(View v, int left, int top, int right, int bottom,
128 int oldLeft, int oldTop, int oldRight, int oldBottom)
129 {
130 if ((right - left != oldRight - oldLeft || bottom - top != oldBottom - oldTop) &&
131 m_popup != null && m_popup.isShowing())
132 setPosition(m_posX, m_posY, m_buttons, m_cursorHandle, m_leftSelectionHandle, m_rightSelectionHandle);
133 }
134
135 @Override
136 public void contextButtonClicked(int buttonId) {
137 switch (buttonId) {
138 case android.R.string.cut:
139 QtNativeInputConnection.cut();
140 break;
141 case android.R.string.copy:
142 QtNativeInputConnection.copy();
143 break;
144 case android.R.string.paste:
145 QtNativeInputConnection.paste();
146 break;
147 case android.R.string.selectAll:
148 QtNativeInputConnection.selectAll();
149 break;
150 }
151 hide();
152 }
153}
Q_CORE_EXPORT QtJniTypes::Activity activity()
static void * context
Qt::MouseButtons m_buttons
Definition qnsview.mm:102
n void setPosition(void) \n\
GLsizei const GLfloat * v
[13]
GLint GLint GLint GLint GLint x
[0]
GLdouble GLdouble GLdouble GLdouble top
GLdouble GLdouble right
GLint left
GLint GLint bottom
GLint y
GLfixed GLfixed GLfixed y2
GLfixed GLfixed x2
QVBoxLayout * layout
Definition parser.h:19