Skip to content

Commit

Permalink
feat: add an update theme config button to inspector (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
damzobridge authored Feb 11, 2024
1 parent 6c7c9af commit c901e24
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 2 deletions.
65 changes: 65 additions & 0 deletions Editor/CustomInspectors/ThemeConfigManagerInspector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using UnityEngine.UIElements;
using UnityEditor.UIElements;
using UnityEngine;
using System;

namespace ZoboUI.Editor.Inspectors
{
Expand All @@ -26,6 +27,10 @@ public class ThemeConfigManagerInspector : UnityEditor.Editor

private readonly string ResetConfigButtonName = "ResetConfigButton";

private readonly string UpdateRequiredSectionName = "UpdateRequiredSection";

private readonly string UpdateConfigButtonName = "UpdateConfigButton";

private void ExportThemeConfigToJson()
{
ThemeConfigManager themeConfigManager = (ThemeConfigManager)target;
Expand Down Expand Up @@ -53,6 +58,7 @@ private void ResetConfig()
EditorUtility.SetDirty(themeConfigManager);

themeConfigManager.Logger.LogProgress("Reset Theme Config");
Repaint();

}

Expand Down Expand Up @@ -91,9 +97,28 @@ private void Refresh()
}


private bool ConfigSchemaUpdateIsRequired()
{
if (target == null || target.GetType() != typeof(ThemeConfigManager))
{
return false;
}

ThemeConfigManager themeConfigManager = (ThemeConfigManager)target;

ThemeConfigDisplayVersion configDisplayVersion = themeConfigManager.ThemeConfigDisplay;

ThemeConfig newConfig = new ThemeConfig();
// We need to check if the schema version of the new config is different from the schema version of the config display. If it is, we need to update the config display
return !String.Equals(newConfig.schemaVersion, configDisplayVersion.SchemaVersion, StringComparison.OrdinalIgnoreCase);
}



private IVisualElementScheduledItem scheduledItem;

private VisualElement updateRequiredSection;

public override VisualElement CreateInspectorGUI()
{
// Create a new VisualElement to be the root of our inspector UI
Expand Down Expand Up @@ -170,6 +195,17 @@ public override VisualElement CreateInspectorGUI()
Button resetConfigButton = myInspector.Q<Button>(ResetConfigButtonName);
resetConfigButton.clickable.clicked += ResetConfig;

updateRequiredSection = myInspector.Q<VisualElement>(UpdateRequiredSectionName);
updateRequiredSection.style.display = DisplayStyle.None;

// Get the update button from the UXML and assign it its click event
Button updateConfigButton = myInspector.Q<Button>(UpdateConfigButtonName);
updateConfigButton.clickable.clicked += UpdateConfigToLatestSchema;

if (ConfigSchemaUpdateIsRequired())
{
updateRequiredSection.style.display = DisplayStyle.Flex;
}


// Get the log level enum dropdown from the UXML
Expand Down Expand Up @@ -224,6 +260,35 @@ public override VisualElement CreateInspectorGUI()
return myInspector;
}

private void UpdateConfigToLatestSchema()
{

if (target == null || target.GetType() != typeof(ThemeConfigManager))
{
return;
}

ThemeConfigManager themeConfigManager = (ThemeConfigManager)target;


var currentThemeConfig = themeConfigManager.ThemeConfig;

var jsonifiedThemeConfig = ThemeConfig.ToJson(currentThemeConfig);

themeConfigManager.LoadThemeConfigDisplayFromJsonString(jsonifiedThemeConfig);

EditorUtility.SetDirty(themeConfigManager);

Repaint();

if (!ConfigSchemaUpdateIsRequired() && updateRequiredSection != null)
{
updateRequiredSection.style.display = DisplayStyle.None;
}

themeConfigManager.Logger.LogProgress("Updated Theme Config to latest schema version");

}
}
}

Expand Down
4 changes: 4 additions & 0 deletions Editor/CustomInspectors/Theme_Config_Inspector_UXML.uxml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<ui:UXML xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" xsi="http://www.w3.org/2001/XMLSchema-instance" engine="UnityEngine.UIElements" editor="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="True">
<Style src="project://database/Packages/com.oyacamp.zoboui/Editor/Theme_Config_Inspector_USS.uss?fileID=7433441132597879392&amp;guid=3d3a920e322b23d4eae19e24e2cb7615&amp;type=3#Theme_Config_Inspector_USS" />
<ui:VisualElement name="VisualElement" style="flex-grow: 1; padding-top: 20px; padding-right: 7px; padding-bottom: 7px; padding-left: 7px;">
<ui:VisualElement name="UpdateRequiredSection" style="flex-grow: 0; border-top-width: 1px; border-bottom-width: 1px; padding-top: 9px; padding-bottom: 9px; margin-bottom: 25px; border-left-color: rgba(185, 185, 185, 0.28); border-right-color: rgba(185, 185, 185, 0.28); border-top-color: rgba(185, 185, 185, 0.28); border-bottom-color: rgba(185, 185, 185, 0.28); flex-direction: row; justify-content: space-between; align-items: center;">
<ui:Label tabindex="-1" text="Looks like you&apos;re using an older version of the Theme Config. Update to the latest version to get any new utilities and fields." parse-escape-sequences="true" display-tooltip-when-elided="true" style="white-space: normal; padding-top: 10px; padding-bottom: 10px; max-width: 90%;" />
<ui:Button text="Update" parse-escape-sequences="true" display-tooltip-when-elided="true" name="UpdateConfigButton" />
</ui:VisualElement>
<ui:VisualElement name="SettingsSection" style="margin-bottom: 62px;">
<ui:Label tabindex="-1" text="Settings" parse-escape-sequences="true" display-tooltip-when-elided="true" name="Label" class="themeconfig__section__heading" />
<ui:Foldout text="USS Generation" name="OutputSection" view-data-key="themeconfig-uss-output-section" class="themeconfig__settings__foldout">
Expand Down
2 changes: 1 addition & 1 deletion Editor/Lib/ConfigDisplay/ThemeConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2148,7 +2148,7 @@ public class ThemeConfig
/// <summary>
/// The version of the themeconfig schema used to generate this theme.
/// </summary>
public readonly string schemaVersion = "1.0.0";
public readonly string schemaVersion = "1.0.1";


public static string ToJson(ThemeConfig config)
Expand Down
2 changes: 1 addition & 1 deletion Editor/PropertyDrawers/StringAsDropdownDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ protected ThemeConfigDisplayVersion GetThemeConfigDisplayVersion(SerializedPrope
var targetObj = property.serializedObject.targetObject;

}
catch (ArgumentNullException ex)
catch (ArgumentNullException)
{
return null;
}
Expand Down

0 comments on commit c901e24

Please sign in to comment.