Skip to content

A Swiss Army knife for developers.

Added smart detection support in text input controls (#782)

Etienne BAUDOUXd2c0dfd70d6a
diff --git a/src/app/dev/DevToys.Business/ViewModels/MainWindowViewModel.cs b/src/app/dev/DevToys.Business/ViewModels/MainWindowViewModel.cs
index fda8b1d69..35a8d8c8e 100644
--- a/src/app/dev/DevToys.Business/ViewModels/MainWindowViewModel.cs
+++ b/src/app/dev/DevToys.Business/ViewModels/MainWindowViewModel.cs
@@ -5,6 +5,7 @@ using CommunityToolkit.Mvvm.Messaging;
using DevToys.Api;
using DevToys.Api.Core;
using DevToys.Business.Models;
+using DevToys.Core.Models;
using DevToys.Core.Tools;
using DevToys.Core.Tools.ViewItems;
using DevToys.Localization.Strings.MainWindow;
@@ -88,10 +89,20 @@ internal sealed partial class MainWindowViewModel : ObservableRecipient
SetProperty(ref _selectedMenuItem, value);
}
+ var guiToolViewItem = value as GuiToolViewItem;
+ if (guiToolViewItem is not null)
+ {
+ _smartDetectionService.ActiveToolInstance = guiToolViewItem.ToolInstance;
+ }
+ else
+ {
+ _smartDetectionService.ActiveToolInstance = null;
+ }
+
OnPropertyChanged(nameof(IsSelectedMenuItemATool));
SelectedMenuItemChanged?.Invoke(this, EventArgs.Empty);
- if (value is GuiToolViewItem guiToolViewItem)
+ if (guiToolViewItem is not null)
{
guiToolViewItem.RaiseGotSelected();
}
diff --git a/src/app/dev/DevToys.Business/ViewModels/ToolGroupPageViewModel.cs b/src/app/dev/DevToys.Business/ViewModels/ToolGroupPageViewModel.cs
index 9bcf8eb73..0935d0a28 100644
--- a/src/app/dev/DevToys.Business/ViewModels/ToolGroupPageViewModel.cs
+++ b/src/app/dev/DevToys.Business/ViewModels/ToolGroupPageViewModel.cs
@@ -2,11 +2,11 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
+using DevToys.Core.Models;
using DevToys.Core.Tools;
using DevToys.Core.Tools.Metadata;
using DevToys.Core.Tools.ViewItems;
using DevToys.Localization.Strings.MainWindow;
-using DevToys.Business.Models;
namespace DevToys.Business.ViewModels;
diff --git a/src/app/dev/DevToys.Business/Models/ChangeSelectedMenuItemMessage.cs b/src/app/dev/DevToys.Core/Models/ChangeSelectedMenuItemMessage.cs
similarity index 50%
rename from src/app/dev/DevToys.Business/Models/ChangeSelectedMenuItemMessage.cs
rename to src/app/dev/DevToys.Core/Models/ChangeSelectedMenuItemMessage.cs
index c519b2c0a..58f5e6992 100644
--- a/src/app/dev/DevToys.Business/Models/ChangeSelectedMenuItemMessage.cs
+++ b/src/app/dev/DevToys.Core/Models/ChangeSelectedMenuItemMessage.cs
@@ -1,23 +1,23 @@
using CommunityToolkit.Mvvm.Messaging.Messages;
using DevToys.Core.Tools;
-namespace DevToys.Business.Models;
+namespace DevToys.Core.Models;
-internal sealed class ChangeSelectedMenuItemMessage : ValueChangedMessage<GuiToolInstance>
+public sealed class ChangeSelectedMenuItemMessage : ValueChangedMessage<GuiToolInstance>
{
- internal ChangeSelectedMenuItemMessage(GuiToolInstance tool)
+ public ChangeSelectedMenuItemMessage(GuiToolInstance tool)
: base(tool)
{
Guard.IsNotNull(tool);
SmartDetectionInfo = null;
}
- internal ChangeSelectedMenuItemMessage(SmartDetectedTool smartDetectionInfo)
+ public ChangeSelectedMenuItemMessage(SmartDetectedTool smartDetectionInfo)
: base(smartDetectionInfo.ToolInstance)
{
Guard.IsNotNull(smartDetectionInfo);
SmartDetectionInfo = smartDetectionInfo;
}
- internal SmartDetectedTool? SmartDetectionInfo { get; }
+ public SmartDetectedTool? SmartDetectionInfo { get; }
}
diff --git a/src/app/dev/DevToys.Core/Tools/SmartDetectionService.cs b/src/app/dev/DevToys.Core/Tools/SmartDetectionService.cs
index 8e3b637d0..f1e73dc6b 100644
--- a/src/app/dev/DevToys.Core/Tools/SmartDetectionService.cs
+++ b/src/app/dev/DevToys.Core/Tools/SmartDetectionService.cs
@@ -1,6 +1,7 @@
using System;
using DevToys.Api;
using DevToys.Core.Tools.Metadata;
+using DevToys.Core.Tools.ViewItems;
using Microsoft.Extensions.Logging;
using Uno.Extensions;
@@ -12,7 +13,6 @@ public sealed partial class SmartDetectionService
private readonly ILogger _logger;
private readonly IReadOnlyList<DetectorNode> _detectorHierarchy;
private readonly Dictionary<string, List<GuiToolInstance>> _dataTypeToToolInstanceMap;
- private readonly GuiToolProvider _guiToolProvider;
[ImportingConstructor]
public SmartDetectionService(
@@ -21,7 +21,6 @@ public sealed partial class SmartDetectionService
GuiToolProvider guiToolProvider)
{
_logger = this.Log();
- _guiToolProvider = guiToolProvider;
// Build a hiearchy of detectors based on their indicated base data type name.
_detectorHierarchy = BuildDetectorNodeHierarchy(dataTypeDetectors);
@@ -30,6 +29,11 @@ public sealed partial class SmartDetectionService
_dataTypeToToolInstanceMap = BuildDataTypeToToolInstanceMap(guiToolProvider);
}
+ /// <summary>
+ /// Presumbly the active tool in the app.
+ /// </summary>
+ public GuiToolInstance? ActiveToolInstance { private get; set; }
+
/// <summary>
/// Detects the best tools that could be used with the given <paramref name="rawData"/>.
/// </summary>
@@ -54,6 +58,8 @@ public sealed partial class SmartDetectionService
// Make sure to be off the UI thread as this method may take many seconds to run.
await TaskSchedulerAwaiter.SwitchOffMainThreadAsync(CancellationToken.None);
+ GuiToolInstance? activeToolInstance = ActiveToolInstance;
+
IReadOnlyList<(DetectorNode, DataDetectionResult)> succeededDetectors = await DetectAsync(cancellationToken, _detectorHierarchy, rawData);
for (int i = 0; i < succeededDetectors.Count; i++)
{
@@ -66,6 +72,11 @@ public sealed partial class SmartDetectionService
{
for (int j = 0; j < toolList.Count; j++)
{
+ if (!strict && activeToolInstance is not null && string.Equals(toolList[j].InternalComponentName, activeToolInstance.InternalComponentName, StringComparison.Ordinal))
+ {
+ // When not strict, ignore the tool if it's the active one.
+ continue;
+ }
cancellationToken.ThrowIfCancellationRequested();
detectedTools.Insert(0, new(toolList[j], detectorNode.Detector.Metadata.DataTypeName, dataDetectionResult.Data));
}
@@ -78,6 +89,11 @@ public sealed partial class SmartDetectionService
{
for (int j = 0; j < toolList.Count; j++)
{
+ if (!strict && activeToolInstance is not null && string.Equals(toolList[j].InternalComponentName, activeToolInstance.InternalComponentName, StringComparison.Ordinal))
+ {
+ // When not strict, ignore the tool if it's the active one.
+ continue;
+ }
cancellationToken.ThrowIfCancellationRequested();
detectedTools.Add(new(toolList[j], detectorNode.Detector.Metadata.DataTypeBaseName, dataDetectionResult.Data));
}
@@ -265,6 +281,6 @@ public sealed partial class SmartDetectionService
/// <summary>
/// Gets the list of child detectors for this node.
/// </summary>
- internal List<DetectorNode>? ChildrenDetectors { get; set; }
+ internal List<DetectorNode>? ChildrenDetectors { get; set; }
}
}
diff --git a/src/app/dev/DevToys.Localization/Strings/ToolPage/ToolPage.Designer.cs b/src/app/dev/DevToys.Localization/Strings/ToolPage/ToolPage.Designer.cs
index 6698b2748..be5dde8f9 100644
--- a/src/app/dev/DevToys.Localization/Strings/ToolPage/ToolPage.Designer.cs
+++ b/src/app/dev/DevToys.Localization/Strings/ToolPage/ToolPage.Designer.cs
@@ -96,6 +96,15 @@ namespace DevToys.Localization.Strings.ToolPage {
}
}
+ /// <summary>
+ /// Looks up a localized string similar to No tool to suggest.
+ /// </summary>
+ public static string NoSmartDetectedTool {
+ get {
+ return ResourceManager.GetString("NoSmartDetectedTool", resourceCulture);
+ }
+ }
+
/// <summary>
/// Looks up a localized string similar to OK.
/// </summary>
diff --git a/src/app/dev/DevToys.Localization/Strings/ToolPage/ToolPage.resx b/src/app/dev/DevToys.Localization/Strings/ToolPage/ToolPage.resx
index 49858b759..bd45cc4e3 100644
--- a/src/app/dev/DevToys.Localization/Strings/ToolPage/ToolPage.resx
+++ b/src/app/dev/DevToys.Localization/Strings/ToolPage/ToolPage.resx
@@ -129,6 +129,9 @@
<data name="Copy" xml:space="preserve">
<value>Copy</value>
</data>
+ <data name="NoSmartDetectedTool" xml:space="preserve">
+ <value>No tool to suggest</value>
+ </data>
<data name="Ok" xml:space="preserve">
<value>OK</value>
</data>
diff --git a/src/app/dev/DevToys.Tools/Tools/Sample/Sample.Designer.cs b/src/app/dev/DevToys.Tools/Tools/Sample/Sample.Designer.cs
index f1c1ce35c..41b32efe2 100644
--- a/src/app/dev/DevToys.Tools/Tools/Sample/Sample.Designer.cs
+++ b/src/app/dev/DevToys.Tools/Tools/Sample/Sample.Designer.cs
@@ -61,7 +61,7 @@ namespace DevToys.Tools.Tools.Sample {
}
/// <summary>
- /// Looks up a localized string similar to Base64 Image Encoder / Decoder.
+ /// Looks up a localized string similar to Just A Basic Tool.
/// </summary>
internal static string CommandDescription {
get {
@@ -88,7 +88,7 @@ namespace DevToys.Tools.Tools.Sample {
}
/// <summary>
- /// Looks up a localized string similar to HTML Encoder / Decoder.
+ /// Looks up a localized string similar to JSON Formatter.
/// </summary>
internal static string LongTitle {
get {
diff --git a/src/app/dev/DevToys.Tools/Tools/Sample/Sample.resx b/src/app/dev/DevToys.Tools/Tools/Sample/Sample.resx
index ace243e7d..4a456879f 100644
--- a/src/app/dev/DevToys.Tools/Tools/Sample/Sample.resx
+++ b/src/app/dev/DevToys.Tools/Tools/Sample/Sample.resx
@@ -118,7 +118,7 @@
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="CommandDescription" xml:space="preserve">
- <value>Base64 Image Encoder / Decoder</value>
+ <value>Just A Basic Tool</value>
</data>
<data name="Description" xml:space="preserve">
<value>Encode or decode all the applicable characters to their corresponding HTML entities</value>
@@ -127,7 +127,7 @@
<value>File to treat</value>
</data>
<data name="LongTitle" xml:space="preserve">
- <value>HTML Encoder / Decoder</value>
+ <value>JSON Formatter</value>
</data>
<data name="Utf8OptionDescription" xml:space="preserve">
<value>A dummy UTF8 option description</value>
diff --git a/src/app/dev/DevToys.UI.Framework/Assets/Icons/Idea-disabled.svg b/src/app/dev/DevToys.UI.Framework/Assets/Icons/Idea-disabled.svg
new file mode 100644
index 000000000..2d63a957e
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Assets/Icons/Idea-disabled.svg
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<svg
+ viewBox="0 0 48 48"
+ version="1.1"
+ id="svg36"
+ sodipodi:docname="Idea-disabled.svg"
+ inkscape:version="1.2 (dc2aedaf03, 2022-05-15)"
+ xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
+ xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
+ xmlns="http://www.w3.org/2000/svg"
+ xmlns:svg="http://www.w3.org/2000/svg">
+ <defs
+ id="defs40" />
+ <sodipodi:namedview
+ id="namedview38"
+ pagecolor="#ffffff"
+ bordercolor="#666666"
+ borderopacity="1.0"
+ inkscape:showpageshadow="2"
+ inkscape:pageopacity="0.0"
+ inkscape:pagecheckerboard="0"
+ inkscape:deskcolor="#d1d1d1"
+ showgrid="false"
+ inkscape:zoom="17.270833"
+ inkscape:cx="24.028951"
+ inkscape:cy="24.028951"
+ inkscape:window-width="1920"
+ inkscape:window-height="1009"
+ inkscape:window-x="-8"
+ inkscape:window-y="-8"
+ inkscape:window-maximized="1"
+ inkscape:current-layer="svg36" />
+ <radialGradient
+ id="4Var25g6pDrxGe_QIeTVza"
+ cx="16.106"
+ cy="-171.073"
+ r="23.43"
+ gradientTransform="matrix(1 0 0 -1 0 118)"
+ gradientUnits="userSpaceOnUse">
+ <stop
+ offset="0"
+ stop-color="#6e757b"
+ id="stop22" />
+ <stop
+ offset="1"
+ stop-color="#4a4e52"
+ id="stop24" />
+ </radialGradient>
+ <path
+ fill="url(#4Var25g6pDrxGe_QIeTVza)"
+ d="M30,34H18c0,0.855,0,3.95,0,3.95c0,3.314,2.686,6,6,6s6-2.686,6-6C30,37.95,30,34.855,30,34z"
+ id="path27" />
+ <linearGradient
+ id="4Var25g6pDrxGe_QIeTVzb"
+ x1="16.365"
+ x2="28.493"
+ y1="2.837"
+ y2="26.454"
+ gradientUnits="userSpaceOnUse">
+ <stop
+ offset="0"
+ stop-color="#ffda1c"
+ id="stop29" />
+ <stop
+ offset="1"
+ stop-color="#feb705"
+ id="stop31" />
+ </linearGradient>
+ <path
+ fill="url(#4Var25g6pDrxGe_QIeTVzb)"
+ d="M36,16c0-6.627-5.373-12-12-12S12,9.373,12,16c0,7,6,12,6,15.8V34h12v-2.2C30,28,36,23,36,16z"
+ id="path34"
+ style="fill:#b3b3b3" />
+</svg>
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UITextInputHeader.xaml b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UITextInputHeader.xaml
index a0a5c437f..7357e6521 100644
--- a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UITextInputHeader.xaml
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UITextInputHeader.xaml
@@ -18,6 +18,7 @@
<UserControl.Resources>
<converters:NegateBooleanConverter x:Key="NegateBooleanConverter"/>
<converters:NullToBooleanConverter x:Key="StringNullOrWhiteSpaceToFalseConverter" IsInverted="True" EnforceNonWhiteSpaceString="True"/>
+ <converters:BooleanToImageSourceConverter x:Key="IdeaIconConverter" SourceIfTrue="ms-appx:///DevToys.UI.Framework/Assets/Icons/Idea.svg" SourceIfFalse="ms-appx:///DevToys.UI.Framework/Assets/Icons/Idea-disabled.svg"/>
<Style x:Name="SeparatorStyle" TargetType="Rectangle">
<Setter Property="Margin" Value="2,0,2,0"/>
@@ -134,28 +135,12 @@
AutomationProperties.Name="{str:StringsExtension Key=SendToSmartDetection}">
<DropDownButton.Content>
<ImageIcon
- Source="ms-appx:///DevToys.UI.Framework/Assets/Icons/Idea.svg"
+ Source="{x:Bind ToolsDetectedBySmartDetection, Mode=OneWay, Converter={StaticResource IdeaIconConverter}}"
Height="22"
- Width="22" />
+ Width="22"/>
</DropDownButton.Content>
<DropDownButton.Flyout>
- <MenuFlyout Placement="Bottom">
- <MenuFlyoutItem Text="TODO">
- <MenuFlyoutItem.Icon>
- <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE725;"/>
- </MenuFlyoutItem.Icon>
- </MenuFlyoutItem>
- <MenuFlyoutItem Text="TODO">
- <MenuFlyoutItem.Icon>
- <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE8CA;"/>
- </MenuFlyoutItem.Icon>
- </MenuFlyoutItem>
- <MenuFlyoutItem Text="TODO">
- <MenuFlyoutItem.Icon>
- <FontIcon FontFamily="Segoe MDL2 Assets" Glyph="&#xE8C2;"/>
- </MenuFlyoutItem.Icon>
- </MenuFlyoutItem>
- </MenuFlyout>
+ <MenuFlyout x:Name="SmartDetectionMenuFlyout" Placement="Bottom"/>
</DropDownButton.Flyout>
</DropDownButton>
</StackPanel>
diff --git a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UITextInputHeader.xaml.cs b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UITextInputHeader.xaml.cs
index a7db26721..07b7c7320 100644
--- a/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UITextInputHeader.xaml.cs
+++ b/src/app/dev/DevToys.UI.Framework/Controls/GuiTool/UITextInputHeader.xaml.cs
@@ -1,5 +1,9 @@
-using DevToys.Api;
+using CommunityToolkit.Mvvm.Messaging;
+using DevToys.Api;
+using DevToys.Core.Models;
+using DevToys.Core.Tools;
using DevToys.Localization.Strings.ToolPage;
+using DevToys.UI.Framework.Converters;
using DevToys.UI.Framework.Helpers;
using DevToys.UI.Framework.Threading;
using Microsoft.Extensions.Logging;
@@ -7,6 +11,7 @@ using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Markup;
+using Microsoft.UI.Xaml.Media;
using Uno.Extensions;
using Windows.ApplicationModel.DataTransfer;
using Windows.Storage;
@@ -19,12 +24,18 @@ namespace DevToys.UI.Framework.Controls.GuiTool;
public sealed partial class UITextInputHeader : UserControl
{
private readonly ILogger _logger;
+ private readonly SmartDetectionService _smartDetectionService;
+ private readonly DisposableSemaphore _disposableSemaphore = new();
+ private readonly StringToStaticResourceConverter _stringToStaticResourceConverter = new();
+
+ private CancellationTokenSource? _cancellationTokenSource;
public UITextInputHeader()
{
this.InitializeComponent();
_logger = this.Log();
+ _smartDetectionService = Parts.MefProvider.Import<SmartDetectionService>();
Loaded += UITextInputHeader_Loaded;
Unloaded += UITextInputHeader_Unloaded;
@@ -128,6 +139,19 @@ public sealed partial class UITextInputHeader : UserControl
set => SetValue(IsLoadingFileProperty, value);
}
+ public static readonly DependencyProperty ToolsDetectedBySmartDetectionProperty
+ = DependencyProperty.Register(
+ nameof(ToolsDetectedBySmartDetection),
+ typeof(bool),
+ typeof(UITextInputHeader),
+ new PropertyMetadata(false));
+
+ public bool ToolsDetectedBySmartDetection
+ {
+ get => (bool)GetValue(ToolsDetectedBySmartDetectionProperty);
+ set => SetValue(ToolsDetectedBySmartDetectionProperty, value);
+ }
+
private void UITextInputHeader_Loaded(object sender, RoutedEventArgs e)
{
UITextInput.TitleChanged += UITextInput_TitleChanged;
@@ -135,12 +159,15 @@ public sealed partial class UITextInputHeader : UserControl
UITextInput.CanCopyWhenEditableChanged += UITextInput_CanCopyWhenEditableChanged;
UITextInput.IsEnabledChanged += UITextInput_IsEnabledChanged;
UITextInput.IsVisibleChanged += UITextInput_IsVisibleChanged;
+ UITextInput.TextChanged += UITextInput_TextChanged;
Title = UITextInput.Title;
IsReadOnly = UITextInput.IsReadOnly;
CanCopyWhenEditable = UITextInput.CanCopyWhenEditable;
IsEnabled = UITextInput.IsEnabled;
Visibility = UITextInput.IsVisible ? Visibility.Visible : Visibility.Collapsed;
IsReadOnlyOrCanCopyWhenEditable = IsReadOnly || CanCopyWhenEditable;
+
+ TriggerSmartDetection();
}
private void UITextInputHeader_Unloaded(object sender, RoutedEventArgs e)
@@ -150,8 +177,14 @@ public sealed partial class UITextInputHeader : UserControl
UITextInput.CanCopyWhenEditableChanged -= UITextInput_CanCopyWhenEditableChanged;
UITextInput.IsEnabledChanged -= UITextInput_IsEnabledChanged;
UITextInput.IsVisibleChanged -= UITextInput_IsVisibleChanged;
+ UITextInput.TextChanged -= UITextInput_TextChanged;
Loaded -= UITextInputHeader_Loaded;
Unloaded -= UITextInputHeader_Unloaded;
+
+ CancellationTokenSource? cancellationSourceToken = _cancellationTokenSource;
+ cancellationSourceToken?.Cancel();
+ cancellationSourceToken?.Dispose();
+ _disposableSemaphore.Dispose();
}
private void UITextInput_TitleChanged(object? sender, EventArgs e)
@@ -181,6 +214,11 @@ public sealed partial class UITextInputHeader : UserControl
Visibility = UITextInput.IsVisible ? Visibility.Visible : Visibility.Collapsed;
}
+ private void UITextInput_TextChanged(object? sender, EventArgs e)
+ {
+ TriggerSmartDetection();
+ }
+
private void Root_SizeChanged(object sender, SizeChangedEventArgs e)
{
if (e.NewSize.Width == e.PreviousSize.Width)
@@ -388,6 +426,14 @@ public sealed partial class UITextInputHeader : UserControl
deferral?.Complete();
}
+ private void SmartDetectionMenuItem_Click(object sender, RoutedEventArgs e)
+ {
+ if (e.OriginalSource is MenuFlyoutItem menuItem && menuItem.Tag is SmartDetectedTool detectedTool)
+ {
+ WeakReferenceMessenger.Default.Send(new ChangeSelectedMenuItemMessage(detectedTool));
+ }
+ }
+
private async Task LoadFileAsync(StorageFile file)
{
IsLoadingFile = true;
@@ -434,6 +480,79 @@ public sealed partial class UITextInputHeader : UserControl
}
}
+ private void TriggerSmartDetection()
+ {
+ if (IsLoaded)
+ {
+ CancellationTokenSource? cancellationSourceToken = _cancellationTokenSource;
+ cancellationSourceToken?.Cancel();
+ cancellationSourceToken?.Dispose();
+ Interlocked.Exchange(ref _cancellationTokenSource, new(TimeSpan.FromSeconds(5)));
+ SmartDetectToolsAsync(UITextInput.Text, _cancellationTokenSource.Token).ForgetSafely();
+ }
+ }
+
+ private async Task SmartDetectToolsAsync(string text, CancellationToken cancellationToken)
+ {
+ await TaskSchedulerAwaiter.SwitchOffMainThreadAsync(cancellationToken);
+
+ IReadOnlyList<SmartDetectedTool> detectedTools;
+ using (await _disposableSemaphore.WaitAsync(cancellationToken))
+ {
+ // Detect tools to recommend.
+ detectedTools
+ = await _smartDetectionService.DetectAsync(text, strict: false, cancellationToken)
+ .ConfigureAwait(true);
+
+ await DispatcherQueue.RunOnUIThreadAsync(() =>
+ {
+ if (!cancellationToken.IsCancellationRequested)
+ {
+ for (int i = 0; i < SmartDetectionMenuFlyout.Items.Count; i++)
+ {
+ if (SmartDetectionMenuFlyout.Items[i] is MenuFlyoutItem menuItem)
+ {
+ menuItem.Click -= SmartDetectionMenuItem_Click;
+ }
+ }
+
+ SmartDetectionMenuFlyout.Items.Clear();
+
+ if (detectedTools.Count > 0)
+ {
+ ToolsDetectedBySmartDetection = true;
+
+ for (int i = 0; i < detectedTools.Count; i++)
+ {
+ SmartDetectedTool detectedTool = detectedTools[i];
+ var menuItem = new MenuFlyoutItem
+ {
+ Icon = new FontIcon
+ {
+ FontFamily = _stringToStaticResourceConverter.Convert(detectedTool.ToolInstance.IconFontName, typeof(FontFamily), null!, null!) as FontFamily,
+ Glyph = detectedTool.ToolInstance.IconGlyph
+ },
+ Text = detectedTool.ToolInstance.LongOrShortDisplayTitle,
+ Tag = detectedTool
+ };
+
+ menuItem.Click += SmartDetectionMenuItem_Click;
+
+ SmartDetectionMenuFlyout.Items.Add(menuItem);
+ }
+ }
+ else
+ {
+ ToolsDetectedBySmartDetection = false;
+
+ var noToolDetectedMenuItem = new MenuFlyoutItem { Text = ToolPage.NoSmartDetectedTool };
+ SmartDetectionMenuFlyout.Items.Add(noToolDetectedMenuItem);
+ }
+ }
+ });
+ }
+ }
+
[LoggerMessage(1, LogLevel.Information, "Loading a file into a text input control. The file size is {fileSize} byte(s).")]
partial void LogOpeningFile(long fileSize);
diff --git a/src/app/dev/DevToys.UI.Framework/Converters/BooleanToImageSourceConverter.cs b/src/app/dev/DevToys.UI.Framework/Converters/BooleanToImageSourceConverter.cs
new file mode 100644
index 000000000..b7e233eca
--- /dev/null
+++ b/src/app/dev/DevToys.UI.Framework/Converters/BooleanToImageSourceConverter.cs
@@ -0,0 +1,44 @@
+using Microsoft.UI.Xaml;
+using Microsoft.UI.Xaml.Data;
+using Microsoft.UI.Xaml.Media;
+
+namespace DevToys.UI.Framework.Converters;
+
+/// <summary>
+/// Convert a <see cref="bool"/> to a <see cref="ImageSource"/> value.
+/// </summary>
+public sealed class BooleanToImageSourceConverter : IValueConverter
+{
+ /// <summary>
+ /// Gets or sets the <see cref="ImageSource"/> when the input is true.
+ /// </summary>
+ public ImageSource? SourceIfTrue { get; set; }
+
+ /// <summary>
+ /// Gets or sets the <see cref="ImageSource"/> when the input is false.
+ /// </summary>
+ public ImageSource? SourceIfFalse { get; set; }
+
+ public object? Convert(object value, Type targetType, object parameter, string language)
+ {
+ bool? valueBool = value as bool?;
+ if (valueBool == null)
+ {
+ return DependencyProperty.UnsetValue;
+ }
+
+ if (valueBool.Value)
+ {
+ return SourceIfTrue;
+ }
+ else
+ {
+ return SourceIfFalse;
+ }
+ }
+
+ public object ConvertBack(object value, Type targetType, object parameter, string language)
+ {
+ throw new NotImplementedException();
+ }
+}
diff --git a/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj b/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj
index 3066377b1..9c6255aab 100644
--- a/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj
+++ b/src/app/dev/DevToys.UI.Framework/DevToys.UI.Framework.csproj
@@ -95,6 +95,7 @@
<ItemGroup>
<ProjectReference Include="..\DevToys.Api\DevToys.Api.csproj" />
+ <ProjectReference Include="..\DevToys.Core\DevToys.Core.csproj" />
<ProjectReference Include="..\DevToys.Localization\DevToys.Localization.csproj" />
</ItemGroup>
diff --git a/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs b/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs
index 23b08299b..998114342 100644
--- a/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs
+++ b/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs
@@ -4,7 +4,6 @@ using DevToys.Business.Models;
using DevToys.Business.ViewModels;
using DevToys.Core.Tools;
using DevToys.Core.Tools.ViewItems;
-using DevToys.Localization.Strings.MainWindow;
using DevToys.UI.Framework.Controls;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;