forked from flutter/flutter-intellij
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWidgetPreviewPanel.java
More file actions
275 lines (242 loc) · 10.8 KB
/
Copy pathWidgetPreviewPanel.java
File metadata and controls
275 lines (242 loc) · 10.8 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
package io.flutter.widgetpreview;
import com.intellij.execution.ExecutionException;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.ide.browsers.BrowserLauncher;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.colors.EditorColorsListener;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.wm.ToolWindow;
import com.intellij.util.messages.MessageBusConnection;
import com.jetbrains.lang.dart.ide.toolingDaemon.DartToolingDaemonService;
import icons.FlutterIcons;
import io.flutter.FlutterBundle;
import io.flutter.FlutterUtils;
import io.flutter.dart.DtdUtils;
import io.flutter.devtools.DevToolsUrl;
import io.flutter.devtools.DevToolsUtils;
import io.flutter.logging.PluginLogger;
import io.flutter.pub.PubRoot;
import io.flutter.run.daemon.DevToolsInstance;
import io.flutter.run.daemon.DevToolsService;
import io.flutter.sdk.FlutterCommand;
import io.flutter.sdk.FlutterSdk;
import io.flutter.sdk.FlutterSdkVersion;
import io.flutter.settings.FlutterSettings;
import io.flutter.utils.MostlySilentColoredProcessHandler;
import io.flutter.utils.OpenApiUtils;
import io.flutter.view.BrowserUrlProvider;
import io.flutter.view.EmbeddedBrowser;
import io.flutter.view.EmbeddedTab;
import io.flutter.view.ViewUtils;
import io.flutter.view.WidgetPreviewUrlProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
public class WidgetPreviewPanel extends SimpleToolWindowPanel implements Disposable {
private static final @NotNull Logger LOG = PluginLogger.createLogger(WidgetPreviewPanel.class);
@NotNull private final Project project;
@NotNull private final ToolWindow toolWindow;
private final @NotNull AtomicReference<ProcessHandler> flutterProcessRef = new AtomicReference<>();
private final AtomicReference<EmbeddedTab> browserTabRef = new AtomicReference<>();
private final JPanel contentPanel;
@NotNull private final ViewUtils viewUtils = new ViewUtils();
private BrowserUrlProvider urlProvider;
public WidgetPreviewPanel(@NotNull Project project, @NotNull ToolWindow toolWindow) {
super(true, true); // vertical, with border
this.project = project;
this.toolWindow = toolWindow;
this.contentPanel = new JPanel(new BorderLayout());
setContent(contentPanel);
showInfoMessage(FlutterBundle.message("widget.preview.initializing"));
// Start the preview process asynchronously
startWidgetPreview();
}
private void startWidgetPreview() {
OpenApiUtils.safeExecuteOnPooledThread(() -> {
try {
// Check versioning of Flutter SDK.
FlutterSdk sdk = FlutterSdk.getFlutterSdk(project);
if (sdk == null) {
showInfoMessage(FlutterBundle.message("flutter.sdk.not.found"));
LOG.info("Flutter SDK was not found");
return;
}
if (sdk.getVersion().fullVersion().equals(FlutterSdkVersion.UNKNOWN_VERSION)) {
LOG.warn("Flutter SDK version is unknown or incomplete.");
viewUtils.presentLabels(toolWindow, List.of("A Flutter SDK was found at the location",
"specified in the settings, however the directory",
"is in an incomplete state. To fix, shut down the IDE,",
"run `flutter doctor` or `flutter --version`",
"and then restart the IDE."));
return;
}
if (!sdk.getVersion().canUseWidgetPreview()) {
LOG.info("Flutter SDK version is too old for widget preview: " + sdk.getVersion().fullVersion());
showInfoMessage(FlutterBundle.message("widget.preview.sdk.too.old"));
return;
}
showInfoMessage(FlutterBundle.message("widget.preview.starting"));
final CompletableFuture<String> urlFuture = new CompletableFuture<>();
urlFuture.whenComplete((url, ex) -> {
if (ex != null) {
LOG.error("Error getting widget preview URL", ex);
final String message = ex.getMessage();
LOG.warn("Widget preview process error: " + message);
showInfoMessage(FlutterBundle.message("widget.preview.error", message != null ? message : ""));
return;
}
ApplicationManager.getApplication().invokeLater(() -> {
if (url == null) {
final String message = "No URL found";
LOG.warn("Widget preview process error: " + message);
showInfoMessage(FlutterBundle.message("widget.preview.error", message));
return;
}
setUrlAndLoad(url);
});
});
final PubRoot root = PubRoot.forFile(project.getProjectFile());
if (root == null) {
LOG.warn("Pub root not found for project: " + project.getName());
showInfoMessage("Pub root could not be found to start widget preview.");
return;
}
boolean isVerboseMode = FlutterSettings.getInstance().isVerboseLogging();
final String dtdUri = getDtdUri();
final String devToolsUri = getDevToolsUri();
final FlutterCommand command = sdk.widgetPreview(root, isVerboseMode, dtdUri, devToolsUri);
LOG.info(command.getDisplayCommand());
final ProcessHandler handler = new MostlySilentColoredProcessHandler(command.createGeneralCommandLine(project));
flutterProcessRef.set(handler);
handler.addProcessListener(new WidgetPreviewListener(urlFuture, isVerboseMode));
handler.startNotify();
}
catch (ExecutionException e) {
LOG.error("Failed to execute widget preview command", e);
throw new RuntimeException(e);
}
});
}
private @Nullable String getDevToolsUri() {
try {
final CompletableFuture<DevToolsInstance> devToolsFuture = DevToolsService.getInstance(project).getDevToolsInstance();
if (devToolsFuture == null) {
LOG.error("DevTools future is null.");
return null;
}
final DevToolsInstance instance = devToolsFuture.get(30, TimeUnit.SECONDS);
if (instance == null) {
LOG.error("DevTools instance is null.");
return null;
}
return new DevToolsUrl.Builder().setDevToolsHost(instance.host()).setDevToolsPort(instance.port()).build().getUrlString();
}
catch (InterruptedException | java.util.concurrent.ExecutionException | TimeoutException e) {
LOG.error("DevTools service failed: ", e);
}
return null;
}
private @Nullable String getDtdUri() {
try {
final DartToolingDaemonService dtd = new DtdUtils().readyDtdService(project).get(30, TimeUnit.SECONDS);
if (dtd == null) {
LOG.error("DTD service is null.");
return null;
}
return dtd.getUri();
}
catch (TimeoutException | java.util.concurrent.ExecutionException | InterruptedException e) {
LOG.error("DTD service is not available after 30 seconds.", e);
}
return null;
}
// This is intended for the first time we load the panel - save the URL and listen for changes.
private void setUrlAndLoad(@NotNull String url) {
LOG.info("Widget preview URL received: " + url);
this.urlProvider = new WidgetPreviewUrlProvider(url, new DevToolsUtils().getIsBackgroundBright());
loadUrl(urlProvider);
listenForReload();
}
private void loadUrl(@NotNull BrowserUrlProvider urlProvider) {
LOG.info("Embedded browser is available: " + (FlutterUtils.embeddedBrowser(project) != null));
showInfoMessage(FlutterBundle.message("widget.preview.loading", urlProvider.getBrowserUrl()));
OpenApiUtils.safeInvokeLater(() -> {
final Consumer<EmbeddedBrowser> onBrowserAvailable = embeddedBrowser -> {
embeddedBrowser.openPanel(toolWindow, "Widget Preview", FlutterIcons.Flutter, urlProvider,
System.out::println,
null);
};
final Runnable onBrowserUnavailable = () -> {
final List<io.flutter.utils.LabelInput> inputs = List.of(
new io.flutter.utils.LabelInput("Embedded browser is not available."),
new io.flutter.utils.LabelInput("Open in external browser", (label, data) -> {
BrowserLauncher.getInstance().browse(urlProvider.getBrowserUrl(), null);
}));
final JPanel panel = viewUtils.createClickableLabelPanel(inputs);
ApplicationManager.getApplication().invokeLater(() -> {
contentPanel.removeAll();
contentPanel.add(panel, BorderLayout.CENTER);
contentPanel.revalidate();
contentPanel.repaint();
});
};
Optional.<EmbeddedBrowser>ofNullable(FlutterUtils.embeddedBrowser(project))
.ifPresentOrElse(onBrowserAvailable, onBrowserUnavailable);
});
}
// TODO(https://github.com/flutter/flutter/issues/177945): Ideally widget preview would change colors based on theme changes events,
// which we already send for the DevTools panels. If this is implemented then we can remove this listening code.
private void listenForReload() {
MessageBusConnection connection = project.getMessageBus().connect();
connection.subscribe(EditorColorsManager.TOPIC, (EditorColorsListener)scheme -> {
if (urlProvider == null) {
return;
}
final boolean changed = urlProvider.maybeUpdateColor();
if (changed) {
loadUrl(urlProvider);
}
});
Disposer.register(toolWindow.getDisposable(), connection);
}
private void showInfoMessage(@NotNull String message) {
ApplicationManager.getApplication().invokeLater(() -> {
contentPanel.removeAll();
contentPanel.add(viewUtils.warningLabel(message), BorderLayout.CENTER);
contentPanel.revalidate();
contentPanel.repaint();
});
}
@Override
public void dispose() {
// Terminate the Flutter process when the tool window is closed
final ProcessHandler process = flutterProcessRef.getAndSet(null);
if (process != null && !process.isProcessTerminated()) {
LOG.info("Terminating Flutter widget-preview process.");
process.destroyProcess();
}
// Dispose the browser tab
final EmbeddedTab tab = browserTabRef.getAndSet(null);
if (tab != null) {
try {
tab.close();
}
catch (Exception ex) {
LOG.info(ex);
}
}
}
}