Skip to content

A Swiss Army knife for developers.

Refactoring

Etienne Baudoux09d8be373ec4
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIElement.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIElement.cs
index d77c02411..fe001d5a3 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIElement.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIElement.cs
@@ -103,7 +103,7 @@ internal abstract class UIElement : IUIElement, INotifyPropertyChanged
public event PropertyChangedEventHandler? PropertyChanged;
- protected void SetPropertyValue<T>(
+ protected bool SetPropertyValue<T>(
ref T field,
T value,
EventHandler? propertyChangedEventHandler,
@@ -114,7 +114,9 @@ internal abstract class UIElement : IUIElement, INotifyPropertyChanged
field = value;
propertyChangedEventHandler?.Invoke(this, EventArgs.Empty);
OnPropertyChanged(propertyName);
+ return true;
}
+ return false;
}
protected void OnPropertyChanged([CallerMemberName] string? propertyName = null)
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIFileSelector.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIFileSelector.cs
index 307baba12..0e5c5cd4d 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIFileSelector.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIFileSelector.cs
@@ -55,10 +55,12 @@ internal sealed class UIFileSelector : UIElement, IUIFileSelector
get => _selectedFiles;
internal set
{
- SetPropertyValue(ref _selectedFiles, value, null);
+ if (SetPropertyValue(ref _selectedFiles, value, null))
+ {
OnFilesSelectedAction?.Invoke(_selectedFiles);
}
}
+ }
public bool CanSelectManyFiles
{
@@ -141,6 +143,15 @@ public static partial class GUI
return element;
}
+ /// <summary>
+ /// Limits the list of selectable files to known supported image formats as defined in <see cref="PredefinedSupportedImageFormats.ImageFileExtensions"/>.
+ /// </summary>
+ public static IUIFileSelector LimitFileTypesToImages(this IUIFileSelector element)
+ {
+ ((UIFileSelector)element).AllowedFileExtensions = PredefinedSupportedImageFormats.ImageFileExtensions;
+ return element;
+ }
+
/// <summary>
/// Allows the user to select only one file at once.
/// </summary>
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.cs
index 4405b4a4b..25adbd7c8 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISinglelineTextInput.cs
@@ -83,10 +83,12 @@ internal class UISingleLineTextInput : UITitledElementWithChildren, IUISingleLin
get => _text ?? string.Empty;
internal set
{
- SetPropertyValue(ref _text, value, TextChanged);
+ if (SetPropertyValue(ref _text, value, TextChanged))
+ {
ActionOnTextChanged?.Invoke(_text ?? string.Empty);
}
}
+ }
public bool IsReadOnly
{
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISwitch.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISwitch.cs
index 6cd162186..a761fa0de 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISwitch.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUISwitch.cs
@@ -55,10 +55,12 @@ internal sealed class UISwitch : UITitledElement, IUISwitch
get => _state;
internal set
{
- SetPropertyValue(ref _state, value, IsOnChanged);
+ if (SetPropertyValue(ref _state, value, IsOnChanged))
+ {
ActionOnToggle?.Invoke(_state);
}
}
+ }
public string? OnText
{
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/PredefinedSupportedImageFormats.cs b/src/app/dev/DevToys.Api/Tool/GUI/PredefinedSupportedImageFormats.cs
new file mode 100644
index 000000000..bd215cac9
--- /dev/null
+++ b/src/app/dev/DevToys.Api/Tool/GUI/PredefinedSupportedImageFormats.cs
@@ -0,0 +1,11 @@
+namespace DevToys.Api;
+
+public static class PredefinedSupportedImageFormats
+{
+ /// <summary>
+ /// A list of file extensions representing image formats supported by default.
+ /// They include BMP, GIF, JPEG, PBM, PNG, TIFF, TGA, WEBP formats.
+ /// </summary>
+ public static readonly string[] ImageFileExtensions
+ = new[] { ".bmp", ".gif", ".jpeg", ".jpg", ".pbm", ".png", ".tiff", ".tga", ".webp" };
+}
diff --git a/src/app/dev/DevToys.Blazor/Core/Extensions/IJSRuntimeExtensions.cs b/src/app/dev/DevToys.Blazor/Core/Extensions/IJSRuntimeExtensions.cs
index dd482aac1..39116a989 100644
--- a/src/app/dev/DevToys.Blazor/Core/Extensions/IJSRuntimeExtensions.cs
+++ b/src/app/dev/DevToys.Blazor/Core/Extensions/IJSRuntimeExtensions.cs
@@ -41,10 +41,6 @@ internal static class IJSRuntimeExtensions
{
return false;
}
- catch (Exception ex)
- {
- return false;
- }
}
/// <summary>
diff --git a/src/app/dev/DevToys.Core/FileHelper.cs b/src/app/dev/DevToys.Core/FileHelper.cs
new file mode 100644
index 000000000..6fc137566
--- /dev/null
+++ b/src/app/dev/DevToys.Core/FileHelper.cs
@@ -0,0 +1,69 @@
+using System.Collections.Concurrent;
+
+namespace DevToys.Core;
+
+public static class FileHelper
+{
+ private static readonly ConcurrentBag<FileInfo> tempFiles = new();
+
+ public static FileInfo CreateTempFile(string baseFolder, string? desiredFileExtension)
+ {
+ string tempFilePath;
+
+ do
+ {
+ tempFilePath = Path.Combine(baseFolder, Path.GetFileName(Path.GetTempFileName()));
+ if (!string.IsNullOrWhiteSpace(desiredFileExtension))
+ {
+ tempFilePath = Path.ChangeExtension(tempFilePath, desiredFileExtension);
+ }
+ } while (File.Exists(tempFilePath));
+
+ Directory.CreateDirectory(baseFolder);
+ File.Create(tempFilePath).Dispose();
+ var fileInfo = new FileInfo(tempFilePath);
+ tempFiles.Add(fileInfo);
+ return fileInfo;
+ }
+
+ public static void ClearTempFiles(string baseFolder)
+ {
+ FileInfo[] files = tempFiles.ToArray();
+ foreach (FileInfo tempFile in files)
+ {
+ try
+ {
+ if (File.Exists(tempFile.FullName))
+ {
+ File.Delete(tempFile.FullName);
+ }
+ }
+ catch
+ {
+ // Swallow.
+ }
+ }
+
+ if (Directory.Exists(baseFolder))
+ {
+ foreach (string tempFilePath in Directory.GetFiles(baseFolder, string.Empty, SearchOption.AllDirectories))
+ {
+ // We found a file that isn't tracked by the current instance of the app.
+ // If the file is older than 2 minutes, let's destroy it.
+
+ var tempFileInfo = new FileInfo(tempFilePath);
+ if (DateTime.Now - tempFileInfo.CreationTime > TimeSpan.FromMinutes(2))
+ {
+ try
+ {
+ File.Delete(tempFilePath);
+ }
+ catch
+ {
+ // Swallow.
+ }
+ }
+ }
+ }
+ }
+}
diff --git a/src/app/dev/DevToys.Core/Tools/GuiToolInstance.cs b/src/app/dev/DevToys.Core/Tools/GuiToolInstance.cs
index 009798474..22399e47f 100644
--- a/src/app/dev/DevToys.Core/Tools/GuiToolInstance.cs
+++ b/src/app/dev/DevToys.Core/Tools/GuiToolInstance.cs
@@ -7,7 +7,7 @@ using Microsoft.Extensions.Logging;
namespace DevToys.Core.Tools;
[DebuggerDisplay($"InternalComponentName = {{{nameof(InternalComponentName)}}}")]
-public sealed partial class GuiToolInstance : ObservableObject
+public sealed partial class GuiToolInstance : ObservableObject, IDisposable
{
private Lazy<UIToolView> _view;
private readonly ILogger _logger;
@@ -81,6 +81,14 @@ public sealed partial class GuiToolInstance : ObservableObject
/// </summary>
public UIToolView View => _view.Value;
+ public void Dispose()
+ {
+ if (_instance.IsValueCreated && _instance.Value is IDisposable disposable)
+ {
+ disposable.Dispose();
+ }
+ }
+
/// <summary>
/// Send data coming from Smart Detection to the tool.
/// </summary>
diff --git a/src/app/dev/DevToys.Core/Tools/GuiToolProvider.cs b/src/app/dev/DevToys.Core/Tools/GuiToolProvider.cs
index c6090b505..2715f1fa0 100644
--- a/src/app/dev/DevToys.Core/Tools/GuiToolProvider.cs
+++ b/src/app/dev/DevToys.Core/Tools/GuiToolProvider.cs
@@ -387,6 +387,17 @@ public sealed partial class GuiToolProvider
return AllTools.FirstOrDefault(t => string.Equals(t.InternalComponentName, name, StringComparison.Ordinal));
}
+ /// <summary>
+ /// Dispose every disposable <see cref="IGuiTool"/>.
+ /// </summary>
+ public void DisposeTools()
+ {
+ for (int i = 0; i < AllTools.Count; i++)
+ {
+ AllTools[i].Dispose();
+ }
+ }
+
private void BuildGuiToolInstances(
IEnumerable<Lazy<IGuiTool, GuiToolMetadata>> guiTools,
out IReadOnlyList<GuiToolInstance> allTools,
diff --git a/src/app/dev/DevToys.Localization/DevToys.Localization.csproj b/src/app/dev/DevToys.Localization/DevToys.Localization.csproj
index 469f86f22..85a587993 100644
--- a/src/app/dev/DevToys.Localization/DevToys.Localization.csproj
+++ b/src/app/dev/DevToys.Localization/DevToys.Localization.csproj
@@ -59,7 +59,9 @@
</ItemGroup>
<ItemGroup>
<EmbeddedResource Update="**\*.resx" />
- <AdditionalFiles Include="**\*.resx" />
+ <AdditionalFiles Include="**\*.resx">
+ <Generator>PublicResXFileCodeGenerator</Generator>
+ </AdditionalFiles>
</ItemGroup>
<ItemGroup>
<Analyzer Include="$(RepoRoot)src\generators\ResxHelperGenerator\bin\netstandard2.0\ResxHelperGenerator.dll" />
@@ -67,10 +69,4 @@
<ItemGroup>
<ProjectReference Include="..\DevToys.Api\DevToys.Api.csproj" />
</ItemGroup>
- <ItemGroup>
- <AdditionalFiles Update="Strings\UIFileSelectorPresenter\UIFileSelectorPresenter.resx">
- <Generator>PublicResXFileCodeGenerator</Generator>
- <LastGenOutput>UIFileSelectorPresenter.Designer.cs</LastGenOutput>
- </AdditionalFiles>
- </ItemGroup>
</Project>
\ No newline at end of file
diff --git a/src/app/dev/DevToys.Localization/Strings/ToolGroupPage/ToolGroupPage.Designer.cs b/src/app/dev/DevToys.Localization/Strings/ToolGroupPage/ToolGroupPage.Designer.cs
index 5ce2f5401..0723e4725 100644
--- a/src/app/dev/DevToys.Localization/Strings/ToolGroupPage/ToolGroupPage.Designer.cs
+++ b/src/app/dev/DevToys.Localization/Strings/ToolGroupPage/ToolGroupPage.Designer.cs
@@ -79,7 +79,7 @@ namespace DevToys.Localization.Strings.ToolGroupPage {
}
/// <summary>
- /// Looks up a localized string similar to Suggest your ideas!.
+ /// Looks up a localized string similar to Suggest an idea.
/// </summary>
public static string NotFindingToolSuggestIdea {
get {