Skip to content

A Swiss Army knife for developers.

Improved Smart Detection behavior (#28)

Etienne Baudoux669c35b193d9
diff --git a/src/dev/impl/DevToys/Core/Settings/PredefinedSettings.cs b/src/dev/impl/DevToys/Core/Settings/PredefinedSettings.cs
index 8a0f80257..50e9eb463 100644
--- a/src/dev/impl/DevToys/Core/Settings/PredefinedSettings.cs
+++ b/src/dev/impl/DevToys/Core/Settings/PredefinedSettings.cs
@@ -53,6 +53,15 @@ namespace DevToys.Core.Settings
isRoaming: true,
defaultValue: true);
+ /// <summary>
+ /// Whether the application should automatically paste the content of the clipboard when selecting a tool that has been automatically recommended by Smart Detection.
+ /// </summary>
+ public static readonly SettingDefinition<bool> SmartDetectionPaste
+ = new(
+ name: nameof(SmartDetectionPaste),
+ isRoaming: true,
+ defaultValue: true);
+
public static readonly string[] SupportedFonts
= new[]
{
diff --git a/src/dev/impl/DevToys/LanguageManager.cs b/src/dev/impl/DevToys/LanguageManager.cs
index cf152f221..4d5cfc677 100644
--- a/src/dev/impl/DevToys/LanguageManager.cs
+++ b/src/dev/impl/DevToys/LanguageManager.cs
@@ -656,6 +656,16 @@ namespace DevToys
/// </summary>
public string SmartDetectionDescription => _resources.GetString("SmartDetectionDescription");
+ /// <summary>
+ /// Gets the resource SmartDetectionHowTo.
+ /// </summary>
+ public string SmartDetectionHowTo => _resources.GetString("SmartDetectionHowTo");
+
+ /// <summary>
+ /// Gets the resource SmartDetectionPaste.
+ /// </summary>
+ public string SmartDetectionPaste => _resources.GetString("SmartDetectionPaste");
+
/// <summary>
/// Gets the resource SourceCode.
/// </summary>
diff --git a/src/dev/impl/DevToys/Strings/en/Settings.resw b/src/dev/impl/DevToys/Strings/en/Settings.resw
index a09cc5886..5768c6fb5 100644
--- a/src/dev/impl/DevToys/Strings/en/Settings.resw
+++ b/src/dev/impl/DevToys/Strings/en/Settings.resw
@@ -195,6 +195,12 @@
<data name="SmartDetectionDescription" xml:space="preserve">
<value>Automatically detect the best tool based on the clipboard content</value>
</data>
+ <data name="SmartDetectionHowTo" xml:space="preserve">
+ <value>How to use Smart Detection</value>
+ </data>
+ <data name="SmartDetectionPaste" xml:space="preserve">
+ <value>Automatically paste the clipboard content when selecting a recommended tool</value>
+ </data>
<data name="SourceCode" xml:space="preserve">
<value>Source code</value>
</data>
diff --git a/src/dev/impl/DevToys/Strings/fr/Settings.resw b/src/dev/impl/DevToys/Strings/fr/Settings.resw
index aa39b8801..d87e59c22 100644
--- a/src/dev/impl/DevToys/Strings/fr/Settings.resw
+++ b/src/dev/impl/DevToys/Strings/fr/Settings.resw
@@ -195,6 +195,12 @@
<data name="SmartDetectionDescription" xml:space="preserve">
<value>Détecter automatiquement le meilleur outil en fonction du contenu du presse-papiers</value>
</data>
+ <data name="SmartDetectionHowTo" xml:space="preserve">
+ <value>Comment utiliser la Détection intelligente</value>
+ </data>
+ <data name="SmartDetectionPaste" xml:space="preserve">
+ <value>Coller automatiquement le contenu du presse-papier lorsqu'un outil recommandé est sélectionné.</value>
+ </data>
<data name="SourceCode" xml:space="preserve">
<value>Code source</value>
</data>
diff --git a/src/dev/impl/DevToys/ViewModels/MainPageViewModel.cs b/src/dev/impl/DevToys/ViewModels/MainPageViewModel.cs
index 301a21933..374ecfbaa 100644
--- a/src/dev/impl/DevToys/ViewModels/MainPageViewModel.cs
+++ b/src/dev/impl/DevToys/ViewModels/MainPageViewModel.cs
@@ -48,6 +48,8 @@ namespace DevToys.ViewModels
private NavigationViewDisplayMode _navigationViewDisplayMode;
private bool _isNavigationViewPaneOpened;
private string? _searchQuery;
+ private string? _clipboardContent;
+ private bool _pasteInFirstSelectedToolIsAllowed;
private bool _isInCompactOverlayMode;
private bool _isUpdatingSelectedItem;
private bool _allowSelectAutomaticallyRecommendedTool = true;
@@ -72,7 +74,7 @@ namespace DevToys.ViewModels
internal MatchedToolProviderViewData? SelectedMenuItem
{
get => _selectedItem;
- set => SetSelectedMenuItem(value, null);
+ set => SetSelectedMenuItem(value, _clipboardContent);
}
/// <summary>
@@ -296,7 +298,7 @@ namespace DevToys.ViewModels
ThreadPriority.Low,
() =>
{
- SelectedMenuItem = toolProviderViewDataToSelect ?? ToolsMenuItems.FirstOrDefault() ?? FooterMenuItems.FirstOrDefault();
+ SetSelectedMenuItem(toolProviderViewDataToSelect ?? ToolsMenuItems.FirstOrDefault() ?? FooterMenuItems.FirstOrDefault(), null);
});
}
@@ -313,6 +315,16 @@ namespace DevToys.ViewModels
{
_selectedItem = value;
IToolViewModel toolViewModel = _toolProviderFactory.GetToolViewModel(_selectedItem.ToolProvider);
+
+ if (!_pasteInFirstSelectedToolIsAllowed // If this is not the first tool we select since the last time tools have been recommended
+ || !_selectedItem.IsRecommended // or that the selected tool isn't recommended
+ || !_settingsProvider.GetSetting(PredefinedSettings.SmartDetectionPaste)) // or that the user doesn't want to paste automatically in recommended tools
+ {
+ clipboardContentData = null;
+ }
+
+ _pasteInFirstSelectedToolIsAllowed = false;
+
Messenger.Send(new NavigateToToolMessage(toolViewModel, clipboardContentData));
OnPropertyChanged(nameof(SelectedMenuItem));
OnPropertyChanged(nameof(HeaderText));
@@ -381,7 +393,7 @@ namespace DevToys.ViewModels
{
var oldSelectedItem = SelectedMenuItem;
ToolsMenuItems.Clear();
- SelectedMenuItem = null;
+ SetSelectedMenuItem(null, null);
foreach (var item in newToolsMenuitems)
{
@@ -394,7 +406,7 @@ namespace DevToys.ViewModels
ToolsMenuItems.Update(newToolsMenuitems.Keys);
footerMenuItems.ForEach(item => FooterMenuItems.Add(item));
- SelectedMenuItem = oldSelectedItem;
+ SetSelectedMenuItem(oldSelectedItem, null);
});
}
catch (Exception ex)
@@ -414,7 +426,13 @@ namespace DevToys.ViewModels
await TaskScheduler.Default;
// Retrieve the clipboard content.
- string clipboardContent = await _clipboard.GetClipboardContentAsTextAsync().ConfigureAwait(false);
+ string? clipboardContent = await _clipboard.GetClipboardContentAsTextAsync().ConfigureAwait(false);
+
+ if (string.Equals(clipboardContent, _clipboardContent))
+ {
+ // The clipboard didn't change. Do no compute recommended tools again.
+ return;
+ }
// Make sure the menu items exist.
await _firstUpdateMenuTask.Value.ConfigureAwait(false);
@@ -442,16 +460,28 @@ namespace DevToys.ViewModels
await Task.WhenAll(tasks).ConfigureAwait(false);
- MatchedToolProviderViewData[] recommendedTools
+ MatchedToolProviderViewData[] newRecommendedTools
= _allToolsMenuitems
.Where(item => item.IsRecommended && !item.Metadata.IsFooterItem)
.ToArray();
+ _clipboardContent = clipboardContent;
+ if (oldRecommendedTools.SequenceEqual(newRecommendedTools))
+ {
+ // The detected recommended tools is the same than before. Let's make sure we won't
+ // paste automatically.
+ _pasteInFirstSelectedToolIsAllowed = false;
+ return;
+ }
+ else
+ {
+ _pasteInFirstSelectedToolIsAllowed = true;
+ }
+
using (await _sempahore.WaitAsync(CancellationToken.None).ConfigureAwait(false))
{
- if (recommendedTools.Length == 1
- && ToolsMenuItems.Contains(recommendedTools[0])
- && (oldRecommendedTools.Length != 1 || oldRecommendedTools[0] != recommendedTools[0]))
+ if (newRecommendedTools.Length == 1
+ && ToolsMenuItems.Contains(newRecommendedTools[0]))
{
// One unique tool is recommended.
// The recommended tool is displayed in the top menu.
@@ -463,7 +493,7 @@ namespace DevToys.ViewModels
{
if (!IsInCompactOverlayMode && _allowSelectAutomaticallyRecommendedTool)
{
- SetSelectedMenuItem(recommendedTools[0], clipboardContent);
+ SetSelectedMenuItem(newRecommendedTools[0], _clipboardContent);
}
});
}
diff --git a/src/dev/impl/DevToys/ViewModels/Tools/Settings/SettingsToolViewModel.cs b/src/dev/impl/DevToys/ViewModels/Tools/Settings/SettingsToolViewModel.cs
index 9400abc1b..6368e544a 100644
--- a/src/dev/impl/DevToys/ViewModels/Tools/Settings/SettingsToolViewModel.cs
+++ b/src/dev/impl/DevToys/ViewModels/Tools/Settings/SettingsToolViewModel.cs
@@ -55,6 +55,12 @@ namespace DevToys.ViewModels.Settings
set => _settingsProvider.SetSetting(PredefinedSettings.SmartDetection, value);
}
+ internal bool SmartDetectionPaste
+ {
+ get => _settingsProvider.GetSetting(PredefinedSettings.SmartDetectionPaste);
+ set => _settingsProvider.SetSetting(PredefinedSettings.SmartDetectionPaste, value);
+ }
+
internal ObservableCollection<string> SupportedFonts { get; } = new();
internal string TextEditorFont
diff --git a/src/dev/impl/DevToys/Views/Tools/Settings/SettingsToolPage.xaml b/src/dev/impl/DevToys/Views/Tools/Settings/SettingsToolPage.xaml
index f30d638dd..5160d842b 100644
--- a/src/dev/impl/DevToys/Views/Tools/Settings/SettingsToolPage.xaml
+++ b/src/dev/impl/DevToys/Views/Tools/Settings/SettingsToolPage.xaml
@@ -64,7 +64,15 @@
</controls:ExpandableSettingControl.Icon>
<ToggleSwitch Style="{StaticResource RightAlignedToggleSwitchStyle}" IsOn="{x:Bind ViewModel.SmartDetection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<controls:ExpandableSettingControl.ExpandableContent>
- <Image Source="/Assets/DemoSmartDetection.gif" MinHeight="300"/>
+ <StackPanel Spacing="4" HorizontalAlignment="Stretch">
+ <CheckBox
+ IsChecked="{x:Bind ViewModel.SmartDetection, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+ Content="{x:Bind ViewModel.Strings.SmartDetectionPaste}"
+ HorizontalAlignment="Stretch"/>
+
+ <TextBlock Text="{x:Bind ViewModel.Strings.SmartDetectionHowTo}"/>
+ <Image Source="/Assets/DemoSmartDetection.gif" MinHeight="300"/>
+ </StackPanel>
</controls:ExpandableSettingControl.ExpandableContent>
</controls:ExpandableSettingControl>