Skip to content

Commit b769a7f

Browse files
committed
Add Back Button
1 parent cfa509f commit b769a7f

File tree

9 files changed

+68
-13
lines changed

9 files changed

+68
-13
lines changed

src/HandyWinget/Assets/ISettings.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ public class ISettings : NotifiyingJsonSettings, IVersionable
2828
public virtual Brush Accent { get; set; }
2929
public virtual bool AutoRefreshInStartup { get; set; } = false;
3030
public virtual bool IsStoreDataGridColumnWidth { get; set; } = false;
31+
public virtual bool IsBackEnabled { get; set; } = true;
3132
public virtual Version Version { get; set; }
3233

3334
private ObservableCollection<DataGridLength> _DataGridColumnWidth = new ObservableCollection<DataGridLength>();

src/HandyWinget/MainWindow.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@
7979
</ui:CommandBar>
8080
<ui:NavigationView Name="navView" SelectionChanged="NavigationView_SelectionChanged" PaneTitle="HandyWinGet" AlwaysShowHeader="True"
8181
Header="{Binding SelectedItem.Content, RelativeSource={RelativeSource Mode=Self}}" IsSettingsVisible="False"
82-
IsBackButtonVisible="Collapsed">
82+
IsBackButtonVisible="Visible" BackRequested="navView_BackRequested" IsBackEnabled="True">
8383
<ui:NavigationView.MenuItems>
8484
<ui:NavigationViewItem Tag="General" Content="General">
8585
<ui:NavigationViewItem.Icon>
@@ -98,7 +98,7 @@
9898
</ui:NavigationViewItem>
9999

100100
</ui:NavigationView.MenuItems>
101-
<ui:Frame x:Name="contentFrame"/>
101+
<ui:Frame x:Name="contentFrame" Navigated="contentFrame_Navigated" Navigating="contentFrame_Navigating"/>
102102
<ui:NavigationView.PaneFooter>
103103
<StackPanel>
104104
<ui:NavigationViewItem Name="nvOpenTerminal" Content="Open Terminal" MouseLeftButtonDown="nvOpenTerminal_MouseLeftButtonDown">

src/HandyWinget/MainWindow.xaml.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
using HandyWinget.Views;
55
using ModernWpf.Controls;
66
using ModernWpf.Controls.Primitives;
7+
using System.Linq;
78
using System.Windows;
89
using System.Windows.Input;
910
using System.Windows.Media;
11+
using System.Windows.Navigation;
1012
using static HandyWinget.Assets.Helper;
1113
namespace HandyWinget
1214
{
@@ -31,6 +33,7 @@ private void LoadSettings()
3133
}
3234

3335
navView.PaneDisplayMode = Settings.PaneDisplayMode;
36+
navView.IsBackButtonVisible = Settings.IsBackEnabled ? NavigationViewBackButtonVisible.Visible : NavigationViewBackButtonVisible.Collapsed;
3437
}
3538

3639
public void CommandButtonsVisibility(Visibility visibility)
@@ -181,5 +184,43 @@ private void nvOpenTerminal_MouseLeftButtonDown(object sender, MouseButtonEventA
181184
{
182185
OpenFlyout("TerminalCommandBar", nvOpenTerminal);
183186
}
187+
188+
private void navView_BackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args)
189+
{
190+
if (contentFrame.CanGoBack)
191+
{
192+
contentFrame.GoBack();
193+
}
194+
}
195+
196+
private void contentFrame_Navigated(object sender, NavigationEventArgs e)
197+
{
198+
var pageName = contentFrame.Content.GetType().Name;
199+
var menuItem = navView.MenuItems
200+
.OfType<NavigationViewItem>()
201+
.Where(item => item.Tag.ToString() == pageName)
202+
.FirstOrDefault();
203+
if (menuItem != null)
204+
{
205+
navView.SelectedItem = menuItem;
206+
}
207+
208+
if (contentFrame.CanGoBack)
209+
{
210+
navView.IsBackEnabled = true;
211+
}
212+
else
213+
{
214+
navView.IsBackEnabled = false;
215+
}
216+
}
217+
218+
private void contentFrame_Navigating(object sender, NavigatingCancelEventArgs e)
219+
{
220+
if (e.NavigationMode == NavigationMode.Back)
221+
{
222+
contentFrame.RemoveBackEntry();
223+
}
224+
}
184225
}
185226
}

src/HandyWinget/Views/CreatePackage.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
<UserControl x:Class="HandyWinget.Views.CreatePackage"
1+
<ui:Page x:Class="HandyWinget.Views.CreatePackage"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:hc="https://handyorg.github.io/handycontrol"
5+
KeepAlive="True"
56
xmlns:ui="http://schemas.modernwpf.com/2019">
67
<hc:ScrollViewer>
78
<StackPanel>
@@ -60,4 +61,4 @@
6061
</StackPanel>
6162
</StackPanel>
6263
</hc:ScrollViewer>
63-
</UserControl>
64+
</ui:Page>

src/HandyWinget/Views/CreatePackage.xaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace HandyWinget.Views
2020
/// <summary>
2121
/// Interaction logic for CreatePackage.xaml
2222
/// </summary>
23-
public partial class CreatePackage : UserControl, INotifyPropertyChanged
23+
public partial class CreatePackage : ModernWpf.Controls.Page, INotifyPropertyChanged
2424
{
2525
#region INotify
2626
public event PropertyChangedEventHandler PropertyChanged;

src/HandyWinget/Views/General.xaml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
<UserControl x:Class="HandyWinget.Views.General"
1+
<ui:Page x:Class="HandyWinget.Views.General"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:hc="https://handyorg.github.io/handycontrol"
44
xmlns:ui="http://schemas.modernwpf.com/2019"
55
xmlns:enum="clr-namespace:HandyWinget.Assets"
6+
KeepAlive="True"
67
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
78
<hc:ScrollViewer>
89
<Grid>
@@ -30,7 +31,8 @@
3031
<ToggleButton x:Name="tgAutoRefresh" Style="{StaticResource ToggleButtonSwitch}" HorizontalAlignment="Left" Margin="10,0" Content="Auto Refresh Packages When Application Startup" Checked="tgAutoRefresh_Checked" Unchecked="tgAutoRefresh_Checked" />
3132

3233
<ToggleButton x:Name="tgSaveDGColumnWidth" Style="{StaticResource ToggleButtonSwitch}" HorizontalAlignment="Left" Margin="10" Content="Save and Restore DataGrid Columns Width" Checked="tgSaveDGColumnWidth_Checked" Unchecked="tgSaveDGColumnWidth_Checked" />
33-
34+
35+
<ToggleButton x:Name="tgIsBackEnabled" Style="{StaticResource ToggleButtonSwitch}" HorizontalAlignment="Left" Margin="10" Content="Show Back Button for Navigation" Checked="tgIsBackEnabled_Checked" Unchecked="tgIsBackEnabled_Checked" />
3436

3537
<Button Click="ResetAccent_Click" Style="{StaticResource DefaultButtonStyle}" Content="Reset Accent Color" Margin="0,20"/>
3638
</StackPanel>
@@ -69,4 +71,4 @@
6971
</Grid>
7072
</hc:ScrollViewer>
7173

72-
</UserControl>
74+
</ui:Page>

src/HandyWinget/Views/General.xaml.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace HandyWinget.Views
1212
/// <summary>
1313
/// Interaction logic for General
1414
/// </summary>
15-
public partial class General : UserControl
15+
public partial class General : ModernWpf.Controls.Page
1616
{
1717
string Version = string.Empty;
1818
public General()
@@ -184,5 +184,15 @@ private void tgSaveDGColumnWidth_Checked(object sender, RoutedEventArgs e)
184184
Settings.IsStoreDataGridColumnWidth = state;
185185
}
186186
}
187+
188+
private void tgIsBackEnabled_Checked(object sender, RoutedEventArgs e)
189+
{
190+
var state = tgIsBackEnabled.IsChecked.Value;
191+
if (state != Settings.IsBackEnabled)
192+
{
193+
Settings.IsBackEnabled = state;
194+
MainWindow.Instance.navView.IsBackButtonVisible = state ? NavigationViewBackButtonVisible.Visible : NavigationViewBackButtonVisible.Collapsed;
195+
}
196+
}
187197
}
188198
}

src/HandyWinget/Views/Packages.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
<UserControl x:Class="HandyWinget.Views.Packages"
1+
<ui:Page x:Class="HandyWinget.Views.Packages"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
44
xmlns:hc="https://handyorg.github.io/handycontrol"
55
xmlns:ui="http://schemas.modernwpf.com/2019"
6+
KeepAlive="True"
67
KeyDown="UserControl_KeyDown">
78
<hc:ToggleBlock Name="tgBlock" VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch">
89
<hc:ToggleBlock.UnCheckedContent>
@@ -161,4 +162,4 @@
161162
</Grid>
162163
</hc:ToggleBlock.CheckedContent>
163164
</hc:ToggleBlock>
164-
</UserControl>
165+
</ui:Page>

src/HandyWinget/Views/Packages.xaml.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
using static HandyWinget.Assets.Helper;
2626
namespace HandyWinget.Views
2727
{
28-
public partial class Packages : UserControl
28+
public partial class Packages : ModernWpf.Controls.Page
2929
{
3030
internal static Packages Instance;
3131

@@ -59,7 +59,6 @@ public Packages()
5959
BindingOperations.EnableCollectionSynchronization(DataList, Lock);
6060
BindingOperations.EnableCollectionSynchronization(_temoList, Lock);
6161
BindingOperations.EnableCollectionSynchronization(_tempVersions, Lock);
62-
6362
DownloadManifests();
6463
SetDataListGrouping();
6564
}

0 commit comments

Comments
 (0)