UI: move "undo history" from a custom popup to a menu type

This lets the undo history expand as a regular sub-menu
instead of being a popup.

Also disable the active undo step menu item as this is a no-op.
This commit is contained in:
Campbell Barton 2021-12-17 15:54:05 +11:00
parent c536eb410c
commit 0e1bb232e6
3 changed files with 68 additions and 76 deletions

@ -588,7 +588,7 @@ class TOPBAR_MT_edit(Menu):
layout.separator()
layout.operator("ed.undo_history", text="Undo History...")
layout.menu("TOPBAR_MT_undo_history")
layout.separator()

@ -35,6 +35,7 @@
#include "BKE_context.h"
#include "BKE_global.h"
#include "BKE_screen.h"
#include "BKE_undo_system.h"
#include "ED_screen.h"
#include "ED_space_api.h"
@ -236,6 +237,59 @@ static void recent_files_menu_register(void)
WM_menutype_add(mt);
}
static void undo_history_draw_menu(const bContext *C, Menu *menu)
{
wmWindowManager *wm = CTX_wm_manager(C);
if (wm->undo_stack == NULL) {
return;
}
int undo_step_count = 0;
for (UndoStep *us = wm->undo_stack->steps.first; us; us = us->next) {
if (us->skip) {
continue;
}
undo_step_count += 1;
}
uiLayout *split = uiLayoutSplit(menu->layout, 0.0f, false);
uiLayout *column = NULL;
const int col_size = 20 + (undo_step_count / 12);
int i = 0;
undo_step_count = 0;
for (UndoStep *us = wm->undo_stack->steps.first; us; us = us->next, i++) {
if (us->skip) {
continue;
}
if (!(undo_step_count % col_size)) {
column = uiLayoutColumn(split, false);
}
const bool is_active = (us == wm->undo_stack->step_active);
uiLayout *row = uiLayoutRow(column, false);
uiLayoutSetEnabled(row, !is_active);
uiItemIntO(row,
IFACE_(us->name),
is_active ? ICON_LAYER_ACTIVE : ICON_NONE,
"ED_OT_undo_history",
"item",
i);
undo_step_count += 1;
}
}
static void undo_history_menu_register(void)
{
MenuType *mt;
mt = MEM_callocN(sizeof(MenuType), __func__);
strcpy(mt->idname, "TOPBAR_MT_undo_history");
strcpy(mt->label, N_("Undo History"));
strcpy(mt->translation_context, BLT_I18NCONTEXT_DEFAULT_BPYRNA);
mt->draw = undo_history_draw_menu;
WM_menutype_add(mt);
}
void ED_spacetype_topbar(void)
{
SpaceType *st = MEM_callocN(sizeof(SpaceType), "spacetype topbar");
@ -278,6 +332,7 @@ void ED_spacetype_topbar(void)
BLI_addhead(&st->regiontypes, art);
recent_files_menu_register();
undo_history_menu_register();
BKE_spacetype_register(st);
}

@ -64,6 +64,7 @@
#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_enum_types.h"
#include "UI_interface.h"
#include "UI_resources.h"
@ -759,81 +760,16 @@ void ED_undo_operator_repeat_cb_evt(bContext *C, void *arg_op, int UNUSED(arg_un
/* -------------------------------------------------------------------- */
/** \name Undo History Operator
*
* See `TOPBAR_MT_undo_history` which is used to access this operator.
* \{ */
/* create enum based on undo items */
static const EnumPropertyItem *rna_undo_itemf(bContext *C, int *totitem)
{
EnumPropertyItem item_tmp = {0}, *item = NULL;
int i = 0;
wmWindowManager *wm = CTX_wm_manager(C);
if (wm->undo_stack == NULL) {
return NULL;
}
for (UndoStep *us = wm->undo_stack->steps.first; us; us = us->next, i++) {
if (us->skip == false) {
item_tmp.identifier = us->name;
item_tmp.name = IFACE_(us->name);
if (us == wm->undo_stack->step_active) {
item_tmp.icon = ICON_LAYER_ACTIVE;
}
else {
item_tmp.icon = ICON_NONE;
}
item_tmp.value = i;
RNA_enum_item_add(&item, totitem, &item_tmp);
}
}
RNA_enum_item_end(&item, totitem);
return item;
}
static int undo_history_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
int totitem = 0;
{
const EnumPropertyItem *item = rna_undo_itemf(C, &totitem);
if (totitem > 0) {
uiPopupMenu *pup = UI_popup_menu_begin(
C, WM_operatortype_name(op->type, op->ptr), ICON_NONE);
uiLayout *layout = UI_popup_menu_layout(pup);
uiLayout *split = uiLayoutSplit(layout, 0.0f, false);
uiLayout *column = NULL;
const int col_size = 20 + totitem / 12;
int i, c;
bool add_col = true;
for (c = 0, i = totitem; i--;) {
if (add_col && !(c % col_size)) {
column = uiLayoutColumn(split, false);
add_col = false;
}
if (item[i].identifier) {
uiItemIntO(column, item[i].name, item[i].icon, op->type->idname, "item", item[i].value);
c++;
add_col = true;
}
}
MEM_freeN((void *)item);
UI_popup_menu_end(C, pup);
}
}
return OPERATOR_CANCELLED;
}
/* NOTE: also check #ed_undo_step() in top if you change notifiers. */
static int undo_history_exec(bContext *C, wmOperator *op)
{
PropertyRNA *prop = RNA_struct_find_property(op->ptr, "item");
if (RNA_property_is_set(op->ptr, prop)) {
int item = RNA_property_int_get(op->ptr, prop);
const int item = RNA_property_int_get(op->ptr, prop);
const int ret = ed_undo_step_by_index(C, item, op->reports);
if (ret & OPERATOR_FINISHED) {
ed_undo_refresh_for_op(C);
@ -845,14 +781,15 @@ static int undo_history_exec(bContext *C, wmOperator *op)
return OPERATOR_CANCELLED;
}
static bool undo_history_poll(bContext *C)
static int undo_history_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
{
if (!ed_undo_is_init_and_screenactive_poll(C)) {
return false;
PropertyRNA *prop = RNA_struct_find_property(op->ptr, "item");
if (RNA_property_is_set(op->ptr, prop)) {
return undo_history_exec(C, op);
}
UndoStack *undo_stack = CTX_wm_manager(C)->undo_stack;
/* More than just original state entry. */
return BLI_listbase_count_at_most(&undo_stack->steps, 2) > 1;
WM_menu_name_call(C, "TOPBAR_MT_undo_history", WM_OP_INVOKE_DEFAULT);
return OPERATOR_FINISHED;
}
void ED_OT_undo_history(wmOperatorType *ot)
@ -865,7 +802,7 @@ void ED_OT_undo_history(wmOperatorType *ot)
/* api callbacks */
ot->invoke = undo_history_invoke;
ot->exec = undo_history_exec;
ot->poll = undo_history_poll;
ot->poll = ed_undo_is_init_and_screenactive_poll;
RNA_def_int(ot->srna, "item", 0, 0, INT_MAX, "Item", "", 0, INT_MAX);
}