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.
This commit is contained in:
Julian Eisel 2021-03-18 15:00:55 +01:00
parent 248d9809ca
commit 2dd040a349
1 changed files with 38 additions and 13 deletions

View File

@ -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,