Skip to content

Commit 8ce4bd9

Browse files
committed
Fix page number issue when navigating with keyboard
1 parent c68a764 commit 8ce4bd9

File tree

9 files changed

+130
-45
lines changed

9 files changed

+130
-45
lines changed

Flow.Launcher/App.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ public App()
6464
.AddSingleton<IPublicAPI, PublicAPIInstance>()
6565
.AddSingleton<MainViewModel>()
6666
.AddSingleton<Theme>()
67+
.AddSingleton<WelcomeViewModel>()
6768
).Build();
6869
Ioc.Default.ConfigureServices(host.Services);
6970
}

Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Flow.Launcher.Infrastructure.UserSettings;
44
using Flow.Launcher.Core.Resource;
55
using CommunityToolkit.Mvvm.DependencyInjection;
6+
using Flow.Launcher.ViewModel;
67

78
namespace Flow.Launcher.Resources.Pages
89
{
@@ -11,6 +12,9 @@ public partial class WelcomePage1
1112
protected override void OnNavigatedTo(NavigationEventArgs e)
1213
{
1314
Settings = Ioc.Default.GetRequiredService<Settings>();
15+
// Sometimes the navigation is not triggered by button click,
16+
// so we need to reset the page number
17+
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 1;
1418
InitializeComponent();
1519
}
1620
private Internationalization _translater => InternationalizationManager.Instance;

Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public partial class WelcomePage2
1616
protected override void OnNavigatedTo(NavigationEventArgs e)
1717
{
1818
Settings = Ioc.Default.GetRequiredService<Settings>();
19+
// Sometimes the navigation is not triggered by button click,
20+
// so we need to reset the page number
21+
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 2;
1922
InitializeComponent();
2023
}
2124

Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Windows.Navigation;
22
using CommunityToolkit.Mvvm.DependencyInjection;
33
using Flow.Launcher.Infrastructure.UserSettings;
4+
using Flow.Launcher.ViewModel;
45

56
namespace Flow.Launcher.Resources.Pages
67
{
@@ -9,6 +10,9 @@ public partial class WelcomePage3
910
protected override void OnNavigatedTo(NavigationEventArgs e)
1011
{
1112
Settings = Ioc.Default.GetRequiredService<Settings>();
13+
// Sometimes the navigation is not triggered by button click,
14+
// so we need to reset the page number
15+
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 3;
1216
InitializeComponent();
1317
}
1418

Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using CommunityToolkit.Mvvm.DependencyInjection;
22
using Flow.Launcher.Infrastructure.UserSettings;
3+
using Flow.Launcher.ViewModel;
34
using System.Windows.Navigation;
45

56
namespace Flow.Launcher.Resources.Pages
@@ -9,6 +10,9 @@ public partial class WelcomePage4
910
protected override void OnNavigatedTo(NavigationEventArgs e)
1011
{
1112
Settings = Ioc.Default.GetRequiredService<Settings>();
13+
// Sometimes the navigation is not triggered by button click,
14+
// so we need to reset the page number
15+
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 4;
1216
InitializeComponent();
1317
}
1418

Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using Microsoft.Win32;
55
using Flow.Launcher.Infrastructure;
66
using CommunityToolkit.Mvvm.DependencyInjection;
7+
using Flow.Launcher.ViewModel;
78

89
namespace Flow.Launcher.Resources.Pages
910
{
@@ -16,6 +17,9 @@ public partial class WelcomePage5
1617
protected override void OnNavigatedTo(NavigationEventArgs e)
1718
{
1819
Settings = Ioc.Default.GetRequiredService<Settings>();
20+
// Sometimes the navigation is not triggered by button click,
21+
// so we need to reset the page number
22+
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 5;
1923
InitializeComponent();
2024
}
2125

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
using Flow.Launcher.Plugin;
2+
3+
namespace Flow.Launcher.ViewModel
4+
{
5+
public partial class WelcomeViewModel : BaseModel
6+
{
7+
public const int MaxPageNum = 5;
8+
9+
public string PageDisplay => $"{PageNum}/5";
10+
11+
private int _pageNum = 1;
12+
public int PageNum
13+
{
14+
get => _pageNum;
15+
set
16+
{
17+
if (_pageNum != value)
18+
{
19+
_pageNum = value;
20+
OnPropertyChanged();
21+
UpdateView();
22+
}
23+
}
24+
}
25+
26+
private bool _backEnabled = false;
27+
public bool BackEnabled
28+
{
29+
get => _backEnabled;
30+
set
31+
{
32+
_backEnabled = value;
33+
OnPropertyChanged();
34+
}
35+
}
36+
37+
private bool _nextEnabled = true;
38+
public bool NextEnabled
39+
{
40+
get => _nextEnabled;
41+
set
42+
{
43+
_nextEnabled = value;
44+
OnPropertyChanged();
45+
}
46+
}
47+
48+
private void UpdateView()
49+
{
50+
OnPropertyChanged(nameof(PageDisplay));
51+
if (PageNum == 1)
52+
{
53+
BackEnabled = false;
54+
NextEnabled = true;
55+
}
56+
else if (PageNum == MaxPageNum)
57+
{
58+
BackEnabled = true;
59+
NextEnabled = false;
60+
}
61+
else
62+
{
63+
BackEnabled = true;
64+
NextEnabled = true;
65+
}
66+
}
67+
}
68+
}

Flow.Launcher/WelcomeWindow.xaml

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
xmlns:local="clr-namespace:Flow.Launcher"
77
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
88
xmlns:ui="http://schemas.modernwpf.com/2019"
9+
xmlns:vm="clr-namespace:Flow.Launcher.ViewModel"
910
Name="FlowWelcomeWindow"
1011
Title="{DynamicResource Welcome_Page1_Title}"
1112
Width="550"
@@ -14,8 +15,10 @@
1415
MinHeight="650"
1516
MaxWidth="550"
1617
MaxHeight="650"
18+
d:DataContext="{d:DesignInstance Type=vm:WelcomeViewModel}"
1719
Activated="OnActivated"
1820
Background="{DynamicResource Color00B}"
21+
Closed="Window_Closed"
1922
Foreground="{DynamicResource PopupTextColor}"
2023
MouseDown="window_MouseDown"
2124
WindowStartupLocation="CenterScreen"
@@ -41,12 +44,12 @@
4144
Grid.Column="0"
4245
Width="16"
4346
Height="16"
44-
Margin="10,4,4,4"
47+
Margin="10 4 4 4"
4548
RenderOptions.BitmapScalingMode="HighQuality"
4649
Source="/Images/app.png" />
4750
<TextBlock
4851
Grid.Column="1"
49-
Margin="4,0,0,0"
52+
Margin="4 0 0 0"
5053
VerticalAlignment="Center"
5154
FontSize="12"
5255
Foreground="{DynamicResource Color05B}"
@@ -95,7 +98,7 @@
9598
Grid.Row="1"
9699
Background="{DynamicResource Color00B}"
97100
BorderBrush="{DynamicResource PopupButtonAreaBorderColor}"
98-
BorderThickness="0,1,0,0">
101+
BorderThickness="0 1 0 0">
99102
<Grid>
100103
<Grid.ColumnDefinitions>
101104
<ColumnDefinition Width="130" />
@@ -109,11 +112,11 @@
109112
VerticalAlignment="Center">
110113
<TextBlock
111114
Name="PageNavigation"
112-
Margin="0,2,0,0"
115+
Margin="0 2 0 0"
113116
HorizontalAlignment="Center"
114117
VerticalAlignment="Center"
115118
FontSize="14"
116-
Text="{Binding PageDisplay, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
119+
Text="{Binding PageDisplay, Mode=OneWay}"
117120
TextAlignment="Center" />
118121
</StackPanel>
119122

@@ -122,25 +125,26 @@
122125
Grid.Column="0"
123126
Width="100"
124127
Height="40"
125-
Margin="20,5,0,5"
128+
Margin="20 5 0 5"
126129
Click="BtnCancel_OnClick"
127130
Content="{DynamicResource Skip}"
128131
DockPanel.Dock="Right"
129132
FontSize="14" />
130133
<DockPanel
131134
Grid.Column="2"
132-
Margin="0,0,20,0"
135+
Margin="0 0 20 0"
133136
VerticalAlignment="Stretch">
134137
<Button
135138
x:Name="NextButton"
136139
Width="40"
137140
Height="40"
138-
Margin="8,5,0,5"
141+
Margin="8 5 0 5"
139142
Click="ForwardButton_Click"
140143
Content="&#xe76c;"
141144
DockPanel.Dock="Right"
142145
FontFamily="/Resources/#Segoe Fluent Icons"
143-
FontSize="18" />
146+
FontSize="18"
147+
IsEnabled="{Binding NextEnabled, Mode=OneWay}" />
144148
<Button
145149
x:Name="BackButton"
146150
Width="40"
@@ -149,7 +153,8 @@
149153
Content="&#xe76b;"
150154
DockPanel.Dock="Right"
151155
FontFamily="/Resources/#Segoe Fluent Icons"
152-
FontSize="18" />
156+
FontSize="18"
157+
IsEnabled="{Binding BackEnabled, Mode=OneWay}" />
153158
<StackPanel />
154159
</DockPanel>
155160

Flow.Launcher/WelcomeWindow.xaml.cs

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,55 @@
44
using System.Windows.Controls;
55
using Flow.Launcher.Resources.Pages;
66
using ModernWpf.Media.Animation;
7+
using CommunityToolkit.Mvvm.DependencyInjection;
8+
using Flow.Launcher.ViewModel;
9+
using Flow.Launcher.Infrastructure.UserSettings;
710

811
namespace Flow.Launcher
912
{
1013
public partial class WelcomeWindow : Window
1114
{
12-
public WelcomeWindow()
13-
{
14-
InitializeComponent();
15-
BackButton.IsEnabled = false;
16-
}
15+
private readonly WelcomeViewModel _viewModel;
1716

18-
private NavigationTransitionInfo _transitionInfo = new SlideNavigationTransitionInfo()
17+
private readonly NavigationTransitionInfo _forwardTransitionInfo = new SlideNavigationTransitionInfo()
1918
{
2019
Effect = SlideNavigationTransitionEffect.FromRight
2120
};
22-
private NavigationTransitionInfo _backTransitionInfo = new SlideNavigationTransitionInfo()
21+
private readonly NavigationTransitionInfo _backTransitionInfo = new SlideNavigationTransitionInfo()
2322
{
2423
Effect = SlideNavigationTransitionEffect.FromLeft
2524
};
2625

27-
private int pageNum = 1;
28-
private int MaxPage = 5;
29-
public string PageDisplay => $"{pageNum}/5";
26+
public WelcomeWindow()
27+
{
28+
_viewModel = Ioc.Default.GetRequiredService<WelcomeViewModel>();
29+
DataContext = _viewModel;
30+
InitializeComponent();
31+
}
3032

31-
private void UpdateView()
33+
private void ForwardButton_Click(object sender, RoutedEventArgs e)
3234
{
33-
PageNavigation.Text = PageDisplay;
34-
if (pageNum == 1)
35+
if (_viewModel.PageNum < WelcomeViewModel.MaxPageNum)
3536
{
36-
BackButton.IsEnabled = false;
37-
NextButton.IsEnabled = true;
38-
}
39-
else if (pageNum == MaxPage)
40-
{
41-
BackButton.IsEnabled = true;
42-
NextButton.IsEnabled = false;
37+
_viewModel.PageNum++;
38+
ContentFrame.Navigate(PageTypeSelector(_viewModel.PageNum), null, _forwardTransitionInfo);
4339
}
4440
else
4541
{
46-
BackButton.IsEnabled = true;
47-
NextButton.IsEnabled = true;
42+
_viewModel.NextEnabled = false;
4843
}
4944
}
5045

51-
private void ForwardButton_Click(object sender, RoutedEventArgs e)
52-
{
53-
pageNum++;
54-
UpdateView();
55-
56-
ContentFrame.Navigate(PageTypeSelector(pageNum), null, _transitionInfo);
57-
}
58-
5946
private void BackwardButton_Click(object sender, RoutedEventArgs e)
6047
{
61-
if (pageNum > 1)
48+
if (_viewModel.PageNum > 1)
6249
{
63-
pageNum--;
64-
UpdateView();
65-
ContentFrame.Navigate(PageTypeSelector(pageNum), null, _backTransitionInfo);
50+
_viewModel.PageNum--;
51+
ContentFrame.Navigate(PageTypeSelector(_viewModel.PageNum), null, _backTransitionInfo);
6652
}
6753
else
6854
{
69-
BackButton.IsEnabled = false;
55+
_viewModel.BackEnabled = false;
7056
}
7157
}
7258

@@ -107,5 +93,11 @@ private void ContentFrame_Loaded(object sender, RoutedEventArgs e)
10793
{
10894
ContentFrame.Navigate(PageTypeSelector(1)); /* Set First Page */
10995
}
96+
97+
private void Window_Closed(object sender, EventArgs e)
98+
{
99+
// Save settings when window is closed
100+
Ioc.Default.GetRequiredService<Settings>().Save();
101+
}
110102
}
111103
}

0 commit comments

Comments
 (0)