UI: add UILayout.template_popup_confirm(..) function #124139

Manually merged
Campbell Barton merged 4 commits from ideasman42/blender:pr-ui-popup-confirm-template into blender-v4.2-release 2024-07-06 09:27:13 +02:00

This makes it possible for popups to have their confirm & cancel buttons
defined in the operator's draw callback.

When used with popups created by: WindowManager::invoke_props_dialog(..)
to override the default confirm/cancel buttons.

In the case of WindowManager::popover(..) & bpy.ops.wm.call_panel(..)
this can be used to add confirm/cancel buttons.

Details:

  • When the confirm or cancel text argument is a blank string the button isn't shown,
    making it possible to only show a single button.
  • The template is similar to UILayout::operator in that it returns the operator properties for the confirm action.
  • MS-Windows alternate ordering of Confirm/Cancel is followed.

Notes for review:

  • This PR addresses changes outlined here: #124098 by letting the operators draw function define it's own confirm & cancel buttons.
  • This PR includes changes to the extensions UI (these would be split out) but have been included to give some context.

Example usage:

Self contained scripts showing how this template can be used for:

  • Popover panels.
  • Single function popup.
  • Model invoke dialog.

Popover panel:

import bpy


class HelloWorldPanel(bpy.types.Panel):
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'TOPBAR'
    bl_region_type = 'WINDOW'

    def draw(self, context):
        layout = self.layout

        obj = context.object

        row = layout.row()
        row.label(text="Hello world!", icon='WORLD_DATA')

        row = layout.row()
        row.label(text="Active object is: " + getattr(obj, "name", "<none>"))
        row = layout.row()
        if obj is not None:
            row.prop(obj, "name")

        row = layout.row()
        row.template_popup_confirm("mesh.primitive_cube_add")


def register():
    bpy.utils.register_class(HelloWorldPanel)


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)


if __name__ == "__main__":
    register()

# Also works when `keep_open` is False.
bpy.ops.wm.call_panel(name=HelloWorldPanel.bl_idname, keep_open=True)

Single function popup:

import bpy
from bpy import context

def show_popup():
    
    def draw_fn(popup, context):
        layout = popup.layout
        layout.label(text="Delete Object?")
        layout.template_popup_confirm("object.delete", text="Do it!", cancel_text="Don't do it!")
    
    wm = context.window_manager
    wm.popover(draw_fn)
    
show_popup()

Model invoke dialog:

import bpy


class ModalOperator(bpy.types.Operator):
    bl_idname = "object.modal_operator"
    bl_label = "Example Operator"
    
    def draw(self, context):
        layout = self.layout
        layout.label(text="Header")
        props = layout.template_popup_confirm("preferences.extension_repo_add", text="Add Repository...")
        props.remote_url = "Test"
        layout.label(text="Footer")

    def invoke(self, context, event):
        wm = context.window_manager
        wm.invoke_props_dialog(self, width=400)

        return {'RUNNING_MODAL'}
    
    def execute(self, context):
        print("Finished!")
        return {'FINISHED'}


def menu_func(self, context):
    self.layout.operator(ModalOperator.bl_idname, text=ModalOperator.bl_label)


def register():
    bpy.utils.register_class(ModalOperator)
    bpy.types.VIEW3D_MT_object.append(menu_func)


def unregister():
    bpy.utils.unregister_class(ModalOperator)
    bpy.types.VIEW3D_MT_object.remove(menu_func)


if __name__ == "__main__":
    register()
    bpy.ops.object.modal_operator('INVOKE_DEFAULT')
This makes it possible for popups to have their confirm & cancel buttons defined in the operator's draw callback. When used with popups created by: `WindowManager::invoke_props_dialog(..)` to override the default confirm/cancel buttons. In the case of `WindowManager::popover(..)` & `bpy.ops.wm.call_panel(..)` this can be used to add confirm/cancel buttons. Details: - When the confirm or cancel text argument is a blank string the button isn't shown, making it possible to only show a single button. - The template is similar to UILayout::operator in that it returns the operator properties for the confirm action. - MS-Windows alternate ordering of Confirm/Cancel is followed. ---- Notes for review: - This PR addresses changes outlined here: #124098 by letting the operators draw function define it's own confirm & cancel buttons. - This PR includes changes to the extensions UI (these would be split out) but have been included to give some context. ---- **Example usage:** Self contained scripts showing how this template can be used for: - Popover panels. - Single function popup. - Model invoke dialog. Popover panel: ```python import bpy class HelloWorldPanel(bpy.types.Panel): bl_label = "Hello World Panel" bl_idname = "OBJECT_PT_hello" bl_space_type = 'TOPBAR' bl_region_type = 'WINDOW' def draw(self, context): layout = self.layout obj = context.object row = layout.row() row.label(text="Hello world!", icon='WORLD_DATA') row = layout.row() row.label(text="Active object is: " + getattr(obj, "name", "<none>")) row = layout.row() if obj is not None: row.prop(obj, "name") row = layout.row() row.template_popup_confirm("mesh.primitive_cube_add") def register(): bpy.utils.register_class(HelloWorldPanel) def unregister(): bpy.utils.unregister_class(HelloWorldPanel) if __name__ == "__main__": register() # Also works when `keep_open` is False. bpy.ops.wm.call_panel(name=HelloWorldPanel.bl_idname, keep_open=True) ``` Single function popup: ```python import bpy from bpy import context def show_popup(): def draw_fn(popup, context): layout = popup.layout layout.label(text="Delete Object?") layout.template_popup_confirm("object.delete", text="Do it!", cancel_text="Don't do it!") wm = context.window_manager wm.popover(draw_fn) show_popup() ``` Model invoke dialog: ```python import bpy class ModalOperator(bpy.types.Operator): bl_idname = "object.modal_operator" bl_label = "Example Operator" def draw(self, context): layout = self.layout layout.label(text="Header") props = layout.template_popup_confirm("preferences.extension_repo_add", text="Add Repository...") props.remote_url = "Test" layout.label(text="Footer") def invoke(self, context, event): wm = context.window_manager wm.invoke_props_dialog(self, width=400) return {'RUNNING_MODAL'} def execute(self, context): print("Finished!") return {'FINISHED'} def menu_func(self, context): self.layout.operator(ModalOperator.bl_idname, text=ModalOperator.bl_label) def register(): bpy.utils.register_class(ModalOperator) bpy.types.VIEW3D_MT_object.append(menu_func) def unregister(): bpy.utils.unregister_class(ModalOperator) bpy.types.VIEW3D_MT_object.remove(menu_func) if __name__ == "__main__": register() bpy.ops.object.modal_operator('INVOKE_DEFAULT') ```
Campbell Barton requested review from Harley Acheson 2024-07-05 03:42:29 +02:00
Campbell Barton force-pushed pr-ui-popup-confirm-template from c260ce5fd3 to 79f55b7b44 2024-07-05 05:29:53 +02:00 Compare
Campbell Barton added 1 commit 2024-07-05 05:51:28 +02:00
Campbell Barton added 1 commit 2024-07-05 06:02:55 +02:00
Member

Yes, this is beautiful. Not seeing any downsides, really.

It might be a bit odd that we'd be using the presence of a button with UI_BUT_ACTIVE_DEFAULT set, but that seems fair.

Yes, this is beautiful. Not seeing any downsides, really. It might be a bit odd that we'd be using the presence of a button with UI_BUT_ACTIVE_DEFAULT set, but that seems fair.
Harley Acheson approved these changes 2024-07-06 00:32:13 +02:00
Campbell Barton manually merged commit 850e715682 into blender-v4.2-release 2024-07-06 09:27:13 +02:00
Author
Owner

Committed with a check for UI_BUT_ACTIVE_DEFAULT so it's never set more than once.

Committed with a check for `UI_BUT_ACTIVE_DEFAULT` so it's never set more than once.
Sign in to join this conversation.
No reviewers
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset System
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Viewport & EEVEE
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Asset Browser Project
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Module
Viewport & EEVEE
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Severity
High
Severity
Low
Severity
Normal
Severity
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#124139
No description provided.