forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDeviceSelectorAction.java
More file actions
290 lines (247 loc) · 10 KB
/
DeviceSelectorAction.java
File metadata and controls
290 lines (247 loc) · 10 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
* Copyright 2016 The Chromium Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
package io.flutter.actions;
import com.intellij.ide.ActivityTracker;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.actionSystem.ex.ComboBoxAction;
import com.intellij.openapi.application.Application;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import com.intellij.openapi.project.DumbAware;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.project.ProjectManager;
import com.intellij.openapi.project.ProjectManagerListener;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.ui.JBColor;
import com.intellij.util.ModalityUiUtil;
import icons.FlutterIcons;
import io.flutter.FlutterBundle;
import io.flutter.run.FlutterDevice;
import io.flutter.run.daemon.DeviceService;
import io.flutter.sdk.AndroidEmulatorManager;
import io.flutter.utils.FlutterModuleUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.*;
import java.awt.Component;
import java.util.*;
public class DeviceSelectorAction extends ComboBoxAction implements DumbAware {
private final List<AnAction> actions = new ArrayList<>();
private final List<Project> knownProjects = Collections.synchronizedList(new ArrayList<>());
private @Nullable SelectDeviceAction selectedDeviceAction;
DeviceSelectorAction() {
setSmallVariant(true);
}
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
@Override
public @NotNull JComponent createCustomComponent(@NotNull Presentation presentation, @NotNull String place) {
final JComponent component = super.createCustomComponent(presentation, place);
// Set component to be transparent to match other toolbar actions
component.setOpaque(false);
// Update child components.
updateComponentChildrenStyles(component);
return component;
}
private void updateComponentChildrenStyles(@NotNull JComponent parent) {
final @Nullable Component[] children = parent.getComponents();
if (children == null) {
return;
}
for (Component child : children) {
if (child instanceof JComponent jComponent) {
jComponent.setOpaque(false);
if (child instanceof JButton jButton) {
jButton.setBorderPainted(false);
jButton.setRolloverEnabled(true);
// Make sure the button uses correct background & foreground.
jButton.setBackground(JBColor.background());
jButton.setForeground(JBColor.foreground());
}
updateComponentChildrenStyles(jComponent);
}
}
}
@Override
protected @NotNull DefaultActionGroup createPopupActionGroup(@NotNull JComponent button, @NotNull DataContext dataContext) {
final DefaultActionGroup group = new DefaultActionGroup();
group.addAll(actions);
return group;
}
@Override
protected boolean shouldShowDisabledActions() {
return true;
}
@Override
public void update(@NotNull AnActionEvent e) {
// Only show device menu when the device daemon process is running.
final Project project = e.getProject();
if (!isSelectorVisible(project)) {
e.getPresentation().setVisible(false);
return;
}
super.update(e);
final Presentation presentation = e.getPresentation();
if (!knownProjects.contains(project)) {
knownProjects.add(project);
final Application application = ApplicationManager.getApplication();
if (application != null) {
application.getMessageBus().connect().subscribe(ProjectManager.TOPIC, new ProjectManagerListener() {
@Override
public void projectClosed(@NotNull Project closedProject) {
knownProjects.remove(closedProject);
}
});
}
Runnable deviceListener = () -> queueUpdate(project, e.getPresentation());
DeviceService.getInstance(project).addListener(deviceListener);
// Listen for android device changes, and rebuild the menu if necessary.
Runnable emulatorListener = () -> queueUpdate(project, e.getPresentation());
AndroidEmulatorManager.getInstance(project).addListener(emulatorListener);
var projectManager = ProjectManager.getInstance();
if (projectManager != null) {
projectManager.addProjectManagerListener(project, new ProjectManagerListener() {
public void projectClosing(@NotNull Project project) {
DeviceService.getInstance(project).removeListener(deviceListener);
AndroidEmulatorManager.getInstance(project).removeListener(emulatorListener);
}
});
}
update(project, presentation);
}
final DeviceService deviceService = DeviceService.getInstance(project);
final FlutterDevice selectedDevice = deviceService.getSelectedDevice();
final Collection<FlutterDevice> devices = deviceService.getConnectedDevices();
if (devices.isEmpty()) {
final boolean isLoading = deviceService.getStatus() == DeviceService.State.LOADING;
if (isLoading) {
presentation.setText(FlutterBundle.message("devicelist.loading"));
}
else {
presentation.setText("<no devices>");
}
}
else if (selectedDevice == null) {
presentation.setText("<no device selected>");
}
else if (selectedDeviceAction != null) {
final Presentation template = selectedDeviceAction.getTemplatePresentation();
presentation.setIcon(template.getIcon());
presentation.setText(selectedDevice.presentationName());
presentation.setEnabled(true);
}
}
private void queueUpdate(@NotNull Project project, @NotNull Presentation presentation) {
ModalityUiUtil.invokeLaterIfNeeded(
ModalityState.defaultModalityState(),
() -> update(project, presentation));
}
private void update(@NotNull Project project, @NotNull Presentation presentation) {
if (project.isDisposed()) {
return; // This check is probably unnecessary, but safe.
}
updateActions(project, presentation);
updateVisibility(project, presentation);
}
private static void updateVisibility(final Project project, final @NotNull Presentation presentation) {
final boolean visible = isSelectorVisible(project);
final JComponent component = presentation.getClientProperty(new Key<>("customComponent"));
if (component != null) {
component.setVisible(visible);
var parent = component.getParent();
if (parent != null) {
parent.doLayout();
parent.repaint();
}
}
}
private static boolean isSelectorVisible(@Nullable Project project) {
if (project == null || !FlutterModuleUtils.hasFlutterModule(project)) {
return false;
}
final DeviceService deviceService = DeviceService.getInstance(project);
return deviceService.isRefreshInProgress() || deviceService.getStatus() != DeviceService.State.INACTIVE;
}
private void updateActions(@NotNull Project project, Presentation presentation) {
actions.clear();
final DeviceService deviceService = DeviceService.getInstance(project);
final FlutterDevice selectedDevice = deviceService.getSelectedDevice();
final Collection<FlutterDevice> devices = deviceService.getConnectedDevices();
selectedDeviceAction = null;
for (FlutterDevice device : devices) {
if (device == null) continue;
final SelectDeviceAction deviceAction = new SelectDeviceAction(device, devices);
actions.add(deviceAction);
if (Objects.equals(device, selectedDevice)) {
selectedDeviceAction = deviceAction;
final Presentation template = deviceAction.getTemplatePresentation();
//noinspection DataFlowIssue
presentation.setIcon(template.getIcon());
//presentation.setText(deviceAction.presentationName());
presentation.setEnabled(true);
}
}
// Show the 'Open iOS Simulator' action.
if (SystemInfo.isMac) {
boolean simulatorOpen = false;
for (AnAction action : actions) {
if (action instanceof SelectDeviceAction deviceAction) {
final FlutterDevice device = deviceAction.device;
if (device.isIOS() && device.emulator()) {
simulatorOpen = true;
}
}
}
actions.add(new Separator());
actions.add(new OpenSimulatorAction(!simulatorOpen));
}
// Add Open Android emulators actions.
final List<OpenEmulatorAction> emulatorActions = OpenEmulatorAction.getEmulatorActions(project);
if (emulatorActions != null && !emulatorActions.isEmpty()) {
actions.add(new Separator());
actions.addAll(emulatorActions);
}
if (!FlutterModuleUtils.hasInternalDartSdkPath(project)) {
actions.add(new Separator());
actions.add(RestartFlutterDaemonAction.forDeviceSelector());
}
var tracker = ActivityTracker.getInstance();
if (tracker != null) {
tracker.inc();
}
}
// Show the current device as selected when the combo box menu opens.
@Override
protected Condition<AnAction> getPreselectCondition() {
return action -> action == selectedDeviceAction;
}
private static class SelectDeviceAction extends AnAction {
@NotNull
private final FlutterDevice device;
SelectDeviceAction(@NotNull FlutterDevice device, @NotNull Collection<FlutterDevice> devices) {
super(device.getUniqueName(devices), null, FlutterIcons.Phone);
this.device = device;
}
public @NotNull String presentationName() {
return device.presentationName();
}
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
final Project project = e.getProject();
final DeviceService service = project == null ? null : DeviceService.getInstance(project);
if (service != null) {
service.setSelectedDevice(device);
}
}
@Override
public @NotNull ActionUpdateThread getActionUpdateThread() {
return ActionUpdateThread.BGT;
}
}
}