From 1f3d1048d499826c365d7feeb596f169f8be1d95 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Thu, 11 Jul 2024 20:40:06 +0200 Subject: [PATCH] Fix #124070: Improved Area Names Using a SpaceType Callback Centralized method to obtain ScrArea names. Areas can add an optional space_name_get callback to provide a name that differs by subtype. If not defined then ED_area_name will return RNA UI name, which is correct in cases without area subtypes. This eliminates the current use of RNA_property_enum_name_gettexted using a temporary context, which results in the reported (hard to duplicate) error. Pull Request: https://projects.blender.org/blender/blender/pulls/124488 --- source/blender/blenkernel/BKE_screen.hh | 3 +++ source/blender/editors/include/ED_screen.hh | 1 + source/blender/editors/screen/screen_edit.cc | 14 ++++++++++++++ .../blender/editors/space_action/space_action.cc | 9 +++++++++ source/blender/editors/space_file/space_file.cc | 9 +++++++++ source/blender/editors/space_graph/space_graph.cc | 9 +++++++++ source/blender/editors/space_image/space_image.cc | 9 +++++++++ source/blender/editors/space_node/space_node.cc | 8 ++++++++ source/blender/windowmanager/intern/wm_window.cc | 13 ++----------- 9 files changed, 64 insertions(+), 11 deletions(-) diff --git a/source/blender/blenkernel/BKE_screen.hh b/source/blender/blenkernel/BKE_screen.hh index 8ff0ad4ce89..514e201fce7 100644 --- a/source/blender/blenkernel/BKE_screen.hh +++ b/source/blender/blenkernel/BKE_screen.hh @@ -126,6 +126,9 @@ struct SpaceType { void (*space_subtype_set)(ScrArea *area, int value); void (*space_subtype_item_extend)(bContext *C, EnumPropertyItem **item, int *totitem); + /* Return a custom name, based on subtype or other reason. */ + blender::StringRefNull (*space_name_get)(ScrArea *area); + /** * Update pointers for all structs directly owned by this space. */ diff --git a/source/blender/editors/include/ED_screen.hh b/source/blender/editors/include/ED_screen.hh index 9bfb9520085..f4a5b254234 100644 --- a/source/blender/editors/include/ED_screen.hh +++ b/source/blender/editors/include/ED_screen.hh @@ -210,6 +210,7 @@ int ED_area_header_switchbutton(const bContext *C, uiBlock *block, int yco); */ void ED_area_init(wmWindowManager *wm, wmWindow *win, ScrArea *area); void ED_area_exit(bContext *C, ScrArea *area); +blender::StringRefNull ED_area_name(ScrArea *area); int ED_screen_area_active(const bContext *C); void ED_screen_global_areas_refresh(wmWindow *win); void ED_screen_global_areas_sync(wmWindow *win); diff --git a/source/blender/editors/screen/screen_edit.cc b/source/blender/editors/screen/screen_edit.cc index 68f4c509f67..999c13877e9 100644 --- a/source/blender/editors/screen/screen_edit.cc +++ b/source/blender/editors/screen/screen_edit.cc @@ -38,6 +38,9 @@ #include "ED_screen.hh" #include "ED_screen_types.hh" +#include "RNA_access.hh" +#include "RNA_enum_types.hh" + #include "UI_interface.hh" #include "WM_message.hh" @@ -902,6 +905,17 @@ void ED_screen_exit(bContext *C, wmWindow *window, bScreen *screen) } } +blender::StringRefNull ED_area_name(ScrArea *area) +{ + if (area->type->space_name_get) { + return area->type->space_name_get(area); + } + + const int index = RNA_enum_from_value(rna_enum_space_type_items, area->spacetype); + const EnumPropertyItem item = rna_enum_space_type_items[index]; + return item.name; +} + /* *********************************** */ /* case when on area-edge or in azones, or outside window */ diff --git a/source/blender/editors/space_action/space_action.cc b/source/blender/editors/space_action/space_action.cc index 4ef7a7badf9..06b341c57e0 100644 --- a/source/blender/editors/space_action/space_action.cc +++ b/source/blender/editors/space_action/space_action.cc @@ -866,6 +866,14 @@ static void action_space_subtype_item_extend(bContext * /*C*/, RNA_enum_items_add(item, totitem, rna_enum_space_action_mode_items); } +static blender::StringRefNull action_space_name_get(ScrArea *area) +{ + SpaceAction *sact = static_cast(area->spacedata.first); + const int index = RNA_enum_from_value(rna_enum_space_action_mode_items, sact->mode); + const EnumPropertyItem item = rna_enum_space_action_mode_items[index]; + return item.name; +} + static void action_space_blend_read_data(BlendDataReader * /*reader*/, SpaceLink *sl) { SpaceAction *saction = (SpaceAction *)sl; @@ -898,6 +906,7 @@ void ED_spacetype_action() st->space_subtype_item_extend = action_space_subtype_item_extend; st->space_subtype_get = action_space_subtype_get; st->space_subtype_set = action_space_subtype_set; + st->space_name_get = action_space_name_get; st->blend_read_data = action_space_blend_read_data; st->blend_read_after_liblink = nullptr; st->blend_write = action_space_blend_write; diff --git a/source/blender/editors/space_file/space_file.cc b/source/blender/editors/space_file/space_file.cc index 4f9ee061b8c..d53d4002ee6 100644 --- a/source/blender/editors/space_file/space_file.cc +++ b/source/blender/editors/space_file/space_file.cc @@ -831,6 +831,14 @@ static void file_space_subtype_item_extend(bContext * /*C*/, EnumPropertyItem ** RNA_enum_items_add(item, totitem, rna_enum_space_file_browse_mode_items); } +static blender::StringRefNull file_space_name_get(ScrArea *area) +{ + SpaceFile *sfile = static_cast(area->spacedata.first); + const int index = RNA_enum_from_value(rna_enum_space_file_browse_mode_items, sfile->browse_mode); + const EnumPropertyItem item = rna_enum_space_file_browse_mode_items[index]; + return item.name; +} + static void file_id_remap(ScrArea *area, SpaceLink *sl, const blender::bke::id::IDRemapper & /*mappings*/) @@ -926,6 +934,7 @@ void ED_spacetype_file() st->space_subtype_item_extend = file_space_subtype_item_extend; st->space_subtype_get = file_space_subtype_get; st->space_subtype_set = file_space_subtype_set; + st->space_name_get = file_space_name_get; st->context = file_context; st->id_remap = file_id_remap; st->foreach_id = file_foreach_id; diff --git a/source/blender/editors/space_graph/space_graph.cc b/source/blender/editors/space_graph/space_graph.cc index c32d2c23f34..0dafd68d453 100644 --- a/source/blender/editors/space_graph/space_graph.cc +++ b/source/blender/editors/space_graph/space_graph.cc @@ -868,6 +868,14 @@ static void graph_space_subtype_item_extend(bContext * /*C*/, RNA_enum_items_add(item, totitem, rna_enum_space_graph_mode_items); } +static blender::StringRefNull graph_space_name_get(ScrArea *area) +{ + SpaceGraph *sgraph = static_cast(area->spacedata.first); + const int index = RNA_enum_from_value(rna_enum_space_graph_mode_items, sgraph->mode); + const EnumPropertyItem item = rna_enum_space_graph_mode_items[index]; + return item.name; +} + static void graph_space_blend_read_data(BlendDataReader *reader, SpaceLink *sl) { SpaceGraph *sipo = (SpaceGraph *)sl; @@ -914,6 +922,7 @@ void ED_spacetype_ipo() st->space_subtype_item_extend = graph_space_subtype_item_extend; st->space_subtype_get = graph_space_subtype_get; st->space_subtype_set = graph_space_subtype_set; + st->space_name_get = graph_space_name_get; st->blend_read_data = graph_space_blend_read_data; st->blend_read_after_liblink = nullptr; st->blend_write = graph_space_blend_write; diff --git a/source/blender/editors/space_image/space_image.cc b/source/blender/editors/space_image/space_image.cc index 513790e5bd0..891235b56fd 100644 --- a/source/blender/editors/space_image/space_image.cc +++ b/source/blender/editors/space_image/space_image.cc @@ -1077,6 +1077,14 @@ static void image_space_subtype_item_extend(bContext * /*C*/, RNA_enum_items_add(item, totitem, rna_enum_space_image_mode_items); } +static blender::StringRefNull image_space_name_get(ScrArea *area) +{ + SpaceImage *sima = static_cast(area->spacedata.first); + const int index = RNA_enum_from_value(rna_enum_space_image_mode_items, sima->mode); + const EnumPropertyItem item = rna_enum_space_image_mode_items[index]; + return item.name; +} + static void image_space_blend_read_data(BlendDataReader * /*reader*/, SpaceLink *sl) { SpaceImage *sima = (SpaceImage *)sl; @@ -1132,6 +1140,7 @@ void ED_spacetype_image() st->space_subtype_item_extend = image_space_subtype_item_extend; st->space_subtype_get = image_space_subtype_get; st->space_subtype_set = image_space_subtype_set; + st->space_name_get = image_space_name_get; st->blend_read_data = image_space_blend_read_data; st->blend_read_after_liblink = nullptr; st->blend_write = image_space_blend_write; diff --git a/source/blender/editors/space_node/space_node.cc b/source/blender/editors/space_node/space_node.cc index 6362b2530a3..49e3f3a9bcf 100644 --- a/source/blender/editors/space_node/space_node.cc +++ b/source/blender/editors/space_node/space_node.cc @@ -1334,6 +1334,13 @@ static void node_space_subtype_item_extend(bContext *C, EnumPropertyItem **item, } } +static blender::StringRefNull node_space_name_get(ScrArea *area) +{ + SpaceNode *snode = (SpaceNode *)area->spacedata.first; + bke::bNodeTreeType *tree_type = bke::ntreeTypeFind(snode->tree_idname); + return tree_type->ui_name; +} + static void node_space_blend_read_data(BlendDataReader *reader, SpaceLink *sl) { SpaceNode *snode = (SpaceNode *)sl; @@ -1387,6 +1394,7 @@ void ED_spacetype_node() st->space_subtype_item_extend = node_space_subtype_item_extend; st->space_subtype_get = node_space_subtype_get; st->space_subtype_set = node_space_subtype_set; + st->space_name_get = node_space_name_get; st->blend_read_data = node_space_blend_read_data; st->blend_read_after_liblink = nullptr; st->blend_write = node_space_blend_write; diff --git a/source/blender/windowmanager/intern/wm_window.cc b/source/blender/windowmanager/intern/wm_window.cc index 8905fc7537d..3ca1dc08d60 100644 --- a/source/blender/windowmanager/intern/wm_window.cc +++ b/source/blender/windowmanager/intern/wm_window.cc @@ -501,17 +501,8 @@ void WM_window_title(wmWindowManager *wm, wmWindow *win, const char *title) const bool is_single = screen && BLI_listbase_is_single(&screen->areabase); ScrArea *area = (screen) ? static_cast(screen->areabase.first) : nullptr; const char *name = "Blender"; - if (is_single && area) { - PointerRNA ptr = RNA_pointer_create(&(screen->id), &RNA_Area, area); - PropertyRNA *prop_ui_type = RNA_struct_find_property(&ptr, "ui_type"); - bContext *C = CTX_create(); - CTX_wm_manager_set(C, wm); - CTX_wm_window_set(C, win); - CTX_wm_screen_set(C, screen); - CTX_wm_area_set(C, area); - RNA_property_enum_name_gettexted( - C, &ptr, prop_ui_type, RNA_property_enum_get(&ptr, prop_ui_type), &name); - CTX_free(C); + if (is_single && area && area->spacetype != SPACE_EMPTY) { + name = IFACE_(ED_area_name(area).c_str()); } GHOST_SetTitle(handle, name); return;