Skip to content

Commit 18eb068

Browse files
authored
[CQ] InspectorView de-duping and null-awareness (#8173)
Some opportunistic `InspectorView` clean-up. * a bit of de-duplication and * some added null-awareness --- - [x] I’ve reviewed the contributor guide and applied the relevant portions to this PR. <details> <summary>Contribution guidelines:</summary><br> - See our [contributor guide]([https://github.com/dart-lang/sdk/blob/main/CONTRIBUTING.md](https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview) for general expectations for PRs. - Larger or significant changes should be discussed in an issue before creating a PR. - Dart contributions to our repos should follow the [Dart style guide](https://dart.dev/guides/language/effective-dart) and use `dart format`. - Java and Kotlin contributions should strive to follow Java and Kotlin best practices ([discussion](#8098)). </details>
1 parent 5f1ec9a commit 18eb068

File tree

6 files changed

+54
-51
lines changed

6 files changed

+54
-51
lines changed

flutter-idea/src/io/flutter/devtools/DevToolsUrl.java

+17-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.flutter.sdk.FlutterSdkUtil;
1010
import io.flutter.sdk.FlutterSdkVersion;
1111
import org.jetbrains.annotations.NotNull;
12+
import org.jetbrains.annotations.Nullable;
1213

1314
import java.net.URLEncoder;
1415
import java.nio.charset.StandardCharsets;
@@ -37,27 +38,28 @@ public class DevToolsUrl {
3738
@NotNull private final DevToolsUtils devToolsUtils;
3839

3940
public static class Builder {
40-
private String devToolsHost;
41+
private @Nullable String devToolsHost;
4142

4243
private int devToolsPort;
43-
private String vmServiceUri;
44+
private @Nullable String vmServiceUri;
4445
private String page;
4546
private Boolean embed;
4647
private String widgetId;
4748
private String hide;
4849

49-
private FlutterSdkVersion flutterSdkVersion;
50+
private @Nullable FlutterSdkVersion flutterSdkVersion;
5051
private WorkspaceCache workspaceCache;
5152
private DevToolsIdeFeature ideFeature;
5253

5354
private DevToolsUtils devToolsUtils;
5455

5556
private FlutterSdkUtil flutterSdkUtil;
5657

57-
public Builder() {}
58+
public Builder() {
59+
}
5860

5961
@NotNull
60-
public Builder setDevToolsHost(String devToolsHost) {
62+
public Builder setDevToolsHost(@Nullable String devToolsHost) {
6163
this.devToolsHost = devToolsHost;
6264
return this;
6365
}
@@ -69,7 +71,7 @@ public Builder setDevToolsPort(int devToolsPort) {
6971
}
7072

7173
@NotNull
72-
public Builder setVmServiceUri(String vmServiceUri) {
74+
public Builder setVmServiceUri(@Nullable String vmServiceUri) {
7375
this.vmServiceUri = vmServiceUri;
7476
return this;
7577
}
@@ -105,7 +107,7 @@ public Builder setDevToolsUtils(DevToolsUtils devToolsUtils) {
105107
}
106108

107109
@NotNull
108-
public Builder setFlutterSdkVersion(FlutterSdkVersion sdkVersion) {
110+
public Builder setFlutterSdkVersion(@Nullable FlutterSdkVersion sdkVersion) {
109111
this.flutterSdkVersion = sdkVersion;
110112
return this;
111113
}
@@ -163,10 +165,12 @@ private DevToolsUrl(Builder builder) {
163165
if (builder.workspaceCache != null && builder.workspaceCache.isBazel()) {
164166
this.canUseDevToolsPathUrl = true;
165167
this.canUseMultiEmbed = true;
166-
} else if (flutterSdkVersion != null) {
168+
}
169+
else if (flutterSdkVersion != null) {
167170
this.canUseDevToolsPathUrl = flutterSdkVersion.canUseDevToolsPathUrls();
168171
this.canUseMultiEmbed = flutterSdkVersion.canUseDevToolsMultiEmbed();
169-
} else {
172+
}
173+
else {
170174
this.canUseDevToolsPathUrl = false;
171175
this.canUseMultiEmbed = false;
172176
}
@@ -192,12 +196,14 @@ public String getUrlString() {
192196
if (!this.canUseMultiEmbed) {
193197
// This is for older versions of DevTools that do not support embed= one vs. many.
194198
params.add("embed=true");
195-
} else {
199+
}
200+
else {
196201
if (hide != null) {
197202
// If we are using the hide param, we can assume that we are trying to embed multiple tabs.
198203
params.add("embedMode=many");
199204
params.add("hide=" + hide);
200-
} else {
205+
}
206+
else {
201207
params.add("embedMode=one");
202208
}
203209
}
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package io.flutter.jxbrowser;
22

3+
import org.jetbrains.annotations.NotNull;
4+
import org.jetbrains.annotations.Nullable;
5+
36
public class InstallationFailedReason {
4-
public FailureType failureType;
5-
public String detail;
7+
public final @NotNull FailureType failureType;
8+
public final @Nullable String detail;
69

7-
public InstallationFailedReason(FailureType failureType) {
8-
this(failureType, null);
9-
}
10+
public InstallationFailedReason(@NotNull FailureType failureType) {
11+
this(failureType, null);
12+
}
1013

11-
InstallationFailedReason(FailureType failureType, String detail) {
12-
this.failureType = failureType;
13-
this.detail = detail;
14-
}
14+
InstallationFailedReason(@NotNull FailureType failureType, @Nullable String detail) {
15+
this.failureType = failureType;
16+
this.detail = detail;
17+
}
1518
}

flutter-idea/src/io/flutter/settings/FlutterSettings.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.intellij.openapi.util.registry.Registry;
1212
import com.intellij.util.EventDispatcher;
1313
import com.jetbrains.lang.dart.analyzer.DartClosingLabelManager;
14+
import org.jetbrains.annotations.NotNull;
1415

1516
import java.util.EventListener;
1617
import java.util.Objects;
@@ -59,12 +60,12 @@ public static void setInstance(FlutterSettings instance) {
5960
testInstance = instance;
6061
}
6162

62-
public static FlutterSettings getInstance() {
63+
public static @NotNull FlutterSettings getInstance() {
6364
if (testInstance != null) {
6465
return testInstance;
6566
}
6667

67-
return Objects.requireNonNull(ApplicationManager.getApplication()).getService(FlutterSettings.class);
68+
return Objects.requireNonNull(Objects.requireNonNull(ApplicationManager.getApplication()).getService(FlutterSettings.class));
6869
}
6970

7071
protected static PropertiesComponent getPropertiesComponent() {

flutter-idea/src/io/flutter/utils/OpenApiUtils.java

+6
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ public class OpenApiUtils {
3838
return modules == null ? Module.EMPTY_ARRAY : modules;
3939
}
4040

41+
public static void safeExecuteOnPooledThread(@NotNull Runnable action) {
42+
var application = ApplicationManager.getApplication();
43+
if (application == null) return;
44+
application.executeOnPooledThread(action);
45+
}
46+
4147
public static void safeRunReadAction(@NotNull Runnable runnable) {
4248
Application application = ApplicationManager.getApplication();
4349
if (application == null) return;

flutter-idea/src/io/flutter/view/InspectorView.java

+15-27
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import com.google.common.annotations.VisibleForTesting;
99
import com.intellij.ide.browsers.BrowserLauncher;
1010
import com.intellij.openapi.Disposable;
11-
import com.intellij.openapi.application.ApplicationManager;
1211
import com.intellij.openapi.components.Storage;
1312
import com.intellij.openapi.diagnostic.Logger;
1413
import com.intellij.openapi.progress.EmptyProgressIndicator;
@@ -84,8 +83,8 @@ public class InspectorView implements Disposable {
8483

8584
private InspectorViewToolWindowManagerListener toolWindowListener;
8685
private int devToolsInstallCount = 0;
87-
private final JxBrowserUtils jxBrowserUtils;
88-
private final JxBrowserManager jxBrowserManager;
86+
private final @NotNull JxBrowserUtils jxBrowserUtils;
87+
private final @NotNull JxBrowserManager jxBrowserManager;
8988

9089
public InspectorView(@NotNull Project project) {
9190
this(project, JxBrowserManager.getInstance(), new JxBrowserUtils(), new ViewUtils());
@@ -95,7 +94,7 @@ public InspectorView(@NotNull Project project) {
9594
@NonInjectable
9695
protected InspectorView(@NotNull Project project,
9796
@NotNull JxBrowserManager jxBrowserManager,
98-
JxBrowserUtils jxBrowserUtils,
97+
@NotNull JxBrowserUtils jxBrowserUtils,
9998
ViewUtils viewUtils) {
10099
myProject = project;
101100
this.jxBrowserUtils = jxBrowserUtils;
@@ -119,11 +118,11 @@ void initToolWindow(@NotNull ToolWindow window) {
119118
updateForEmptyContent(window);
120119
}
121120

122-
private void addBrowserInspectorViewContent(FlutterApp app,
123-
ToolWindow toolWindow,
121+
private void addBrowserInspectorViewContent(@NotNull FlutterApp app,
122+
@NotNull ToolWindow toolWindow,
124123
boolean isEmbedded,
125124
DevToolsIdeFeature ideFeature,
126-
DevToolsInstance devToolsInstance) {
125+
@NotNull DevToolsInstance devToolsInstance) {
127126
assert (SwingUtilities.isEventDispatchThread());
128127

129128
final ContentManager contentManager = toolWindow.getContentManager();
@@ -199,7 +198,7 @@ private void addBrowserInspectorViewContent(FlutterApp app,
199198
}
200199
}
201200

202-
private Optional<EmbeddedBrowser> embeddedBrowserOptional() {
201+
private @NotNull Optional<EmbeddedBrowser> embeddedBrowserOptional() {
203202
if (myProject.isDisposed()) {
204203
return Optional.empty();
205204
}
@@ -264,6 +263,10 @@ private void openInspectorWithDevTools(FlutterApp app,
264263
boolean isEmbedded,
265264
DevToolsIdeFeature ideFeature,
266265
boolean forceDevToolsRestart) {
266+
if (toolWindow == null) {
267+
LOG.error("Unable to open Inspector with DevTools: toolwindow is null");
268+
return;
269+
}
267270
AsyncUtils.whenCompleteUiThread(
268271
forceDevToolsRestart
269272
? DevToolsService.getInstance(myProject).getDevToolsInstanceWithForcedRestart()
@@ -281,7 +284,7 @@ private void openInspectorWithDevTools(FlutterApp app,
281284
return;
282285
}
283286

284-
if (instance == null) {
287+
if (instance == null || app == null) {
285288
viewUtils.presentLabel(toolWindow, DEVTOOLS_FAILED_LABEL);
286289
return;
287290
}
@@ -311,7 +314,7 @@ protected void handleJxBrowserInstallationInProgress(FlutterApp app, ToolWindow
311314

312315
protected void startJxBrowserInstallationWaitingThread(FlutterApp app, ToolWindow toolWindow,
313316
DevToolsIdeFeature ideFeature) {
314-
ApplicationManager.getApplication().executeOnPooledThread(() -> {
317+
OpenApiUtils.safeExecuteOnPooledThread(() -> {
315318
waitForJxBrowserInstallation(app, toolWindow, ideFeature);
316319
});
317320
}
@@ -393,21 +396,6 @@ protected void presentOpenDevToolsOptionWithMessage(FlutterApp app,
393396
viewUtils.presentClickableLabel(toolWindow, inputs);
394397
}
395398

396-
private void replacePanelLabel(ToolWindow toolWindow, JComponent label) {
397-
OpenApiUtils.safeInvokeLater(() -> {
398-
final ContentManager contentManager = toolWindow.getContentManager();
399-
if (contentManager.isDisposed()) {
400-
return;
401-
}
402-
403-
final JPanel panel = new JPanel(new BorderLayout());
404-
panel.add(label, BorderLayout.CENTER);
405-
final Content content = contentManager.getFactory().createContent(panel, null, false);
406-
contentManager.removeAllContents(true);
407-
contentManager.addContent(content);
408-
});
409-
}
410-
411399
private void debugActiveHelper(@NotNull FlutterApp app) {
412400
final ToolWindowManager toolWindowManager = ToolWindowManager.getInstance(myProject);
413401
if (!(toolWindowManager instanceof ToolWindowManagerEx)) {
@@ -478,7 +466,7 @@ else if (jxBrowserStatus.equals(JxBrowserStatus.NOT_INSTALLED) || jxBrowserStatu
478466
}
479467
}
480468

481-
private void updateForEmptyContent(ToolWindow toolWindow) {
469+
private void updateForEmptyContent(@NotNull ToolWindow toolWindow) {
482470
// There's a possible race here where the tool window gets disposed while we're displaying contents.
483471
if (toolWindow.isDisposed()) {
484472
return;
@@ -495,7 +483,7 @@ private void updateForEmptyContent(ToolWindow toolWindow) {
495483
}
496484

497485
// Returns true if the toolWindow was initially closed but opened automatically on app launch.
498-
private DevToolsIdeFeature updateToolWindowVisibility(ToolWindow flutterToolWindow) {
486+
private @Nullable DevToolsIdeFeature updateToolWindowVisibility(@NotNull ToolWindow flutterToolWindow) {
499487
if (flutterToolWindow.isVisible()) {
500488
return DevToolsIdeFeature.TOOL_WINDOW_RELOAD;
501489
}

flutter-idea/src/io/flutter/view/ViewUtils.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public void presentLabels(@NotNull ToolWindow toolWindow, @NotNull List<String>
5454
}
5555

5656

57-
5857
public void presentClickableLabel(ToolWindow toolWindow, List<LabelInput> labels) {
5958
final JPanel panel = new JPanel(new GridLayout(0, 1));
6059

@@ -79,7 +78,7 @@ public void presentClickableLabel(ToolWindow toolWindow, List<LabelInput> labels
7978
replacePanelLabel(toolWindow, center);
8079
}
8180

82-
private void replacePanelLabel(ToolWindow toolWindow, JComponent label) {
81+
public void replacePanelLabel(ToolWindow toolWindow, JComponent label) {
8382
OpenApiUtils.safeInvokeLater(() -> {
8483
final ContentManager contentManager = toolWindow.getContentManager();
8584
if (contentManager.isDisposed()) {

0 commit comments

Comments
 (0)