Skip to content

A Swiss Army knife for developers.

Implemented Back Button support

Etienne Baudoux18cde2b446b7
diff --git a/src/app/dev/DevToys.UI/ViewModels/MainWindowViewModel.cs b/src/app/dev/DevToys.UI/ViewModels/MainWindowViewModel.cs
index 21219d2c6..8c2323be4 100644
--- a/src/app/dev/DevToys.UI/ViewModels/MainWindowViewModel.cs
+++ b/src/app/dev/DevToys.UI/ViewModels/MainWindowViewModel.cs
@@ -13,8 +13,10 @@ namespace DevToys.UI.ViewModels;
internal sealed partial class MainWindowViewModel : ObservableRecipient
{
private readonly GuiToolProvider _guiToolProvider;
+ private readonly Stack<INotifyPropertyChanged> _navigationHistory = new();
private INotifyPropertyChanged? _selectedMenuItem;
+ private bool _isGoingBack;
[ImportingConstructor]
public MainWindowViewModel(GuiToolProvider guiToolProvider)
@@ -48,6 +50,13 @@ internal sealed partial class MainWindowViewModel : ObservableRecipient
get => _selectedMenuItem;
set
{
+ // Save the previous selected item.
+ if (!_isGoingBack && _selectedMenuItem is not null && value is not null && _selectedMenuItem != value)
+ {
+ _navigationHistory.Push(_selectedMenuItem);
+ OnPropertyChanged(nameof(CanGoBack));
+ }
+
if (value is null && _selectedMenuItem is not null)
{
// Somehow, the NavigationView end up with no selected item. An example of scenario where it can happen is when
@@ -72,10 +81,31 @@ internal sealed partial class MainWindowViewModel : ObservableRecipient
}
/// <summary>
- /// Indicates whether the <see cref="SelectedMenuItem"/> is a tool or not.
+ /// Gets whether the <see cref="SelectedMenuItem"/> is a tool or not.
/// </summary>
internal bool IsSelectedMenuItemATool => SelectedMenuItem is GuiToolViewItem;
+ /// <summary>
+ /// Gets whether the user can navigate back to the previous <see cref="SelectedMenuItem"/>.
+ /// </summary>
+ internal bool CanGoBack => _navigationHistory.Count > 0;
+
+ /// <summary>
+ /// Navigates back to the previous <see cref="SelectedMenuItem"/>.
+ /// </summary>
+ internal void GoBack()
+ {
+ Guard.IsTrue(CanGoBack);
+
+ _isGoingBack = true;
+ INotifyPropertyChanged previousItem = _navigationHistory.Pop();
+ OnPropertyChanged(nameof(CanGoBack));
+
+ SelectedMenuItem = GetBestMenuItemToSelect(previousItem);
+
+ _isGoingBack = false;
+ }
+
// Can't use CommunityToolkit.MVVM due to https://github.com/dotnet/roslyn/issues/57239#issuecomment-1437895948
/// <summary>
/// Gets or sets the search query typed by the user.
@@ -122,6 +152,11 @@ internal sealed partial class MainWindowViewModel : ObservableRecipient
Guard.IsNotEmpty((IReadOnlyList<INotifyPropertyChanged>)HeaderAndBodyToolViewItems);
Guard.IsNotNull(currentSelectedMenuItem);
+ if (currentSelectedMenuItem is GroupViewItem groupViewItem)
+ {
+ return groupViewItem;
+ }
+
GuiToolInstance? guiToolInstance = null;
if (currentSelectedMenuItem is GuiToolViewItem guiToolViewItem)
{
diff --git a/src/app/dev/DevToys.UI/Views/MainWindow.xaml b/src/app/dev/DevToys.UI/Views/MainWindow.xaml
index ada644ba2..14d9ae673 100644
--- a/src/app/dev/DevToys.UI/Views/MainWindow.xaml
+++ b/src/app/dev/DevToys.UI/Views/MainWindow.xaml
@@ -117,7 +117,7 @@
IsTitleBarAutoPaddingEnabled="False"
IsSettingsVisible="False"
IsBackButtonVisible="Visible"
- IsBackEnabled="True"
+ IsBackEnabled="{x:Bind ViewModel.CanGoBack, Mode=OneWay}"
CompactModeThresholdWidth="640"
ExpandedModeThresholdWidth="1008"
IsTabStop="False"
@@ -129,7 +129,8 @@
DisplayModeChanged="MenuNavigationView_DisplayModeChanged"
PaneClosing="MenuNavigationView_PaneClosing"
PaneOpening="MenuNavigationView_PaneOpening"
- Loaded="MenuNavigationView_Loaded">
+ Loaded="MenuNavigationView_Loaded"
+ BackRequested="MenuNavigationView_BackRequested">
<NavigationView.AutoSuggestBox>
<AutoSuggestBox
diff --git a/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs b/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs
index 591d3e5d5..eaeb38e9e 100644
--- a/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs
+++ b/src/app/dev/DevToys.UI/Views/MainWindow.xaml.cs
@@ -1,4 +1,5 @@
-using DevToys.Api;
+using System.Security.Cryptography;
+using DevToys.Api;
using DevToys.Api.Core.Theme;
using DevToys.Core.Tools;
using DevToys.Core.Tools.ViewItems;
@@ -141,6 +142,11 @@ public sealed partial class MainWindow : BackdropPage
UpdateVisualState();
}
+ private void MenuNavigationView_BackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args)
+ {
+ ViewModel.GoBack();
+ }
+
private void ViewModel_SelectedMenuItemChanged(object? sender, EventArgs e)
{
if (ViewModel.SelectedMenuItem is not null)