Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian Eisel <julian@blender.org>2021-03-18 17:00:55 +0300
committerJulian Eisel <julian@blender.org>2021-03-18 17:09:31 +0300
commit2dd040a3495309566d6b2582e264b281752bcdd5 (patch)
tree46172bfc5d549bc6ab36d3ddfe98fea527ca550d
parent248d9809cabe483049fdc50f777a540180b4635b (diff)
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.
-rw-r--r--source/blender/editors/interface/interface_templates.c51
1 files changed, 38 insertions, 13 deletions
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,