mirror of
https://github.com/blender/blender-addons-contrib.git
synced 2025-07-23 00:49:46 +00:00
Extensions: support enable/disable for themes
This commit is contained in:
@ -603,6 +603,10 @@ def _preferences_theme_state_create():
|
||||
# It's possible the XML was renamed after upgrading, detect another.
|
||||
dirpath = os.path.dirname(filepath)
|
||||
|
||||
# Not essential, just avoids a demoted error from `scandir` which seems like it may be a bug.
|
||||
if not os.path.exists(dirpath):
|
||||
return None, None
|
||||
|
||||
filepath = ""
|
||||
for entry in scandir_with_demoted_errors(dirpath):
|
||||
if entry.is_dir():
|
||||
@ -1822,6 +1826,39 @@ class BlPkgPkgDisable_TODO(Operator):
|
||||
return {'CANCELLED'}
|
||||
|
||||
|
||||
class BlPkgPkgThemeEnable(Operator):
|
||||
"""Turn off this theme"""
|
||||
bl_idname = "bl_pkg.extension_theme_enable"
|
||||
bl_label = "Enable theme extension"
|
||||
|
||||
pkg_id: rna_prop_pkg_id
|
||||
repo_index: rna_prop_repo_index
|
||||
|
||||
def execute(self, context):
|
||||
self.repo_index
|
||||
repo_item = extension_repos_read_index(self.repo_index)
|
||||
extension_theme_enable(repo_item.directory, self.pkg_id)
|
||||
print(repo_item.directory, self.pkg_id)
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
class BlPkgPkgThemeDisable(Operator):
|
||||
"""Turn off this theme"""
|
||||
bl_idname = "bl_pkg.extension_theme_disable"
|
||||
bl_label = "Disable theme extension"
|
||||
|
||||
pkg_id: rna_prop_pkg_id
|
||||
repo_index: rna_prop_repo_index
|
||||
|
||||
def execute(self, context):
|
||||
import os
|
||||
repo_item = extension_repos_read_index(self.repo_index)
|
||||
dirpath = os.path.join(repo_item.directory, self.pkg_id)
|
||||
if os.path.samefile(dirpath, os.path.dirname(context.preferences.themes[0].filepath)):
|
||||
bpy.ops.preferences.reset_default_theme()
|
||||
return {'FINISHED'}
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Non Wrapped Actions
|
||||
#
|
||||
@ -2061,6 +2098,9 @@ classes = (
|
||||
BlPkgPkgUninstall,
|
||||
BlPkgPkgDisable_TODO,
|
||||
|
||||
BlPkgPkgThemeEnable,
|
||||
BlPkgPkgThemeDisable,
|
||||
|
||||
BlPkgPkgUpgradeAll,
|
||||
BlPkgPkgInstallMarked,
|
||||
BlPkgPkgUninstallMarked,
|
||||
|
@ -70,6 +70,21 @@ def license_info_to_text(license_list):
|
||||
return ", ".join(result)
|
||||
|
||||
|
||||
def pkg_repo_and_id_from_theme_path(repos_all, filepath):
|
||||
import os
|
||||
if not filepath:
|
||||
return None
|
||||
|
||||
# Strip the `theme.xml` filename.
|
||||
dirpath = os.path.dirname(filepath)
|
||||
repo_directory, pkg_id = os.path.split(dirpath)
|
||||
for repo_index, repo in enumerate(repos_all):
|
||||
if not os.path.samefile(repo_directory, repo.directory):
|
||||
continue
|
||||
return repo_index, pkg_id
|
||||
return None
|
||||
|
||||
|
||||
# -----------------------------------------------------------------------------
|
||||
# Extensions UI (Legacy)
|
||||
|
||||
@ -289,8 +304,11 @@ def extensions_panel_draw_impl(
|
||||
|
||||
# To access enabled add-ons.
|
||||
show_addons = filter_by_type in {"", "add-on"}
|
||||
show_themes = filter_by_type in {"", "theme"}
|
||||
if show_addons:
|
||||
used_addon_module_name_map = {addon.module: addon for addon in context.preferences.addons}
|
||||
if show_themes:
|
||||
active_theme_info = pkg_repo_and_id_from_theme_path(repos_all, context.preferences.themes[0].filepath)
|
||||
|
||||
# Collect exceptions accessing repositories, and optionally show them.
|
||||
errors_on_draw = []
|
||||
@ -365,7 +383,13 @@ def extensions_panel_draw_impl(
|
||||
if installed_only and (is_installed == 0):
|
||||
continue
|
||||
|
||||
is_addon = (item_remote["type"] == "add-on")
|
||||
is_addon = False
|
||||
is_theme = False
|
||||
match item_remote["type"]:
|
||||
case "add-on":
|
||||
is_addon = True
|
||||
case "theme":
|
||||
is_theme = True
|
||||
|
||||
if is_addon:
|
||||
if is_installed:
|
||||
@ -376,6 +400,9 @@ def extensions_panel_draw_impl(
|
||||
else:
|
||||
is_enabled = False
|
||||
addon_module_name = None
|
||||
elif is_theme:
|
||||
is_enabled = (repo_index, pkg_id) == active_theme_info
|
||||
addon_module_name = None
|
||||
else:
|
||||
# TODO: ability to disable.
|
||||
is_enabled = is_installed
|
||||
@ -420,6 +447,16 @@ def extensions_panel_draw_impl(
|
||||
text="",
|
||||
emboss=False,
|
||||
).module = addon_module_name
|
||||
elif is_theme:
|
||||
props = row.operator(
|
||||
"bl_pkg.extension_theme_disable" if is_enabled else "bl_pkg.extension_theme_enable",
|
||||
icon='CHECKBOX_HLT' if is_enabled else 'CHECKBOX_DEHLT',
|
||||
text="",
|
||||
emboss=False,
|
||||
)
|
||||
props.repo_index = repo_index
|
||||
props.pkg_id = pkg_id
|
||||
del props
|
||||
else:
|
||||
# Use a place-holder checkbox icon to avoid odd text alignment when mixing with installed add-ons.
|
||||
# Non add-ons have no concept of "enabled" right now, use installed.
|
||||
|
Reference in New Issue
Block a user