UI Code Quality: Rename UI_ACTIVE button flag to UI_HOVER #114113

Merged
Julian Eisel merged 5 commits from JulianEisel/blender:temp-rename-uibut-active-flag into main 2023-10-25 18:36:38 +02:00
7 changed files with 63 additions and 54 deletions

View File

@ -343,10 +343,10 @@ enum {
/** This but is "inside" a box item (currently used to change theme colors). */
UI_BUT_BOX_ITEM = 1 << 20,
/** Active left part of number button */
UI_BUT_ACTIVE_LEFT = 1 << 21,
/** Active right part of number button */
UI_BUT_ACTIVE_RIGHT = 1 << 22,
/** Mouse is hovering left part of number button */
UI_BUT_HOVER_LEFT = 1 << 21,
/** Mouse is hovering right part of number button */
UI_BUT_HOVER_RIGHT = 1 << 22,
/** Reverse order of consecutive off/on icons */
UI_BUT_ICON_REVERSE = 1 << 23,

View File

@ -1007,7 +1007,7 @@ static bool ui_but_update_from_old_block(const bContext *C,
/* Stupid special case: The active button may be inside (as in, overlapped on top) a row
* button which we also want to keep highlighted then. */
if (ELEM(but->type, UI_BTYPE_VIEW_ITEM, UI_BTYPE_LISTROW)) {
flag_copy |= UI_ACTIVE;
flag_copy |= UI_HOVER;
}
but->flag = (but->flag & ~flag_copy) | (oldbut->flag & flag_copy);

View File

@ -4569,8 +4569,8 @@ static int ui_do_but_BUT(bContext *C, uiBut *but, uiHandleButtonData *data, cons
return WM_UI_HANDLER_BREAK;
}
if (event->type == LEFTMOUSE && event->val == KM_RELEASE && but->block->handle) {
/* regular buttons will be 'UI_SELECT', menu items 'UI_ACTIVE' */
if (!(but->flag & (UI_SELECT | UI_ACTIVE))) {
/* regular buttons will be 'UI_SELECT', menu items 'UI_HOVER' */
if (!(but->flag & (UI_SELECT | UI_HOVER))) {
data->cancel = true;
}
button_activate_state(C, but, BUTTON_STATE_EXIT);
@ -5347,7 +5347,7 @@ static bool ui_numedit_but_NUM(uiButNumber *but,
static void ui_numedit_set_active(uiBut *but)
{
const int oldflag = but->drawflag;
but->drawflag &= ~(UI_BUT_ACTIVE_LEFT | UI_BUT_ACTIVE_RIGHT);
but->drawflag &= ~(UI_BUT_HOVER_LEFT | UI_BUT_HOVER_RIGHT);
uiHandleButtonData *data = but->active;
if (!data) {
@ -5365,16 +5365,16 @@ static void ui_numedit_set_active(uiBut *but)
ui_window_to_block(data->region, but->block, &mx, &my);
if (mx < (but->rect.xmin + handle_width)) {
but->drawflag |= UI_BUT_ACTIVE_LEFT;
but->drawflag |= UI_BUT_HOVER_LEFT;
}
else if (mx > (but->rect.xmax - handle_width)) {
but->drawflag |= UI_BUT_ACTIVE_RIGHT;
but->drawflag |= UI_BUT_HOVER_RIGHT;
}
}
/* Don't change the cursor once pressed. */
if ((but->flag & UI_SELECT) == 0) {
if ((but->drawflag & UI_BUT_ACTIVE_LEFT) || (but->drawflag & UI_BUT_ACTIVE_RIGHT)) {
if ((but->drawflag & UI_BUT_HOVER_LEFT) || (but->drawflag & UI_BUT_HOVER_RIGHT)) {
if (data->changed_cursor) {
WM_cursor_modal_restore(data->window);
data->changed_cursor = false;
@ -5425,14 +5425,14 @@ static int ui_do_but_NUM(
}
else if (type == WHEELDOWNMOUSE && (event->modifier & KM_CTRL)) {
mx = but->rect.xmin;
but->drawflag &= ~UI_BUT_ACTIVE_RIGHT;
but->drawflag |= UI_BUT_ACTIVE_LEFT;
but->drawflag &= ~UI_BUT_HOVER_RIGHT;
but->drawflag |= UI_BUT_HOVER_LEFT;
click = 1;
}
else if ((type == WHEELUPMOUSE) && (event->modifier & KM_CTRL)) {
mx = but->rect.xmax;
but->drawflag &= ~UI_BUT_ACTIVE_LEFT;
but->drawflag |= UI_BUT_ACTIVE_RIGHT;
but->drawflag &= ~UI_BUT_HOVER_LEFT;
but->drawflag |= UI_BUT_HOVER_RIGHT;
click = 1;
}
else if (event->val == KM_PRESS) {
@ -5534,14 +5534,14 @@ static int ui_do_but_NUM(
if (!ui_but_is_float(but)) {
/* Integer Value. */
if (but->drawflag & (UI_BUT_ACTIVE_LEFT | UI_BUT_ACTIVE_RIGHT)) {
if (but->drawflag & (UI_BUT_HOVER_LEFT | UI_BUT_HOVER_RIGHT)) {
button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
const int value_step = int(number_but->step_size);
BLI_assert(value_step > 0);
const int softmin = round_fl_to_int_clamp(but->softmin);
const int softmax = round_fl_to_int_clamp(but->softmax);
const double value_test = (but->drawflag & UI_BUT_ACTIVE_LEFT) ?
const double value_test = (but->drawflag & UI_BUT_HOVER_LEFT) ?
double(max_ii(softmin, int(data->value) - value_step)) :
double(min_ii(softmax, int(data->value) + value_step));
if (value_test != data->value) {
@ -5558,7 +5558,7 @@ static int ui_do_but_NUM(
}
else {
/* Float Value. */
if (but->drawflag & (UI_BUT_ACTIVE_LEFT | UI_BUT_ACTIVE_RIGHT)) {
if (but->drawflag & (UI_BUT_HOVER_LEFT | UI_BUT_HOVER_RIGHT)) {
const PropertyScaleType scale_type = ui_but_scale_type(but);
button_activate_state(C, but, BUTTON_STATE_NUM_EDITING);
@ -5578,7 +5578,7 @@ static int ui_do_but_NUM(
}
BLI_assert(value_step > 0.0f);
const double value_test =
(but->drawflag & UI_BUT_ACTIVE_LEFT) ?
(but->drawflag & UI_BUT_HOVER_LEFT) ?
double(max_ff(but->softmin, float(data->value - value_step))) :
double(min_ff(but->softmax, float(data->value + value_step)));
if (value_test != data->value) {
@ -8623,9 +8623,9 @@ static void button_activate_init(bContext *C,
data->state = BUTTON_STATE_INIT;
/* activate button */
but->flag |= UI_ACTIVE;
/* Activate button. Sets the hover flag to enable button highlights, usually the button is
* initially activated because it's hovered. */
but->flag |= UI_HOVER;
but->active = data;
/* we disable auto_open in the block after a threshold, because we still
@ -8813,7 +8813,7 @@ static void button_activate_exit(
/* clean up button */
MEM_SAFE_FREE(but->active);
but->flag &= ~(UI_ACTIVE | UI_SELECT);
but->flag &= ~(UI_HOVER | UI_SELECT);
but->flag |= UI_BUT_LAST_ACTIVE;
if (!onfree) {
ui_but_update(but);
@ -9411,14 +9411,14 @@ static int ui_handle_button_event(bContext *C, const wmEvent *event, uiBut *but)
}
if (!(but->flag & UI_SELECT)) {
but->flag |= (UI_SELECT | UI_ACTIVE);
but->flag |= (UI_SELECT | UI_HOVER);
data->cancel = false;
ED_region_tag_redraw_no_rebuild(data->region);
}
}
else {
if (but->flag & UI_SELECT) {
but->flag &= ~(UI_SELECT | UI_ACTIVE);
but->flag &= ~(UI_SELECT | UI_HOVER);
data->cancel = true;
ED_region_tag_redraw_no_rebuild(data->region);
}
@ -9831,7 +9831,7 @@ static int ui_handle_viewlist_items_hover(const wmEvent *event, const ARegion *r
LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
if (ELEM(but->type, UI_BTYPE_VIEW_ITEM, UI_BTYPE_LISTROW)) {
but->flag &= ~UI_ACTIVE;
but->flag &= ~UI_HOVER;
has_item = true;
}
}
@ -9848,7 +9848,7 @@ static int ui_handle_viewlist_items_hover(const wmEvent *event, const ARegion *r
hovered_row_but = ui_list_row_find_mouse_over(region, event->xy);
}
if (hovered_row_but) {
hovered_row_but->flag |= UI_ACTIVE;
hovered_row_but->flag |= UI_HOVER;
}
return WM_UI_HANDLER_CONTINUE;

View File

@ -70,7 +70,14 @@ enum {
UI_SELECT = (1 << 0),
/** Temporarily hidden (scrolled out of the view). */
UI_SCROLLED = (1 << 1),
UI_ACTIVE = (1 << 2),
/**
Review

This comment could be more explicit... e.g.

When enabled, this buttons #uiBut::active is non-null which handles interactions. 

Also worth stating....

There should only ever be one UI_HOVER button per #uiBlock.

... this is implied with the term ACTIVE since Blender typically only ever has one active item, it's not so clearly the case once renamed.

This comment could be more explicit... e.g. ``` When enabled, this buttons #uiBut::active is non-null which handles interactions. ``` Also worth stating.... ``` There should only ever be one UI_HOVER button per #uiBlock. ``` ... this is implied with the term `ACTIVE` since Blender typically only ever has one active item, it's not so clearly the case once renamed.
* The button is hovered by the mouse and should be drawn with a hover highlight. Also set
* sometimes to highlight buttons without actually hovering it (e.g. for arrow navigation in
* menus). UI handling code manages this mostly and usually does this together with making the
* button active/focused (see #uiBut::active). This means events will be forwarded to it and
* further handlers/shortcuts can be used while hovering it.
*/
UI_HOVER = (1 << 2),
UI_HAS_ICON = (1 << 3),
UI_HIDDEN = (1 << 4),
/** Display selected, doesn't impact interaction. */
@ -283,7 +290,10 @@ struct uiBut {
const ImBuf *imb = nullptr;
float imb_scale = 0;
/** Active button data (set when the user is hovering or interacting with a button). */
/**
* Active button data, set when the user is hovering or interacting with a button (#UI_HOVER and
* #UI_SELECT state mostly).
*/
uiHandleButtonData *active = nullptr;
/** Custom button data (borrowed, not owned). */
@ -1215,7 +1225,7 @@ enum uiMenuItemSeparatorType {
* Helper call to draw a menu item without a button.
*
* \param but_flag: Button flags (#uiBut.flag) indicating the state of the item, typically
* #UI_ACTIVE, #UI_BUT_DISABLED, #UI_BUT_INACTIVE.
* #UI_HOVER, #UI_BUT_DISABLED, #UI_BUT_INACTIVE.
* \param separator_type: The kind of separator which controls if and how the string is clipped.
* \param r_xmax: The right hand position of the text, this takes into the icon, padding and text
* clipping when there is not enough room to display the full text.

View File

@ -564,7 +564,7 @@ static void ui_searchbox_region_draw_fn(const bContext *C, ARegion *region)
if (data->preview) {
/* draw items */
for (int a = 0; a < data->items.totitem; a++) {
const int but_flag = ((a == data->active) ? UI_ACTIVE : 0) | data->items.but_flags[a];
const int but_flag = ((a == data->active) ? UI_HOVER : 0) | data->items.but_flags[a];
/* ensure icon is up-to-date */
ui_icon_ensure_deferred(C, data->items.icons[a], data->preview);
@ -598,7 +598,7 @@ static void ui_searchbox_region_draw_fn(const bContext *C, ARegion *region)
const int search_sep_len = data->sep_string ? strlen(data->sep_string) : 0;
/* draw items */
for (int a = 0; a < data->items.totitem; a++) {
const int but_flag = ((a == data->active) ? UI_ACTIVE : 0) | data->items.but_flags[a];
const int but_flag = ((a == data->active) ? UI_HOVER : 0) | data->items.but_flags[a];
char *name = data->items.names[a];
int icon = data->items.icons[a];
char *name_sep_test = nullptr;
@ -975,7 +975,7 @@ static void ui_searchbox_region_draw_cb__operator(const bContext * /*C*/, ARegio
/* widget itself */
/* NOTE: i18n messages extracting tool does the same, please keep it in sync. */
{
const int but_flag = ((a == data->active) ? UI_ACTIVE : 0) | data->items.but_flags[a];
const int but_flag = ((a == data->active) ? UI_HOVER : 0) | data->items.but_flags[a];
wmOperatorType *ot = static_cast<wmOperatorType *>(data->items.pointers[a]);
char text_pre[128];

View File

@ -1393,7 +1393,7 @@ static void widget_draw_icon(
if (but->flag & UI_SELECT) {
/* pass */
}
else if (but->flag & UI_ACTIVE) {
else if (but->flag & UI_HOVER) {
/* pass */
}
else {
@ -1449,11 +1449,11 @@ static void widget_draw_icon(
const bool has_theme = UI_icon_get_theme_color(icon, color);
/* to indicate draggable */
if (ui_but_drag_is_draggable(but) && (but->flag & UI_ACTIVE)) {
if (ui_but_drag_is_draggable(but) && (but->flag & UI_HOVER)) {
UI_icon_draw_ex(
xs, ys, icon, aspect, 1.25f, 0.0f, color, has_theme, &but->icon_overlay_text);
}
else if (but->flag & (UI_ACTIVE | UI_SELECT | UI_SELECT_DRAW)) {
else if (but->flag & (UI_HOVER | UI_SELECT | UI_SELECT_DRAW)) {
UI_icon_draw_ex(
xs, ys, icon, aspect, alpha, 0.0f, color, has_theme, &but->icon_overlay_text);
}
@ -2624,8 +2624,8 @@ static void widget_state(uiWidgetType *wt, const uiWidgetStateInfo *state, eUIEm
/* Add "hover" highlight. Ideally this could apply in all cases,
* even if UI_SELECT. But currently this causes some flickering
* as buttons can be created and updated without respect to mouse
* position and so can draw without UI_ACTIVE set. See D6503. */
if (state->but_flag & UI_ACTIVE) {
* position and so can draw without UI_HOVER set. See D6503. */
if (state->but_flag & UI_HOVER) {
widget_active_color(&wt->wcol);
}
}
@ -2747,8 +2747,7 @@ static void widget_state_pie_menu_item(uiWidgetType *wt,
{
wt->wcol = *(wt->wcol_theme);
/* active and disabled (not so common) */
if ((state->but_flag & UI_BUT_DISABLED) && (state->but_flag & UI_ACTIVE)) {
if ((state->but_flag & UI_BUT_DISABLED) && (state->but_flag & UI_HOVER)) {
color_blend_v3_v3(wt->wcol.text, wt->wcol.text_sel, 0.5f);
/* draw the backdrop at low alpha, helps navigating with keys
* when disabled items are active */
@ -2757,7 +2756,7 @@ static void widget_state_pie_menu_item(uiWidgetType *wt,
}
else {
/* regular active */
if (state->but_flag & (UI_SELECT | UI_ACTIVE)) {
if (state->but_flag & (UI_SELECT | UI_HOVER)) {
copy_v3_v3_uchar(wt->wcol.text, wt->wcol.text_sel);
}
else if (state->but_flag & (UI_BUT_DISABLED | UI_BUT_INACTIVE)) {
@ -2768,7 +2767,7 @@ static void widget_state_pie_menu_item(uiWidgetType *wt,
if (state->but_flag & UI_SELECT) {
copy_v4_v4_uchar(wt->wcol.inner, wt->wcol.inner_sel);
}
else if (state->but_flag & UI_ACTIVE) {
else if (state->but_flag & UI_HOVER) {
copy_v4_v4_uchar(wt->wcol.inner, wt->wcol.item);
}
}
@ -2781,7 +2780,7 @@ static void widget_state_menu_item(uiWidgetType *wt,
{
wt->wcol = *(wt->wcol_theme);
if ((state->but_flag & UI_BUT_DISABLED) && (state->but_flag & UI_ACTIVE)) {
if ((state->but_flag & UI_BUT_DISABLED) && (state->but_flag & UI_HOVER)) {
/* Hovering over disabled item. */
wt->wcol.text[3] = 128;
color_blend_v3_v3(wt->wcol.inner, wt->wcol.text, 0.5f);
@ -2793,7 +2792,7 @@ static void widget_state_menu_item(uiWidgetType *wt,
}
else if (state->but_flag & UI_BUT_INACTIVE) {
/* Inactive. */
if (state->but_flag & UI_ACTIVE) {
if (state->but_flag & UI_HOVER) {
color_blend_v3_v3(wt->wcol.inner, wt->wcol.text, 0.2f);
wt->wcol.inner[3] = 255;
}
@ -2811,7 +2810,7 @@ static void widget_state_menu_item(uiWidgetType *wt,
copy_v4_v4_uchar(wt->wcol.inner, wt->wcol.inner_sel);
copy_v4_v4_uchar(wt->wcol.text, wt->wcol.text_sel);
}
else if (state->but_flag & UI_ACTIVE) {
else if (state->but_flag & UI_HOVER) {
/* Regular hover. */
color_blend_v3_v3(wt->wcol.inner, wt->wcol.text, 0.2f);
copy_v3_v3_uchar(wt->wcol.text, wt->wcol.text_sel);
@ -3445,7 +3444,7 @@ static void widget_numbut_draw(uiWidgetColors *wcol,
}
/* decoration */
if ((state->but_flag & UI_ACTIVE) && !state->is_text_input) {
if ((state->but_flag & UI_HOVER) && !state->is_text_input) {
uiWidgetColors wcol_zone;
uiWidgetBase wtb_zone;
rcti rect_zone;
@ -3458,7 +3457,7 @@ static void widget_numbut_draw(uiWidgetColors *wcol,
wcol_zone = *wcol;
copy_v3_v3_uchar(wcol_zone.item, wcol->text);
if (state->but_drawflag & UI_BUT_ACTIVE_LEFT) {
if (state->but_drawflag & UI_BUT_HOVER_LEFT) {
widget_active_color(&wcol_zone);
}
@ -3478,7 +3477,7 @@ static void widget_numbut_draw(uiWidgetColors *wcol,
wcol_zone = *wcol;
copy_v3_v3_uchar(wcol_zone.item, wcol->text);
if (state->but_drawflag & UI_BUT_ACTIVE_RIGHT) {
if (state->but_drawflag & UI_BUT_HOVER_RIGHT) {
widget_active_color(&wcol_zone);
}
@ -3497,7 +3496,7 @@ static void widget_numbut_draw(uiWidgetColors *wcol,
wcol_zone = *wcol;
copy_v3_v3_uchar(wcol_zone.item, wcol->text);
if (!(state->but_drawflag & (UI_BUT_ACTIVE_LEFT | UI_BUT_ACTIVE_RIGHT))) {
if (!(state->but_drawflag & (UI_BUT_HOVER_LEFT | UI_BUT_HOVER_RIGHT))) {
widget_active_color(&wcol_zone);
}
@ -4150,11 +4149,11 @@ static void widget_pulldownbut(uiWidgetColors *wcol,
float back[4];
UI_GetThemeColor4fv(TH_BACK, back);
if ((state->but_flag & UI_ACTIVE) || (back[3] < 1.0f)) {
if ((state->but_flag & UI_HOVER) || (back[3] < 1.0f)) {
uiWidgetBase wtb;
const float rad = widget_radius_from_zoom(zoom, wcol);
if (state->but_flag & UI_ACTIVE) {
if (state->but_flag & UI_HOVER) {
copy_v4_v4_uchar(wcol->inner, wcol->inner_sel);
copy_v3_v3_uchar(wcol->text, wcol->text_sel);
copy_v3_v3_uchar(wcol->outline, wcol->inner);
@ -4272,7 +4271,7 @@ static void widget_list_itembut(uiBut *but,
const float rad = widget_radius_from_zoom(zoom, wcol);
round_box_edges(&wtb, UI_CNR_ALL, &draw_rect, rad);
if (state->but_flag & UI_ACTIVE && !(state->but_flag & UI_SELECT)) {
if (state->but_flag & UI_HOVER && !(state->but_flag & UI_SELECT)) {
copy_v3_v3_uchar(wcol->inner, wcol->text);
wcol->inner[3] = 20;
}
@ -5156,7 +5155,7 @@ void ui_draw_but(const bContext *C, ARegion *region, uiStyle *style, uiBut *but,
#ifdef USE_UI_POPOVER_ONCE
if (but->block->flag & UI_BLOCK_POPOVER_ONCE) {
if ((but->flag & UI_ACTIVE) && ui_but_is_popover_once_compat(but)) {
if ((but->flag & UI_HOVER) && ui_but_is_popover_once_compat(but)) {
state.but_flag |= UI_BUT_ACTIVE_DEFAULT;
}
}

View File

@ -493,7 +493,7 @@ bool AbstractTreeViewItem::is_hovered() const
* Get the matching button from the previous redraw instead. */
uiButViewItem *old_item_but = ui_block_view_find_matching_view_item_but_in_old_block(
view_item_but_->block, this_item_handle);
return old_item_but && (old_item_but->flag & UI_ACTIVE);
return old_item_but && (old_item_but->flag & UI_HOVER);
}
bool AbstractTreeViewItem::is_collapsed() const