Cleanup: Early-exit in button handling code, minor cleanups
This should decrease cognitive load because: - Intention is more explicit - Shorter visual scope of branches (no need to search for matching closing brackets and `else` blocks) - Visually less busy code because condition-checks and code that "actually does things" are separated, with less indentation - Avoids chaining together a bunch of conditions to a single `if` Also: Use `const`, correct comment placement, whitespace improvements.
This commit is contained in:
@@ -542,7 +542,7 @@ static bool but_copypaste_profile_alive = false;
|
|||||||
|
|
||||||
bool ui_but_is_editing(const uiBut *but)
|
bool ui_but_is_editing(const uiBut *but)
|
||||||
{
|
{
|
||||||
uiHandleButtonData *data = but->active;
|
const uiHandleButtonData *data = but->active;
|
||||||
return (data && ELEM(data->state, BUTTON_STATE_TEXT_EDITING, BUTTON_STATE_NUM_EDITING));
|
return (data && ELEM(data->state, BUTTON_STATE_TEXT_EDITING, BUTTON_STATE_NUM_EDITING));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -660,21 +660,23 @@ static bool ui_but_dragedit_update_mval(uiHandleButtonData *data, int mx)
|
|||||||
static bool ui_rna_is_userdef(PointerRNA *ptr, PropertyRNA *prop)
|
static bool ui_rna_is_userdef(PointerRNA *ptr, PropertyRNA *prop)
|
||||||
{
|
{
|
||||||
/* Not very elegant, but ensures preference changes force re-save. */
|
/* Not very elegant, but ensures preference changes force re-save. */
|
||||||
bool tag = false;
|
|
||||||
if (prop && !(RNA_property_flag(prop) & PROP_NO_DEG_UPDATE)) {
|
if (!prop) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (RNA_property_flag(prop) & PROP_NO_DEG_UPDATE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
StructRNA *base = RNA_struct_base(ptr->type);
|
StructRNA *base = RNA_struct_base(ptr->type);
|
||||||
if (base == NULL) {
|
if (base == NULL) {
|
||||||
base = ptr->type;
|
base = ptr->type;
|
||||||
}
|
}
|
||||||
if (ELEM(base,
|
return ELEM(base,
|
||||||
&RNA_AddonPreferences,
|
&RNA_AddonPreferences,
|
||||||
&RNA_KeyConfigPreferences,
|
&RNA_KeyConfigPreferences,
|
||||||
&RNA_KeyMapItem,
|
&RNA_KeyMapItem,
|
||||||
&RNA_UserAssetLibrary)) {
|
&RNA_UserAssetLibrary);
|
||||||
tag = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return tag;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool UI_but_is_userdef(const uiBut *but)
|
bool UI_but_is_userdef(const uiBut *but)
|
||||||
@@ -900,7 +902,10 @@ static void ui_apply_but_func(bContext *C, uiBut *but)
|
|||||||
/* typically call ui_apply_but_undo(), ui_apply_but_autokey() */
|
/* typically call ui_apply_but_undo(), ui_apply_but_autokey() */
|
||||||
static void ui_apply_but_undo(uiBut *but)
|
static void ui_apply_but_undo(uiBut *but)
|
||||||
{
|
{
|
||||||
if (but->flag & UI_BUT_UNDO) {
|
if (!(but->flag & UI_BUT_UNDO)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const char *str = NULL;
|
const char *str = NULL;
|
||||||
size_t str_len_clip = SIZE_MAX - 1;
|
size_t str_len_clip = SIZE_MAX - 1;
|
||||||
bool skip_undo = false;
|
bool skip_undo = false;
|
||||||
@@ -958,7 +963,6 @@ static void ui_apply_but_undo(uiBut *but)
|
|||||||
uiAfterFunc *after = ui_afterfunc_new();
|
uiAfterFunc *after = ui_afterfunc_new();
|
||||||
BLI_strncpy(after->undostr, str, min_zz(str_len_clip + 1, sizeof(after->undostr)));
|
BLI_strncpy(after->undostr, str, min_zz(str_len_clip + 1, sizeof(after->undostr)));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static void ui_apply_but_autokey(bContext *C, uiBut *but)
|
static void ui_apply_but_autokey(bContext *C, uiBut *but)
|
||||||
{
|
{
|
||||||
@@ -967,15 +971,16 @@ static void ui_apply_but_autokey(bContext *C, uiBut *but)
|
|||||||
/* try autokey */
|
/* try autokey */
|
||||||
ui_but_anim_autokey(C, but, scene, scene->r.cfra);
|
ui_but_anim_autokey(C, but, scene, scene->r.cfra);
|
||||||
|
|
||||||
/* make a little report about what we've done! */
|
if (!but->rnaprop) {
|
||||||
if (but->rnaprop) {
|
return;
|
||||||
char *buf;
|
}
|
||||||
|
|
||||||
if (RNA_property_subtype(but->rnaprop) == PROP_PASSWORD) {
|
if (RNA_property_subtype(but->rnaprop) == PROP_PASSWORD) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf = WM_prop_pystring_assign(C, &but->rnapoin, but->rnaprop, but->rnaindex);
|
/* make a little report about what we've done! */
|
||||||
|
char *buf = WM_prop_pystring_assign(C, &but->rnapoin, but->rnaprop, but->rnaindex);
|
||||||
if (buf) {
|
if (buf) {
|
||||||
BKE_report(CTX_wm_reports(C), RPT_PROPERTY, buf);
|
BKE_report(CTX_wm_reports(C), RPT_PROPERTY, buf);
|
||||||
MEM_freeN(buf);
|
MEM_freeN(buf);
|
||||||
@@ -983,7 +988,6 @@ static void ui_apply_but_autokey(bContext *C, uiBut *but)
|
|||||||
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_INFO_REPORT, NULL);
|
WM_event_add_notifier(C, NC_SPACE | ND_SPACE_INFO_REPORT, NULL);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
static void ui_apply_but_funcs_after(bContext *C)
|
static void ui_apply_but_funcs_after(bContext *C)
|
||||||
{
|
{
|
||||||
@@ -1631,14 +1635,23 @@ static bool ui_drag_toggle_set_xy_xy(
|
|||||||
LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
|
LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
|
||||||
/* NOTE: ctrl is always true here because (at least for now)
|
/* NOTE: ctrl is always true here because (at least for now)
|
||||||
* we always want to consider text control in this case, even when not embossed. */
|
* we always want to consider text control in this case, even when not embossed. */
|
||||||
if (ui_but_is_interactive(but, true)) {
|
|
||||||
if (BLI_rctf_isect_segment(&but->rect, xy_a_block, xy_b_block)) {
|
|
||||||
|
|
||||||
/* execute the button */
|
if (!ui_but_is_interactive(but, true)) {
|
||||||
if (ui_drag_toggle_but_is_supported(but)) {
|
continue;
|
||||||
|
}
|
||||||
|
if (!BLI_rctf_isect_segment(&but->rect, xy_a_block, xy_b_block)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!ui_drag_toggle_but_is_supported(but)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
/* is it pressed? */
|
/* is it pressed? */
|
||||||
const int pushed_state_but = ui_drag_toggle_but_pushed_state(but);
|
const int pushed_state_but = ui_drag_toggle_but_pushed_state(but);
|
||||||
if (pushed_state_but != pushed_state) {
|
if (pushed_state_but == pushed_state) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* execute the button */
|
||||||
UI_but_execute(C, region, but);
|
UI_but_execute(C, region, but);
|
||||||
if (do_check) {
|
if (do_check) {
|
||||||
ui_but_update_edited(but);
|
ui_but_update_edited(but);
|
||||||
@@ -1649,11 +1662,7 @@ static bool ui_drag_toggle_set_xy_xy(
|
|||||||
changed = true;
|
changed = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* done */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (changed) {
|
if (changed) {
|
||||||
/* apply now, not on release (or if handlers are canceled for whatever reason) */
|
/* apply now, not on release (or if handlers are canceled for whatever reason) */
|
||||||
ui_apply_but_funcs_after(C);
|
ui_apply_but_funcs_after(C);
|
||||||
|
|||||||
Reference in New Issue
Block a user