Repository: godotengine/godot (language: cpp)

Issue:
FileSystem panel loses focus after Renaming or Duplicating files
**Godot version:**
3.2.alpha 35944aebdeb4c3b5869aaeedaaded02397b7ce92
**OS/device including version:**
5.3.7.arch1-1
**Issue description:**
Deleting, renaming or duplicating a file in the FileSystem panel opens a prompt window. If you close this window while deleting a file (either by confirming the deletion or cancelling it) the FileSystem panel regains focus. This doesn't happen when duplicating or renaming a file. **edit:** [and it would be nice if it did.](https://github.com/godotengine/godot/issues/33126#issuecomment-547577403)
**Steps to reproduce:**
1. Select a file in the FileSystem panel
2. Duplicate the file either by right-click -> Duplicate or the shortcut Ctrl+D
3. Type a new name and press Return(Enter) to confirm



Discussion / hints:
What would be the intended behaviour?

I the deleting behaviour correct or the renaming and duplicating behaviour correct?
Oh sorry for being unclear: I believe having the FileSystem panel regain focus after duplicating/renaming should be the way it behaves. This would make it behave the same way as the Scene panel where it's possible to navigate using the arrow keys and duplicate/rename/delete using shortcut keys (trying to do that in the FileSystem means going back and forth from the keyboard and mouse to refocus the panel.)
Still valid in 3.2.3
Although when you do something with keyboard only, the file system keeps the focus properly. It loses it only when you move the mouse over viewport.
>Although when you do something with keyboard only, the file system keeps the focus properly. It loses it only when you move the mouse over viewport.

I don't know if the `keyboard only` method has stopped working since KoBeWi's comment.

Because for me in:
v3.4.1.stable.official [aa1b95889]
Windows 10 21H1

The FileSystem panel doesn't regain focus when renaming (`F2`) or duplicating (`Ctrl d`).

Pressing `arrow up` or `arrow down` doesn't do anything.

And if a Scene panel node (ex: Node2D) was selected before renaming or duplicating.
Then `F2` makes the `Node2D` text editable.

So I tried adding `tree->grab_focus()` at the end of the `FileSystemDock::_duplicate_operation_confirm` function. But:

- After the rename dialog gets closed, viewport automatically gains focus because the mouse is hovering over it. But if you move the rename dialog so that it no longer hovers over the viewport, after the dialog gets closed, the file system dock properly regains focus. 
@IgorKordiukiewicz  You should probably use `call_deferred()` to restore focus.
Even after trying to restore focus with `call_deferred()` it doesn't restore it, because the viewport just automatically gains focus when mouse is hovering over it.
It regains focus only when moving cursor or closing the dialog. Deferring the call should fix the latter, so it's weird it won't work 🤔

Fix patch:
diff --git a/editor/filesystem_dock.cpp b/editor/filesystem_dock.cpp
index 4fafdefbb598..267514ffde6a 100644
--- a/editor/filesystem_dock.cpp
+++ b/editor/filesystem_dock.cpp
@@ -1338,6 +1338,10 @@ void FileSystemDock::_fs_changed() {
 	}
 
 	set_process(false);
+	if (had_focus) {
+		had_focus->grab_focus();
+		had_focus = nullptr;
+	}
 }
 
 void FileSystemDock::_set_scanning_mode() {
@@ -2675,6 +2679,12 @@ bool FileSystemDock::_matches_all_search_tokens(const String &p_text) {
 }
 
 void FileSystemDock::_rescan() {
+	if (tree->has_focus()) {
+		had_focus = tree;
+	} else if (files->has_focus()) {
+		had_focus = files;
+	}
+
 	_set_scanning_mode();
 	EditorFileSystem::get_singleton()->scan();
 }
diff --git a/editor/filesystem_dock.h b/editor/filesystem_dock.h
index 0ed0de3d59e9..dac535efb8e9 100644
--- a/editor/filesystem_dock.h
+++ b/editor/filesystem_dock.h
@@ -235,6 +235,7 @@ class FileSystemDock : public VBoxContainer {
 	bool import_dock_needs_update = false;
 	TreeItem *resources_item = nullptr;
 	TreeItem *favorites_item = nullptr;
+	Control *had_focus = nullptr;
 
 	bool holding_branch = false;
 	Vector<TreeItem *> tree_items_selected_on_drag_begin;


Test patch: