From 7ab0fb85b89ff13a99335cfbb5e5d8425ba9a25b Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Tue, 3 Oct 2023 09:31:40 -0700 Subject: [PATCH 1/2] Fix #102591: Improvements to Context Menu Path Operators Improvements to `ui_but_menu_add_path_operators` so that it properly differentiates between files and directories even if the filepath is not slash terminated. It will also not add the operators to the menu if the filepath does not exist. --- .../interface/interface_context_menu.cc | 37 +++++++++++++------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/source/blender/editors/interface/interface_context_menu.cc b/source/blender/editors/interface/interface_context_menu.cc index 4f5d7cd5a29..a527ef8307f 100644 --- a/source/blender/editors/interface/interface_context_menu.cc +++ b/source/blender/editors/interface/interface_context_menu.cc @@ -15,6 +15,7 @@ #include "DNA_scene_types.h" #include "DNA_screen_types.h" +#include "BLI_fileops.h" #include "BLI_path_util.h" #include "BLI_string.h" #include "BLI_utildefines.h" @@ -429,7 +430,7 @@ static void ui_but_user_menu_add(bContext *C, uiBut *but, bUserMenu *um) } } -static void ui_but_menu_add_path_operators(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop) +static bool ui_but_menu_add_path_operators(uiLayout *layout, PointerRNA *ptr, PropertyRNA *prop) { const PropertySubType subtype = RNA_property_subtype(prop); wmOperatorType *ot = WM_operatortype_find("WM_OT_path_open", true); @@ -442,9 +443,12 @@ static void ui_but_menu_add_path_operators(uiLayout *layout, PointerRNA *ptr, Pr UNUSED_VARS_NDEBUG(subtype); RNA_property_string_get(ptr, prop, filepath); - BLI_path_split_dir_file(filepath, dir, sizeof(dir), file, sizeof(file)); - if (file[0]) { + if (!BLI_exists(filepath)) { + return false; + } + + if (BLI_is_file(filepath)) { BLI_assert(subtype == PROP_FILEPATH); uiItemFullO_ptr(layout, ot, @@ -456,16 +460,24 @@ static void ui_but_menu_add_path_operators(uiLayout *layout, PointerRNA *ptr, Pr &props_ptr); RNA_string_set(&props_ptr, "filepath", filepath); } + else { + /* This is a directory, so ensure it ends in a slash. */ + BLI_path_slash_ensure(filepath, ARRAY_SIZE(filepath)); + } + + BLI_path_split_dir_file(filepath, dir, sizeof(dir), file, sizeof(file)); uiItemFullO_ptr(layout, - ot, - CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Location Externally"), - ICON_NONE, - nullptr, - WM_OP_INVOKE_DEFAULT, - UI_ITEM_NONE, - &props_ptr); + ot, + CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Location Externally"), + ICON_NONE, + nullptr, + WM_OP_INVOKE_DEFAULT, + UI_ITEM_NONE, + &props_ptr); RNA_string_set(&props_ptr, "filepath", dir); + + return true; } static void set_layout_context_from_button(bContext *C, uiLayout *layout, uiBut *but) @@ -932,8 +944,9 @@ bool ui_popup_context_menu_for_button(bContext *C, uiBut *but, const wmEvent *ev uiItemS(layout); if (type == PROP_STRING && ELEM(subtype, PROP_FILEPATH, PROP_DIRPATH)) { - ui_but_menu_add_path_operators(layout, ptr, prop); - uiItemS(layout); + if (ui_but_menu_add_path_operators(layout, ptr, prop)) { + uiItemS(layout); + } } } -- 2.30.2 From cae6c83c5e6d874655c1c785fd367fe282f2887e Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Tue, 3 Oct 2023 09:39:49 -0700 Subject: [PATCH 2/2] Unwanted formatting changes. --- .../editors/interface/interface_context_menu.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/source/blender/editors/interface/interface_context_menu.cc b/source/blender/editors/interface/interface_context_menu.cc index a527ef8307f..db4efe9250e 100644 --- a/source/blender/editors/interface/interface_context_menu.cc +++ b/source/blender/editors/interface/interface_context_menu.cc @@ -468,13 +468,13 @@ static bool ui_but_menu_add_path_operators(uiLayout *layout, PointerRNA *ptr, Pr BLI_path_split_dir_file(filepath, dir, sizeof(dir), file, sizeof(file)); uiItemFullO_ptr(layout, - ot, - CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Location Externally"), - ICON_NONE, - nullptr, - WM_OP_INVOKE_DEFAULT, - UI_ITEM_NONE, - &props_ptr); + ot, + CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Open Location Externally"), + ICON_NONE, + nullptr, + WM_OP_INVOKE_DEFAULT, + UI_ITEM_NONE, + &props_ptr); RNA_string_set(&props_ptr, "filepath", dir); return true; -- 2.30.2