Fix page number issue when navigating with keyboard

This commit is contained in:
Jack251970 2025-03-06 10:44:15 +08:00
parent c68a764dcf
commit 8ce4bd92e2
9 changed files with 132 additions and 47 deletions

View file

@ -64,6 +64,7 @@ namespace Flow.Launcher
.AddSingleton<IPublicAPI, PublicAPIInstance>() .AddSingleton<IPublicAPI, PublicAPIInstance>()
.AddSingleton<MainViewModel>() .AddSingleton<MainViewModel>()
.AddSingleton<Theme>() .AddSingleton<Theme>()
.AddSingleton<WelcomeViewModel>()
).Build(); ).Build();
Ioc.Default.ConfigureServices(host.Services); Ioc.Default.ConfigureServices(host.Services);
} }

View file

@ -3,6 +3,7 @@ using System.Windows.Navigation;
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Core.Resource; using Flow.Launcher.Core.Resource;
using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.ViewModel;
namespace Flow.Launcher.Resources.Pages namespace Flow.Launcher.Resources.Pages
{ {
@ -11,6 +12,9 @@ namespace Flow.Launcher.Resources.Pages
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
Settings = Ioc.Default.GetRequiredService<Settings>(); Settings = Ioc.Default.GetRequiredService<Settings>();
// Sometimes the navigation is not triggered by button click,
// so we need to reset the page number
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 1;
InitializeComponent(); InitializeComponent();
} }
private Internationalization _translater => InternationalizationManager.Instance; private Internationalization _translater => InternationalizationManager.Instance;

View file

@ -16,6 +16,9 @@ namespace Flow.Launcher.Resources.Pages
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
Settings = Ioc.Default.GetRequiredService<Settings>(); Settings = Ioc.Default.GetRequiredService<Settings>();
// Sometimes the navigation is not triggered by button click,
// so we need to reset the page number
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 2;
InitializeComponent(); InitializeComponent();
} }

View file

@ -1,6 +1,7 @@
using System.Windows.Navigation; using System.Windows.Navigation;
using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.ViewModel;
namespace Flow.Launcher.Resources.Pages namespace Flow.Launcher.Resources.Pages
{ {
@ -9,6 +10,9 @@ namespace Flow.Launcher.Resources.Pages
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
Settings = Ioc.Default.GetRequiredService<Settings>(); Settings = Ioc.Default.GetRequiredService<Settings>();
// Sometimes the navigation is not triggered by button click,
// so we need to reset the page number
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 3;
InitializeComponent(); InitializeComponent();
} }

View file

@ -1,5 +1,6 @@
using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.ViewModel;
using System.Windows.Navigation; using System.Windows.Navigation;
namespace Flow.Launcher.Resources.Pages namespace Flow.Launcher.Resources.Pages
@ -9,6 +10,9 @@ namespace Flow.Launcher.Resources.Pages
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
Settings = Ioc.Default.GetRequiredService<Settings>(); Settings = Ioc.Default.GetRequiredService<Settings>();
// Sometimes the navigation is not triggered by button click,
// so we need to reset the page number
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 4;
InitializeComponent(); InitializeComponent();
} }

View file

@ -4,6 +4,7 @@ using Flow.Launcher.Infrastructure.UserSettings;
using Microsoft.Win32; using Microsoft.Win32;
using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure;
using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.ViewModel;
namespace Flow.Launcher.Resources.Pages namespace Flow.Launcher.Resources.Pages
{ {
@ -16,6 +17,9 @@ namespace Flow.Launcher.Resources.Pages
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
Settings = Ioc.Default.GetRequiredService<Settings>(); Settings = Ioc.Default.GetRequiredService<Settings>();
// Sometimes the navigation is not triggered by button click,
// so we need to reset the page number
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 5;
InitializeComponent(); InitializeComponent();
} }

View file

@ -0,0 +1,68 @@
using Flow.Launcher.Plugin;
namespace Flow.Launcher.ViewModel
{
public partial class WelcomeViewModel : BaseModel
{
public const int MaxPageNum = 5;
public string PageDisplay => $"{PageNum}/5";
private int _pageNum = 1;
public int PageNum
{
get => _pageNum;
set
{
if (_pageNum != value)
{
_pageNum = value;
OnPropertyChanged();
UpdateView();
}
}
}
private bool _backEnabled = false;
public bool BackEnabled
{
get => _backEnabled;
set
{
_backEnabled = value;
OnPropertyChanged();
}
}
private bool _nextEnabled = true;
public bool NextEnabled
{
get => _nextEnabled;
set
{
_nextEnabled = value;
OnPropertyChanged();
}
}
private void UpdateView()
{
OnPropertyChanged(nameof(PageDisplay));
if (PageNum == 1)
{
BackEnabled = false;
NextEnabled = true;
}
else if (PageNum == MaxPageNum)
{
BackEnabled = true;
NextEnabled = false;
}
else
{
BackEnabled = true;
NextEnabled = true;
}
}
}
}

View file

@ -6,6 +6,7 @@
xmlns:local="clr-namespace:Flow.Launcher" xmlns:local="clr-namespace:Flow.Launcher"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:ui="http://schemas.modernwpf.com/2019" xmlns:ui="http://schemas.modernwpf.com/2019"
xmlns:vm="clr-namespace:Flow.Launcher.ViewModel"
Name="FlowWelcomeWindow" Name="FlowWelcomeWindow"
Title="{DynamicResource Welcome_Page1_Title}" Title="{DynamicResource Welcome_Page1_Title}"
Width="550" Width="550"
@ -14,8 +15,10 @@
MinHeight="650" MinHeight="650"
MaxWidth="550" MaxWidth="550"
MaxHeight="650" MaxHeight="650"
d:DataContext="{d:DesignInstance Type=vm:WelcomeViewModel}"
Activated="OnActivated" Activated="OnActivated"
Background="{DynamicResource Color00B}" Background="{DynamicResource Color00B}"
Closed="Window_Closed"
Foreground="{DynamicResource PopupTextColor}" Foreground="{DynamicResource PopupTextColor}"
MouseDown="window_MouseDown" MouseDown="window_MouseDown"
WindowStartupLocation="CenterScreen" WindowStartupLocation="CenterScreen"
@ -41,12 +44,12 @@
Grid.Column="0" Grid.Column="0"
Width="16" Width="16"
Height="16" Height="16"
Margin="10,4,4,4" Margin="10 4 4 4"
RenderOptions.BitmapScalingMode="HighQuality" RenderOptions.BitmapScalingMode="HighQuality"
Source="/Images/app.png" /> Source="/Images/app.png" />
<TextBlock <TextBlock
Grid.Column="1" Grid.Column="1"
Margin="4,0,0,0" Margin="4 0 0 0"
VerticalAlignment="Center" VerticalAlignment="Center"
FontSize="12" FontSize="12"
Foreground="{DynamicResource Color05B}" Foreground="{DynamicResource Color05B}"
@ -95,7 +98,7 @@
Grid.Row="1" Grid.Row="1"
Background="{DynamicResource Color00B}" Background="{DynamicResource Color00B}"
BorderBrush="{DynamicResource PopupButtonAreaBorderColor}" BorderBrush="{DynamicResource PopupButtonAreaBorderColor}"
BorderThickness="0,1,0,0"> BorderThickness="0 1 0 0">
<Grid> <Grid>
<Grid.ColumnDefinitions> <Grid.ColumnDefinitions>
<ColumnDefinition Width="130" /> <ColumnDefinition Width="130" />
@ -109,11 +112,11 @@
VerticalAlignment="Center"> VerticalAlignment="Center">
<TextBlock <TextBlock
Name="PageNavigation" Name="PageNavigation"
Margin="0,2,0,0" Margin="0 2 0 0"
HorizontalAlignment="Center" HorizontalAlignment="Center"
VerticalAlignment="Center" VerticalAlignment="Center"
FontSize="14" FontSize="14"
Text="{Binding PageDisplay, Mode=OneWay, UpdateSourceTrigger=PropertyChanged, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" Text="{Binding PageDisplay, Mode=OneWay}"
TextAlignment="Center" /> TextAlignment="Center" />
</StackPanel> </StackPanel>
@ -122,25 +125,26 @@
Grid.Column="0" Grid.Column="0"
Width="100" Width="100"
Height="40" Height="40"
Margin="20,5,0,5" Margin="20 5 0 5"
Click="BtnCancel_OnClick" Click="BtnCancel_OnClick"
Content="{DynamicResource Skip}" Content="{DynamicResource Skip}"
DockPanel.Dock="Right" DockPanel.Dock="Right"
FontSize="14" /> FontSize="14" />
<DockPanel <DockPanel
Grid.Column="2" Grid.Column="2"
Margin="0,0,20,0" Margin="0 0 20 0"
VerticalAlignment="Stretch"> VerticalAlignment="Stretch">
<Button <Button
x:Name="NextButton" x:Name="NextButton"
Width="40" Width="40"
Height="40" Height="40"
Margin="8,5,0,5" Margin="8 5 0 5"
Click="ForwardButton_Click" Click="ForwardButton_Click"
Content="&#xe76c;" Content="&#xe76c;"
DockPanel.Dock="Right" DockPanel.Dock="Right"
FontFamily="/Resources/#Segoe Fluent Icons" FontFamily="/Resources/#Segoe Fluent Icons"
FontSize="18" /> FontSize="18"
IsEnabled="{Binding NextEnabled, Mode=OneWay}" />
<Button <Button
x:Name="BackButton" x:Name="BackButton"
Width="40" Width="40"
@ -149,7 +153,8 @@
Content="&#xe76b;" Content="&#xe76b;"
DockPanel.Dock="Right" DockPanel.Dock="Right"
FontFamily="/Resources/#Segoe Fluent Icons" FontFamily="/Resources/#Segoe Fluent Icons"
FontSize="18" /> FontSize="18"
IsEnabled="{Binding BackEnabled, Mode=OneWay}" />
<StackPanel /> <StackPanel />
</DockPanel> </DockPanel>

View file

@ -4,69 +4,55 @@ using System.Windows.Input;
using System.Windows.Controls; using System.Windows.Controls;
using Flow.Launcher.Resources.Pages; using Flow.Launcher.Resources.Pages;
using ModernWpf.Media.Animation; using ModernWpf.Media.Animation;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.ViewModel;
using Flow.Launcher.Infrastructure.UserSettings;
namespace Flow.Launcher namespace Flow.Launcher
{ {
public partial class WelcomeWindow : Window public partial class WelcomeWindow : Window
{ {
public WelcomeWindow() private readonly WelcomeViewModel _viewModel;
{
InitializeComponent();
BackButton.IsEnabled = false;
}
private NavigationTransitionInfo _transitionInfo = new SlideNavigationTransitionInfo() private readonly NavigationTransitionInfo _forwardTransitionInfo = new SlideNavigationTransitionInfo()
{ {
Effect = SlideNavigationTransitionEffect.FromRight Effect = SlideNavigationTransitionEffect.FromRight
}; };
private NavigationTransitionInfo _backTransitionInfo = new SlideNavigationTransitionInfo() private readonly NavigationTransitionInfo _backTransitionInfo = new SlideNavigationTransitionInfo()
{ {
Effect = SlideNavigationTransitionEffect.FromLeft Effect = SlideNavigationTransitionEffect.FromLeft
}; };
private int pageNum = 1; public WelcomeWindow()
private int MaxPage = 5;
public string PageDisplay => $"{pageNum}/5";
private void UpdateView()
{ {
PageNavigation.Text = PageDisplay; _viewModel = Ioc.Default.GetRequiredService<WelcomeViewModel>();
if (pageNum == 1) DataContext = _viewModel;
{ InitializeComponent();
BackButton.IsEnabled = false;
NextButton.IsEnabled = true;
}
else if (pageNum == MaxPage)
{
BackButton.IsEnabled = true;
NextButton.IsEnabled = false;
}
else
{
BackButton.IsEnabled = true;
NextButton.IsEnabled = true;
}
} }
private void ForwardButton_Click(object sender, RoutedEventArgs e) private void ForwardButton_Click(object sender, RoutedEventArgs e)
{ {
pageNum++; if (_viewModel.PageNum < WelcomeViewModel.MaxPageNum)
UpdateView(); {
_viewModel.PageNum++;
ContentFrame.Navigate(PageTypeSelector(pageNum), null, _transitionInfo); ContentFrame.Navigate(PageTypeSelector(_viewModel.PageNum), null, _forwardTransitionInfo);
}
else
{
_viewModel.NextEnabled = false;
}
} }
private void BackwardButton_Click(object sender, RoutedEventArgs e) private void BackwardButton_Click(object sender, RoutedEventArgs e)
{ {
if (pageNum > 1) if (_viewModel.PageNum > 1)
{ {
pageNum--; _viewModel.PageNum--;
UpdateView(); ContentFrame.Navigate(PageTypeSelector(_viewModel.PageNum), null, _backTransitionInfo);
ContentFrame.Navigate(PageTypeSelector(pageNum), null, _backTransitionInfo);
} }
else else
{ {
BackButton.IsEnabled = false; _viewModel.BackEnabled = false;
} }
} }
@ -107,5 +93,11 @@ namespace Flow.Launcher
{ {
ContentFrame.Navigate(PageTypeSelector(1)); /* Set First Page */ ContentFrame.Navigate(PageTypeSelector(1)); /* Set First Page */
} }
private void Window_Closed(object sender, EventArgs e)
{
// Save settings when window is closed
Ioc.Default.GetRequiredService<Settings>().Save();
}
} }
} }