Skip to content

A Swiss Army knife for developers.

Small refactoring

Etienne Baudouxdb29b04e58fa
diff --git a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDropDownListItem.cs b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDropDownListItem.cs
index a6395e162..d768a5909 100644
--- a/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDropDownListItem.cs
+++ b/src/app/dev/DevToys.Api/Tool/GUI/Components/IUIDropDownListItem.cs
@@ -32,6 +32,15 @@ internal sealed class UIDropDownListItem : IUIDropDownListItem
public static partial class GUI
{
+ /// <summary>
+ /// Create component that represents an item in a drop down list.
+ /// </summary>
+ /// <param name="value">The value associated to the item. It will be used to generate the text to display in the item</param>
+ public static IUIDropDownListItem Item(object? value)
+ {
+ return Item(text: value?.ToString() ?? string.Empty, value);
+ }
+
/// <summary>
/// Create component that represents an item in a drop down list.
/// </summary>
diff --git a/src/app/dev/DevToys.Tools/SmartDetection/ColorBlindnessSimulatorImageFileDataTypeDetector.cs b/src/app/dev/DevToys.Tools/SmartDetection/StaticImageFileDataTypeDetector.cs
similarity index 82%
rename from src/app/dev/DevToys.Tools/SmartDetection/ColorBlindnessSimulatorImageFileDataTypeDetector.cs
rename to src/app/dev/DevToys.Tools/SmartDetection/StaticImageFileDataTypeDetector.cs
index c449fd8a0..45a1e3282 100644
--- a/src/app/dev/DevToys.Tools/SmartDetection/ColorBlindnessSimulatorImageFileDataTypeDetector.cs
+++ b/src/app/dev/DevToys.Tools/SmartDetection/StaticImageFileDataTypeDetector.cs
@@ -1,8 +1,8 @@
namespace DevToys.Tools.SmartDetection;
[Export(typeof(IDataTypeDetector))]
-[DataTypeName("ColorBlindnessSimulatorImageFile", baseName: PredefinedCommonDataTypeNames.File)]
-internal sealed partial class ColorBlindnessSimulatorImageFileDataTypeDetector : IDataTypeDetector
+[DataTypeName("StaticImageFile", baseName: PredefinedCommonDataTypeNames.File)]
+internal sealed partial class StaticImageFileDataTypeDetector : IDataTypeDetector
{
internal static readonly string[] SupportedFileTypes = new[]
{
diff --git a/src/app/dev/DevToys.Tools/SmartDetection/StaticImageFilesDataTypeDetector.cs b/src/app/dev/DevToys.Tools/SmartDetection/StaticImageFilesDataTypeDetector.cs
new file mode 100644
index 000000000..66315c2eb
--- /dev/null
+++ b/src/app/dev/DevToys.Tools/SmartDetection/StaticImageFilesDataTypeDetector.cs
@@ -0,0 +1,29 @@
+namespace DevToys.Tools.SmartDetection;
+
+[Export(typeof(IDataTypeDetector))]
+[DataTypeName("StaticImageFiles", baseName: PredefinedCommonDataTypeNames.Files)]
+internal sealed partial class StaticImageFilesDataTypeDetector : IDataTypeDetector
+{
+ public ValueTask<DataDetectionResult> TryDetectDataAsync(object data, DataDetectionResult? resultFromBaseDetector, CancellationToken cancellationToken)
+ {
+ if (resultFromBaseDetector is not null
+ && resultFromBaseDetector.Data is FileInfo[] dataFiles)
+ {
+ var files = new List<FileInfo>();
+ for (int i = 0; i < dataFiles.Length; i++)
+ {
+ if (StaticImageFileDataTypeDetector.SupportedFileTypes.Contains(dataFiles[i].Extension, StringComparer.OrdinalIgnoreCase))
+ {
+ files.Add(dataFiles[i]);
+ }
+ }
+
+ if (files.Count > 0)
+ {
+ return ValueTask.FromResult(new DataDetectionResult(Success: true, Data: files.ToArray()));
+ }
+ }
+
+ return ValueTask.FromResult(DataDetectionResult.Unsuccessful);
+ }
+}
diff --git a/src/app/dev/DevToys.Tools/Tools/EncodersDecoders/GZip/GZipEncoderDecoderCommandLineTool.cs b/src/app/dev/DevToys.Tools/Tools/EncodersDecoders/GZip/GZipEncoderDecoderCommandLineTool.cs
index 168f27e99..a5f9a8095 100644
--- a/src/app/dev/DevToys.Tools/Tools/EncodersDecoders/GZip/GZipEncoderDecoderCommandLineTool.cs
+++ b/src/app/dev/DevToys.Tools/Tools/EncodersDecoders/GZip/GZipEncoderDecoderCommandLineTool.cs
@@ -17,7 +17,7 @@ internal sealed class GZipEncoderDecoderCommandLineTool : ICommandLineTool
Alias = "i",
IsRequired = true,
DescriptionResourceName = nameof(GZipEncoderDecoder.InputOptionDescription))]
- private string? Input { get; set; }
+ private string? Input { get; set; } // TODO: Support file as input too.
[CommandLineOption(
Name = "mode",
diff --git a/src/app/dev/DevToys.Tools/Tools/Generators/HashAndChecksum/HashAndChecksumGeneratorCommandLineTool.cs b/src/app/dev/DevToys.Tools/Tools/Generators/HashAndChecksum/HashAndChecksumGeneratorCommandLineTool.cs
index 40ebbcf23..582435ad7 100644
--- a/src/app/dev/DevToys.Tools/Tools/Generators/HashAndChecksum/HashAndChecksumGeneratorCommandLineTool.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Generators/HashAndChecksum/HashAndChecksumGeneratorCommandLineTool.cs
@@ -111,8 +111,13 @@ internal sealed class HashAndChecksumGeneratorCommandLineTool : ICommandLineTool
return 0;
}
+ catch (Exception ex) when (ex is TaskCanceledException or OperationCanceledException)
+ {
+ return -1;
+ }
catch
{
+ // TODO: Log exception.
return -1;
}
}
diff --git a/src/app/dev/DevToys.Tools/Tools/Graphic/ColorBlindnessSimulator/ColorBlindnessSimulatorCommandLineTool.cs b/src/app/dev/DevToys.Tools/Tools/Graphic/ColorBlindnessSimulator/ColorBlindnessSimulatorCommandLineTool.cs
index 10e873765..265391ed0 100644
--- a/src/app/dev/DevToys.Tools/Tools/Graphic/ColorBlindnessSimulator/ColorBlindnessSimulatorCommandLineTool.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Graphic/ColorBlindnessSimulator/ColorBlindnessSimulatorCommandLineTool.cs
@@ -51,14 +51,14 @@ internal sealed class ColorBlindnessSimulatorCommandLineTool : ICommandLineTool
}
string fileExtension = Input.Extension;
- bool isSupportedImageFile = ColorBlindnessSimulatorImageFileDataTypeDetector.SupportedFileTypes.Any(ext => ext.Equals(fileExtension, StringComparison.OrdinalIgnoreCase));
+ bool isSupportedImageFile = StaticImageFileDataTypeDetector.SupportedFileTypes.Any(ext => ext.Equals(fileExtension, StringComparison.OrdinalIgnoreCase));
if (!isSupportedImageFile)
{
Console.Error.WriteLine(
string.Format(
GlobalStrings.FileTypeNotSupported,
Input.FullName,
- string.Join(", ", ColorBlindnessSimulatorImageFileDataTypeDetector.SupportedFileTypes)));
+ string.Join(", ", StaticImageFileDataTypeDetector.SupportedFileTypes)));
return -1;
}
diff --git a/src/app/dev/DevToys.Tools/Tools/Graphic/ColorBlindnessSimulator/ColorBlindnessSimulatorGuiTool.cs b/src/app/dev/DevToys.Tools/Tools/Graphic/ColorBlindnessSimulator/ColorBlindnessSimulatorGuiTool.cs
index d5e6f1c89..e604e19ee 100644
--- a/src/app/dev/DevToys.Tools/Tools/Graphic/ColorBlindnessSimulator/ColorBlindnessSimulatorGuiTool.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Graphic/ColorBlindnessSimulator/ColorBlindnessSimulatorGuiTool.cs
@@ -20,7 +20,7 @@ namespace DevToys.Tools.Tools.Graphic.ColorBlindnessSimulator;
DescriptionResourceName = nameof(ColorBlindnessSimulator.Description),
AccessibleNameResourceName = nameof(ColorBlindnessSimulator.AccessibleName))]
[AcceptedDataTypeName(PredefinedCommonDataTypeNames.Image)]
-[AcceptedDataTypeName("ColorBlindnessSimulatorImageFile")]
+[AcceptedDataTypeName("StaticImageFile")]
internal sealed class ColorBlindnessSimulatorGuiTool : IGuiTool, IDisposable
{
private enum GridRows
@@ -86,7 +86,7 @@ internal sealed class ColorBlindnessSimulatorGuiTool : IGuiTool, IDisposable
_fileSelector
.CanSelectOneFile()
- .LimitFileTypesTo(ColorBlindnessSimulatorImageFileDataTypeDetector.SupportedFileTypes)
+ .LimitFileTypesTo(StaticImageFileDataTypeDetector.SupportedFileTypes)
.OnFilesSelected(OnFilesSelected)),
Cell(
@@ -153,7 +153,7 @@ internal sealed class ColorBlindnessSimulatorGuiTool : IGuiTool, IDisposable
_fileSelector.WithFiles(SandboxedFileReader.FromFileInfo(temporaryFile)); // This will trigger a new simulation.
}
- else if (dataTypeName == "ColorBlindnessSimulatorImageFile" && parsedData is FileInfo file)
+ else if (dataTypeName == "StaticImageFile" && parsedData is FileInfo file)
{
_fileSelector.WithFiles(SandboxedFileReader.FromFileInfo(file)); // This will trigger a new simulation.
}