[Bf-blender-cvs] [2dd040a3495] master: UI: Dynamically increase data-block name button size if there's space

Julian Eisel noreply at git.blender.org
Thu Mar 18 15:09:34 CET 2021


Commit: 2dd040a3495309566d6b2582e264b281752bcdd5
Author: Julian Eisel
Date:   Thu Mar 18 15:00:55 2021 +0100
Branches: master
https://developer.blender.org/rB2dd040a3495309566d6b2582e264b281752bcdd5

UI: Dynamically increase data-block name button size if there's space

Data-block selectors (or other selectors using the
`UILayout.teamplate_search` or `UILayout.template_search_preview`)
in headers used a fixed size width for the name button. Often that meant
the text would be clipped to fit even though there was plenty of
whitespace surrounding the data-block selector.

Now the name button can increase in width with the contained string,
clamped to some arbitrary and quite big maximum (3 times the minimum
width).
We could further take the available space into account, to reduce the
need for scrolling in the header. But I don't think that is much of an
issue and the name clipping it would re-introduce would probably be more
annoying.

===================================================================

M	source/blender/editors/interface/interface_templates.c

===================================================================

diff --git a/source/blender/editors/interface/interface_templates.c b/source/blender/editors/interface/interface_templates.c
index 42b6e771fe4..0a625cc2b56 100644
--- a/source/blender/editors/interface/interface_templates.c
+++ b/source/blender/editors/interface/interface_templates.c
@@ -102,7 +102,7 @@
 // #define USE_OP_RESET_BUT
 
 /* defines for templateID/TemplateSearch */
-#define TEMPLATE_SEARCH_TEXTBUT_WIDTH (UI_UNIT_X * 6)
+#define TEMPLATE_SEARCH_TEXTBUT_MIN_WIDTH (UI_UNIT_X * 6)
 #define TEMPLATE_SEARCH_TEXTBUT_HEIGHT UI_UNIT_Y
 
 void UI_template_fix_linking(void)
@@ -125,6 +125,33 @@ void uiTemplateHeader(uiLayout *layout, bContext *C)
 /** \name Search Menu Helpers
  * \{ */
 
+static int template_search_textbut_width(PointerRNA *ptr, PropertyRNA *name_prop)
+{
+  char str[UI_MAX_DRAW_STR];
+  int buf_len = 0;
+
+  BLI_assert(RNA_property_type(name_prop) == PROP_STRING);
+
+  const char *name = RNA_property_string_get_alloc(ptr, name_prop, str, sizeof(str), &buf_len);
+
+  const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
+  const int margin = UI_UNIT_X * 0.75f;
+  const int estimated_width = UI_fontstyle_string_width(fstyle, name) + margin;
+
+  if (name != str) {
+    MEM_freeN((void *)name);
+  }
+
+  /* Clamp to some min/max width. */
+  return CLAMPIS(
+      estimated_width, TEMPLATE_SEARCH_TEXTBUT_MIN_WIDTH, TEMPLATE_SEARCH_TEXTBUT_MIN_WIDTH * 3);
+}
+
+static int template_search_textbut_height(void)
+{
+  return TEMPLATE_SEARCH_TEXTBUT_HEIGHT;
+}
+
 /**
  * Add a block button for the search menu for templateID and templateSearch.
  */
@@ -954,6 +981,10 @@ static void template_ID(const bContext *C,
     char name[UI_MAX_NAME_STR];
     const bool user_alert = (id->us <= 0);
 
+    const int width = template_search_textbut_width(&idptr,
+                                                    RNA_struct_find_property(&idptr, "name"));
+    const int height = template_search_textbut_height();
+
     // text_idbutton(id, name);
     name[0] = '\0';
     but = uiDefButR(block,
@@ -962,8 +993,8 @@ static void template_ID(const bContext *C,
                     name,
                     0,
                     0,
-                    TEMPLATE_SEARCH_TEXTBUT_WIDTH,
-                    TEMPLATE_SEARCH_TEXTBUT_HEIGHT,
+                    width,
+                    height,
                     &idptr,
                     "name",
                     -1,
@@ -1682,16 +1713,10 @@ static void template_search_add_button_name(uiBlock *block,
                                             PointerRNA *active_ptr,
                                             const StructRNA *type)
 {
-  uiDefAutoButR(block,
-                active_ptr,
-                RNA_struct_name_property(type),
-                0,
-                "",
-                ICON_NONE,
-                0,
-                0,
-                TEMPLATE_SEARCH_TEXTBUT_WIDTH,
-                TEMPLATE_SEARCH_TEXTBUT_HEIGHT);
+  PropertyRNA *name_prop = RNA_struct_name_property(type);
+  const int width = template_search_textbut_width(active_ptr, name_prop);
+  const int height = template_search_textbut_height();
+  uiDefAutoButR(block, active_ptr, name_prop, 0, "", ICON_NONE, 0, 0, width, height);
 }
 
 static void template_search_add_button_operator(uiBlock *block,



More information about the Bf-blender-cvs mailing list