Skip to content

Commit

Permalink
Add cues to encourage deletion in DeleteDirectories UI
Browse files Browse the repository at this point in the history
  • Loading branch information
HebaruSan committed Aug 26, 2023
1 parent 9f72701 commit bc071c0
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 40 deletions.
3 changes: 2 additions & 1 deletion GUI/Controls/DeleteDirectories.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions GUI/Controls/DeleteDirectories.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public void LoadDirs(GameInstance ksp, HashSet<string> possibleConfigOnlyDirs)
.ToArray();
Util.Invoke(this, () =>
{
DeleteButton.Focus();
DirectoriesListView.Items.Clear();
DirectoriesListView.Items.AddRange(items);
DirectoriesListView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent);
Expand Down Expand Up @@ -79,6 +80,12 @@ public bool Wait(out HashSet<string> toDelete)
}
}

protected override void OnResize(EventArgs e)
{
base.OnResize(e);
ExplanationLabel.Height = Util.LabelStringHeight(CreateGraphics(), ExplanationLabel);
}

/// <summary>
/// Open the user guide when the user presses F1
/// </summary>
Expand Down
3 changes: 1 addition & 2 deletions GUI/Controls/DeleteDirectories.resx
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="ExplanationLabel.Text" xml:space="preserve"><value>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.</value></data>
<data name="ExplanationLabel.Text" xml:space="preserve"><value>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.</value></data>
<data name="DirectoryColumn.Text" xml:space="preserve"><value>Directories</value></data>
<data name="FileColumn.Text" xml:space="preserve"><value>Directory Contents</value></data>
<data name="SelectDirPrompt.Text" xml:space="preserve"><value>Click a directory at the left to see its contents</value></data>
Expand Down
7 changes: 2 additions & 5 deletions GUI/Controls/ModInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
13 changes: 3 additions & 10 deletions GUI/Controls/ModInfoTabs/Metadata.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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))));
}
}
Expand All @@ -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));
}
}
Expand Down
12 changes: 4 additions & 8 deletions GUI/Dialogs/ErrorDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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));
}
Expand Down
15 changes: 1 addition & 14 deletions GUI/Dialogs/YesNoDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public Tuple<DialogResult, bool> 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
Expand All @@ -67,19 +67,6 @@ private void SetupSuppressable(string text, string yesText, string noText, strin
SuppressCheckbox.Visible = true;
}

/// <summary>
/// Simple syntactic sugar around Graphics.MeasureString
/// </summary>
/// <param name="text">String to measure size of</param>
/// <param name="maxWidth">Number of pixels allowed horizontally</param>
/// <returns>
/// Number of pixels needed vertically to fit the string
/// </returns>
private int StringHeight(string text, int maxWidth)
{
return (int)CreateGraphics().MeasureString(text, DescriptionLabel.Font, maxWidth).Height;
}

public void HideYesNoDialog()
{
Util.Invoke(this, Close);
Expand Down
30 changes: 30 additions & 0 deletions GUI/Util.cs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,36 @@ public static EventHandler<EventT> Debounce<EventT>(
};
}

/// <summary>
/// Simple syntactic sugar around Graphics.MeasureString
/// </summary>
/// <param name="g">The graphics context</param>
/// <param name="font">The font to be used for the text</param>
/// <param name="text">String to measure size of</param>
/// <param name="maxWidth">Number of pixels allowed horizontally</param>
/// <returns>
/// Number of pixels needed vertically to fit the string
/// </returns>
public static int StringHeight(Graphics g, string text, Font font, int maxWidth)
=> (int)g.MeasureString(text, font, (int)(maxWidth / XScale(g))).Height;

/// <summary>
/// Calculate how much vertical space is needed to display a label's text
/// </summary>
/// <param name="g">The graphics context</param>
/// <param name="lbl">The label</param>
/// <returns>
/// Number of pixels needed vertically to show the label's full text
/// </returns>
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));
}
}

0 comments on commit bc071c0

Please sign in to comment.