15
15
import com .intellij .openapi .vfs .VirtualFile ;
16
16
import com .intellij .ui .EditorNotificationPanel ;
17
17
import com .intellij .ui .EditorNotifications ;
18
- import com .intellij .ui .HyperlinkLabel ;
19
18
import icons .FlutterIcons ;
20
19
import org .jetbrains .annotations .NonNls ;
21
20
import org .jetbrains .annotations .NotNull ;
@@ -42,29 +41,41 @@ public EditorNotificationPanel createNotificationPanel(@NotNull VirtualFile file
42
41
if (!file .isInLocalFileSystem ()) {
43
42
return null ;
44
43
}
45
- final VirtualFile root = findRootDir (file );
44
+ return createPanelForFile (file , findRootDir (file , myProject .getBaseDir ()));
45
+ }
46
+
47
+ private EditorNotificationPanel createPanelForFile (VirtualFile file , VirtualFile root ) {
46
48
if (root == null ) {
47
49
return null ;
48
50
}
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
+ }
49
61
50
- final String actionName ;
62
+ private static String getActionName (VirtualFile root ) {
63
+ if (root == null ) {
64
+ return null ;
65
+ }
51
66
if (root .getName ().equals ("android" )) {
52
- actionName = "flutter.androidstudio.open" ;
67
+ return "flutter.androidstudio.open" ;
53
68
}
54
69
else if (root .getName ().equals ("ios" )) {
55
- actionName = "flutter.xcode.open" ;
70
+ return "flutter.xcode.open" ;
56
71
}
57
72
else {
58
73
return null ;
59
74
}
60
-
61
- NativeEditorActionsPanel panel = new NativeEditorActionsPanel (file , root , actionName );
62
- return panel .isValidForFile () ? panel : null ;
63
75
}
64
76
65
- private VirtualFile findRootDir (@ NotNull VirtualFile file ) {
77
+ private static VirtualFile findRootDir (@ NotNull VirtualFile file , VirtualFile projectDir ) {
66
78
// Return the top-most parent of file that is a child of the project directory.
67
- final VirtualFile projectDir = myProject .getBaseDir ();
68
79
VirtualFile parent = file .getParent ();
69
80
if (projectDir .equals (parent )) {
70
81
return null ;
@@ -84,6 +95,7 @@ class NativeEditorActionsPanel extends EditorNotificationPanel {
84
95
final VirtualFile myFile ;
85
96
final VirtualFile myRoot ;
86
97
final AnAction myAction ;
98
+ final boolean isVisible ;
87
99
88
100
NativeEditorActionsPanel (VirtualFile file , VirtualFile root , String actionName ) {
89
101
super (EditorColors .GUTTER_BACKGROUND );
@@ -94,29 +106,25 @@ class NativeEditorActionsPanel extends EditorNotificationPanel {
94
106
icon (FlutterIcons .Flutter );
95
107
text ("Flutter commands" );
96
108
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 ());
100
114
}
101
115
102
116
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 ) {
108
118
// The menu items are visible for certain elements outside the module directories.
109
119
return FileUtil .isAncestor (myRoot .getPath (), myFile .getPath (), true );
110
120
}
111
121
return false ;
112
122
}
113
123
114
124
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 );
118
125
// 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 ()));
120
128
}
121
129
122
130
private DataContext makeContext () {
0 commit comments