Repository: spyder-ide/spyder

Issue:
Right-clicking an empty folder doesn't show the context menu of the Files pane
Entering inside a folder without elements and then right clicking inside it doesn't show the context menu of the Files pane:
![explorer1](https://user-images.githubusercontent.com/16781833/84448408-3f51c880-ac10-11ea-9110-0240471561bb.gif)

_Originally posted by @dalthviz in https://github.com/spyder-ide/spyder/pull/12401#issuecomment-642978296_


Discussion:
@ccordoba12 do you think this should go for 4.1.4? Seems like an easy enough fix.
Sure. @steff456, please take a look at this one.
I'm not able to reproduce this issue in macOs or linux in the current 4.x branch

![prueba](https://user-images.githubusercontent.com/20992645/84699863-e89e0480-af17-11ea-9c50-07937314646f.gif)

![image](https://user-images.githubusercontent.com/20992645/84699996-18e5a300-af18-11ea-8068-30e1e1086e49.png)



Resolving patch:
diff --git a/spyder/plugins/explorer/widgets/explorer.py b/spyder/plugins/explorer/widgets/explorer.py
--- a/spyder/plugins/explorer/widgets/explorer.py
+++ b/spyder/plugins/explorer/widgets/explorer.py
@@ -287,8 +287,8 @@ def get_selected_filenames(self):
         if self.selectionMode() == self.ExtendedSelection:
             if self.selectionModel() is None:
                 return []
-            return [self.get_filename(idx) for idx in
-                    self.selectionModel().selectedRows()]
+            rows = self.selectionModel().selectedRows()
+            return [self.get_filename(idx) for idx in rows]
         else:
             return [self.get_filename(self.currentIndex())]
 
@@ -401,24 +401,23 @@ def toggle_all(self, checked):
 
     def create_file_new_actions(self, fnames):
         """Return actions for submenu 'New...'"""
-        if not fnames:
-            return []
+        root_path = self.fsmodel.rootPath()
         new_file_act = create_action(self, _("File..."),
                                      icon=ima.icon('filenew'),
                                      triggered=lambda:
-                                     self.new_file(fnames[-1]))
+                                     self.new_file(root_path))
         new_module_act = create_action(self, _("Module..."),
                                        icon=ima.icon('spyder'),
                                        triggered=lambda:
-                                         self.new_module(fnames[-1]))
+                                       self.new_module(root_path))
         new_folder_act = create_action(self, _("Folder..."),
                                        icon=ima.icon('folder_new'),
                                        triggered=lambda:
-                                        self.new_folder(fnames[-1]))
+                                       self.new_folder(root_path))
         new_package_act = create_action(self, _("Package..."),
                                         icon=ima.icon('package_new'),
                                         triggered=lambda:
-                                         self.new_package(fnames[-1]))
+                                        self.new_package(root_path))
         return [new_file_act, new_folder_act, None,
                 new_module_act, new_package_act]
 
@@ -508,6 +507,8 @@ def create_file_manage_actions(self, fnames):
                 assoc = self.get_file_associations(fnames[0])
             elif len(fnames) > 1:
                 assoc = self.get_common_file_associations(fnames)
+            else:
+                assoc = []
 
             if len(assoc) >= 1:
                 actions.append(open_with_menu)
@@ -540,9 +541,10 @@ def create_file_manage_actions(self, fnames):
         external_fileexp_action = create_action(
             self, text, triggered=self.show_in_external_file_explorer)
         actions += [delete_action, rename_action]
-        basedir = fixpath(osp.dirname(fnames[0]))
-        if all([fixpath(osp.dirname(_fn)) == basedir for _fn in fnames]):
-            actions.append(move_action)
+        if fnames:
+            basedir = fixpath(osp.dirname(fnames[0]))
+            if all([fixpath(osp.dirname(_fn)) == basedir for _fn in fnames]):
+                actions.append(move_action)
         actions += [None]
         actions += [copy_file_clipboard_action, save_file_clipboard_action,
                     copy_absolute_path_action, copy_relative_path_action]
@@ -554,7 +556,9 @@ def create_file_manage_actions(self, fnames):
         if only_notebooks and nbexporter is not None:
             actions.append(ipynb_convert_action)
 
-        dirname = fnames[0] if osp.isdir(fnames[0]) else osp.dirname(fnames[0])
+        if fnames:
+            dirname = (
+                fnames[0] if osp.isdir(fnames[0]) else osp.dirname(fnames[0]))
         if len(fnames) == 1:
             # VCS support is quite limited for now, so we are enabling the VCS
             # related actions only when a single file/folder is selected:
@@ -569,6 +573,17 @@ def create_file_manage_actions(self, fnames):
                                         triggered=browse_slot)
                 actions += [None, vcs_ci, vcs_log]
 
+        # Needed to disable actions in the context menu if there's an
+        # empty folder. See spyder-ide/spyder#13004
+        if len(fnames) == 1 and self.selectionModel():
+            rows = self.selectionModel().selectedRows()
+            if (self.fsmodel.type(rows[0]) == 'Folder' and
+                    self.fsmodel.rowCount(rows[0]) == 0):
+                fnames = []
+        if not fnames:
+            for action in actions:
+                if action:
+                    action.setDisabled(True)
         return actions
 
     def create_folder_manage_actions(self, fnames):
@@ -607,8 +622,7 @@ def create_context_menu_actions(self):
             actions += import_actions
         if actions:
             actions.append(None)
-        if fnames:
-            actions += self.create_file_manage_actions(fnames)
+        actions += self.create_file_manage_actions(fnames)
         if actions:
             actions.append(None)
         if fnames and all([osp.isdir(_fn) for _fn in fnames]):
@@ -644,6 +658,7 @@ def contextMenuEvent(self, event):
         try:
             self.update_menu()
             self.menu.popup(event.globalPos())
+            event.accept()
         except AttributeError:
             pass
 


Test patch: