From 1dffd078ed3ed0a9c1ae52fcc28d792e47af65d5 Mon Sep 17 00:00:00 2001 From: Jesse Talavera-Greenberg Date: Tue, 22 Nov 2022 20:58:43 -0500 Subject: [PATCH 1/5] Replace most uses of Begin/End editor layout methods with equivalent scopes --- Editor/ProfileEditor.cs | 34 +++++++++++----------------------- 1 file changed, 11 insertions(+), 23 deletions(-) diff --git a/Editor/ProfileEditor.cs b/Editor/ProfileEditor.cs index 739e8fc..cc26531 100644 --- a/Editor/ProfileEditor.cs +++ b/Editor/ProfileEditor.cs @@ -474,7 +474,7 @@ void InitializeGUI() void SourceProfileGUI() { if (editorProfile != null) { - EditorGUILayout.BeginHorizontal(); + using (new EditorGUILayout.HorizontalScope()) { if (sourceProfiles == null) { sourceProfiles = BuildProfile.AllBuildProfiles.Prepend(null).ToArray(); @@ -501,7 +501,6 @@ void SourceProfileGUI() } GUI.enabled = true; } - EditorGUILayout.EndHorizontal(); } } @@ -525,7 +524,7 @@ protected void BuildGUI() EditorGUILayout.Space(); var usesActive = buildProfile.UsesActiveBuildTarget(); - EditorGUILayout.BeginHorizontal(); + using (new EditorGUILayout.HorizontalScope()) { GUILayout.Label("Build Targets", boldLabel); if (GUILayout.Button(GUIContent.none, plusStyle)) { @@ -544,11 +543,10 @@ protected void BuildGUI() } GUILayout.FlexibleSpace(); } - EditorGUILayout.EndHorizontal(); foreach (var target in buildProfile.BuildTargets) { - EditorGUILayout.BeginHorizontal(); - EditorGUI.BeginDisabledGroup(usesActive); + using var _ = new EditorGUILayout.HorizontalScope(); + using (new EditorGUI.DisabledGroupScope(usesActive)) { if (GUILayout.Button(GUIContent.none, minusStyle)) { delayedRemovals.Add(() => { @@ -557,7 +555,6 @@ protected void BuildGUI() } EditorGUILayout.LabelField(target.ToString()); } - EditorGUI.EndDisabledGroup(); var path = buildProfile.GetLastBuildPath(target); if (!string.IsNullOrEmpty(path) @@ -565,27 +562,23 @@ protected void BuildGUI() && GUILayout.Button("Show", EditorStyles.miniButton, GUILayout.ExpandWidth(false))) { EditorUtility.RevealInFinder(path); } - EditorGUI.BeginDisabledGroup(BuildRunner.Current != null); + using (new EditorGUI.DisabledGroupScope(BuildRunner.Current != null)) { if (GUILayout.Button("Build", EditorStyles.miniButton, GUILayout.ExpandWidth(false))) { BuildManager.Build(buildProfile, target); } } - EditorGUI.EndDisabledGroup(); - - EditorGUILayout.EndHorizontal(); } EditorGUILayout.Space(); - EditorGUI.BeginDisabledGroup(BuildRunner.Current != null); + using (new EditorGUI.DisabledGroupScope(BuildRunner.Current != null)) { var count = buildProfile.BuildTargets.Count(); if (GUILayout.Button("Build " + count + " Target" + (count > 1 ? "s" : ""), EditorStyles.miniButton)) { BuildManager.Build(buildProfile); } } - EditorGUI.EndDisabledGroup(); } protected void AddBuildTarget(object userData) @@ -673,12 +666,11 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal } if (category != lastCategory) { - EditorGUILayout.BeginHorizontal(categoryBackground); + using (new EditorGUILayout.HorizontalScope(categoryBackground)) { var path = pathBase + "_" + category; categoryExpanded = Foldout(path, true, category, categoryFoldout); } - EditorGUILayout.EndHorizontal(); lastCategory = category; } else if (categoryExpanded) { GUILayout.Label(GUIContent.none, separator); @@ -707,9 +699,9 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal if (option.Variance == OptionVariance.Dictionary) { // Disable when editing the default variant - EditorGUI.BeginDisabledGroup(isDefault); + using (new EditorGUI.DisabledGroupScope(isDefault)) { - EditorGUI.BeginDisabledGroup(isDefault); + using (new EditorGUI.DisabledGroupScope(isDefault)) { var newParam = EditorGUILayout.DelayedTextField(option.VariantParameter, width); if (newParam != option.VariantParameter) { @@ -721,9 +713,7 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal Option.changed = true; } } - EditorGUI.EndDisabledGroup(); } - EditorGUI.EndDisabledGroup(); } else { EditorGUILayout.PrefixLabel(" "); } @@ -759,7 +749,7 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal color.a = 1; GUI.color = color; - EditorGUILayout.BeginHorizontal(includeBackground, GUILayout.Width(buildColumnWidth), GUILayout.Height(lineHeight)); + using (new EditorGUILayout.HorizontalScope(includeBackground, GUILayout.Width(buildColumnWidth), GUILayout.Height(lineHeight))) { if ( buildProfile != null @@ -769,18 +759,16 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal || (option.Capabilities & OptionCapabilities.HasAssociatedFeature) != 0 ) ) { - EditorGUI.BeginDisabledGroup(!optionAvailable); + using var _ = new EditorGUI.DisabledGroupScope(!optionAvailable); var root = buildProfile.Store.GetOrCreateRoot(option.Name); GUILayout.Label(LabelForInclusion(root, option.Capabilities), inclusionLabel); if (optionAvailable) { DoInclusionMenu(root, option.Capabilities); } - EditorGUI.EndDisabledGroup(); } else { EditorGUILayout.Space(); } } - EditorGUILayout.EndHorizontal(); } else { // Not including a layout group here somehow makes the parent group taller EditorGUILayout.BeginHorizontal(GUILayout.Width(0)); From 401fc0186c6ba2f5f7277ee9f57f82e9adb383c2 Mon Sep 17 00:00:00 2001 From: Jesse Talavera-Greenberg Date: Tue, 22 Nov 2022 21:01:53 -0500 Subject: [PATCH 2/5] Change a use of BeginChangeCheck to ChangeCheckScope --- Editor/ProfileEditor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Editor/ProfileEditor.cs b/Editor/ProfileEditor.cs index cc26531..20c0d81 100644 --- a/Editor/ProfileEditor.cs +++ b/Editor/ProfileEditor.cs @@ -142,7 +142,7 @@ public static bool Foldout(bool foldout, string content, GUIStyle style = null) /// public static bool Foldout(string path, bool def, string content, GUIStyle style = null) { - EditorGUI.BeginChangeCheck(); + using var _ = new EditorGUI.ChangeCheckScope(); var wasExpanded = EditorProfile.Instance.IsExpanded(path); if (def) wasExpanded = !wasExpanded; From 8921842d8d5f00bb77a0ecf2f6dd090d1764902d Mon Sep 17 00:00:00 2001 From: Jesse Talavera-Greenberg Date: Tue, 22 Nov 2022 21:06:51 -0500 Subject: [PATCH 3/5] Introduce some more scopes for editor layout --- Editor/ProfileEditor.cs | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/Editor/ProfileEditor.cs b/Editor/ProfileEditor.cs index 20c0d81..faa423b 100644 --- a/Editor/ProfileEditor.cs +++ b/Editor/ProfileEditor.cs @@ -113,19 +113,21 @@ public static bool Foldout(bool foldout, string content, GUIStyle style = null) if (style == null) style = EditorStyles.foldout; // Wrap foldout into a layout region to get the foldout's rect - var rect = EditorGUILayout.BeginHorizontal(GUIStyle.none); - var clicked = false; - - // Determine if it is being clicked on the foldout before - // calling it to avoid it eating the event - if (Event.current.type == EventType.MouseUp - && rect.Contains(Event.current.mousePosition)) { - clicked = true; - } + bool newFoldout = false; + bool clicked = false; + using (var rectScope = new EditorGUILayout.HorizontalScope(GUIStyle.none)) + { + var rect = rectScope.rect; - var newFoldout = EditorGUILayout.Foldout(foldout, content, style); + // Determine if it is being clicked on the foldout before + // calling it to avoid it eating the event + if (Event.current.type == EventType.MouseUp + && rect.Contains(Event.current.mousePosition)) { + clicked = true; + } - EditorGUILayout.EndHorizontal(); + newFoldout = EditorGUILayout.Foldout(foldout, content, style); + } // Act in case we saw a click but the foldout didn't toggle if (newFoldout == foldout && clicked) { @@ -683,8 +685,10 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal // Option GUI var lineHeight = EditorGUIUtility.singleLineHeight + linePadding; - var rect = EditorGUILayout.BeginHorizontal(GUILayout.Height(lineHeight)); + Rect rect; + using (var rectScope = new EditorGUILayout.HorizontalScope(GUILayout.Height(lineHeight))) { + rect = rectScope.rect; // Variant container if (option.IsDefaultVariant && !showDefaultVariant) { EditorGUILayout.LabelField(displayName, width); @@ -771,11 +775,9 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal } } else { // Not including a layout group here somehow makes the parent group taller - EditorGUILayout.BeginHorizontal(GUILayout.Width(0)); - EditorGUILayout.EndHorizontal(); + using var _ = new EditorGUILayout.HorizontalScope(GUILayout.Width(0)); } } - EditorGUILayout.EndHorizontal(); // Expansion toggle if (expandable) { From a02244558e848001ec5c9a4241b0e4bf96594851 Mon Sep 17 00:00:00 2001 From: Jesse Talavera-Greenberg Date: Tue, 22 Nov 2022 21:15:38 -0500 Subject: [PATCH 4/5] Use IndentLevelScope instead of modifying the indent level directly --- Editor/ProfileEditor.cs | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/Editor/ProfileEditor.cs b/Editor/ProfileEditor.cs index faa423b..19805b8 100644 --- a/Editor/ProfileEditor.cs +++ b/Editor/ProfileEditor.cs @@ -635,8 +635,7 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal var width = GUILayout.Width(EditorGUIUtility.labelWidth - 4); var isRoot = (option.Parent == null && !showDefaultVariant); - var lastDepth = EditorGUI.indentLevel; - EditorGUI.indentLevel = depth; + using var indentScope = new EditorGUI.IndentLevelScope(EditorGUI.indentLevel - depth); if (buildProfile != null && isRoot) { optionAvailable = option.IsAvailable(buildProfile.BuildTargets); @@ -722,10 +721,10 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal EditorGUILayout.PrefixLabel(" "); } - var level = EditorGUI.indentLevel; - EditorGUI.indentLevel = 0; - profile.EditOption(option); - EditorGUI.indentLevel = level; + using (new EditorGUI.IndentLevelScope(-EditorGUI.indentLevel)) + { // Briefly set indent level to 0 + profile.EditOption(option); + } if (!isDefault) { if (GUILayout.Button(GUIContent.none, minusStyle)) { @@ -741,10 +740,10 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal tempContent.text = displayName; EditorGUILayout.PrefixLabel(tempContent); - var level = EditorGUI.indentLevel; - EditorGUI.indentLevel = 0; - profile.EditOption(option); - EditorGUI.indentLevel = level; + using (new EditorGUI.IndentLevelScope(-EditorGUI.indentLevel)) + { // Briefly set indent level to 0 + profile.EditOption(option); + } } // Include in build toggle @@ -789,8 +788,6 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal } } - EditorGUI.indentLevel = lastDepth; - return isExpanded; } From c672e4ca0736383173befa70c43080d9166551d0 Mon Sep 17 00:00:00 2001 From: Jesse Talavera-Greenberg Date: Tue, 22 Nov 2022 21:19:00 -0500 Subject: [PATCH 5/5] Revert "Use IndentLevelScope instead of modifying the indent level directly" This reverts commit a02244558e848001ec5c9a4241b0e4bf96594851. --- Editor/ProfileEditor.cs | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/Editor/ProfileEditor.cs b/Editor/ProfileEditor.cs index 19805b8..faa423b 100644 --- a/Editor/ProfileEditor.cs +++ b/Editor/ProfileEditor.cs @@ -635,7 +635,8 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal var width = GUILayout.Width(EditorGUIUtility.labelWidth - 4); var isRoot = (option.Parent == null && !showDefaultVariant); - using var indentScope = new EditorGUI.IndentLevelScope(EditorGUI.indentLevel - depth); + var lastDepth = EditorGUI.indentLevel; + EditorGUI.indentLevel = depth; if (buildProfile != null && isRoot) { optionAvailable = option.IsAvailable(buildProfile.BuildTargets); @@ -721,10 +722,10 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal EditorGUILayout.PrefixLabel(" "); } - using (new EditorGUI.IndentLevelScope(-EditorGUI.indentLevel)) - { // Briefly set indent level to 0 - profile.EditOption(option); - } + var level = EditorGUI.indentLevel; + EditorGUI.indentLevel = 0; + profile.EditOption(option); + EditorGUI.indentLevel = level; if (!isDefault) { if (GUILayout.Button(GUIContent.none, minusStyle)) { @@ -740,10 +741,10 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal tempContent.text = displayName; EditorGUILayout.PrefixLabel(tempContent); - using (new EditorGUI.IndentLevelScope(-EditorGUI.indentLevel)) - { // Briefly set indent level to 0 - profile.EditOption(option); - } + var level = EditorGUI.indentLevel; + EditorGUI.indentLevel = 0; + profile.EditOption(option); + EditorGUI.indentLevel = level; } // Include in build toggle @@ -788,6 +789,8 @@ protected bool OptionGUI(Option option, int depth, bool showDefaultVariant = fal } } + EditorGUI.indentLevel = lastDepth; + return isExpanded; }