From 57729aa8bbac4ab1f11ba59ca0cf378e03af25a1 Mon Sep 17 00:00:00 2001 From: Harley Acheson Date: Thu, 14 Mar 2024 22:40:04 +0100 Subject: [PATCH] UI: Improved Operator to Delete Custom Keyconfigs Changes to python operators that add and remove custom keyconfigs. Removal gets poll function, separate name and description, and confirmation. Pull Request: https://projects.blender.org/blender/blender/pulls/118778 --- scripts/modules/rna_keymap_ui.py | 2 +- scripts/startup/bl_operators/presets.py | 41 ++++++++++++++++++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/scripts/modules/rna_keymap_ui.py b/scripts/modules/rna_keymap_ui.py index 46a7f01e23c..990cf1400ea 100644 --- a/scripts/modules/rna_keymap_ui.py +++ b/scripts/modules/rna_keymap_ui.py @@ -375,7 +375,7 @@ def draw_keymaps(context, layout): rowsub.menu("USERPREF_MT_keyconfigs", text=text) rowsub.operator("wm.keyconfig_preset_add", text="", icon='ADD') - rowsub.operator("wm.keyconfig_preset_add", text="", icon='REMOVE').remove_active = True + rowsub.operator("wm.keyconfig_preset_remove", text="", icon='REMOVE') rowsub = split.row(align=True) rowsub.operator("preferences.keyconfig_import", text="Import...", icon='IMPORT') diff --git a/scripts/startup/bl_operators/presets.py b/scripts/startup/bl_operators/presets.py index 9c9254b5f73..505a3166cc4 100644 --- a/scripts/startup/bl_operators/presets.py +++ b/scripts/startup/bl_operators/presets.py @@ -598,9 +598,9 @@ class RemovePresetInterfaceTheme(AddPresetBase, Operator): class AddPresetKeyconfig(AddPresetBase, Operator): - """Add or remove a Key-config Preset""" + """Add a custom keymap configuration to the preset list""" bl_idname = "wm.keyconfig_preset_add" - bl_label = "Add Keyconfig Preset" + bl_label = "Add Custom Keymap Configuration" preset_menu = "USERPREF_MT_keyconfigs" preset_subdir = "keyconfig" @@ -608,16 +608,42 @@ class AddPresetKeyconfig(AddPresetBase, Operator): bpy.ops.preferences.keyconfig_export(filepath=filepath) bpy.utils.keyconfig_set(filepath) + +class RemovePresetKeyconfig(AddPresetBase, Operator): + """Remove a custom keymap configuration from the preset list""" + bl_idname = "wm.keyconfig_preset_remove" + bl_label = "Remove Keymap Configuration" + preset_menu = "USERPREF_MT_keyconfigs" + preset_subdir = "keyconfig" + + remove_active: BoolProperty( + default=True, + options={'HIDDEN', 'SKIP_SAVE'}, + ) + + @classmethod + def poll(cls, context): + from bpy.utils import is_path_builtin + keyconfigs = bpy.context.window_manager.keyconfigs + preset_menu_class = getattr(bpy.types, cls.preset_menu) + name = keyconfigs.active.name + filepath = bpy.utils.preset_find(name, cls.preset_subdir, ext = ".py") + if not bool(filepath) or is_path_builtin(filepath): + cls.poll_message_set("Built-in keymap configurations cannot be removed") + return False + return True + def pre_cb(self, context): keyconfigs = bpy.context.window_manager.keyconfigs - if self.remove_active: - preset_menu_class = getattr(bpy.types, self.preset_menu) - preset_menu_class.bl_label = keyconfigs.active.name + preset_menu_class = getattr(bpy.types, self.preset_menu) + preset_menu_class.bl_label = keyconfigs.active.name def post_cb(self, context): keyconfigs = bpy.context.window_manager.keyconfigs - if self.remove_active: - keyconfigs.remove(keyconfigs.active) + keyconfigs.remove(keyconfigs.active) + + def invoke(self, context, event): + return context.window_manager.invoke_confirm(self, event, title="Remove Keymap Configuration", confirm_text="Delete") class AddPresetOperator(AddPresetBase, Operator): @@ -840,6 +866,7 @@ classes = ( AddPresetInterfaceTheme, RemovePresetInterfaceTheme, AddPresetKeyconfig, + RemovePresetKeyconfig, AddPresetNodeColor, AddPresetOperator, AddPresetRender,