FreeRDP
Loading...
Searching...
No Matches
FloatingToolbar.java
1/*
2 Floating toolbar for RDP session controls
3
4 Copyright 2026 Ibrahim Sevinc <ibrahim.sevinc.mail@gmail.com>
5
6 This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7 If a copy of the MPL was not distributed with this file, You can obtain one at
8 http://mozilla.org/MPL/2.0/.
9*/
10
11package com.freerdp.freerdpcore.presentation;
12
13import android.app.Activity;
14import android.os.Build;
15import android.view.GestureDetector;
16import android.view.MotionEvent;
17import android.view.View;
18import android.view.ViewTreeObserver;
19import android.widget.LinearLayout;
20
21import com.freerdp.freerdpcore.R;
22
23public class FloatingToolbar
24{
25 public interface Listener
26 {
27 void onToggleTouchPointer();
28 void onToggleKeyboard();
29 }
30
31 private enum Edge
32 {
33 LEFT,
34 RIGHT,
35 TOP,
36 BOTTOM
37 }
38
39 private final LinearLayout container;
40 private final LinearLayout buttons;
41 private boolean expanded = false;
42 private int insetLeft = 0, insetTop = 0, insetRight = 0, insetBottom = 0;
43 private Edge snappedEdge = Edge.LEFT;
44 private float snappedFraction = 0.4f;
45
46 public void setInsets(int left, int top, int right, int bottom)
47 {
48 insetLeft = left;
49 insetTop = top;
50 insetRight = right;
51 insetBottom = bottom;
52 }
53
54 public FloatingToolbar(Activity activity, Listener listener)
55 {
56 container = activity.findViewById(R.id.floating_toolbar_container);
57 buttons = activity.findViewById(R.id.floating_toolbar_buttons);
58 View handle = activity.findViewById(R.id.floating_toolbar_handle);
59 setTooltip(handle);
60
61 GestureDetector gestureDetector =
63 @Override public boolean onSingleTapConfirmed(MotionEvent e)
64 {
65 toggle();
66 return true;
67 }
68 });
69
70 View.OnTouchListener dragListener = buildDragListener(activity, gestureDetector, handle);
71 container.setOnTouchListener(dragListener);
72 handle.setOnTouchListener(dragListener);
73 for (int i = 0; i < buttons.getChildCount(); i++)
74 buttons.getChildAt(i).setOnTouchListener(dragListener);
75
76 bindButton(activity, R.id.floating_toolbar_touch_pointer, listener::onToggleTouchPointer);
77 bindButton(activity, R.id.floating_toolbar_sys_keyboard, listener::onToggleKeyboard);
78
79 ViewTreeObserver vto = container.getViewTreeObserver();
80 vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
81 @Override public void onGlobalLayout()
82 {
83 container.getViewTreeObserver().removeOnGlobalLayoutListener(this);
84 View parent = (View)container.getParent();
85 if (parent == null)
86 return;
87 positionInitial(parent);
88 parent.addOnLayoutChangeListener((v, left, top, right, bottom, oldLeft, oldTop,
89 oldRight, oldBottom) -> {
90 if (right - left != oldRight - oldLeft || bottom - top != oldBottom - oldTop)
91 resnapToSameEdge();
92 });
93 }
94 });
95 }
96
97 private void positionInitial(View parent)
98 {
99 container.setOrientation(LinearLayout.VERTICAL);
100 buttons.setOrientation(LinearLayout.VERTICAL);
101 snappedEdge = Edge.LEFT;
102 snappedFraction = 0.4f;
103 float ph = parent.getHeight();
104 float ch = container.getHeight();
105 container.setX(insetLeft);
106 container.setY(
107 Math.max(insetTop, insetTop + snappedFraction * (ph - insetTop - insetBottom - ch)));
108 }
109
110 private void resnapToSameEdge()
111 {
112 View parent = (View)container.getParent();
113 if (parent == null)
114 return;
115
116 int orientation = (snappedEdge == Edge.LEFT || snappedEdge == Edge.RIGHT)
117 ? LinearLayout.VERTICAL
118 : LinearLayout.HORIZONTAL;
119 container.setOrientation(orientation);
120 buttons.setOrientation(orientation);
121
122 container.post(() -> {
123 float pw = parent.getWidth(), ph = parent.getHeight();
124 float w = container.getWidth(), h = container.getHeight();
125 float tx, ty;
126 switch (snappedEdge)
127 {
128 case LEFT:
129 tx = insetLeft;
130 ty = insetTop + snappedFraction * (ph - insetTop - insetBottom - h);
131 break;
132 case RIGHT:
133 tx = pw - w - insetRight;
134 ty = insetTop + snappedFraction * (ph - insetTop - insetBottom - h);
135 break;
136 case TOP:
137 tx = insetLeft + snappedFraction * (pw - insetLeft - insetRight - w);
138 ty = insetTop;
139 break;
140 default:
141 tx = insetLeft + snappedFraction * (pw - insetLeft - insetRight - w);
142 ty = ph - h - insetBottom;
143 break;
144 }
145 tx = Math.max(insetLeft, Math.min(tx, pw - w - insetRight));
146 ty = Math.max(insetTop, Math.min(ty, ph - h - insetBottom));
147 container.animate().x(tx).y(ty).setDuration(250).start();
148 });
149 }
150
151 private void toggle()
152 {
153 expanded = !expanded;
154 buttons.setVisibility(expanded ? View.VISIBLE : View.GONE);
155 snap();
156 }
157
158 private void bindButton(Activity activity, int id, Runnable action)
159 {
160 View v = activity.findViewById(id);
161 if (v != null)
162 {
163 setTooltip(v);
164 v.setOnClickListener(ignored -> action.run());
165 }
166 }
167
168 private static void setTooltip(View v)
169 {
170 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
171 v.setTooltipText(v.getContentDescription());
172 }
173
174 private View.OnTouchListener buildDragListener(Activity activity,
175 GestureDetector gestureDetector, View handle)
176 {
177 int touchSlop = android.view.ViewConfiguration.get(activity).getScaledTouchSlop();
178 return new View.OnTouchListener() {
179 private float startX, startY, offsetX, offsetY;
180 private boolean dragging;
181
182 @Override public boolean onTouch(View v, MotionEvent e)
183 {
184 if (v == handle || v == container)
185 gestureDetector.onTouchEvent(e);
186
187 switch (e.getActionMasked())
188 {
189 case MotionEvent.ACTION_DOWN:
190 startX = e.getRawX();
191 startY = e.getRawY();
192 offsetX = container.getX() - startX;
193 offsetY = container.getY() - startY;
194 dragging = false;
195 v.onTouchEvent(e);
196 break;
197 case MotionEvent.ACTION_MOVE:
198 if (!dragging && (Math.abs(e.getRawX() - startX) > touchSlop ||
199 Math.abs(e.getRawY() - startY) > touchSlop))
200 {
201 dragging = true;
202 MotionEvent cancel = MotionEvent.obtain(e);
203 cancel.setAction(MotionEvent.ACTION_CANCEL);
204 v.onTouchEvent(cancel);
205 cancel.recycle();
206 }
207 if (dragging)
208 container.animate()
209 .x(e.getRawX() + offsetX)
210 .y(e.getRawY() + offsetY)
211 .setDuration(0)
212 .start();
213 else
214 v.onTouchEvent(e);
215 break;
216 case MotionEvent.ACTION_UP:
217 case MotionEvent.ACTION_CANCEL:
218 if (!dragging)
219 v.onTouchEvent(e);
220 snap();
221 break;
222 }
223 return true;
224 }
225 };
226 }
227
228 private void snap()
229 {
230 View parent = (View)container.getParent();
231 if (parent == null)
232 return;
233
234 float px = container.getX(), py = container.getY();
235 float pw = parent.getWidth(), ph = parent.getHeight();
236 float cw = container.getWidth(), ch = container.getHeight();
237
238 float dLeft = px - insetLeft;
239 float dRight = Math.max(0, pw - px - cw - insetRight);
240 float dTop = py - insetTop;
241 float dBottom = Math.max(0, ph - py - ch - insetBottom);
242 float min = Math.min(Math.min(dLeft, dRight), Math.min(dTop, dBottom));
243
244 if (min == dLeft)
245 snappedEdge = Edge.LEFT;
246 else if (min == dRight)
247 snappedEdge = Edge.RIGHT;
248 else if (min == dTop)
249 snappedEdge = Edge.TOP;
250 else
251 snappedEdge = Edge.BOTTOM;
252
253 int orientation = (snappedEdge == Edge.LEFT || snappedEdge == Edge.RIGHT)
254 ? LinearLayout.VERTICAL
255 : LinearLayout.HORIZONTAL;
256 container.setOrientation(orientation);
257 buttons.setOrientation(orientation);
258
259 container.post(() -> {
260 float w = container.getWidth(), h = container.getHeight();
261 float tx = container.getX(), ty = container.getY();
262 switch (snappedEdge)
263 {
264 case LEFT:
265 tx = insetLeft;
266 break;
267 case RIGHT:
268 tx = pw - w - insetRight;
269 break;
270 case TOP:
271 ty = insetTop;
272 break;
273 case BOTTOM:
274 ty = ph - h - insetBottom;
275 break;
276 }
277 tx = Math.max(insetLeft, Math.min(tx, pw - w - insetRight));
278 ty = Math.max(insetTop, Math.min(ty, ph - h - insetBottom));
279 if (snappedEdge == Edge.LEFT || snappedEdge == Edge.RIGHT)
280 {
281 float available = ph - insetTop - insetBottom - h;
282 snappedFraction =
283 available > 0 ? Math.max(0f, Math.min(1f, (ty - insetTop) / available)) : 0.5f;
284 }
285 else
286 {
287 float available = pw - insetLeft - insetRight - w;
288 snappedFraction =
289 available > 0 ? Math.max(0f, Math.min(1f, (tx - insetLeft) / available)) : 0.5f;
290 }
291 container.animate().x(tx).y(ty).setDuration(250).start();
292 });
293 }
294}