UI: Add support for row-aligned icon buttons in menus

This adds support for drawing icon buttons as a row in menus. This is
needed for drawing collection color tagging icons in the outliner
context menu in T77777.

Part of T77408

Differential Revision: https://developer.blender.org/D8317
This commit is contained in:
Nathan Craddock 2020-09-15 07:19:57 -06:00
parent 13c7df1054
commit e17df47303
Notes: blender-bot 2023-10-04 09:42:55 +02:00
Referenced by issue #77408, Continued Outliner Improvements Design
3 changed files with 59 additions and 3 deletions

View File

@ -367,6 +367,12 @@ void UI_block_translate(uiBlock *block, int x, int y)
BLI_rctf_translate(&block->rect, x, y);
}
static bool ui_but_is_row_alignment_group(const uiBut *left, const uiBut *right)
{
const bool is_same_align_group = (left->alignnr && (left->alignnr == right->alignnr));
return is_same_align_group && (left->rect.xmin < right->rect.xmin);
}
static void ui_block_bounds_calc_text(uiBlock *block, float offset)
{
const uiStyle *style = UI_style_get();
@ -385,7 +391,26 @@ static void ui_block_bounds_calc_text(uiBlock *block, float offset)
}
}
if (bt->next && bt->rect.xmin < bt->next->rect.xmin) {
/* Skip all buttons that are in a horizontal alignment group.
* We don't want to split them appart (but still check the row's width and apply current
* offsets). */
if (bt->next && ui_but_is_row_alignment_group(bt, bt->next)) {
int width = 0;
int alignnr = bt->alignnr;
for (col_bt = bt; col_bt && col_bt->alignnr == alignnr; col_bt = col_bt->next) {
width += BLI_rctf_size_x(&col_bt->rect);
col_bt->rect.xmin += x1addval;
col_bt->rect.xmax += x1addval;
}
if (width > i) {
i = width;
}
/* Give the following code the last button in the alignment group, there might have to be a
* split immediately after. */
bt = col_bt ? col_bt->prev : NULL;
}
if (bt && bt->next && bt->rect.xmin < bt->next->rect.xmin) {
/* End of this column, and it's not the last one. */
for (col_bt = init_col_bt; col_bt->prev != bt; col_bt = col_bt->next) {
col_bt->rect.xmin = x1addval;
@ -403,6 +428,17 @@ static void ui_block_bounds_calc_text(uiBlock *block, float offset)
/* Last column. */
for (col_bt = init_col_bt; col_bt; col_bt = col_bt->next) {
/* Recognize a horizontally arranged alignment group and skip its items. */
if (col_bt->next && ui_but_is_row_alignment_group(col_bt, col_bt->next)) {
int alignnr = col_bt->alignnr;
for (; col_bt && col_bt->alignnr == alignnr; col_bt = col_bt->next) {
/* pass */
}
}
if (!col_bt) {
break;
}
col_bt->rect.xmin = x1addval;
col_bt->rect.xmax = max_ff(x1addval + i + block->bounds, offset + block->minbounds);

View File

@ -1434,6 +1434,26 @@ void uiItemsFullEnumO_items(uiLayout *layout,
if (radial) {
target = uiLayoutRadial(layout);
}
else if ((uiLayoutGetLocalDir(layout) == UI_LAYOUT_HORIZONTAL) && (flag & UI_ITEM_R_ICON_ONLY)) {
target = layout;
UI_block_layout_set_current(block, target);
/* Add a blank button to the beginning of the row. */
uiDefIconBut(block,
UI_BTYPE_LABEL,
0,
ICON_BLANK1,
0,
0,
1.25f * UI_UNIT_X,
UI_UNIT_Y,
NULL,
0,
0,
0,
0,
NULL);
}
else {
split = uiLayoutSplit(layout, 0.0f, false);
target = uiLayoutColumn(split, layout->align);
@ -1489,7 +1509,7 @@ void uiItemsFullEnumO_items(uiLayout *layout,
if (item->name) {
uiBut *but;
if (item != item_array && !radial) {
if (item != item_array && !radial && split) {
target = uiLayoutColumn(split, layout->align);
/* inconsistent, but menus with labels do not look good flipped */

View File

@ -2378,7 +2378,7 @@ static void widget_draw_text_icon(const uiFontStyle *fstyle,
rect->xmin += 0.3f * U.widget_unit;
}
}
else if (ui_block_is_menu(but->block)) {
else if (ui_block_is_menu(but->block) && but->alignnr == 0) {
rect->xmin += 0.2f * U.widget_unit;
}