From e28630ee85aa68c880533ac4cbe20539b0ec171c Mon Sep 17 00:00:00 2001 From: Robin North Date: Thu, 25 Nov 2021 11:56:05 +0000 Subject: [PATCH] Improved appearance of long UI button labels --- CHANGELOG.md | 6 +++- Editor/Build/Action/BuildAction.cs | 2 +- .../Build/Action/UI/BuildActionListDrawer.cs | 4 ++- Editor/Build/Platform/BuildPlatform.cs | 2 +- .../Platform/UI/BuildPlatformListDrawer.cs | 4 ++- .../UI/ProjectConfigurationsDrawer.cs | 9 +++--- Editor/Build/UI/UnityBuildGUIUtility.cs | 31 +++++++++++++++++++ Editor/Generic/ExtensionMethods.cs | 6 ++-- 8 files changed, 53 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 007d10a..14a3cb1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,7 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). - +## [Unreleased] + +### Changed + +- Improved appearance of long UI button labels ## [5.0.0] - 2021-11-19 diff --git a/Editor/Build/Action/BuildAction.cs b/Editor/Build/Action/BuildAction.cs index fe7991d..3723b62 100644 --- a/Editor/Build/Action/BuildAction.cs +++ b/Editor/Build/Action/BuildAction.cs @@ -88,7 +88,7 @@ public override string ToString() { string name = actionName; name += !string.IsNullOrEmpty(note) ? - " (" + note.Truncate(30, "...") + ")" : + " (" + note + ")" : ""; return name; diff --git a/Editor/Build/Action/UI/BuildActionListDrawer.cs b/Editor/Build/Action/UI/BuildActionListDrawer.cs index 2041e8f..f42a7a4 100644 --- a/Editor/Build/Action/UI/BuildActionListDrawer.cs +++ b/Editor/Build/Action/UI/BuildActionListDrawer.cs @@ -75,10 +75,12 @@ private void DrawActions(SerializedProperty property) EditorGUILayout.BeginHorizontal(); bool show = listEntry.isExpanded; + string tooltip = buildAction.ToString(); + string text = UnityBuildGUIUtility.ToLabel(tooltip, 50); buildAction.actionEnabled = EditorGUILayout.Toggle(buildAction.actionEnabled, GUILayout.Width(15)); EditorGUI.BeginDisabledGroup(!buildAction.actionEnabled); - UnityBuildGUIUtility.DropdownHeader(buildAction.ToString(), ref show, false, GUILayout.ExpandWidth(true)); + UnityBuildGUIUtility.DropdownHeader(new GUIContent(text, tooltip), ref show, false, GUILayout.ExpandWidth(true)); EditorGUI.EndDisabledGroup(); listEntry.isExpanded = show; diff --git a/Editor/Build/Platform/BuildPlatform.cs b/Editor/Build/Platform/BuildPlatform.cs index d5199a2..a619857 100644 --- a/Editor/Build/Platform/BuildPlatform.cs +++ b/Editor/Build/Platform/BuildPlatform.cs @@ -194,7 +194,7 @@ public override string ToString() architecturesAndVariants.AddRange(variants.Select(item => item.ToString())); name += architecturesAndVariants.Count > 0 ? - " (" + string.Join(", ", architecturesAndVariants).Truncate(50, "...") + ")" : + " (" + string.Join(", ", architecturesAndVariants) + ")" : ""; return name; diff --git a/Editor/Build/Platform/UI/BuildPlatformListDrawer.cs b/Editor/Build/Platform/UI/BuildPlatformListDrawer.cs index b33f57c..c07818f 100644 --- a/Editor/Build/Platform/UI/BuildPlatformListDrawer.cs +++ b/Editor/Build/Platform/UI/BuildPlatformListDrawer.cs @@ -91,8 +91,10 @@ private void DrawPlatforms(SerializedProperty property) EditorGUILayout.BeginHorizontal(); bool show = listEntry.isExpanded; + string tooltip = buildPlatform.ToString(); + string text = UnityBuildGUIUtility.ToLabel(tooltip); - UnityBuildGUIUtility.DropdownHeader(buildPlatform.ToString(), ref show, false, GUILayout.ExpandWidth(true)); + UnityBuildGUIUtility.DropdownHeader(new GUIContent(text, tooltip), ref show, false, GUILayout.ExpandWidth(true)); listEntry.isExpanded = show; diff --git a/Editor/Build/Settings/UI/ProjectConfigurationsDrawer.cs b/Editor/Build/Settings/UI/ProjectConfigurationsDrawer.cs index ff4b2f8..2da2f7a 100644 --- a/Editor/Build/Settings/UI/ProjectConfigurationsDrawer.cs +++ b/Editor/Build/Settings/UI/ProjectConfigurationsDrawer.cs @@ -221,20 +221,21 @@ private void DisplayConfigTree(string key, Configuration config, int depth, bool if (displayButton) { - string displayText; + string tooltip = key.Replace(",", ", "); + string text; if (treeView.boolValue) { string[] split = key.Split('/'); - displayText = split[split.Length - 1]; + text = split[split.Length - 1]; } else { - displayText = key.Replace(",", ", "); + text = UnityBuildGUIUtility.ToLabel(tooltip); config.enabled = EditorGUILayout.Toggle(config.enabled, GUILayout.ExpandWidth(false), GUILayout.MaxWidth(10)); } - if (GUILayout.Button(displayText, UnityBuildGUIUtility.dropdownHeaderStyle)) + if (GUILayout.Button(new GUIContent(text, tooltip), UnityBuildGUIUtility.dropdownHeaderStyle)) { selectedKeyChain.stringValue = key; } diff --git a/Editor/Build/UI/UnityBuildGUIUtility.cs b/Editor/Build/UI/UnityBuildGUIUtility.cs index 1d3b287..f813b62 100644 --- a/Editor/Build/UI/UnityBuildGUIUtility.cs +++ b/Editor/Build/UI/UnityBuildGUIUtility.cs @@ -86,6 +86,11 @@ public static void OpenHelp(string anchor = "") } public static void DropdownHeader(string content, ref bool showDropdown, bool noColor, params GUILayoutOption[] options) + { + DropdownHeader(new GUIContent { text = content }, ref showDropdown, noColor, options); + } + + public static void DropdownHeader(GUIContent content, ref bool showDropdown, bool noColor, params GUILayoutOption[] options) { if (!noColor) GUI.backgroundColor = instance._mainHeaderColor; @@ -105,6 +110,32 @@ public static void HelpButton(string anchor = "") OpenHelp(anchor); } + public static string ToLabel(string input, int maxLength = 60) + { + string output = input; + string suffix = "..."; + char[] trimChars = new char[] { ' ', ',', '.', '/' }; + + Match match = Regex.Match(input, @"(?.*)\((?.*)\)"); + + if (match.Success) + { + string primary = match.Groups["primary"].Value; + string secondary = match.Groups["secondary"].Value; + + if (!string.IsNullOrEmpty(match.Groups["secondary"].Value)) + { + output = primary + "(" + secondary.Truncate(maxLength - primary.Length + 2, suffix, trimChars) + ")"; + } + else + { + output = primary.Truncate(maxLength, suffix, trimChars); + } + } + + return output; + } + public static string ToWords(string input) { return Regex.Replace(input, "([a-z](?=[A-Z])|[A-Z](?=[A-Z][a-z]))", "$1 "); diff --git a/Editor/Generic/ExtensionMethods.cs b/Editor/Generic/ExtensionMethods.cs index 1d30f7f..79016bc 100644 --- a/Editor/Generic/ExtensionMethods.cs +++ b/Editor/Generic/ExtensionMethods.cs @@ -3,11 +3,13 @@ namespace SuperUnityBuild.BuildTool { public static class ExtensionMethods { - public static string Truncate(this string value, int maxLength, string truncateSuffix = "") + public static string Truncate(this string value, int maxLength, string suffix = "", char[] trimChars = null) { + trimChars = trimChars ?? new char[] { ' ' }; + return (string.IsNullOrEmpty(value) || value.Length <= maxLength) ? value : - value.Substring(0, maxLength) + truncateSuffix; + value.Substring(0, maxLength).Trim(trimChars) + suffix; } } }