Skip to content

Commit

Permalink
Improved appearance of long UI button labels
Browse files Browse the repository at this point in the history
  • Loading branch information
robinnorth committed Nov 25, 2021
1 parent 4ce720c commit e28630e
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 11 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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] -->
## [Unreleased]

### Changed

- Improved appearance of long UI button labels

## [5.0.0] - 2021-11-19

Expand Down
2 changes: 1 addition & 1 deletion Editor/Build/Action/BuildAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public override string ToString()
{
string name = actionName;
name += !string.IsNullOrEmpty(note) ?
" (" + note.Truncate(30, "...") + ")" :
" (" + note + ")" :
"";

return name;
Expand Down
4 changes: 3 additions & 1 deletion Editor/Build/Action/UI/BuildActionListDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion Editor/Build/Platform/BuildPlatform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion Editor/Build/Platform/UI/BuildPlatformListDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
9 changes: 5 additions & 4 deletions Editor/Build/Settings/UI/ProjectConfigurationsDrawer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
31 changes: 31 additions & 0 deletions Editor/Build/UI/UnityBuildGUIUtility.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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, @"(?<primary>.*)\((?<secondary>.*)\)");

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 ");
Expand Down
6 changes: 4 additions & 2 deletions Editor/Generic/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit e28630e

Please sign in to comment.