A Swiss Army knife for developers.
C# blazorcross-platformcsharpdesktopdesktop-appdesktop-applicationdeveloper-toolsextensiblefluentfluent-designlinuxmacosmicasyntax-highlightingtypescriptwindowswindows-10windows-11winui
Ported UUID Generator to DevToys 2.0 (#1000)
Etienne BAUDOUX30b86ff5d989
diff --git a/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.cs b/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.csindex 663194640..135348c7d 100644--- a/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.cs+++ b/src/app/dev/DevToys.Blazor/Components/Text/TextBox/TextBox.razor.cs@@ -1,4 +1,5 @@using System.Collections.ObjectModel;+using System.Globalization;using DevToys.Core.Settings;using Microsoft.AspNetCore.Components.Forms;@@ -140,7 +141,31 @@ public partial class TextBox : MefComponentBase, IFocusableinternal Task SetTextAsync(string text){- Text = text;+ if (Type == TextBoxTypes.Number)+ {+ if (double.TryParse(text, out double value))+ {+ if (value < Min)+ {+ value = Min;+ }+ else if (value > Max)+ {+ value = Max;+ }++ Text = value.ToString(CultureInfo.InvariantCulture);+ }+ else+ {+ Text = Math.Max(Min, Math.Min(0, Max)).ToString(CultureInfo.InvariantCulture);+ }+ }+ else+ {+ Text = text;+ }+return TextChanged.InvokeAsync(Text);}diff --git a/src/app/dev/DevToys.Tools/DevToys.Tools.csproj b/src/app/dev/DevToys.Tools/DevToys.Tools.csprojindex cb1255575..7e2f4084b 100644--- a/src/app/dev/DevToys.Tools/DevToys.Tools.csproj+++ b/src/app/dev/DevToys.Tools/DevToys.Tools.csproj@@ -84,6 +84,11 @@<AutoGen>True</AutoGen><DependentUpon>HashAndChecksumGenerator.resx</DependentUpon></Compile>+ <Compile Update="Tools\Generators\UUID\UUIDGenerator.Designer.cs">+ <DesignTime>True</DesignTime>+ <AutoGen>True</AutoGen>+ <DependentUpon>UUIDGenerator.resx</DependentUpon>+ </Compile><Compile Update="Tools\Graphic\ColorBlindnessSimulator\ColorBlindnessSimulator.Designer.cs"><DesignTime>True</DesignTime><AutoGen>True</AutoGen>@@ -175,6 +180,10 @@<Generator>ResXFileCodeGenerator</Generator><LastGenOutput>HashAndChecksumGenerator.Designer.cs</LastGenOutput></EmbeddedResource>+ <EmbeddedResource Update="Tools\Generators\UUID\UUIDGenerator.resx">+ <Generator>ResXFileCodeGenerator</Generator>+ <LastGenOutput>UUIDGenerator.Designer.cs</LastGenOutput>+ </EmbeddedResource><EmbeddedResource Update="Tools\Graphic\ColorBlindnessSimulator\ColorBlindnessSimulator.resx"><Generator>ResXFileCodeGenerator</Generator><LastGenOutput>ColorBlindnessSimulator.Designer.cs</LastGenOutput>diff --git a/src/app/dev/DevToys.Tools/Helpers/UuidHelper.cs b/src/app/dev/DevToys.Tools/Helpers/UuidHelper.csnew file mode 100644index 000000000..2991679a7--- /dev/null+++ b/src/app/dev/DevToys.Tools/Helpers/UuidHelper.cs@@ -0,0 +1,114 @@+using DevToys.Tools.Models;++namespace DevToys.Tools.Helpers;+internal static class UuidHelper+{+ private enum GuidVersion+ {+ TimeBased = 0x01,+ Reserved = 0x02,+ NameBased = 0x03,+ Random = 0x04+ }++ private static readonly Random random = new();++ private static readonly DateTimeOffset gregorianCalendarStart = new(1582, 10, 15, 0, 0, 0, TimeSpan.Zero);+ private const int VariantByte = 8;+ private const int VariantByteMask = 0x3f;+ private const int VariantByteShift = 0x80;+ private const int VersionByte = 7;+ private const int VersionByteMask = 0x0f;+ private const int VersionByteShift = 4;+ private const int ByteArraySize = 16;+ private const byte TimestampByte = 0;+ private const byte NodeByte = 10;+ private const byte GuidClockSequenceByte = 8;++ internal static string GenerateUuid(UuidVersion version, bool hyphens, bool uppercase)+ {+ string guidStringFormat;+ if (hyphens)+ {+ guidStringFormat = "D";+ }+ else+ {+ guidStringFormat = "N";+ }++ string? uuid;+ if (version == UuidVersion.One)+ {+ uuid = GenerateTimeBasedGuid().ToString(guidStringFormat);+ }+ else+ {+ uuid = Guid.NewGuid().ToString(guidStringFormat);+ }++ if (uppercase)+ {+ uuid = uuid.ToUpperInvariant();+ }++ return uuid;+ }++ private static Guid GenerateTimeBasedGuid()+ {+ DateTime dateTime = DateTime.UtcNow;+ long ticks = dateTime.Ticks - gregorianCalendarStart.Ticks;++ byte[] guid = new byte[ByteArraySize];+ byte[] timestamp = BitConverter.GetBytes(ticks);++ // copy node+ byte[]? nodes = GenerateNodeBytes();+ Array.Copy(nodes, 0, guid, NodeByte, Math.Min(6, nodes.Length));++ // copy clock sequence+ byte[]? clockSequence = GenerateClockSequenceBytes(dateTime);+ Array.Copy(clockSequence, 0, guid, GuidClockSequenceByte, Math.Min(2, clockSequence.Length));++ // copy timestamp+ Array.Copy(timestamp, 0, guid, TimestampByte, Math.Min(8, timestamp.Length));++ // set the variant+ guid[VariantByte] &= (byte)VariantByteMask;+ guid[VariantByte] |= (byte)VariantByteShift;++ // set the version+ guid[VersionByte] &= (byte)VersionByteMask;+ guid[VersionByte] |= (byte)((byte)GuidVersion.TimeBased << VersionByteShift);++ return new Guid(guid);+ }++ public static byte[] GenerateNodeBytes()+ {+ byte[]? node = new byte[6];++ random.NextBytes(node);+ return node;+ }++ public static byte[] GenerateClockSequenceBytes(DateTime dt)+ {+ DateTime utc = dt.ToUniversalTime();++ byte[]? bytes = BitConverter.GetBytes(utc.Ticks);++ if (bytes.Length == 0)+ {+ return new byte[] { 0x0, 0x0 };+ }++ if (bytes.Length == 1)+ {+ return new byte[] { 0x0, bytes[0] };+ }++ return new byte[] { bytes[0], bytes[1] };+ }+}diff --git a/src/app/dev/DevToys.Tools/Models/UuidVersion.cs b/src/app/dev/DevToys.Tools/Models/UuidVersion.csnew file mode 100644index 000000000..7a88c14cf--- /dev/null+++ b/src/app/dev/DevToys.Tools/Models/UuidVersion.cs@@ -0,0 +1,7 @@+namespace DevToys.Tools.Models;++internal enum UuidVersion+{+ One,+ Four+}diff --git a/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGenerator.Designer.cs b/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGenerator.Designer.csnew file mode 100644index 000000000..799484cec--- /dev/null+++ b/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGenerator.Designer.cs@@ -0,0 +1,234 @@+//------------------------------------------------------------------------------+// <auto-generated>+// This code was generated by a tool.+// Runtime Version:4.0.30319.42000+//+// Changes to this file may cause incorrect behavior and will be lost if+// the code is regenerated.+// </auto-generated>+//------------------------------------------------------------------------------++namespace DevToys.Tools.Tools.Generators.UUID {+ using System;+++ /// <summary>+ /// A strongly-typed resource class, for looking up localized strings, etc.+ /// </summary>+ // This class was auto-generated by the StronglyTypedResourceBuilder+ // class via a tool like ResGen or Visual Studio.+ // To add or remove a member, edit your .ResX file then rerun ResGen+ // with the /str option, or rebuild your VS project.+ [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]+ [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]+ [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]+ internal class UUIDGenerator {++ private static global::System.Resources.ResourceManager resourceMan;++ private static global::System.Globalization.CultureInfo resourceCulture;++ [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]+ internal UUIDGenerator() {+ }++ /// <summary>+ /// Returns the cached ResourceManager instance used by this class.+ /// </summary>+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]+ internal static global::System.Resources.ResourceManager ResourceManager {+ get {+ if (object.ReferenceEquals(resourceMan, null)) {+ global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("DevToys.Tools.Tools.Generators.UUID.UUIDGenerator", typeof(UUIDGenerator).Assembly);+ resourceMan = temp;+ }+ return resourceMan;+ }+ }++ /// <summary>+ /// Overrides the current thread's CurrentUICulture property for all+ /// resource lookups using this strongly typed resource class.+ /// </summary>+ [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]+ internal static global::System.Globalization.CultureInfo Culture {+ get {+ return resourceCulture;+ }+ set {+ resourceCulture = value;+ }+ }++ /// <summary>+ /// Looks up a localized string similar to UUID Generator tool.+ /// </summary>+ internal static string AccessibleName {+ get {+ return ResourceManager.GetString("AccessibleName", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Configuration.+ /// </summary>+ internal static string ConfigurationTitle {+ get {+ return ResourceManager.GetString("ConfigurationTitle", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Generate UUIDs version 1 and 4 (GUID).+ /// </summary>+ internal static string Description {+ get {+ return ResourceManager.GetString("Description", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Generate UUID(s).+ /// </summary>+ internal static string GenerateButton {+ get {+ return ResourceManager.GetString("GenerateButton", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Generate.+ /// </summary>+ internal static string GenerateTitle {+ get {+ return ResourceManager.GetString("GenerateTitle", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Hyphens.+ /// </summary>+ internal static string Hyphens {+ get {+ return ResourceManager.GetString("Hyphens", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Whether the generated UUID should have hyphens or not.+ /// </summary>+ internal static string HyphensOptionDescription {+ get {+ return ResourceManager.GetString("HyphensOptionDescription", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to UUID Generator.+ /// </summary>+ internal static string LongDisplayTitle {+ get {+ return ResourceManager.GetString("LongDisplayTitle", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to x.+ /// </summary>+ internal static string MultiplySymbol {+ get {+ return ResourceManager.GetString("MultiplySymbol", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to UUID(s).+ /// </summary>+ internal static string Output {+ get {+ return ResourceManager.GetString("Output", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to GUID.+ /// </summary>+ internal static string SearchKeywords {+ get {+ return ResourceManager.GetString("SearchKeywords", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to UUID.+ /// </summary>+ internal static string ShortDisplayTitle {+ get {+ return ResourceManager.GetString("ShortDisplayTitle", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Uppercase.+ /// </summary>+ internal static string Uppercase {+ get {+ return ResourceManager.GetString("Uppercase", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Whether the generated UUID should be in uppercase or not.+ /// </summary>+ internal static string UppercaseOptionDescription {+ get {+ return ResourceManager.GetString("UppercaseOptionDescription", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to 4 (GUID).+ /// </summary>+ internal static string UuidVersionFour {+ get {+ return ResourceManager.GetString("UuidVersionFour", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to 1.+ /// </summary>+ internal static string UuidVersionOne {+ get {+ return ResourceManager.GetString("UuidVersionOne", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to Choose the version of UUID to generate.+ /// </summary>+ internal static string VersionDescription {+ get {+ return ResourceManager.GetString("VersionDescription", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to The UUID version to generate.+ /// </summary>+ internal static string VersionOptionDescription {+ get {+ return ResourceManager.GetString("VersionOptionDescription", resourceCulture);+ }+ }++ /// <summary>+ /// Looks up a localized string similar to UUID version.+ /// </summary>+ internal static string VersionTitle {+ get {+ return ResourceManager.GetString("VersionTitle", resourceCulture);+ }+ }+ }+}diff --git a/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGenerator.resx b/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGenerator.resxnew file mode 100644index 000000000..47da26afa--- /dev/null+++ b/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGenerator.resx@@ -0,0 +1,177 @@+<?xml version="1.0" encoding="utf-8"?>+<root>+ <!--+ Microsoft ResX Schema++ Version 2.0++ The primary goals of this format is to allow a simple XML format+ that is mostly human readable. The generation and parsing of the+ various data types are done through the TypeConverter classes+ associated with the data types.++ Example:++ ... ado.net/XML headers & schema ...+ <resheader name="resmimetype">text/microsoft-resx</resheader>+ <resheader name="version">2.0</resheader>+ <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>+ <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>+ <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>+ <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>+ <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">+ <value>[base64 mime encoded serialized .NET Framework object]</value>+ </data>+ <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">+ <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>+ <comment>This is a comment</comment>+ </data>++ There are any number of "resheader" rows that contain simple+ name/value pairs.++ Each data row contains a name, and value. The row also contains a+ type or mimetype. Type corresponds to a .NET class that support+ text/value conversion through the TypeConverter architecture.+ Classes that don't support this are serialized and stored with the+ mimetype set.++ The mimetype is used for serialized objects, and tells the+ ResXResourceReader how to depersist the object. This is currently not+ extensible. For a given mimetype the value must be set accordingly:++ Note - application/x-microsoft.net.object.binary.base64 is the format+ that the ResXResourceWriter will generate, however the reader can+ read any of the formats listed below.++ mimetype: application/x-microsoft.net.object.binary.base64+ value : The object must be serialized with+ : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter+ : and then encoded with base64 encoding.++ mimetype: application/x-microsoft.net.object.soap.base64+ value : The object must be serialized with+ : System.Runtime.Serialization.Formatters.Soap.SoapFormatter+ : and then encoded with base64 encoding.++ mimetype: application/x-microsoft.net.object.bytearray.base64+ value : The object must be serialized into a byte array+ : using a System.ComponentModel.TypeConverter+ : and then encoded with base64 encoding.+ -->+ <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">+ <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />+ <xsd:element name="root" msdata:IsDataSet="true">+ <xsd:complexType>+ <xsd:choice maxOccurs="unbounded">+ <xsd:element name="metadata">+ <xsd:complexType>+ <xsd:sequence>+ <xsd:element name="value" type="xsd:string" minOccurs="0" />+ </xsd:sequence>+ <xsd:attribute name="name" use="required" type="xsd:string" />+ <xsd:attribute name="type" type="xsd:string" />+ <xsd:attribute name="mimetype" type="xsd:string" />+ <xsd:attribute ref="xml:space" />+ </xsd:complexType>+ </xsd:element>+ <xsd:element name="assembly">+ <xsd:complexType>+ <xsd:attribute name="alias" type="xsd:string" />+ <xsd:attribute name="name" type="xsd:string" />+ </xsd:complexType>+ </xsd:element>+ <xsd:element name="data">+ <xsd:complexType>+ <xsd:sequence>+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />+ <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />+ </xsd:sequence>+ <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />+ <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />+ <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />+ <xsd:attribute ref="xml:space" />+ </xsd:complexType>+ </xsd:element>+ <xsd:element name="resheader">+ <xsd:complexType>+ <xsd:sequence>+ <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />+ </xsd:sequence>+ <xsd:attribute name="name" type="xsd:string" use="required" />+ </xsd:complexType>+ </xsd:element>+ </xsd:choice>+ </xsd:complexType>+ </xsd:element>+ </xsd:schema>+ <resheader name="resmimetype">+ <value>text/microsoft-resx</value>+ </resheader>+ <resheader name="version">+ <value>2.0</value>+ </resheader>+ <resheader name="reader">+ <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>+ </resheader>+ <resheader name="writer">+ <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>+ </resheader>+ <data name="AccessibleName" xml:space="preserve">+ <value>UUID Generator tool</value>+ </data>+ <data name="ConfigurationTitle" xml:space="preserve">+ <value>Configuration</value>+ </data>+ <data name="Description" xml:space="preserve">+ <value>Generate UUIDs version 1 and 4 (GUID)</value>+ </data>+ <data name="GenerateButton" xml:space="preserve">+ <value>Generate UUID(s)</value>+ </data>+ <data name="GenerateTitle" xml:space="preserve">+ <value>Generate</value>+ </data>+ <data name="Hyphens" xml:space="preserve">+ <value>Hyphens</value>+ </data>+ <data name="HyphensOptionDescription" xml:space="preserve">+ <value>Whether the generated UUID should have hyphens or not</value>+ </data>+ <data name="LongDisplayTitle" xml:space="preserve">+ <value>UUID Generator</value>+ </data>+ <data name="MultiplySymbol" xml:space="preserve">+ <value>x</value>+ </data>+ <data name="Output" xml:space="preserve">+ <value>UUID(s)</value>+ </data>+ <data name="SearchKeywords" xml:space="preserve">+ <value>GUID</value>+ </data>+ <data name="ShortDisplayTitle" xml:space="preserve">+ <value>UUID</value>+ </data>+ <data name="Uppercase" xml:space="preserve">+ <value>Uppercase</value>+ </data>+ <data name="UppercaseOptionDescription" xml:space="preserve">+ <value>Whether the generated UUID should be in uppercase or not</value>+ </data>+ <data name="UuidVersionFour" xml:space="preserve">+ <value>4 (GUID)</value>+ </data>+ <data name="UuidVersionOne" xml:space="preserve">+ <value>1</value>+ </data>+ <data name="VersionDescription" xml:space="preserve">+ <value>Choose the version of UUID to generate</value>+ </data>+ <data name="VersionOptionDescription" xml:space="preserve">+ <value>The UUID version to generate</value>+ </data>+ <data name="VersionTitle" xml:space="preserve">+ <value>UUID version</value>+ </data>+</root>\ No newline at end of filediff --git a/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGeneratorCommandLineTool.cs b/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGeneratorCommandLineTool.csnew file mode 100644index 000000000..8ee3935ad--- /dev/null+++ b/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGeneratorCommandLineTool.cs@@ -0,0 +1,45 @@+using DevToys.Tools.Helpers;+using DevToys.Tools.Models;+using Microsoft.Extensions.Logging;++namespace DevToys.Tools.Tools.Generators.UUID;++[Export(typeof(ICommandLineTool))]+[Name("UUIDGenerator")]+[CommandName(+ Name = "uuid",+ Alias = "guid",+ ResourceManagerBaseName = "DevToys.Tools.Tools.Generators.UUID.UUIDGenerator",+ DescriptionResourceName = nameof(UUIDGenerator.Description))]+internal sealed class UUIDGeneratorCommandLineTool : ICommandLineTool+{+ [CommandLineOption(+ Name = "version",+ Alias = "v",+ DescriptionResourceName = nameof(UUIDGenerator.VersionOptionDescription))]+ internal UuidVersion Version { get; set; } = UuidVersion.Four;++ [CommandLineOption(+ Name = "hyphens",+ Alias = "h",+ DescriptionResourceName = nameof(UUIDGenerator.HyphensOptionDescription))]+ internal bool Hyphens { get; set; } = true;++ [CommandLineOption(+ Name = "uppercase",+ Alias = "u",+ DescriptionResourceName = nameof(UUIDGenerator.UppercaseOptionDescription))]+ internal bool Uppercase { get; set; } = false;++ public ValueTask<int> InvokeAsync(ILogger logger, CancellationToken cancellationToken)+ {+ string newUuid+ = UuidHelper.GenerateUuid(+ Version,+ Hyphens,+ Uppercase);++ Console.WriteLine(newUuid);+ return ValueTask.FromResult(0);+ }+}diff --git a/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGeneratorGuidTool.cs b/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGeneratorGuidTool.csnew file mode 100644index 000000000..7d9e0fd64--- /dev/null+++ b/src/app/dev/DevToys.Tools/Tools/Generators/UUID/UUIDGeneratorGuidTool.cs@@ -0,0 +1,198 @@+using System.Text;+using DevToys.Tools.Helpers;+using DevToys.Tools.Models;++namespace DevToys.Tools.Tools.Generators.UUID;++[Export(typeof(IGuiTool))]+[Name("UUIDGenerator")]+[ToolDisplayInformation(+ IconFontName = "DevToys-Tools-Icons",+ IconGlyph = '\u0106',+ GroupName = PredefinedCommonToolGroupNames.Generators,+ ResourceManagerAssemblyIdentifier = nameof(DevToysToolsResourceManagerAssemblyIdentifier),+ ResourceManagerBaseName = "DevToys.Tools.Tools.Generators.UUID.UUIDGenerator",+ ShortDisplayTitleResourceName = nameof(UUIDGenerator.ShortDisplayTitle),+ LongDisplayTitleResourceName = nameof(UUIDGenerator.LongDisplayTitle),+ DescriptionResourceName = nameof(UUIDGenerator.Description),+ AccessibleNameResourceName = nameof(UUIDGenerator.AccessibleName))]+internal sealed class UUIDGeneratorGuidTool : IGuiTool+{+ /// <summary>+ /// Whether the generated UUID should have hyphens or not.+ /// </summary>+ private static readonly SettingDefinition<bool> hyphens+ = new(+ name: $"{nameof(UUIDGeneratorGuidTool)}.{nameof(hyphens)}",+ defaultValue: true);++ /// <summary>+ /// Whether the generated UUID should be uppercase or lowercase.+ /// </summary>+ private static readonly SettingDefinition<bool> uppercase+ = new(+ name: $"{nameof(UUIDGeneratorGuidTool)}.{nameof(uppercase)}",+ defaultValue: false);++ /// <summary>+ /// The UUID Version to generate.+ /// </summary>+ private static readonly SettingDefinition<UuidVersion> version+ = new(+ name: $"{nameof(UUIDGeneratorGuidTool)}.{nameof(version)}",+ defaultValue: UuidVersion.Four);++ /// <summary>+ /// How many GUIDs should be generated at once.+ /// </summary>+ private static readonly SettingDefinition<int> uuidToGenerate+ = new(+ name: $"{nameof(UUIDGeneratorGuidTool)}.{nameof(uuidToGenerate)}",+ defaultValue: 1);++ private enum GridColumn+ {+ Stretch+ }++ private enum GridRow+ {+ Settings,+ Results+ }++ private readonly ISettingsProvider _settingsProvider;+ private readonly IUIMultiLineTextInput _outputText = MultilineTextInput();++ [ImportingConstructor]+ public UUIDGeneratorGuidTool(ISettingsProvider settingsProvider)+ {+ _settingsProvider = settingsProvider;++ OnGenerateButtonClick();+ }++ public UIToolView View+ => new(+ isScrollable: true,+ Grid()+ .ColumnLargeSpacing()+ .RowLargeSpacing()+ .Rows(+ (GridRow.Settings, Auto),+ (GridRow.Results, new UIGridLength(1, UIGridUnitType.Fraction)))+ .Columns(+ (GridColumn.Stretch, new UIGridLength(1, UIGridUnitType.Fraction)))++ .Cells(+ Cell(+ GridRow.Settings,+ GridColumn.Stretch,+ Stack()+ .Vertical()+ .LargeSpacing()+ .WithChildren(++ Stack()+ .Vertical()+ .WithChildren(++ Label().Text(UUIDGenerator.ConfigurationTitle),++ Setting()+ .Icon("FluentSystemIcons", '\uF4E0')+ .Title(UUIDGenerator.Hyphens)+ .Handle(+ _settingsProvider,+ hyphens,+ OnSettingChanged),++ Setting()+ .Icon("FluentSystemIcons", '\uF7B2')+ .Title(UUIDGenerator.Uppercase)+ .Handle(+ _settingsProvider,+ uppercase,+ OnSettingChanged),++ Setting()+ .Icon("FluentSystemIcons", '\uF587')+ .Title(UUIDGenerator.VersionTitle)+ .Description(UUIDGenerator.VersionDescription)+ .Handle(+ _settingsProvider,+ version,+ OnSettingChanged,+ Item(UUIDGenerator.UuidVersionOne, UuidVersion.One),+ Item(UUIDGenerator.UuidVersionFour, UuidVersion.Four))),++ Stack()+ .Vertical()+ .WithChildren(++ Label().Text(UUIDGenerator.GenerateTitle),+ Stack()+ .Horizontal()+ .SmallSpacing()+ .WithChildren(++ Button()+ .AccentAppearance()+ .Text(UUIDGenerator.GenerateButton)+ .OnClick(OnGenerateButtonClick),++ Label().Style(UILabelStyle.BodyStrong).Text(UUIDGenerator.MultiplySymbol),++ NumberInput()+ .HideCommandBar()+ .Minimum(1)+ .Maximum(10000)+ .OnValueChanged(OnNumberOfUuidsToGenerateChanged)+ .Value(_settingsProvider.GetSetting(uuidToGenerate)))))),++ Cell(+ GridRow.Results,+ GridColumn.Stretch,++ _outputText+ .Title(UUIDGenerator.Output)+ .ReadOnly())));++ public void OnDataReceived(string dataTypeName, object? parsedData)+ {+ }++ private void OnSettingChanged(bool value)+ {+ OnGenerateButtonClick();+ }++ private void OnSettingChanged(UuidVersion value)+ {+ OnGenerateButtonClick();+ }++ private void OnNumberOfUuidsToGenerateChanged(double value)+ {+ _settingsProvider.SetSetting(uuidToGenerate, (int)value);+ OnGenerateButtonClick();+ }++ private void OnGenerateButtonClick()+ {+ var newGuids = new StringBuilder();++ for (int i = 0; i < Math.Max(_settingsProvider.GetSetting(uuidToGenerate), 1); i++)+ {+ string newUuid+ = UuidHelper.GenerateUuid(+ _settingsProvider.GetSetting(version),+ _settingsProvider.GetSetting(hyphens),+ _settingsProvider.GetSetting(uppercase));++ newGuids.AppendLine(newUuid);+ }++ _outputText.Text(newGuids.ToString());+ }+}diff --git a/src/app/tests/DevToys.UnitTests/Tools/Helpers/UuidHelperTests.cs b/src/app/tests/DevToys.UnitTests/Tools/Helpers/UuidHelperTests.csnew file mode 100644index 000000000..00e3ac57a--- /dev/null+++ b/src/app/tests/DevToys.UnitTests/Tools/Helpers/UuidHelperTests.cs@@ -0,0 +1,34 @@+using DevToys.Tools.Helpers;+using DevToys.Tools.Models;++namespace DevToys.UnitTests.Tools.Helpers;++public class UuidHelperTests+{+ [Theory]+ [InlineData(UuidVersion.One, true, true)]+ [InlineData(UuidVersion.One, false, true)]+ [InlineData(UuidVersion.One, true, false)]+ [InlineData(UuidVersion.One, false, false)]+ [InlineData(UuidVersion.Four, true, true)]+ [InlineData(UuidVersion.Four, false, true)]+ [InlineData(UuidVersion.Four, true, false)]+ [InlineData(UuidVersion.Four, false, false)]+ internal void GenerateUuid(UuidVersion uuidVersion, bool hyphens, bool uppercase)+ {+ string newUuid+ = UuidHelper.GenerateUuid(+ uuidVersion,+ hyphens,+ uppercase);+ TestUuid(newUuid, hyphens, uppercase);+ }++ private void TestUuid(string uuid, bool hyphens, bool uppercase)+ {+ Guid.TryParse(uuid, out _).Should().BeTrue();+ uuid.Contains("-").Should().Be(hyphens);+ uuid.Length.Should().Be(hyphens ? 36 : 32);+ uuid.Should().Be(uppercase ? uuid.ToUpperInvariant() : uuid.ToLowerInvariant());+ }+}