UI: Use popover for toolbar popup operator

- Currently the popup closes immediately, could be made configurable.
- Support exiting popups when their submenu's are accessed.
This commit is contained in:
Campbell Barton 2018-05-19 10:22:44 +02:00
parent 671797e22e
commit d3c89f50a0
3 changed files with 23 additions and 2 deletions

@ -2365,14 +2365,15 @@ class WM_OT_toolbar(Operator):
from bl_ui.space_toolsystem_common import ToolSelectPanelHelper
cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
if cls is None:
self.report({'WARNING'}, "Toolbar not found for {space_type!r}")
self.report({'WARNING'}, f"Toolbar not found for {space_type!r}")
return {'CANCELLED'}
def draw_menu(popover, context):
cls.draw_cls(popover.layout, context, detect_layout=False)
wm = context.window_manager
wm.popup_menu(draw_menu)
# wm.popup_menu(draw_menu) # this also works
wm.popover(draw_menu)
return {'FINISHED'}

@ -250,6 +250,11 @@ void UI_popover_end(bContext *C, uiPopover *pup)
UI_popup_handlers_add(C, &window->modalhandlers, handle, 0);
WM_event_add_mousemove(C);
handle->popup = true;
/* TODO(campbell): we may want to make this configurable.
* The begin/end stype of calling popups doesn't allow to 'can_refresh' to be set.
* For now close this style of popvers when accessed. */
UI_block_flag_disable(pup->block, UI_BLOCK_KEEP_OPEN);
}
uiLayout *UI_popover_layout(uiPopover *pup)

@ -704,6 +704,21 @@ uiPopupBlockHandle *ui_popup_block_create(
void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
{
/* If this popup is created from a popover which does NOT have keep-open flag set,
* then close the popover too. We could extend this to other popup types too. */
ARegion *ar = handle->popup_create_vars.butregion;
if (ar != NULL) {
for (uiBlock *block = ar->uiblocks.first; block; block = block->next) {
if (block->handle &&
(block->flag & UI_BLOCK_POPOVER) &&
(block->flag & UI_BLOCK_KEEP_OPEN) == 0)
{
uiPopupBlockHandle *menu = block->handle;
menu->menuretval = UI_RETURN_OK;
}
}
}
if (handle->popup_create_vars.free_func) {
handle->popup_create_vars.free_func(handle, handle->popup_create_vars.arg);
}