Fix preference persistence by marking preferences as dirty, Used poll to disable operators instead of hiding them

This commit is contained in:
doc-code 2024-08-28 19:26:46 +02:00
parent 814b755bb7
commit 439aac2702

View File

@ -33,8 +33,7 @@ class CustomTemplatesPreferences(AddonPreferences):
col.menu("CT_MT_export", icon='DOWNARROW_HLT', text="") col.menu("CT_MT_export", icon='DOWNARROW_HLT', text="")
col.separator() col.separator()
col.operator("ct.add", icon='ADD', text="") col.operator("ct.add", icon='ADD', text="")
if self.projects: col.operator("ct.remove", icon='REMOVE', text="")
col.operator("ct.remove", icon='REMOVE', text="")
col.separator() col.separator()
col.operator("ct.move_up", icon='TRIA_UP', text="") col.operator("ct.move_up", icon='TRIA_UP', text="")
col.operator("ct.move_down", icon='TRIA_DOWN', text="") col.operator("ct.move_down", icon='TRIA_DOWN', text="")
@ -86,6 +85,10 @@ class CT_OT_export_templates(bpy.types.Operator):
context.window_manager.fileselect_add(self) context.window_manager.fileselect_add(self)
return {'RUNNING_MODAL'} return {'RUNNING_MODAL'}
@classmethod
def poll(cls, context):
return len(context.preferences.addons[base_package].preferences.projects) > 0
class CT_OT_import_templates(bpy.types.Operator): class CT_OT_import_templates(bpy.types.Operator):
bl_idname = "ct.import_templates" bl_idname = "ct.import_templates"
bl_label = "Import custom templates" bl_label = "Import custom templates"
@ -106,6 +109,7 @@ class CT_OT_import_templates(bpy.types.Operator):
item.name = project["name"] item.name = project["name"]
item.path = project["path"] item.path = project["path"]
prefs.active_template_index = 0 prefs.active_template_index = 0
context.preferences.is_dirty = True
self.report({'INFO'}, f"Projects imported from {self.filepath}") self.report({'INFO'}, f"Projects imported from {self.filepath}")
else: else:
@ -126,6 +130,7 @@ class CT_OT_add(Operator):
prefs = context.preferences.addons[base_package].preferences prefs = context.preferences.addons[base_package].preferences
prefs.projects.add() prefs.projects.add()
prefs.active_template_index = len(prefs.projects) - 1 prefs.active_template_index = len(prefs.projects) - 1
context.preferences.is_dirty = True
return {'FINISHED'} return {'FINISHED'}
class CT_OT_remove(Operator): class CT_OT_remove(Operator):
@ -138,8 +143,13 @@ class CT_OT_remove(Operator):
prefs.projects.remove(prefs.active_template_index) prefs.projects.remove(prefs.active_template_index)
prefs.active_template_index = min( prefs.active_template_index = min(
max(0, prefs.active_template_index - 1), len(prefs.projects) - 1) max(0, prefs.active_template_index - 1), len(prefs.projects) - 1)
context.preferences.is_dirty = True
return {'FINISHED'} return {'FINISHED'}
@classmethod
def poll(cls, context):
return len(context.preferences.addons[base_package].preferences.projects) > 0
class CT_OT_move_up(Operator): class CT_OT_move_up(Operator):
bl_idname = "ct.move_up" bl_idname = "ct.move_up"
bl_label = "Move Up" bl_label = "Move Up"
@ -152,11 +162,15 @@ class CT_OT_move_up(Operator):
if index > 0: if index > 0:
prefs.projects.move(index, index - 1) prefs.projects.move(index, index - 1)
prefs.active_template_index -= 1 prefs.active_template_index -= 1
context.preferences.is_dirty = True
else: else:
self.report({'WARNING'}, "Template is already at the top") self.report({'WARNING'}, "Template is already at the top")
return {'FINISHED'} return {'FINISHED'}
@classmethod
def poll(cls, context):
return len(context.preferences.addons[base_package].preferences.projects) > 0
class CT_OT_move_down(Operator): class CT_OT_move_down(Operator):
bl_idname = "ct.move_down" bl_idname = "ct.move_down"
bl_label = "Move Down" bl_label = "Move Down"
@ -169,11 +183,15 @@ class CT_OT_move_down(Operator):
if index < len(prefs.projects) - 1: if index < len(prefs.projects) - 1:
prefs.projects.move(index, index + 1) prefs.projects.move(index, index + 1)
prefs.active_template_index += 1 prefs.active_template_index += 1
context.preferences.is_dirty = True
else: else:
self.report({'WARNING'}, "Template is already at the bottom") self.report({'WARNING'}, "Template is already at the bottom")
return {'FINISHED'} return {'FINISHED'}
@classmethod
def poll(cls, context):
return len(context.preferences.addons[base_package].preferences.projects) > 0
# Popups of File > Defaults # Popups of File > Defaults
def already_present(self, prefs, path): def already_present(self, prefs, path):
for p in prefs.projects: for p in prefs.projects:
@ -199,6 +217,7 @@ class CT_OT_add_template_popup(Operator):
new_project = prefs.projects.add() new_project = prefs.projects.add()
new_project.name = self.project_name new_project.name = self.project_name
new_project.path = current_file_path new_project.path = current_file_path
context.preferences.is_dirty = True
else: else:
self.report({'ERROR'}, "Current file is not saved on disk.") self.report({'ERROR'}, "Current file is not saved on disk.")
return {'FINISHED'} return {'FINISHED'}
@ -220,6 +239,7 @@ class CT_OT_select_template_popup(Operator):
new_project = prefs.projects.add() new_project = prefs.projects.add()
new_project.name = self.project_name new_project.name = self.project_name
new_project.path = self.project_path new_project.path = self.project_path
context.preferences.is_dirty = True
return {'FINISHED'} return {'FINISHED'}
def invoke(self, context, event): def invoke(self, context, event):