Cleanup: Decrease variable scope in UI region popup code

This commit is contained in:
Hans Goudey 2022-10-12 12:19:21 -05:00
parent ba67a383fa
commit 605f87256e
2 changed files with 29 additions and 37 deletions

View File

@ -175,11 +175,7 @@ struct uiPopupMenu {
static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, void *arg_pup)
{
uiBlock *block;
uiPopupMenu *pup = static_cast<uiPopupMenu *>(arg_pup);
int minwidth, width, height;
char direction;
bool flip;
if (pup->menu_func) {
pup->block->handle = handle;
@ -188,6 +184,7 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
}
/* Find block minimum width. */
int minwidth;
if (uiLayoutGetUnitsX(pup->layout) != 0.0f) {
/* Use the minimum width from the layout if it's set. */
minwidth = uiLayoutGetUnitsX(pup->layout) * UI_UNIT_X;
@ -207,6 +204,7 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
}
/* Find block direction. */
char direction;
if (pup->but) {
if (pup->block->direction != 0) {
/* allow overriding the direction from menu_func */
@ -220,9 +218,9 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
direction = UI_DIR_DOWN;
}
flip = (direction == UI_DIR_DOWN);
bool flip = (direction == UI_DIR_DOWN);
block = pup->block;
uiBlock *block = pup->block;
/* in some cases we create the block before the region,
* so we set it delayed here if necessary */
@ -232,6 +230,7 @@ static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, voi
block->direction = direction;
int width, height;
UI_block_layout_resolve(block, &width, &height);
UI_block_flag_enable(block, UI_BLOCK_MOVEMOUSE_QUIT);
@ -318,7 +317,6 @@ uiPopupBlockHandle *ui_popup_menu_create(
{
wmWindow *window = CTX_wm_window(C);
const uiStyle *style = UI_style_get_dpi();
uiPopupBlockHandle *handle;
uiPopupMenu *pup = MEM_cnew<uiPopupMenu>(__func__);
pup->block = UI_block_begin(C, nullptr, __func__, UI_EMBOSS_PULLDOWN);
@ -357,7 +355,8 @@ uiPopupBlockHandle *ui_popup_menu_create(
pup->menu_func = menu_func;
pup->menu_arg = arg;
handle = ui_popup_block_create(C, butregion, but, nullptr, ui_block_func_POPUP, pup, nullptr);
uiPopupBlockHandle *handle = ui_popup_block_create(
C, butregion, but, nullptr, ui_block_func_POPUP, pup, nullptr);
if (!but) {
handle->popup = true;
@ -384,7 +383,6 @@ uiPopupMenu *UI_popup_menu_begin_ex(bContext *C,
{
const uiStyle *style = UI_style_get_dpi();
uiPopupMenu *pup = MEM_cnew<uiPopupMenu>(__func__);
uiBut *but;
pup->block = UI_block_begin(C, nullptr, block_name, UI_EMBOSS_PULLDOWN);
pup->block->flag |= UI_BLOCK_POPUP_MEMORY | UI_BLOCK_IS_FLIP;
@ -423,7 +421,7 @@ uiPopupMenu *UI_popup_menu_begin_ex(bContext *C,
"");
}
else {
but = uiDefBut(
uiBut *but = uiDefBut(
pup->block, UI_BTYPE_LABEL, 0, title, 0, 0, 200, UI_UNIT_Y, nullptr, 0.0, 0.0, 0, 0, "");
but->drawflag = UI_BUT_TEXT_LEFT;
}
@ -448,20 +446,20 @@ void UI_popup_menu_but_set(uiPopupMenu *pup, struct ARegion *butregion, uiBut *b
void UI_popup_menu_end(bContext *C, uiPopupMenu *pup)
{
wmWindow *window = CTX_wm_window(C);
uiPopupBlockHandle *menu;
uiBut *but = nullptr;
ARegion *butregion = nullptr;
pup->popup = true;
pup->mx = window->eventstate->xy[0];
pup->my = window->eventstate->xy[1];
uiBut *but = nullptr;
ARegion *butregion = nullptr;
if (pup->but) {
but = pup->but;
butregion = pup->butregion;
}
menu = ui_popup_block_create(C, butregion, but, nullptr, ui_block_func_POPUP, pup, nullptr);
uiPopupBlockHandle *menu = ui_popup_block_create(
C, butregion, but, nullptr, ui_block_func_POPUP, pup, nullptr);
menu->popup = true;
UI_popup_handlers_add(C, &window->modalhandlers, menu, 0);
@ -545,8 +543,6 @@ void UI_popup_menu_reports(bContext *C, ReportList *reports)
int UI_popup_menu_invoke(bContext *C, const char *idname, ReportList *reports)
{
uiPopupMenu *pup;
uiLayout *layout;
MenuType *mt = WM_menutype_find(idname, true);
if (mt == nullptr) {
@ -559,8 +555,9 @@ int UI_popup_menu_invoke(bContext *C, const char *idname, ReportList *reports)
return (OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH);
}
pup = UI_popup_menu_begin(C, CTX_IFACE_(mt->translation_context, mt->label), ICON_NONE);
layout = UI_popup_menu_layout(pup);
uiPopupMenu *pup = UI_popup_menu_begin(
C, CTX_IFACE_(mt->translation_context, mt->label), ICON_NONE);
uiLayout *layout = UI_popup_menu_layout(pup);
UI_menutype_draw(C, mt, layout);
@ -579,9 +576,9 @@ void UI_popup_block_invoke_ex(
bContext *C, uiBlockCreateFunc func, void *arg, uiFreeArgFunc arg_free, bool can_refresh)
{
wmWindow *window = CTX_wm_window(C);
uiPopupBlockHandle *handle;
handle = ui_popup_block_create(C, nullptr, nullptr, func, nullptr, arg, arg_free);
uiPopupBlockHandle *handle = ui_popup_block_create(
C, nullptr, nullptr, func, nullptr, arg, arg_free);
handle->popup = true;
/* It can be useful to disable refresh (even though it will work)
@ -607,9 +604,9 @@ void UI_popup_block_ex(bContext *C,
wmOperator *op)
{
wmWindow *window = CTX_wm_window(C);
uiPopupBlockHandle *handle;
handle = ui_popup_block_create(C, nullptr, nullptr, func, nullptr, arg, nullptr);
uiPopupBlockHandle *handle = ui_popup_block_create(
C, nullptr, nullptr, func, nullptr, arg, nullptr);
handle->popup = true;
handle->retvalue = 1;
handle->can_refresh = true;

View File

@ -69,7 +69,6 @@ static void ui_popup_block_position(wmWindow *window,
/* Compute button position in window coordinates using the source
* button region/block, to position the popup attached to it. */
rctf butrct;
if (!handle->refresh) {
ui_block_to_window_rctf(butregion, but->block, &butrct, &but->rect);
@ -417,14 +416,13 @@ static void ui_popup_block_clip(wmWindow *window, uiBlock *block)
{
const float xmin_orig = block->rect.xmin;
const int margin = UI_SCREEN_MARGIN;
int winx, winy;
if (block->flag & UI_BLOCK_NO_WIN_CLIP) {
return;
}
winx = WM_window_pixels_x(window);
winy = WM_window_pixels_y(window);
const int winx = WM_window_pixels_x(window);
const int winy = WM_window_pixels_y(window);
/* shift to left if outside of view */
if (block->rect.xmax > winx - margin) {
@ -549,7 +547,6 @@ uiBlock *ui_popup_block_refresh(bContext *C,
void *arg = handle->popup_create_vars.arg;
uiBlock *block_old = static_cast<uiBlock *>(region->uiblocks.first);
uiBlock *block;
handle->refresh = (block_old != nullptr);
@ -561,6 +558,7 @@ uiBlock *ui_popup_block_refresh(bContext *C,
#endif
/* create ui block */
uiBlock *block;
if (create_func) {
block = create_func(C, region, arg);
}
@ -618,16 +616,14 @@ uiBlock *ui_popup_block_refresh(bContext *C,
if (block->flag & UI_BLOCK_RADIAL) {
const int win_width = UI_SCREEN_MARGIN;
int winx, winy;
int x_offset = 0, y_offset = 0;
winx = WM_window_pixels_x(window);
winy = WM_window_pixels_y(window);
const int winx = WM_window_pixels_x(window);
const int winy = WM_window_pixels_y(window);
copy_v2_v2(block->pie_data.pie_center_init, block->pie_data.pie_center_spawned);
/* only try translation if area is large enough */
int x_offset = 0;
if (BLI_rctf_size_x(&block->rect) < winx - (2.0f * win_width)) {
if (block->rect.xmin < win_width) {
x_offset += win_width - block->rect.xmin;
@ -637,6 +633,7 @@ uiBlock *ui_popup_block_refresh(bContext *C,
}
}
int y_offset = 0;
if (BLI_rctf_size_y(&block->rect) < winy - (2.0f * win_width)) {
if (block->rect.ymin < win_width) {
y_offset += win_width - block->rect.ymin;
@ -756,9 +753,6 @@ uiPopupBlockHandle *ui_popup_block_create(bContext *C,
{
wmWindow *window = CTX_wm_window(C);
uiBut *activebut = UI_context_active_but_get(C);
static ARegionType type;
ARegion *region;
uiBlock *block;
/* disable tooltips from buttons below */
if (activebut) {
@ -787,9 +781,10 @@ uiPopupBlockHandle *ui_popup_block_create(bContext *C,
handle->can_refresh = false;
/* create area region */
region = ui_region_temp_add(CTX_wm_screen(C));
ARegion *region = ui_region_temp_add(CTX_wm_screen(C));
handle->region = region;
static ARegionType type;
memset(&type, 0, sizeof(ARegionType));
type.draw = ui_block_region_draw;
type.layout = ui_block_region_refresh;
@ -798,7 +793,7 @@ uiPopupBlockHandle *ui_popup_block_create(bContext *C,
UI_region_handlers_add(&region->handlers);
block = ui_popup_block_refresh(C, handle, butregion, but);
uiBlock *block = ui_popup_block_refresh(C, handle, butregion, but);
handle = block->handle;
/* keep centered on window resizing */