Skip to content

Commit 4b19d3b

Browse files
authored
[CQ] De-duplicate color parsing and app creation code (#8082)
Clean up some code duplication in color parsing and Flutter app creation. ## Pre-launch Checklist - [x] I read the [Contributor Guide] and followed the process outlined there for submitting PRs. - [x] I read the [Tree Hygiene] wiki page, which explains my responsibilities. - [x] I read the [Flutter Style Guide] _recently_, and have followed its advice. - [x] I signed the [CLA]. - [ ] I listed at least one issue that this PR fixes in the description above. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I added new tests to check the change I am making, or this PR is [test-exempt]. - [x] All existing and new tests are passing. If you need help, consider asking for advice on the #hackers-new channel on [Discord]. <!-- Links --> [Contributor Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#overview [Tree Hygiene]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md [test-exempt]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#tests [Flutter Style Guide]: https://github.com/flutter/flutter/blob/master/docs/contributing/Style-guide-for-Flutter-repo.md [CLA]: https://cla.developers.google.com/ [flutter/tests]: https://github.com/flutter/tests [breaking change policy]: https://github.com/flutter/flutter/blob/master/docs/contributing/Tree-hygiene.md#handling-breaking-changes [Discord]: https://github.com/flutter/flutter/blob/master/docs/contributing/Chat.md
1 parent 5e5c861 commit 4b19d3b

File tree

4 files changed

+29
-52
lines changed

4 files changed

+29
-52
lines changed

flutter-idea/src/io/flutter/editor/FlutterColors.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ private static String maybeTrimSuffix(String value, String suffix) {
112112
return value;
113113
}
114114

115-
private static Color parseColor(String hexValue) {
115+
static @Nullable Color parseColor(String hexValue) {
116116
if (hexValue == null) {
117117
return null;
118118
}

flutter-idea/src/io/flutter/editor/FlutterCupertinoColors.java

+3-20
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class FlutterCupertinoColors {
3737
for (Map.Entry<Object, Object> entry : colors.entrySet()) {
3838
final String name = (String)entry.getKey();
3939
final String value = (String)entry.getValue();
40-
final Color color = parseColor(value);
40+
final Color color = FlutterColors.parseColor(value);
4141
if (color != null) {
4242
colorToName.put(color, name);
4343
}
@@ -90,25 +90,8 @@ private static String maybeTrimSuffix(String value, String suffix) {
9090
return value;
9191
}
9292

93-
private static Color parseColor(String hexValue) {
94-
if (hexValue == null) {
95-
return null;
96-
}
97-
98-
try {
99-
// argb to r, g, b, a
100-
final long value = Long.parseLong(hexValue, 16);
101-
102-
//noinspection UseJBColor
103-
return new Color((int)(value >> 16) & 0xFF, (int)(value >> 8) & 0xFF, (int)value & 0xFF, (int)(value >> 24) & 0xFF);
104-
}
105-
catch (IllegalArgumentException e) {
106-
return null;
107-
}
108-
}
109-
110-
private static Color getColorValue(String name) {
93+
private static @Nullable Color getColorValue(String name) {
11194
final String hexValue = colors.getProperty(name);
112-
return parseColor(hexValue);
95+
return FlutterColors.parseColor(hexValue);
11396
}
11497
}

flutter-idea/src/io/flutter/run/SdkAttachConfig.java

+1-16
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,7 @@ public LaunchState getState(@NotNull Executor executor, @NotNull ExecutionEnviro
8181
if (device == null) return null;
8282

8383
final GeneralCommandLine command = getCommand(env, device);
84-
85-
final FlutterApp app = FlutterApp.start(env, project, module, mode, device, command,
86-
StringUtil.capitalize(mode.mode()) + "App",
87-
"StopApp");
88-
89-
// Stop the app if the Flutter SDK changes.
90-
final FlutterSdkManager.Listener sdkListener = new FlutterSdkManager.Listener() {
91-
@Override
92-
public void flutterSdkRemoved() {
93-
app.shutdownAsync();
94-
}
95-
};
96-
FlutterSdkManager.getInstance(project).addListener(sdkListener);
97-
Disposer.register(app, () -> FlutterSdkManager.getInstance(project).removeListener(sdkListener));
98-
99-
return app;
84+
return getFlutterApp(env, device, project, module, mode, command);
10085
};
10186

10287
final LaunchState launcher = new AttachState(env, mainFile.getAppDir(), mainFile.getFile(), this, createAppCallback);

flutter-idea/src/io/flutter/run/SdkRunConfig.java

+24-15
Original file line numberDiff line numberDiff line change
@@ -187,28 +187,37 @@ public LaunchState getState(@NotNull Executor executor, @NotNull ExecutionEnviro
187187
}
188188
}
189189

190-
final FlutterApp app = FlutterApp.start(env, project, module, mode, device, command,
191-
StringUtil.capitalize(mode.mode()) + "App",
192-
"StopApp");
193-
194-
// Stop the app if the Flutter SDK changes.
195-
final FlutterSdkManager.Listener sdkListener = new FlutterSdkManager.Listener() {
196-
@Override
197-
public void flutterSdkRemoved() {
198-
app.shutdownAsync();
199-
}
200-
};
201-
FlutterSdkManager.getInstance(project).addListener(sdkListener);
202-
Disposer.register(app, () -> FlutterSdkManager.getInstance(project).removeListener(sdkListener));
203-
204-
return app;
190+
return getFlutterApp(env, device, project, module, mode, command);
205191
};
206192

207193
final LaunchState launcher = new LaunchState(env, mainFile.getAppDir(), mainFile.getFile(), this, createAppCallback);
208194
addConsoleFilters(launcher, env, mainFile, module);
209195
return launcher;
210196
}
211197

198+
static @NotNull FlutterApp getFlutterApp(@NotNull ExecutionEnvironment env,
199+
@NotNull FlutterDevice device,
200+
Project project,
201+
Module module,
202+
RunMode mode,
203+
GeneralCommandLine command) throws ExecutionException {
204+
final FlutterApp app = FlutterApp.start(env, project, module, mode, device, command,
205+
StringUtil.capitalize(mode.mode()) + "App",
206+
"StopApp");
207+
208+
// Stop the app if the Flutter SDK changes.
209+
final FlutterSdkManager.Listener sdkListener = new FlutterSdkManager.Listener() {
210+
@Override
211+
public void flutterSdkRemoved() {
212+
app.shutdownAsync();
213+
}
214+
};
215+
FlutterSdkManager.getInstance(project).addListener(sdkListener);
216+
Disposer.register(app, () -> FlutterSdkManager.getInstance(project).removeListener(sdkListener));
217+
218+
return app;
219+
}
220+
212221
protected void addConsoleFilters(@NotNull LaunchState launcher,
213222
@NotNull ExecutionEnvironment env,
214223
@NotNull MainFile mainFile,

0 commit comments

Comments
 (0)