Bug in addon to Change header color dynamically Bug in addon to change header color dynamically

I made an add-on that changes the theme color of the header if the automatic keyframe feature is enabled.

The color change function works fine.

However, the following bug occurs in the object gizmo transform gizmo.


Mouse hovers no longer highlight
Drag operation is delayed a little

Why do bugs occur in Gizmo by changing the theme color? I have no idea.




python

import bpy

bl_info = {
    "name" : "header_color_change",
    "author" : "Bookyakuno",
    "version" : (1, 0, 0),
    "blender" : (2, 80, 0),
    "location" : "hoge",
    "description" : "hoge",
    "category" : "UI"
}


def TEST_HT_header_color_change(self, context):
    layout = self.layout
    layout.label(text="")


    if bpy.context.scene.tool_settings.use_keyframe_insert_auto == True:
        bpy.context.preferences.themes[0].topbar.space.header = (0.4, 0.000000, 0.000000, 1.000000)

    else:
        bpy.context.preferences.themes[0].topbar.space.header = (0.137255, 0.137255, 0.137255, 1.000000)

    return {'FINISHED'}


def register():
    bpy.types.TOPBAR_HT_upper_bar.append(TEST_HT_header_color_change)

def unregister():
    bpy.types.TOPBAR_HT_upper_bar.remove(TEST_HT_header_color_change)

if __name__ == "__main__":
    register()


'''
I think that the issue comes from changing preference data during a UI draw, that is, you are accessing data that should not be altered while drawing the UI.

An alternative approach that seems to work is using handlers to alter the theme settings. In 2.80 the scene_update_* handlers have been renamed to depsgraph_update_*.

def TEST_prefset(scene):
    prefs = bpy.context.preferences

    if scene.tool_settings.use_keyframe_insert_auto == True:
        prefs.themes[0].topbar.space.header = (0.4, 0.000000, 0.000000, 1.000000)

    else:
        prefs.themes[0].topbar.space.header = (0.137255, 0.137255, 0.137255, 1.000000)


def register():
    bpy.app.handlers.depsgraph_update_post.append(TEST_prefset)

def unregister():
    bpy.app.handlers.depsgraph_update_post.remove(TEST_prefset)