Skip to content

Commit

Permalink
Tweak previous fix to include removing unrenderable sources from config
Browse files Browse the repository at this point in the history
In addition to the check added previously, when `SaltSourcesForm` attempts to populate, it will remove any source where the string is unrenderable by the graphics API.
  • Loading branch information
nitz committed Aug 6, 2024
1 parent 64fe7e6 commit aa663fd
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 31 deletions.
56 changes: 55 additions & 1 deletion SaltSources.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;

namespace KeePassDiceware
{
Expand Down Expand Up @@ -32,6 +33,22 @@ public class SaltSource : ICloneable
"\u00F0\u00F1\u00F2\u00F3\u00F4\u00F5\u00F6\u00F7" +
"\u00F8\u00F9\u00FA\u00FB\u00FC\u00FD\u00FE\u00FF";

private static bool? _runtimeCanHandleEmoji = null;
internal static bool RuntimeCanHandleEmoji
{
get
{
if (_runtimeCanHandleEmoji.HasValue)
{
return _runtimeCanHandleEmoji.Value;
}

_runtimeCanHandleEmoji = TestRuntimeRenderSupport(Emojis);

return _runtimeCanHandleEmoji.Value;
}
}

public string Name { get; set; }
[Browsable(false)]
public string Key => Name.Replace(" ", string.Empty);
Expand Down Expand Up @@ -60,6 +77,21 @@ public bool Enabled
}
}

internal bool? _canRender = null;
public bool CanRender
{
get
{
if (_canRender.HasValue)
{
return _canRender.Value;
}

_canRender = TestRuntimeRenderSupport(Pool);

return _canRender.Value;
}
}

public SaltSource(string name, string pool, bool enabled = true)
{
Expand Down Expand Up @@ -104,8 +136,30 @@ public static List<SaltSource> DefaultSources
}

/// <summary>
/// As <see cref="DefaultSources"/>, but without the "Emoji" source."/>
/// As <see cref="DefaultSources"/>, but without the "Emoji" source.
/// </summary>
public static List<SaltSource> DefaultSourcesWithoutEmoji => DefaultSources.FindAll(s => s.Name != "Emoji");

/// <summary>
/// Attempts to render the given string with the graphics API.
/// </summary>
/// <param name="testString">The string to attempt to render</param>
/// <returns>True if the render was successful, otherwise false</returns>
internal static bool TestRuntimeRenderSupport(string testString)
{
try
{
// attempt to render the given string to a temporary bitmap.
using Bitmap test = new(1, 1);
using var g = Graphics.FromImage(test);
g.DrawString(testString, SystemFonts.DefaultFont, Brushes.Black, 0, 0);
return true;
}
catch
{
// if drawing fails for any reason, assume this runtime can't handle characters in that string
return false;
}
}
}
}
45 changes: 15 additions & 30 deletions SaltSourcesForm.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;

Expand All @@ -11,34 +10,6 @@ namespace KeePassDiceware
{
public partial class SaltSourcesForm : Form
{
private static bool? _runtimeCanHandleEmoji = null;
private static bool RuntimeCanHandleEmoji
{
get
{
if (_runtimeCanHandleEmoji.HasValue)
{
return _runtimeCanHandleEmoji.Value;
}

try
{
// attempt to render an emoji string to a temporary bitmap.
using Bitmap test = new(1, 1);
using var g = Graphics.FromImage(test);
g.DrawString(SaltSource.Emojis, SystemFonts.DefaultFont, Brushes.Black, 0, 0);
_runtimeCanHandleEmoji = true;
}
catch
{
// if drawing an emoji fails for any reason, assume this runtime can't handle emoji.
_runtimeCanHandleEmoji = false;
}

return _runtimeCanHandleEmoji.Value;
}
}

public List<SaltSource> Result { get; private set; } = null;
private BindingList<SaltSource> DataSource { get; set; } = null;

Expand Down Expand Up @@ -100,6 +71,20 @@ internal void PopulateSaltSources(List<SaltSource> saltSources)
{
_pendingErrors.Clear();

int totalCount = saltSources.Count;

saltSources = saltSources.Where(ss => ss.CanRender).ToList();

int removed = totalCount - saltSources.Count;

if (removed > 0)
{
string removedS = removed == 1 ? string.Empty : "s";
string removedHelpingVerb = removed == 1 ? "was" : "were";
MessageBox.Show(this, $"{removed} salt source{removedS} {removedHelpingVerb} removed because they are not supported by the current runtime.",
$"Unsupported Source{removedS} Removed", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}

DataSource = new(saltSources.ConvertAll(ss => ss.Clone() as SaltSource))
{
AllowNew = true,
Expand Down Expand Up @@ -196,7 +181,7 @@ private int ValidateSourceNames()

private void RestoreDefaultsButton_Click(object sender, EventArgs e)
{
PopulateSaltSources(RuntimeCanHandleEmoji
PopulateSaltSources(SaltSource.RuntimeCanHandleEmoji
? SaltSource.DefaultSources
: SaltSource.DefaultSourcesWithoutEmoji
);
Expand Down

0 comments on commit aa663fd

Please sign in to comment.