Skip to content

Commit 9e8396c

Browse files
committed
Always use base directory in CONTEXT_SLOT_FILESYSTEM_CREATE
1 parent e45cc68 commit 9e8396c

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
@@ -3325,7 +3325,8 @@ void FileSystemDock::_file_and_folders_fill_popup(PopupMenu *p_popup, const Vect
33253325
new_menu->add_icon_item(get_editor_theme_icon(SNAME("Object")), TTRC("Resource..."), FILE_MENU_NEW_RESOURCE);
33263326
new_menu->add_icon_item(get_editor_theme_icon(SNAME("TextFile")), TTRC("TextFile..."), FILE_MENU_NEW_TEXTFILE);
33273327

3328-
EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(new_menu, EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, p_paths);
3328+
const PackedStringArray folder_path = { p_paths[0].get_base_dir() };
3329+
EditorContextMenuPluginManager::get_singleton()->add_options_from_plugins(new_menu, EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, folder_path);
33293330
p_popup->add_separator();
33303331
}
33313332

@@ -3726,13 +3727,25 @@ void FileSystemDock::_tree_gui_input(Ref<InputEvent> p_event) {
37263727
if (option_id > -1) {
37273728
_tree_rmb_option(option_id);
37283729
} else {
3730+
bool create = false;
37293731
Callable custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM, p_event);
37303732
if (!custom_callback.is_valid()) {
3733+
create = true;
37313734
custom_callback = EditorContextMenuPluginManager::get_singleton()->match_custom_shortcut(EditorContextMenuPlugin::CONTEXT_SLOT_FILESYSTEM_CREATE, p_event);
37323735
}
37333736

37343737
if (custom_callback.is_valid()) {
3735-
EditorContextMenuPluginManager::get_singleton()->invoke_callback(custom_callback, _tree_get_selected(false));
3738+
PackedStringArray selected = _tree_get_selected(false);
3739+
if (create) {
3740+
if (selected.is_empty()) {
3741+
selected.append("res://");
3742+
} else if (selected.size() == 1) {
3743+
selected.write[0] = selected[0].get_base_dir();
3744+
} else {
3745+
return;
3746+
}
3747+
}
3748+
EditorContextMenuPluginManager::get_singleton()->invoke_callback(custom_callback, selected);
37363749
} else {
37373750
return;
37383751
}

0 commit comments

Comments
 (0)