Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
MouseCursorPlugin.java
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5package io.flutter.plugin.mouse;
6
7import static io.flutter.Build.API_LEVELS;
8
9import android.annotation.TargetApi;
10import android.view.PointerIcon;
11import androidx.annotation.NonNull;
12import androidx.annotation.RequiresApi;
13import io.flutter.embedding.engine.systemchannels.MouseCursorChannel;
14import java.util.HashMap;
15
16/** A mandatory plugin that handles mouse cursor requests. */
17@TargetApi(API_LEVELS.API_24)
18@RequiresApi(API_LEVELS.API_24)
19public class MouseCursorPlugin {
20 @NonNull private final MouseCursorViewDelegate mView;
21 @NonNull private final MouseCursorChannel mouseCursorChannel;
22
24 @NonNull MouseCursorViewDelegate view, @NonNull MouseCursorChannel mouseCursorChannel) {
25 mView = view;
26
27 this.mouseCursorChannel = mouseCursorChannel;
28 mouseCursorChannel.setMethodHandler(
29 new MouseCursorChannel.MouseCursorMethodHandler() {
30 @Override
31 public void activateSystemCursor(@NonNull String kind) {
32 mView.setPointerIcon(resolveSystemCursor(kind));
33 }
34 });
35 }
36
37 /**
38 * Return a pointer icon object for a system cursor.
39 *
40 * <p>This method guarantees to return a non-null object.
41 */
42 private PointerIcon resolveSystemCursor(@NonNull String kind) {
43 if (MouseCursorPlugin.systemCursorConstants == null) {
44 // Initialize the map when first used, because the map can grow big in the future (~70)
45 // and most mobile devices will not use them.
46 MouseCursorPlugin.systemCursorConstants =
47 new HashMap<String, Integer>() {
48 private static final long serialVersionUID = 1L;
49
50 {
51 put("alias", PointerIcon.TYPE_ALIAS);
52 put("allScroll", PointerIcon.TYPE_ALL_SCROLL);
53 put("basic", PointerIcon.TYPE_ARROW);
54 put("cell", PointerIcon.TYPE_CELL);
55 put("click", PointerIcon.TYPE_HAND);
56 put("contextMenu", PointerIcon.TYPE_CONTEXT_MENU);
57 put("copy", PointerIcon.TYPE_COPY);
58 put("forbidden", PointerIcon.TYPE_NO_DROP);
59 put("grab", PointerIcon.TYPE_GRAB);
60 put("grabbing", PointerIcon.TYPE_GRABBING);
61 put("help", PointerIcon.TYPE_HELP);
62 put("move", PointerIcon.TYPE_ALL_SCROLL);
63 put("none", PointerIcon.TYPE_NULL);
64 put("noDrop", PointerIcon.TYPE_NO_DROP);
65 put("precise", PointerIcon.TYPE_CROSSHAIR);
66 put("text", PointerIcon.TYPE_TEXT);
67 put("resizeColumn", PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW);
68 put("resizeDown", PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW);
69 put("resizeUpLeft", PointerIcon.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW);
70 put("resizeDownRight", PointerIcon.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW);
71 put("resizeLeft", PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW);
72 put("resizeLeftRight", PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW);
73 put("resizeRight", PointerIcon.TYPE_HORIZONTAL_DOUBLE_ARROW);
74 put("resizeRow", PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW);
75 put("resizeUp", PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW);
76 put("resizeUpDown", PointerIcon.TYPE_VERTICAL_DOUBLE_ARROW);
77 put("resizeUpLeft", PointerIcon.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW);
78 put("resizeUpRight", PointerIcon.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW);
79 put("resizeUpLeftDownRight", PointerIcon.TYPE_TOP_LEFT_DIAGONAL_DOUBLE_ARROW);
80 put("resizeUpRightDownLeft", PointerIcon.TYPE_TOP_RIGHT_DIAGONAL_DOUBLE_ARROW);
81 put("verticalText", PointerIcon.TYPE_VERTICAL_TEXT);
82 put("wait", PointerIcon.TYPE_WAIT);
83 put("zoomIn", PointerIcon.TYPE_ZOOM_IN);
84 put("zoomOut", PointerIcon.TYPE_ZOOM_OUT);
85 }
86 };
87 }
88
89 final int cursorConstant =
90 MouseCursorPlugin.systemCursorConstants.getOrDefault(kind, PointerIcon.TYPE_ARROW);
91 return mView.getSystemPointerIcon(cursorConstant);
92 }
93
94 /**
95 * Detaches the text input plugin from the platform views controller.
96 *
97 * <p>The MouseCursorPlugin instance should not be used after calling this.
98 */
99 public void destroy() {
100 mouseCursorChannel.setMethodHandler(null);
101 }
102
103 /**
104 * A map from Flutter's system cursor {@code kind} to Android's pointer icon constants.
105 *
106 * <p>It is null until the first time a system cursor is requested, at which time it is filled
107 * with the entire mapping.
108 */
109 @NonNull private static HashMap<String, Integer> systemCursorConstants;
110
111 /**
112 * Delegate interface for requesting the system to display a pointer icon object.
113 *
114 * <p>Typically implemented by an {@link android.view.View}, such as a {@code FlutterView}.
115 */
116 public interface MouseCursorViewDelegate {
117 /**
118 * Gets a system pointer icon object for the given {@code type}.
119 *
120 * <p>If typeis not recognized, returns the default pointer icon.
121 *
122 * <p>This is typically implemented by calling {@link android.view.PointerIcon#getSystemIcon}
123 * with the context associated with this view.
124 */
125 @NonNull
126 public PointerIcon getSystemPointerIcon(int type);
127
128 /**
129 * Request the pointer to display the specified icon object.
130 *
131 * <p>If the delegate is implemented by a {@link android.view.View}, then this method is
132 * automatically implemented by View.
133 */
134 public void setPointerIcon(@NonNull PointerIcon icon);
135 }
136}
MouseCursorPlugin( @NonNull MouseCursorViewDelegate view, @NonNull MouseCursorChannel mouseCursorChannel)