Skip to content

A Swiss Army knife for developers.

Added auto expansion of menu item group when navigating to a tool from a tool group page.

Etienne Baudouxc9cc7614057f
diff --git a/src/app/dev/DevToys.Core/Tools/GuiToolProvider.cs b/src/app/dev/DevToys.Core/Tools/GuiToolProvider.cs
index 59a8acb42..8c721f2a6 100644
--- a/src/app/dev/DevToys.Core/Tools/GuiToolProvider.cs
+++ b/src/app/dev/DevToys.Core/Tools/GuiToolProvider.cs
@@ -549,7 +549,7 @@ public sealed partial class GuiToolProvider
displayTitle: MainMenu.FavoriteToolsDisplayTitle,
accessibleName: MainMenu.FavoriteToolsAccessibleName,
children: new(favoriteTools),
- isExpandedByDefault: true);
+ menuItemShouldBeExpandedByDefault: true);
return _favoriteToolsGroupViewItem;
}
diff --git a/src/app/dev/DevToys.Core/Tools/ViewItems/GroupViewItem.cs b/src/app/dev/DevToys.Core/Tools/ViewItems/GroupViewItem.cs
index cd960e1d6..eb1830432 100644
--- a/src/app/dev/DevToys.Core/Tools/ViewItems/GroupViewItem.cs
+++ b/src/app/dev/DevToys.Core/Tools/ViewItems/GroupViewItem.cs
@@ -1,5 +1,6 @@
using System.Collections.ObjectModel;
using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Messaging;
using DevToys.Api;
namespace DevToys.Core.Tools.ViewItems;
@@ -10,6 +11,9 @@ namespace DevToys.Core.Tools.ViewItems;
[DebuggerDisplay($"DisplayTitle = {{{nameof(DisplayTitle)}}}")]
public sealed class GroupViewItem : ObservableObject
{
+ private TaskCompletionSource? _tcs;
+ private bool _childItemJustGotSelected;
+
internal GroupViewItem(
string internalName,
GuiToolGroup groupInfo,
@@ -31,7 +35,7 @@ public sealed class GroupViewItem : ObservableObject
string displayTitle,
string accessibleName,
ObservableCollection<GuiToolViewItem>? children = null,
- bool isExpandedByDefault = false)
+ bool menuItemShouldBeExpandedByDefault = false)
{
Guard.IsNotNullOrWhiteSpace(internalName);
Guard.IsNotNullOrWhiteSpace(iconFontName);
@@ -43,7 +47,12 @@ public sealed class GroupViewItem : ObservableObject
IconFontName = iconFontName;
IconGlyph = iconGlyph;
Children = children;
- IsExpandedByDefault = isExpandedByDefault;
+ MenuItemShouldBeExpandedByDefault = menuItemShouldBeExpandedByDefault;
+
+ if (children is not null)
+ {
+ children.CollectionChanged += Children_CollectionChanged;
+ }
}
/// <summary>
@@ -79,5 +88,75 @@ public sealed class GroupViewItem : ObservableObject
/// <summary>
/// Gets whether the group should be expanded by default.
/// </summary>
- public bool IsExpandedByDefault { get; }
+ public bool MenuItemShouldBeExpandedByDefault { get; }
+
+ /// <summary>
+ /// Gets whether the group should be expanded.
+ /// </summary>
+ public bool MenuItemShouldBeExpanded
+ {
+ get
+ {
+ if (Children is not null)
+ {
+ if (Children.Any(item => item.IsRecommended))
+ {
+ return true;
+ }
+ }
+
+ if (_childItemJustGotSelected)
+ {
+ Task.Delay(1000)
+ .ContinueWith(_ =>
+ {
+ _childItemJustGotSelected = false;
+ });
+ return true;
+ }
+
+ return false;
+ }
+ }
+
+ private void Children_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
+ {
+ if (e.OldItems is not null)
+ {
+ foreach (object? item in e.OldItems)
+ {
+ if (item is GuiToolViewItem guiToolViewItem)
+ {
+ guiToolViewItem.PropertyChanged -= Child_PropertyChanged;
+ guiToolViewItem.GotSelected -= Child_GotSelected;
+ }
+ }
+ }
+
+ if (e.NewItems is not null)
+ {
+ foreach (object? item in e.NewItems)
+ {
+ if (item is GuiToolViewItem guiToolViewItem)
+ {
+ guiToolViewItem.PropertyChanged += Child_PropertyChanged;
+ guiToolViewItem.GotSelected += Child_GotSelected;
+ }
+ }
+ }
+ }
+
+ private void Child_PropertyChanged(object? sender, PropertyChangedEventArgs e)
+ {
+ if (e.PropertyName == nameof(GuiToolViewItem.IsRecommended))
+ {
+ OnPropertyChanged(nameof(MenuItemShouldBeExpanded));
+ }
+ }
+
+ private void Child_GotSelected(object? sender, EventArgs e)
+ {
+ _childItemJustGotSelected = true;
+ OnPropertyChanged(nameof(MenuItemShouldBeExpanded));
+ }
}
diff --git a/src/app/dev/DevToys.Core/Tools/ViewItems/GuiToolViewItem.cs b/src/app/dev/DevToys.Core/Tools/ViewItems/GuiToolViewItem.cs
index af63bd4c6..374862d42 100644
--- a/src/app/dev/DevToys.Core/Tools/ViewItems/GuiToolViewItem.cs
+++ b/src/app/dev/DevToys.Core/Tools/ViewItems/GuiToolViewItem.cs
@@ -19,6 +19,11 @@ public sealed partial class GuiToolViewItem : ObservableObject
_showLongDisplayTitle = showLongDisplayTitle;
}
+ /// <summary>
+ /// Raised when the tool got selected in the menu.
+ /// </summary>
+ public event EventHandler? GotSelected;
+
/// <summary>
/// Gets or sets the list of spans that matched a search.
/// </summary>
@@ -48,4 +53,9 @@ public sealed partial class GuiToolViewItem : ObservableObject
return ToolInstance.LongDisplayTitle;
}
}
+
+ public void RaiseGotSelected()
+ {
+ GotSelected?.Invoke(this, EventArgs.Empty);
+ }
}
diff --git a/src/app/dev/DevToys.UI/ViewModels/MainWindowViewModel.cs b/src/app/dev/DevToys.UI/ViewModels/MainWindowViewModel.cs
index ee2a6e875..21219d2c6 100644
--- a/src/app/dev/DevToys.UI/ViewModels/MainWindowViewModel.cs
+++ b/src/app/dev/DevToys.UI/ViewModels/MainWindowViewModel.cs
@@ -61,44 +61,16 @@ internal sealed partial class MainWindowViewModel : ObservableRecipient
SetProperty(ref _selectedMenuItem, value);
}
- OnPropertyChanged(nameof(HeaderText));
- OnPropertyChanged(nameof(IsSelectedMenuItemSupportFavorite));
- OnPropertyChanged(nameof(IsSelectedMenuItemAFavoriteTool));
OnPropertyChanged(nameof(IsSelectedMenuItemATool));
SelectedMenuItemChanged?.Invoke(this, EventArgs.Empty);
- }
- }
- /// <summary>
- /// Gets the text to show in the header of the app according to the <see cref="SelectedMenuItem"/>.
- /// </summary>
- internal string? HeaderText
- {
- get
- {
- if (SelectedMenuItem is GuiToolViewItem guiToolViewItem)
+ if (value is GuiToolViewItem guiToolViewItem)
{
- return guiToolViewItem.ToolInstance.LongDisplayTitle;
+ guiToolViewItem.RaiseGotSelected();
}
- else if (SelectedMenuItem is GroupViewItem groupViewItem)
- {
- return groupViewItem.DisplayTitle;
- }
-
- return string.Empty;
}
}
- /// <summary>
- /// Indicates whether the <see cref="SelectedMenuItem"/> can be added or removed from favorites.
- /// </summary>
- internal bool IsSelectedMenuItemSupportFavorite => SelectedMenuItem is GuiToolViewItem guiToolViewItem && !guiToolViewItem.ToolInstance.NotFavorable;
-
- /// <summary>
- /// Indicates whether the <see cref="SelectedMenuItem"/> is a favorite tool or not.
- /// </summary>
- internal bool IsSelectedMenuItemAFavoriteTool => SelectedMenuItem is GuiToolViewItem guiToolViewItem && _guiToolProvider.GetToolIsFavorite(guiToolViewItem.ToolInstance);
-
/// <summary>
/// Indicates whether the <see cref="SelectedMenuItem"/> is a tool or not.
/// </summary>
@@ -145,19 +117,6 @@ internal sealed partial class MainWindowViewModel : ObservableRecipient
SelectedMenuItem = GetBestMenuItemToSelect(selectedSearchResultItem);
}
- /// <summary>
- /// Toggles the favorite status of the <see cref="SelectedMenuItem"/>.
- /// </summary>
- [RelayCommand]
- private void ToggleSelectedMenuItemFavorite()
- {
- if (SelectedMenuItem is GuiToolViewItem guiToolViewItem)
- {
- _guiToolProvider.SetToolIsFavorite(guiToolViewItem.ToolInstance, !_guiToolProvider.GetToolIsFavorite(guiToolViewItem.ToolInstance));
- OnPropertyChanged(nameof(IsSelectedMenuItemAFavoriteTool));
- }
- }
-
private INotifyPropertyChanged GetBestMenuItemToSelect(object currentSelectedMenuItem)
{
Guard.IsNotEmpty((IReadOnlyList<INotifyPropertyChanged>)HeaderAndBodyToolViewItems);
diff --git a/src/app/dev/DevToys.UI/ViewModels/ToolPageViewModel.cs b/src/app/dev/DevToys.UI/ViewModels/ToolPageViewModel.cs
index da3d58210..509c147bf 100644
--- a/src/app/dev/DevToys.UI/ViewModels/ToolPageViewModel.cs
+++ b/src/app/dev/DevToys.UI/ViewModels/ToolPageViewModel.cs
@@ -1,4 +1,6 @@
using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Input;
+using DevToys.Core.Tools;
using DevToys.Core.Tools.ViewItems;
namespace DevToys.UI.ViewModels;
@@ -7,13 +9,42 @@ namespace DevToys.UI.ViewModels;
[PartCreationPolicy(CreationPolicy.NonShared)]
internal sealed partial class ToolPageViewModel : ObservableRecipient
{
+#pragma warning disable IDE0044 // Add readonly modifier
+ [Import]
+ private GuiToolProvider _guiToolProvider = null!;
+#pragma warning restore IDE0044 // Add readonly modifier
+
+ private GuiToolViewItem? _guiToolViewItem;
+
[ObservableProperty]
private string _headerText = string.Empty;
+ /// <summary>
+ /// Indicates whether the <see cref="SelectedMenuItem"/> can be added or removed from favorites.
+ /// </summary>
+ internal bool IsSelectedMenuItemSupportFavorite => _guiToolViewItem is not null && !_guiToolViewItem.ToolInstance.NotFavorable;
+
+ /// <summary>
+ /// Indicates whether the <see cref="SelectedMenuItem"/> is a favorite tool or not.
+ /// </summary>
+ internal bool IsSelectedMenuItemAFavoriteTool => _guiToolViewItem is not null && _guiToolProvider.GetToolIsFavorite(_guiToolViewItem.ToolInstance);
+
internal void Load(GuiToolViewItem guiToolViewItem)
{
Guard.IsNotNull(guiToolViewItem);
+ _guiToolViewItem = guiToolViewItem;
HeaderText = guiToolViewItem.ToolInstance.LongDisplayTitle;
}
+
+ /// <summary>
+ /// Toggles the favorite status of the <see cref="SelectedMenuItem"/>.
+ /// </summary>
+ [RelayCommand]
+ private void ToggleSelectedMenuItemFavorite()
+ {
+ Guard.IsNotNull(_guiToolViewItem);
+ _guiToolProvider.SetToolIsFavorite(_guiToolViewItem.ToolInstance, !_guiToolProvider.GetToolIsFavorite(_guiToolViewItem.ToolInstance));
+ OnPropertyChanged(nameof(IsSelectedMenuItemAFavoriteTool));
+ }
}
diff --git a/src/app/dev/DevToys.UI/Views/MainWindow.xaml b/src/app/dev/DevToys.UI/Views/MainWindow.xaml
index 4acaa621b..ba241ff31 100644
--- a/src/app/dev/DevToys.UI/Views/MainWindow.xaml
+++ b/src/app/dev/DevToys.UI/Views/MainWindow.xaml
@@ -9,6 +9,7 @@
xmlns:macos="http://uno.ui/macos"
xmlns:not_win="http://uno.ui/not_win"
xmlns:wasm="http://uno.ui/wasm"
+ xmlns:local="using:DevToys.UI.Views"
xmlns:tools="using:DevToys.Core.Tools.ViewItems"
xmlns:dts="using:DevToys.UI.DataTemplateSelectors"
xmlns:strings="using:DevToys.Localization.Strings.MainWindow"
@@ -193,7 +194,7 @@
<NavigationViewItem
AutomationProperties.Name="{x:Bind AccessibleName}"
MenuItemsSource="{x:Bind Children}"
- IsExpanded="{x:Bind IsExpandedByDefault, Mode=OneWay}">
+ IsExpanded="{x:Bind local:MainWindow.UpdateMenuItemShouldBeExpanded(MenuItemShouldBeExpanded), Mode=OneWay}">
<NavigationViewItem.Icon>
<FontIcon
FontFamily="{x:Bind IconFontName, Converter={StaticResource StringToStaticResourceConverter}}"
diff --git a/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs b/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs
index 25f14a0fd..86266afda 100644
--- a/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs
+++ b/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs
@@ -1,6 +1,5 @@
using DevToys.Api;
using DevToys.Api.Core.Theme;
-using DevToys.Core.Tools;
using DevToys.Core.Tools.ViewItems;
using DevToys.UI.Framework.Controls;
using DevToys.UI.Models;
@@ -22,6 +21,8 @@ public sealed partial class MainWindow : BackdropPage
private const string NavigationViewCompactStateName = "NavigationViewCompact";
private const string NavigationViewMinimalStateName = "NavigationViewMinimal";
+ private static MainWindow? MainWindowInstance;
+
private readonly IMefProvider _mefProvider;
private NavigationViewDisplayMode _navigationViewDisplayMode;
@@ -32,6 +33,8 @@ public sealed partial class MainWindow : BackdropPage
Guard.IsNotNull(mefProvider);
_mefProvider = mefProvider;
+ MainWindowInstance = this;
+
InitializeComponent();
WindowStartupLocation = WindowStartupLocation.CenterScreen;
Resize(1250, 800);
@@ -83,7 +86,7 @@ public sealed partial class MainWindow : BackdropPage
// So we manually expand the items that should be expanded on load.
foreach (INotifyPropertyChanged? item in ViewModel.HeaderAndBodyToolViewItems)
{
- if (item is GroupViewItem groupViewItem && groupViewItem.IsExpandedByDefault)
+ if (item is GroupViewItem groupViewItem && groupViewItem.MenuItemShouldBeExpandedByDefault)
{
var menuItem = MenuNavigationView.ContainerFromMenuItem(item) as NavigationViewItem;
if (menuItem is not null)
@@ -211,4 +214,10 @@ public sealed partial class MainWindow : BackdropPage
visualStateListener.SetVisualState(visualStateName);
}
}
+
+ private static bool UpdateMenuItemShouldBeExpanded(bool menuItemShouldBeExpanded)
+ {
+ Guard.IsNotNull(MainWindowInstance);
+ return menuItemShouldBeExpanded && MainWindowInstance._navigationViewDisplayMode == NavigationViewDisplayMode.Expanded;
+ }
}
diff --git a/src/app/dev/DevToys.UI/Views/ToolPage.xaml b/src/app/dev/DevToys.UI/Views/ToolPage.xaml
index dc6e3a216..edad32148 100644
--- a/src/app/dev/DevToys.UI/Views/ToolPage.xaml
+++ b/src/app/dev/DevToys.UI/Views/ToolPage.xaml
@@ -7,12 +7,17 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:collections="using:CommunityToolkit.Mvvm.Collections"
+ xmlns:converters="using:DevToys.UI.Framework.Converters"
xmlns:ix="using:Microsoft.Xaml.Interactions.Core"
xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:tools="using:DevToys.Core.Tools"
xmlns:strings="using:DevToys.Localization.Strings.MainWindow"
mc:Ignorable="d">
-
+ <Page.Resources>
+ <converters:BooleanToStringConverter x:Key="BooleanToFavoriteText" StringIfTrue="{x:Bind strings:MainWindow.RemoveFromFavorites}" StringIfFalse="{x:Bind strings:MainWindow.AddToFavorites}"/>
+ <converters:BooleanToStringConverter x:Key="BooleanToFavoriteIconGlyph" StringIfTrue="&#xF71A;" StringIfFalse="&#xF70F;"/>
+ </Page.Resources>
+
<Grid>
<Grid
x:Name="ToolHeader"
@@ -27,19 +32,27 @@
Text="{x:Bind ViewModel.HeaderText, Mode=OneWay}"
TextTrimming="CharacterEllipsis"
FontSize="24"/>
- <win:Border
+ <Border
Grid.Column="1">
- <win:CommandBar
+ <CommandBar
IsOpen="False"
DefaultLabelPosition="Right">
+ <AppBarButton
+ Command="{x:Bind ViewModel.ToggleSelectedMenuItemFavoriteCommand}"
+ Visibility="{x:Bind ViewModel.IsSelectedMenuItemSupportFavorite, Mode=OneWay, Converter={StaticResource BooleanToVisibilityConverter}}"
+ Label="{x:Bind ViewModel.IsSelectedMenuItemAFavoriteTool, Mode=OneWay, Converter={StaticResource BooleanToFavoriteText}}">
+ <AppBarButton.Icon>
+ <FontIcon Glyph="{x:Bind ViewModel.IsSelectedMenuItemAFavoriteTool, Mode=OneWay, Converter={StaticResource BooleanToFavoriteIconGlyph}}"/>
+ </AppBarButton.Icon>
+ </AppBarButton>
<win:AppBarButton
Label="{x:Bind strings:MainWindow.PinToStart}">
<win:AppBarButton.Icon>
<win:FontIcon Glyph="&#xF602;"/>
</win:AppBarButton.Icon>
</win:AppBarButton>
- </win:CommandBar>
- </win:Border>
+ </CommandBar>
+ </Border>
</Grid>
</Grid>
</Page>