Refactor: Add ability for UI block to own operator

Instead of only referencing an existing one. This will be used for
collection exporter and presets, to make sure the operator instance
stays alive long enough for the preset to be able to be applied.

Pull Request: https://projects.blender.org/blender/blender/pulls/120034
This commit is contained in:
Brecht Van Lommel
2024-03-28 20:40:59 +01:00
parent bef3a7b978
commit d99f2e8eb7
4 changed files with 32 additions and 1 deletions

View File

@@ -841,6 +841,11 @@ bool UI_block_is_search_only(const uiBlock *block);
*/
void UI_block_set_search_only(uiBlock *block, bool search_only);
/**
* Used for operator presets.
*/
void UI_block_set_active_operator(uiBlock *block, wmOperator *op, const bool free);
/**
* Can be called with C==NULL.
*/

View File

@@ -3434,6 +3434,29 @@ static void ui_but_free(const bContext *C, uiBut *but)
MEM_delete(but);
}
static void ui_block_free_active_operator(uiBlock *block)
{
if (block->ui_operator_free) {
/* This assumes the operator instance owns the pointer. This is not
* true for all operators by default, but it can be copied when needed. */
MEM_freeN(block->ui_operator->ptr);
MEM_freeN(block->ui_operator);
}
block->ui_operator_free = false;
block->ui_operator = nullptr;
}
void UI_block_set_active_operator(uiBlock *block, wmOperator *op, const bool free)
{
if (op != block->ui_operator) {
ui_block_free_active_operator(block);
block->ui_operator = op;
block->ui_operator_free = free;
}
}
void UI_block_free(const bContext *C, uiBlock *block)
{
UI_butstore_clear(block);
@@ -3450,6 +3473,8 @@ void UI_block_free(const bContext *C, uiBlock *block)
MEM_freeN(block->func_argN);
}
ui_block_free_active_operator(block);
BLI_freelistN(&block->saferct);
BLI_freelistN(&block->color_pickers.list);
BLI_freelistN(&block->dynamic_listeners);

View File

@@ -613,6 +613,7 @@ struct uiBlock {
/** use so presets can find the operator,
* across menus and from nested popups which fail for operator context. */
wmOperator *ui_operator;
bool ui_operator_free;
/** XXX hack for dynamic operator enums */
void *evil_C;

View File

@@ -2727,7 +2727,7 @@ static eAutoPropButsReturn template_operator_property_buts_draw_single(
PointerRNA op_ptr;
uiLayout *row;
block->ui_operator = op;
UI_block_set_active_operator(block, op, false);
row = uiLayoutRow(layout, true);
uiItemM(row, "WM_MT_operator_presets", nullptr, ICON_NONE);