From bc071c0c3d493fd9bdea2da2b94affbbb93dea33 Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Wed, 23 Aug 2023 11:45:14 -0500 Subject: [PATCH] Add cues to encourage deletion in DeleteDirectories UI --- GUI/Controls/DeleteDirectories.Designer.cs | 3 ++- GUI/Controls/DeleteDirectories.cs | 7 +++++ GUI/Controls/DeleteDirectories.resx | 3 +-- GUI/Controls/ModInfo.cs | 7 ++--- GUI/Controls/ModInfoTabs/Metadata.cs | 13 +++------- GUI/Dialogs/ErrorDialog.cs | 12 +++------ GUI/Dialogs/YesNoDialog.cs | 15 +---------- GUI/Util.cs | 30 ++++++++++++++++++++++ 8 files changed, 50 insertions(+), 40 deletions(-) diff --git a/GUI/Controls/DeleteDirectories.Designer.cs b/GUI/Controls/DeleteDirectories.Designer.cs index d1c0f4982e..a4d2f84164 100644 --- a/GUI/Controls/DeleteDirectories.Designer.cs +++ b/GUI/Controls/DeleteDirectories.Designer.cs @@ -1,4 +1,4 @@ -namespace CKAN.GUI +namespace CKAN.GUI { partial class DeleteDirectories { @@ -159,6 +159,7 @@ private void InitializeComponent() this.DeleteButton.AutoSize = true; this.DeleteButton.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowOnly; this.DeleteButton.FlatStyle = System.Windows.Forms.FlatStyle.Flat; + this.DeleteButton.Font = new System.Drawing.Font(System.Drawing.SystemFonts.DefaultFont, System.Drawing.FontStyle.Bold); this.DeleteButton.Name = "DeleteButton"; this.DeleteButton.Size = new System.Drawing.Size(112, 30); this.DeleteButton.TabIndex = 4; diff --git a/GUI/Controls/DeleteDirectories.cs b/GUI/Controls/DeleteDirectories.cs index 4ca60fb9bf..6ddd6254bc 100644 --- a/GUI/Controls/DeleteDirectories.cs +++ b/GUI/Controls/DeleteDirectories.cs @@ -34,6 +34,7 @@ public void LoadDirs(GameInstance ksp, HashSet possibleConfigOnlyDirs) .ToArray(); Util.Invoke(this, () => { + DeleteButton.Focus(); DirectoriesListView.Items.Clear(); DirectoriesListView.Items.AddRange(items); DirectoriesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); @@ -79,6 +80,12 @@ public bool Wait(out HashSet toDelete) } } + protected override void OnResize(EventArgs e) + { + base.OnResize(e); + ExplanationLabel.Height = Util.LabelStringHeight(CreateGraphics(), ExplanationLabel); + } + /// /// Open the user guide when the user presses F1 /// diff --git a/GUI/Controls/DeleteDirectories.resx b/GUI/Controls/DeleteDirectories.resx index 27c463582a..44b15b106a 100644 --- a/GUI/Controls/DeleteDirectories.resx +++ b/GUI/Controls/DeleteDirectories.resx @@ -117,8 +117,7 @@ System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 - The below directories are leftover after removing some mods. They contain files that were not installed by CKAN (probably either generated by a mod or manually installed). CKAN does not automatically delete files it did not install, but you can choose to remove them if it looks safe to do so (recommended). -Note that if you decide not to remove a directory, ModuleManager may incorrectly think that mod is still installed. + Warning, some folders have been left behind because CKAN does not know whether it is safe to delete their remaining files. Keeping these folders may break other mods! If you do not need these files, deleting them is recommended. Directories Directory Contents Click a directory at the left to see its contents diff --git a/GUI/Controls/ModInfo.cs b/GUI/Controls/ModInfo.cs index 0b9ee457a1..bc2eeb62fe 100644 --- a/GUI/Controls/ModInfo.cs +++ b/GUI/Controls/ModInfo.cs @@ -96,13 +96,10 @@ private void ModInfoTabControl_SelectedIndexChanged(object sender, EventArgs e) private GameInstanceManager manager => Main.Instance.Manager; - private int StringHeight(string text, Font font, int maxWidth) - => (int)CreateGraphics().MeasureString(text, font, maxWidth).Height; - private int TextBoxStringHeight(TextBox tb) => tb.Padding.Vertical + tb.Margin.Vertical - + StringHeight(tb.Text, tb.Font, - tb.Width - tb.Padding.Horizontal - tb.Margin.Horizontal); + + Util.StringHeight(CreateGraphics(), tb.Text, tb.Font, + tb.Width - tb.Padding.Horizontal - tb.Margin.Horizontal); private int DescriptionHeight => TextBoxStringHeight(MetadataModuleDescriptionTextBox); diff --git a/GUI/Controls/ModInfoTabs/Metadata.cs b/GUI/Controls/ModInfoTabs/Metadata.cs index e6abe7ed0f..3cb6e3cb2d 100644 --- a/GUI/Controls/ModInfoTabs/Metadata.cs +++ b/GUI/Controls/ModInfoTabs/Metadata.cs @@ -139,16 +139,9 @@ private void LinkLabel_KeyDown(object sender, KeyEventArgs e) } } - private int StringHeight(string text, Font font, int maxWidth) - => (int)CreateGraphics().MeasureString(text, font, maxWidth).Height; - private int LinkLabelStringHeight(LinkLabel lb, int fitWidth) => lb.Padding.Vertical + lb.Margin.Vertical + 10 - + StringHeight(lb.Text, lb.Font, fitWidth); - - private int LabelStringHeight(Label lb) - => lb.Padding.Vertical + lb.Margin.Vertical + 10 - + StringHeight(lb.Text, lb.Font, lb.Width); + + Util.StringHeight(CreateGraphics(), lb.Text, lb.Font, fitWidth); protected override void OnResize(EventArgs e) { @@ -212,7 +205,7 @@ private void AddResourceLink(string label, Uri link) MetadataTable.RowStyles.Add( new RowStyle(SizeType.Absolute, Math.Max( // "Remote version file" wraps - LabelStringHeight(lbl), + Util.LabelStringHeight(CreateGraphics(), lbl), LinkLabelStringHeight(llbl, RightColumnWidth)))); } } @@ -229,7 +222,7 @@ private void ResizeResourceRows() { MetadataTable.RowStyles[row].Height = Math.Max( // "Remote version file" wraps - LabelStringHeight(lab), + Util.LabelStringHeight(CreateGraphics(), lab), LinkLabelStringHeight(link, rWidth)); } } diff --git a/GUI/Dialogs/ErrorDialog.cs b/GUI/Dialogs/ErrorDialog.cs index 1dbc198d86..6edfc6179c 100644 --- a/GUI/Dialogs/ErrorDialog.cs +++ b/GUI/Dialogs/ErrorDialog.cs @@ -29,9 +29,10 @@ public void ShowErrorDialog(string text, params object[] args) ClientSize.Width, Math.Min( maxHeight, - padding + StringHeight(ErrorMessage.Text, ErrorMessage.Width - 4) - ) - ); + padding + Util.StringHeight(CreateGraphics(), + ErrorMessage.Text, + ErrorMessage.Font, + ErrorMessage.Width - 4))); if (!Visible) { StartPosition = Main.Instance.actuallyVisible @@ -58,11 +59,6 @@ protected override void OnClosed(EventArgs e) ErrorMessage.Text = ""; } - private int StringHeight(string text, int maxWidth) - { - return (int)CreateGraphics().MeasureString(text, ErrorMessage.Font, maxWidth).Height; - } - private const int maxHeight = 600; private static readonly ILog log = LogManager.GetLogger(typeof(ErrorDialog)); } diff --git a/GUI/Dialogs/YesNoDialog.cs b/GUI/Dialogs/YesNoDialog.cs index 5b92ce04d6..18c35026bb 100644 --- a/GUI/Dialogs/YesNoDialog.cs +++ b/GUI/Dialogs/YesNoDialog.cs @@ -42,7 +42,7 @@ public Tuple ShowSuppressableYesNoDialog(Form parentForm, st private void Setup(string text, string yesText, string noText) { - var height = StringHeight(text, ClientSize.Width - 25) + 2 * 54; + var height = Util.StringHeight(CreateGraphics(), text, DescriptionLabel.Font, ClientSize.Width - 25) + 2 * 54; DescriptionLabel.Text = text; DescriptionLabel.TextAlign = text.Contains("\n") ? HorizontalAlignment.Left @@ -67,19 +67,6 @@ private void SetupSuppressable(string text, string yesText, string noText, strin SuppressCheckbox.Visible = true; } - /// - /// Simple syntactic sugar around Graphics.MeasureString - /// - /// String to measure size of - /// Number of pixels allowed horizontally - /// - /// Number of pixels needed vertically to fit the string - /// - private int StringHeight(string text, int maxWidth) - { - return (int)CreateGraphics().MeasureString(text, DescriptionLabel.Font, maxWidth).Height; - } - public void HideYesNoDialog() { Util.Invoke(this, Close); diff --git a/GUI/Util.cs b/GUI/Util.cs index 72f292e2b3..cbf6512916 100644 --- a/GUI/Util.cs +++ b/GUI/Util.cs @@ -274,6 +274,36 @@ public static EventHandler Debounce( }; } + /// + /// Simple syntactic sugar around Graphics.MeasureString + /// + /// The graphics context + /// The font to be used for the text + /// String to measure size of + /// Number of pixels allowed horizontally + /// + /// Number of pixels needed vertically to fit the string + /// + public static int StringHeight(Graphics g, string text, Font font, int maxWidth) + => (int)g.MeasureString(text, font, (int)(maxWidth / XScale(g))).Height; + + /// + /// Calculate how much vertical space is needed to display a label's text + /// + /// The graphics context + /// The label + /// + /// Number of pixels needed vertically to show the label's full text + /// + public static int LabelStringHeight(Graphics g, Label lbl) + => (int)(YScale(g) * (lbl.Margin.Vertical + lbl.Padding.Vertical + + StringHeight(g, lbl.Text, lbl.Font, + (lbl.Width - lbl.Margin.Horizontal + - lbl.Padding.Horizontal)))); + + private static float XScale(Graphics g) => g.DpiX / 96f; + private static float YScale(Graphics g) => g.DpiY / 96f; + private static readonly ILog log = LogManager.GetLogger(typeof(Util)); } }