Skip to content

Commit fce7d84

Browse files
authored
Update the action label depending on selection (#2242)
* Update the action label depending on selection * Review comments
1 parent c394ae9 commit fce7d84

File tree

5 files changed

+66
-29
lines changed

5 files changed

+66
-29
lines changed

flutter-intellij-community.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,4 @@
4545
<orderEntry type="library" name="Dart SDK" level="project" />
4646
<orderEntry type="library" name="Dart Packages" level="project" />
4747
</component>
48-
</module>
48+
</module>

flutter-intellij.iml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,4 @@
5959
<orderEntry type="library" name="Dart SDK" level="project" />
6060
<orderEntry type="library" name="Dart Packages" level="project" />
6161
</component>
62-
</module>
62+
</module>

src/io/flutter/FlutterBundle.properties

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,3 +162,8 @@ old.android.studio.message=Your path contains Android Studio 2.x,\n\
162162
which does not support Flutter.\n\n\
163163
You can change it using the flutter command-line tool via:\n\
164164
flutter\u00a0config\u00a0--android-studio-dir\u00a0{0}path{0}to{0}Android\u00a0Studio\u00a03.x
165+
166+
flutter.androidstudio.open.module.text=Open Android module in Android Studio
167+
flutter.androidstudio.open.module.description=Open Android Studio in a new window to edit the top-level Android project
168+
flutter.androidstudio.open.file.text=Open for Editing in Android Studio
169+
flutter.androidstudio.open.file.description=Open Android Studio in a new window to edit this file

src/io/flutter/actions/OpenInAndroidStudioAction.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,14 @@
3030

3131
public class OpenInAndroidStudioAction extends AnAction {
3232

33+
private static final String LABEL_FILE = FlutterBundle.message("flutter.androidstudio.open.file.text");
34+
private static final String DESCR_FILE = FlutterBundle.message("flutter.androidstudio.open.file.description");
35+
private static final String LABEL_MODULE = FlutterBundle.message("flutter.androidstudio.open.module.text");
36+
private static final String DESCR_MODULE = FlutterBundle.message("flutter.androidstudio.open.module.description");
37+
3338
@Override
3439
public void update(AnActionEvent event) {
35-
final boolean enabled = findProjectFile(event) != null;
36-
37-
final Presentation presentation = event.getPresentation();
38-
presentation.setEnabled(enabled);
39-
presentation.setVisible(enabled);
40+
updatePresentation(event, event.getPresentation());
4041
}
4142

4243
@Override
@@ -58,6 +59,29 @@ public void actionPerformed(AnActionEvent e) {
5859
openFileInStudio(projectFile, androidStudioPath, sourceFile);
5960
}
6061

62+
private static void updatePresentation(AnActionEvent event, Presentation state) {
63+
if (findProjectFile(event) == null) {
64+
state.setVisible(false);
65+
}
66+
else {
67+
VirtualFile file = event.getData(CommonDataKeys.VIRTUAL_FILE);
68+
String label, descr;
69+
if (file != null && !file.isDirectory()) {
70+
// The file will be opened in an editor in the new IDE window.
71+
label = LABEL_FILE;
72+
descr = DESCR_FILE;
73+
}
74+
else {
75+
// The new IDE window will be opened on the Android module but there is no file selected for editing.
76+
label = LABEL_MODULE;
77+
descr = DESCR_MODULE;
78+
}
79+
state.setVisible(true);
80+
state.setText(label);
81+
state.setDescription(descr);
82+
}
83+
}
84+
6185
protected static boolean isProjectFileName(String name) {
6286
// Note: If the project content is rearranged to have the android module file within the android directory, this will fail.
6387
return name.endsWith("_android.iml");

src/io/flutter/editor/NativeEditorNotificationProvider.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import com.intellij.openapi.vfs.VirtualFile;
1616
import com.intellij.ui.EditorNotificationPanel;
1717
import com.intellij.ui.EditorNotifications;
18-
import com.intellij.ui.HyperlinkLabel;
1918
import icons.FlutterIcons;
2019
import org.jetbrains.annotations.NonNls;
2120
import org.jetbrains.annotations.NotNull;
@@ -42,29 +41,41 @@ public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file
4241
if (!file.isInLocalFileSystem()) {
4342
return null;
4443
}
45-
final VirtualFile root = findRootDir(file);
44+
return createPanelForFile(file, findRootDir(file, myProject.getBaseDir()));
45+
}
46+
47+
private EditorNotificationPanel createPanelForFile(VirtualFile file, VirtualFile root) {
4648
if (root == null) {
4749
return null;
4850
}
51+
return createPanelForAction(file, root, getActionName(root));
52+
}
53+
54+
private EditorNotificationPanel createPanelForAction(VirtualFile file, VirtualFile root, String actionName) {
55+
if (actionName == null) {
56+
return null;
57+
}
58+
NativeEditorActionsPanel panel = new NativeEditorActionsPanel(file, root, actionName);
59+
return panel.isValidForFile() ? panel : null;
60+
}
4961

50-
final String actionName;
62+
private static String getActionName(VirtualFile root) {
63+
if (root == null) {
64+
return null;
65+
}
5166
if (root.getName().equals("android")) {
52-
actionName = "flutter.androidstudio.open";
67+
return "flutter.androidstudio.open";
5368
}
5469
else if (root.getName().equals("ios")) {
55-
actionName = "flutter.xcode.open";
70+
return "flutter.xcode.open";
5671
}
5772
else {
5873
return null;
5974
}
60-
61-
NativeEditorActionsPanel panel = new NativeEditorActionsPanel(file, root, actionName);
62-
return panel.isValidForFile() ? panel : null;
6375
}
6476

65-
private VirtualFile findRootDir(@NotNull VirtualFile file) {
77+
private static VirtualFile findRootDir(@NotNull VirtualFile file, VirtualFile projectDir) {
6678
// Return the top-most parent of file that is a child of the project directory.
67-
final VirtualFile projectDir = myProject.getBaseDir();
6879
VirtualFile parent = file.getParent();
6980
if (projectDir.equals(parent)) {
7081
return null;
@@ -84,6 +95,7 @@ class NativeEditorActionsPanel extends EditorNotificationPanel {
8495
final VirtualFile myFile;
8596
final VirtualFile myRoot;
8697
final AnAction myAction;
98+
final boolean isVisible;
8799

88100
NativeEditorActionsPanel(VirtualFile file, VirtualFile root, String actionName) {
89101
super(EditorColors.GUTTER_BACKGROUND);
@@ -94,29 +106,25 @@ class NativeEditorActionsPanel extends EditorNotificationPanel {
94106
icon(FlutterIcons.Flutter);
95107
text("Flutter commands");
96108

97-
final Presentation present = myAction.getTemplatePresentation();
98-
final HyperlinkLabel label = createActionLabel(present.getText(), this::performAction);
99-
label.setToolTipText(present.getDescription());
109+
// Ensure this project is a Flutter project by updating the menu action. It will only be visible for Flutter projects.
110+
myAction.update(AnActionEvent.createFromDataContext(ActionPlaces.EDITOR_TOOLBAR, myAction.getTemplatePresentation(), makeContext()));
111+
isVisible = myAction.getTemplatePresentation().isVisible();
112+
createActionLabel(myAction.getTemplatePresentation().getText(), this::performAction)
113+
.setToolTipText(myAction.getTemplatePresentation().getDescription());
100114
}
101115

102116
private boolean isValidForFile() {
103-
final DataContext context = makeContext();
104-
final AnActionEvent event = AnActionEvent.createFromDataContext(ActionPlaces.EDITOR_TOOLBAR, null, context);
105-
// Ensure this project is a Flutter project by updating the menu action. It will only be visible for Flutter projects.
106-
myAction.update(event);
107-
if (event.getPresentation().isVisible()) {
117+
if (isVisible) {
108118
// The menu items are visible for certain elements outside the module directories.
109119
return FileUtil.isAncestor(myRoot.getPath(), myFile.getPath(), true);
110120
}
111121
return false;
112122
}
113123

114124
private void performAction() {
115-
final Presentation present = myAction.getTemplatePresentation();
116-
final DataContext context = makeContext();
117-
final AnActionEvent event = AnActionEvent.createFromDataContext(ActionPlaces.EDITOR_TOOLBAR, present, context);
118125
// Open Xcode or Android Studio. If already running AS then just open a new window.
119-
myAction.actionPerformed(event);
126+
myAction.actionPerformed(
127+
AnActionEvent.createFromDataContext(ActionPlaces.EDITOR_TOOLBAR, myAction.getTemplatePresentation(), makeContext()));
120128
}
121129

122130
private DataContext makeContext() {

0 commit comments

Comments
 (0)