1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Merge pull request #107085 from KoBeWi/new_in_folder

Always use base directory in `CONTEXT_SLOT_FILESYSTEM_CREATE`
This commit is contained in:
Rémi Verschelde
2025-06-13 01:30:43 +02:00
2 changed files with 23 additions and 3 deletions

View File

@@ -89,7 +89,14 @@
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.
</constant>
<constant name="CONTEXT_SLOT_FILESYSTEM_CREATE" value="3" enum="ContextMenuSlot">
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.
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.
[codeblock]
func _popup_menu(paths):
if paths.is_empty():
add_context_menu_item("New Image File...", create_image)
else:
add_context_menu_item("Image File...", create_image)
[/codeblock]
</constant>
<constant name="CONTEXT_SLOT_SCRIPT_EDITOR_CODE" value="4" enum="ContextMenuSlot">
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:

View File

@@ -3342,7 +3342,8 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, const Vect
new_menu->add_icon_item(get_editor_theme_icon(SNAME("Object")), TTRC("Resource..."), FILE_MENU_NEW_RESOURCE);
new_menu->add_icon_item(get_editor_theme_icon(SNAME("TextFile")), TTRC("TextFile..."), FILE_MENU_NEW_TEXTFILE);
EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(new_menu, EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, p_paths);
const PackedStringArray folder_path = { p_paths[0].get_base_dir() };
EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(new_menu, EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, folder_path);
p_popup->add_separator();
}
@@ -3743,13 +3744,25 @@ void FileSystemDock::_tree_gui_input(Ref<InputEvent> p_event) {
if (option_id > -1) {
_tree_rmb_option(option_id);
} else {
bool create = false;
Callable custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM, p_event);
if (!custom_callback.is_valid()) {
create = true;
custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, p_event);
}
if (custom_callback.is_valid()) {
EditorContextMenuPluginManager::get_singleton()->invoke_callback(custom_callback, _tree_get_selected(false));
PackedStringArray selected = _tree_get_selected(false);
if (create) {
if (selected.is_empty()) {
selected.append("res://");
} else if (selected.size() == 1) {
selected.write[0] = selected[0].get_base_dir();
} else {
return;
}
}
EditorContextMenuPluginManager::get_singleton()->invoke_callback(custom_callback, selected);
} else {
return;
}