Skip to content

Commit 856da68

Browse files
committed
Merge pull request #107085 from KoBeWi/new_in_folder
Always use base directory in `CONTEXT_SLOT_FILESYSTEM_CREATE`
2 parents 2ac31ec + 9e8396c commit 856da68

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

doc/classes/EditorContextMenuPlugin.xml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,14 @@
8989
Context menu of Script editor's script tabs. [method _popup_menu] will be called with the path to the currently edited script, while option callback will receive reference to that script.
9090
</constant>
9191
<constant name="CONTEXT_SLOT_FILESYSTEM_CREATE" value="3" enum="ContextMenuSlot">
92-
The "Create..." submenu of FileSystem dock's context menu, or the "New" section of the main context menu when empty space is clicked. [method _popup_menu] and option callback will be called with the path of the currently selected folder, wrapped in a list. When clicking the empty space, the list of paths for popup method will be empty.
92+
The "Create..." submenu of FileSystem dock's context menu, or the "New" section of the main context menu when empty space is clicked. [method _popup_menu] and option callback will be called with the path of the currently selected folder. When clicking the empty space, the list of paths for popup method will be empty.
93+
[codeblock]
94+
func _popup_menu(paths):
95+
if paths.is_empty():
96+
add_context_menu_item("New Image File...", create_image)
97+
else:
98+
add_context_menu_item("Image File...", create_image)
99+
[/codeblock]
93100
</constant>
94101
<constant name="CONTEXT_SLOT_SCRIPT_EDITOR_CODE" value="4" enum="ContextMenuSlot">
95102
Context menu of Script editor's code editor. [method _popup_menu] will be called with the path to the [CodeEdit] node. You can fetch it using this code:

editor/filesystem_dock.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3342,7 +3342,8 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, const Vect
33423342
new_menu->add_icon_item(get_editor_theme_icon(SNAME("Object")), TTRC("Resource..."), FILE_MENU_NEW_RESOURCE);
33433343
new_menu->add_icon_item(get_editor_theme_icon(SNAME("TextFile")), TTRC("TextFile..."), FILE_MENU_NEW_TEXTFILE);
33443344

3345-
EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(new_menu, EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, p_paths);
3345+
const PackedStringArray folder_path = { p_paths[0].get_base_dir() };
3346+
EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(new_menu, EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, folder_path);
33463347
p_popup->add_separator();
33473348
}
33483349

@@ -3743,13 +3744,25 @@ void FileSystemDock::_tree_gui_input(Ref<InputEvent> p_event) {
37433744
if (option_id > -1) {
37443745
_tree_rmb_option(option_id);
37453746
} else {
3747+
bool create = false;
37463748
Callable custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM, p_event);
37473749
if (!custom_callback.is_valid()) {
3750+
create = true;
37483751
custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, p_event);
37493752
}
37503753

37513754
if (custom_callback.is_valid()) {
3752-
EditorContextMenuPluginManager::get_singleton()->invoke_callback(custom_callback, _tree_get_selected(false));
3755+
PackedStringArray selected = _tree_get_selected(false);
3756+
if (create) {
3757+
if (selected.is_empty()) {
3758+
selected.append("res://");
3759+
} else if (selected.size() == 1) {
3760+
selected.write[0] = selected[0].get_base_dir();
3761+
} else {
3762+
return;
3763+
}
3764+
}
3765+
EditorContextMenuPluginManager::get_singleton()->invoke_callback(custom_callback, selected);
37533766
} else {
37543767
return;
37553768
}

0 commit comments

Comments
 (0)