From 92e6e53ab4a01d7eb201b6499dd814fdd92f573a Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 5 Mar 2025 13:39:25 +0800 Subject: [PATCH 1/7] Use dependency injection instead of navigation parameter --- Flow.Launcher/MainWindow.xaml.cs | 2 +- Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs | 11 ++++------- Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs | 8 ++------ Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs | 9 +++------ Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs | 9 +++------ Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs | 9 +++------ .../ViewModels/SettingsPaneAboutViewModel.cs | 2 +- Flow.Launcher/WelcomeWindow.xaml.cs | 12 ++++-------- 8 files changed, 21 insertions(+), 41 deletions(-) diff --git a/Flow.Launcher/MainWindow.xaml.cs b/Flow.Launcher/MainWindow.xaml.cs index 3f1bae090..3616e4c59 100644 --- a/Flow.Launcher/MainWindow.xaml.cs +++ b/Flow.Launcher/MainWindow.xaml.cs @@ -445,7 +445,7 @@ namespace Flow.Launcher private void OpenWelcomeWindow() { - var WelcomeWindow = new WelcomeWindow(_settings); + var WelcomeWindow = new WelcomeWindow(); WelcomeWindow.Show(); } diff --git a/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs index 98fb47288..8d4312ba5 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs @@ -1,8 +1,8 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Windows.Navigation; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Core.Resource; +using CommunityToolkit.Mvvm.DependencyInjection; namespace Flow.Launcher.Resources.Pages { @@ -10,10 +10,7 @@ namespace Flow.Launcher.Resources.Pages { protected override void OnNavigatedTo(NavigationEventArgs e) { - if (e.ExtraData is Settings settings) - Settings = settings; - else - throw new ArgumentException("Unexpected Navigation Parameter for Settings"); + Settings = Ioc.Default.GetRequiredService(); InitializeComponent(); } private Internationalization _translater => InternationalizationManager.Instance; @@ -37,4 +34,4 @@ namespace Flow.Launcher.Resources.Pages } } -} \ No newline at end of file +} diff --git a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs index 1ed5747cd..4a4679a4a 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs @@ -1,11 +1,11 @@ using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure.Hotkey; using Flow.Launcher.Infrastructure.UserSettings; -using System; using System.Windows.Navigation; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.ViewModel; using System.Windows.Media; +using CommunityToolkit.Mvvm.DependencyInjection; namespace Flow.Launcher.Resources.Pages { @@ -15,11 +15,7 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { - if (e.ExtraData is Settings settings) - Settings = settings; - else - throw new ArgumentException("Unexpected Parameter setting."); - + Settings = Ioc.Default.GetRequiredService(); InitializeComponent(); } diff --git a/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs index 9051e7c27..38cb8642a 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs @@ -1,5 +1,5 @@ -using System; -using System.Windows.Navigation; +using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher.Resources.Pages @@ -8,10 +8,7 @@ namespace Flow.Launcher.Resources.Pages { protected override void OnNavigatedTo(NavigationEventArgs e) { - if (e.ExtraData is Settings settings) - Settings = settings; - else if(Settings is null) - throw new ArgumentException("Unexpected Navigation Parameter for Settings"); + Settings = Ioc.Default.GetRequiredService(); InitializeComponent(); } diff --git a/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs index 11bbcd6ed..fc6568935 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs @@ -1,5 +1,5 @@ -using Flow.Launcher.Infrastructure.UserSettings; -using System; +using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Infrastructure.UserSettings; using System.Windows.Navigation; namespace Flow.Launcher.Resources.Pages @@ -8,10 +8,7 @@ namespace Flow.Launcher.Resources.Pages { protected override void OnNavigatedTo(NavigationEventArgs e) { - if (e.ExtraData is Settings settings) - Settings = settings; - else - throw new ArgumentException("Unexpected Navigation Parameter for Settings"); + Settings = Ioc.Default.GetRequiredService(); InitializeComponent(); } diff --git a/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs index abb805303..5327eb5c8 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs @@ -1,9 +1,9 @@ -using System; -using System.Windows; +using System.Windows; using System.Windows.Navigation; using Flow.Launcher.Infrastructure.UserSettings; using Microsoft.Win32; using Flow.Launcher.Infrastructure; +using CommunityToolkit.Mvvm.DependencyInjection; namespace Flow.Launcher.Resources.Pages { @@ -15,10 +15,7 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { - if (e.ExtraData is Settings settings) - Settings = settings; - else - throw new ArgumentException("Unexpected Navigation Parameter for Settings"); + Settings = Ioc.Default.GetRequiredService(); InitializeComponent(); } diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs index ade650284..6606b00f0 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneAboutViewModel.cs @@ -54,7 +54,7 @@ public partial class SettingsPaneAboutViewModel : BaseModel [RelayCommand] private void OpenWelcomeWindow() { - var window = new WelcomeWindow(_settings); + var window = new WelcomeWindow(); window.ShowDialog(); } diff --git a/Flow.Launcher/WelcomeWindow.xaml.cs b/Flow.Launcher/WelcomeWindow.xaml.cs index c3723a8c5..1d55b5741 100644 --- a/Flow.Launcher/WelcomeWindow.xaml.cs +++ b/Flow.Launcher/WelcomeWindow.xaml.cs @@ -2,7 +2,6 @@ using System.Windows; using System.Windows.Input; using System.Windows.Controls; -using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Resources.Pages; using ModernWpf.Media.Animation; @@ -10,13 +9,10 @@ namespace Flow.Launcher { public partial class WelcomeWindow : Window { - private readonly Settings settings; - - public WelcomeWindow(Settings settings) + public WelcomeWindow() { InitializeComponent(); BackButton.IsEnabled = false; - this.settings = settings; } private NavigationTransitionInfo _transitionInfo = new SlideNavigationTransitionInfo() @@ -57,7 +53,7 @@ namespace Flow.Launcher pageNum++; UpdateView(); - ContentFrame.Navigate(PageTypeSelector(pageNum), settings, _transitionInfo); + ContentFrame.Navigate(PageTypeSelector(pageNum), null, _transitionInfo); } private void BackwardButton_Click(object sender, RoutedEventArgs e) @@ -66,7 +62,7 @@ namespace Flow.Launcher { pageNum--; UpdateView(); - ContentFrame.Navigate(PageTypeSelector(pageNum), settings, _backTransitionInfo); + ContentFrame.Navigate(PageTypeSelector(pageNum), null, _backTransitionInfo); } else { @@ -109,7 +105,7 @@ namespace Flow.Launcher private void ContentFrame_Loaded(object sender, RoutedEventArgs e) { - ContentFrame.Navigate(PageTypeSelector(1), settings); /* Set First Page */ + ContentFrame.Navigate(PageTypeSelector(1)); /* Set First Page */ } } } From f2248e93b18d92dfc1c4d9d0920f2d9167044702 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 5 Mar 2025 14:42:08 +0800 Subject: [PATCH 2/7] Use dependency injection instead of dependency property --- Flow.Launcher/CustomQueryHotkeySetting.xaml | 1 - Flow.Launcher/HotkeyControl.xaml.cs | 139 +++++++++++++++--- Flow.Launcher/HotkeyControlDialog.xaml.cs | 7 +- .../Resources/Pages/WelcomePage2.xaml | 3 +- .../Views/SettingsPaneHotkey.xaml | 42 ++---- 5 files changed, 137 insertions(+), 55 deletions(-) diff --git a/Flow.Launcher/CustomQueryHotkeySetting.xaml b/Flow.Launcher/CustomQueryHotkeySetting.xaml index 70ebb404b..ccf4de870 100644 --- a/Flow.Launcher/CustomQueryHotkeySetting.xaml +++ b/Flow.Launcher/CustomQueryHotkeySetting.xaml @@ -103,7 +103,6 @@ HorizontalAlignment="Left" VerticalAlignment="Center" HorizontalContentAlignment="Left" - HotkeySettings="{Binding Settings}" DefaultHotkey="" /> (); + public string Hotkey { - get { return (string)GetValue(HotkeyProperty); } - set { SetValue(HotkeyProperty, value); } + get + { + return Type switch + { + HotkeyType.Hotkey => _settings.Hotkey, + HotkeyType.PreviewHotkey => _settings.PreviewHotkey, + HotkeyType.OpenContextMenuHotkey => _settings.OpenContextMenuHotkey, + HotkeyType.SettingWindowHotkey => _settings.SettingWindowHotkey, + HotkeyType.CycleHistoryUpHotkey => _settings.CycleHistoryUpHotkey, + HotkeyType.CycleHistoryDownHotkey => _settings.CycleHistoryDownHotkey, + HotkeyType.SelectPrevPageHotkey => _settings.SelectPrevPageHotkey, + HotkeyType.SelectNextPageHotkey => _settings.SelectNextPageHotkey, + HotkeyType.AutoCompleteHotkey => _settings.AutoCompleteHotkey, + HotkeyType.AutoCompleteHotkey2 => _settings.AutoCompleteHotkey2, + HotkeyType.SelectPrevItemHotkey => _settings.SelectPrevItemHotkey, + HotkeyType.SelectPrevItemHotkey2 => _settings.SelectPrevItemHotkey2, + HotkeyType.SelectNextItemHotkey => _settings.SelectNextItemHotkey, + HotkeyType.SelectNextItemHotkey2 => _settings.SelectNextItemHotkey2, + _ => string.Empty + }; + } + set + { + switch (Type) + { + case HotkeyType.Hotkey: + _settings.Hotkey = value; + break; + case HotkeyType.PreviewHotkey: + _settings.PreviewHotkey = value; + break; + case HotkeyType.OpenContextMenuHotkey: + _settings.OpenContextMenuHotkey = value; + break; + case HotkeyType.SettingWindowHotkey: + _settings.SettingWindowHotkey = value; + break; + case HotkeyType.CycleHistoryUpHotkey: + _settings.CycleHistoryUpHotkey = value; + break; + case HotkeyType.CycleHistoryDownHotkey: + _settings.CycleHistoryDownHotkey = value; + break; + case HotkeyType.SelectPrevPageHotkey: + _settings.SelectPrevPageHotkey = value; + break; + case HotkeyType.SelectNextPageHotkey: + _settings.SelectNextPageHotkey = value; + break; + case HotkeyType.AutoCompleteHotkey: + _settings.AutoCompleteHotkey = value; + break; + case HotkeyType.AutoCompleteHotkey2: + _settings.AutoCompleteHotkey2 = value; + break; + case HotkeyType.SelectPrevItemHotkey: + _settings.SelectPrevItemHotkey = value; + break; + case HotkeyType.SelectNextItemHotkey: + _settings.SelectNextItemHotkey = value; + break; + case HotkeyType.SelectPrevItemHotkey2: + _settings.SelectPrevItemHotkey2 = value; + break; + case HotkeyType.SelectNextItemHotkey2: + _settings.SelectNextItemHotkey2 = value; + break; + default: + return; + } + + // After setting the hotkey, we need to refresh the interface + RefreshHotkeyInterface(Hotkey); + } } public HotkeyControl() @@ -108,7 +198,14 @@ namespace Flow.Launcher InitializeComponent(); HotkeyList.ItemsSource = KeysToDisplay; - SetKeysToDisplay(CurrentHotkey); + + RefreshHotkeyInterface(Hotkey); + } + + public void RefreshHotkeyInterface(string hotkey) + { + SetKeysToDisplay(new HotkeyModel(Hotkey)); + CurrentHotkey = new HotkeyModel(Hotkey); } private static bool CheckHotkeyAvailability(HotkeyModel hotkey, bool validateKeyGesture) => @@ -133,7 +230,7 @@ namespace Flow.Launcher HotKeyMapper.RemoveHotkey(Hotkey); } - var dialog = new HotkeyControlDialog(Hotkey, DefaultHotkey, HotkeySettings, WindowTitle); + var dialog = new HotkeyControlDialog(Hotkey, DefaultHotkey, WindowTitle); await dialog.ShowAsync(); switch (dialog.ResultType) { diff --git a/Flow.Launcher/HotkeyControlDialog.xaml.cs b/Flow.Launcher/HotkeyControlDialog.xaml.cs index a4d21a782..2f8c5eb26 100644 --- a/Flow.Launcher/HotkeyControlDialog.xaml.cs +++ b/Flow.Launcher/HotkeyControlDialog.xaml.cs @@ -4,9 +4,11 @@ using System.Linq; using System.Windows; using System.Windows.Input; using ChefKeys; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.Core.Resource; using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure.Hotkey; +using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; using ModernWpf.Controls; @@ -16,7 +18,7 @@ namespace Flow.Launcher; public partial class HotkeyControlDialog : ContentDialog { - private IHotkeySettings _hotkeySettings; + private static readonly IHotkeySettings _hotkeySettings = Ioc.Default.GetRequiredService(); private Action? _overwriteOtherHotkey; private string DefaultHotkey { get; } public string WindowTitle { get; } @@ -36,7 +38,7 @@ public partial class HotkeyControlDialog : ContentDialog private static bool isOpenFlowHotkey; - public HotkeyControlDialog(string hotkey, string defaultHotkey, IHotkeySettings hotkeySettings, string windowTitle = "") + public HotkeyControlDialog(string hotkey, string defaultHotkey, string windowTitle = "") { WindowTitle = windowTitle switch { @@ -45,7 +47,6 @@ public partial class HotkeyControlDialog : ContentDialog }; DefaultHotkey = defaultHotkey; CurrentHotkey = new HotkeyModel(hotkey); - _hotkeySettings = hotkeySettings; SetKeysToDisplay(CurrentHotkey); InitializeComponent(); diff --git a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml index 6c6fcbb62..04c76d027 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml +++ b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml @@ -114,8 +114,7 @@ Margin="0,8,0,0" ChangeHotkey="{Binding SetTogglingHotkeyCommand}" DefaultHotkey="Alt+Space" - Hotkey="{Binding Settings.Hotkey}" - HotkeySettings="{Binding Settings}" + Type="Hotkey" ValidateKeyGesture="True" WindowTitle="{DynamicResource flowlauncherHotkey}" /> diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml index 22e3960ef..b1d72ede5 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml @@ -34,8 +34,7 @@ @@ -46,8 +45,7 @@ Sub="{DynamicResource previewHotkeyToolTip}"> @@ -105,8 +103,7 @@ Type="Inside"> @@ -221,8 +213,7 @@ @@ -244,8 +234,7 @@ @@ -267,8 +255,7 @@ From c68a764dcfcf1842224bee88ed136de67d08f510 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 5 Mar 2025 14:52:14 +0800 Subject: [PATCH 3/7] Improve code quality --- Flow.Launcher/HotkeyControl.xaml.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Flow.Launcher/HotkeyControl.xaml.cs b/Flow.Launcher/HotkeyControl.xaml.cs index 869774882..273d18e3f 100644 --- a/Flow.Launcher/HotkeyControl.xaml.cs +++ b/Flow.Launcher/HotkeyControl.xaml.cs @@ -111,7 +111,7 @@ namespace Flow.Launcher SelectNextItemHotkey2 } - // We can initialize settings in static field because it has been construct in App constuctor + // We can initialize settings in static field because it has been constructed in App constuctor // and it will not construct settings instances twice private static readonly Settings _settings = Ioc.Default.GetRequiredService(); @@ -202,7 +202,7 @@ namespace Flow.Launcher RefreshHotkeyInterface(Hotkey); } - public void RefreshHotkeyInterface(string hotkey) + private void RefreshHotkeyInterface(string hotkey) { SetKeysToDisplay(new HotkeyModel(Hotkey)); CurrentHotkey = new HotkeyModel(Hotkey); From 31d47a2340173b09ec30b1d1e6ab201796204fc1 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 5 Mar 2025 15:18:06 +0800 Subject: [PATCH 4/7] Use dependency injection instead of navigation parameter --- .../SettingPages/Views/SettingsPaneAbout.xaml.cs | 10 ++++++---- .../SettingPages/Views/SettingsPaneGeneral.xaml.cs | 11 +++++++---- .../SettingPages/Views/SettingsPaneHotkey.xaml.cs | 8 ++++---- .../Views/SettingsPanePluginStore.xaml.cs | 8 ++++---- .../SettingPages/Views/SettingsPanePlugins.xaml.cs | 8 ++++---- .../SettingPages/Views/SettingsPaneProxy.xaml.cs | 10 ++++++---- .../SettingPages/Views/SettingsPaneTheme.xaml.cs | 10 ++++------ Flow.Launcher/SettingWindow.xaml.cs | 13 ++----------- 8 files changed, 37 insertions(+), 41 deletions(-) diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs index de6a4df5e..1ecc02aa6 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs @@ -1,6 +1,8 @@ -using System; -using System.Windows.Navigation; +using System.Windows.Navigation; +using Flow.Launcher.Core; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.Infrastructure.UserSettings; +using CommunityToolkit.Mvvm.DependencyInjection; namespace Flow.Launcher.SettingPages.Views; @@ -12,8 +14,8 @@ public partial class SettingsPaneAbout { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings, Updater: { } updater }) - throw new ArgumentException("Settings are required for SettingsPaneAbout."); + var settings = Ioc.Default.GetRequiredService(); + var updater = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPaneAboutViewModel(settings, updater); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs index f95015b2e..dd7fd13a9 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs @@ -1,5 +1,7 @@ -using System; -using System.Windows.Navigation; +using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Core; +using Flow.Launcher.Core.Configuration; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.SettingPages.ViewModels; @@ -13,8 +15,9 @@ public partial class SettingsPaneGeneral { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings, Updater: {} updater, Portable: {} portable }) - throw new ArgumentException("Settings, Updater and Portable are required for SettingsPaneGeneral."); + var settings = Ioc.Default.GetRequiredService(); + var updater = Ioc.Default.GetRequiredService(); + var portable = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPaneGeneralViewModel(settings, updater, portable); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs index 061eabf51..eb100da0c 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs @@ -1,6 +1,7 @@ -using System; -using System.Windows.Navigation; +using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher.SettingPages.Views; @@ -12,8 +13,7 @@ public partial class SettingsPaneHotkey { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings }) - throw new ArgumentException("Settings are required for SettingsPaneHotkey."); + var settings = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPaneHotkeyViewModel(settings); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs index db4763319..3bd24bc13 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs @@ -1,10 +1,11 @@ -using System; -using System.ComponentModel; +using System.ComponentModel; using System.Windows.Data; using System.Windows.Input; using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; using Flow.Launcher.ViewModel; +using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher.SettingPages.Views; @@ -16,8 +17,7 @@ public partial class SettingsPanePluginStore { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings }) - throw new ArgumentException($"Settings are required for {nameof(SettingsPanePluginStore)}."); + var settings = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPanePluginStoreViewModel(); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs index d48505c3d..f6b435186 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs @@ -1,7 +1,8 @@ -using System; -using System.Windows.Input; +using System.Windows.Input; using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher.SettingPages.Views; @@ -13,8 +14,7 @@ public partial class SettingsPanePlugins { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings }) - throw new ArgumentException("Settings are required for SettingsPaneHotkey."); + var settings = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPanePluginsViewModel(settings); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs index 95c88d627..26350b8bb 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs @@ -1,6 +1,8 @@ -using System; -using System.Windows.Navigation; +using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Core; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher.SettingPages.Views; @@ -12,8 +14,8 @@ public partial class SettingsPaneProxy { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings, Updater: { } updater }) - throw new ArgumentException($"Settings are required for {nameof(SettingsPaneProxy)}."); + var settings = Ioc.Default.GetRequiredService(); + var updater = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPaneProxyViewModel(settings, updater); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs index 7f32728c2..bf7502e19 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs @@ -1,10 +1,9 @@ -using System; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Media; +using System.Windows.Controls; using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; using Page = ModernWpf.Controls.Page; +using Flow.Launcher.Infrastructure.UserSettings; namespace Flow.Launcher.SettingPages.Views; @@ -16,8 +15,7 @@ public partial class SettingsPaneTheme : Page { if (!IsInitialized) { - if (e.ExtraData is not SettingWindow.PaneData { Settings: { } settings }) - throw new ArgumentException($"Settings are required for {nameof(SettingsPaneTheme)}."); + var settings = Ioc.Default.GetRequiredService(); _viewModel = new SettingsPaneThemeViewModel(settings); DataContext = _viewModel; InitializeComponent(); diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index f87194c30..30b51f992 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -4,8 +4,6 @@ using System.Windows.Forms; using System.Windows.Input; using System.Windows.Interop; using CommunityToolkit.Mvvm.DependencyInjection; -using Flow.Launcher.Core; -using Flow.Launcher.Core.Configuration; using Flow.Launcher.Helper; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; @@ -18,8 +16,6 @@ namespace Flow.Launcher; public partial class SettingWindow { - private readonly Updater _updater; - private readonly IPortable _portable; private readonly IPublicAPI _api; private readonly Settings _settings; private readonly SettingWindowViewModel _viewModel; @@ -30,8 +26,6 @@ public partial class SettingWindow _settings = Ioc.Default.GetRequiredService(); DataContext = viewModel; _viewModel = viewModel; - _updater = Ioc.Default.GetRequiredService(); - _portable = Ioc.Default.GetRequiredService(); _api = Ioc.Default.GetRequiredService(); InitializePosition(); InitializeComponent(); @@ -166,10 +160,9 @@ public partial class SettingWindow private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) { - var paneData = new PaneData(_settings, _updater, _portable); if (args.IsSettingsSelected) { - ContentFrame.Navigate(typeof(SettingsPaneGeneral), paneData); + ContentFrame.Navigate(typeof(SettingsPaneGeneral)); } else { @@ -191,7 +184,7 @@ public partial class SettingWindow nameof(About) => typeof(SettingsPaneAbout), _ => typeof(SettingsPaneGeneral) }; - ContentFrame.Navigate(pageType, paneData); + ContentFrame.Navigate(pageType); } } @@ -211,6 +204,4 @@ public partial class SettingWindow { NavView.SelectedItem ??= NavView.MenuItems[0]; /* Set First Page */ } - - public record PaneData(Settings Settings, Updater Updater, IPortable Portable); } From 8ce4bd92e2d39331b40b5a6cc1344b4dd262a3a6 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Thu, 6 Mar 2025 10:44:15 +0800 Subject: [PATCH 5/7] Fix page number issue when navigating with keyboard --- Flow.Launcher/App.xaml.cs | 1 + .../Resources/Pages/WelcomePage1.xaml.cs | 4 ++ .../Resources/Pages/WelcomePage2.xaml.cs | 3 + .../Resources/Pages/WelcomePage3.xaml.cs | 4 ++ .../Resources/Pages/WelcomePage4.xaml.cs | 4 ++ .../Resources/Pages/WelcomePage5.xaml.cs | 4 ++ Flow.Launcher/ViewModel/WelcomeViewModel.cs | 68 +++++++++++++++++++ Flow.Launcher/WelcomeWindow.xaml | 25 ++++--- Flow.Launcher/WelcomeWindow.xaml.cs | 66 ++++++++---------- 9 files changed, 132 insertions(+), 47 deletions(-) create mode 100644 Flow.Launcher/ViewModel/WelcomeViewModel.cs diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 447eca792..07b721b99 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -64,6 +64,7 @@ namespace Flow.Launcher .AddSingleton() .AddSingleton() .AddSingleton() + .AddSingleton() ).Build(); Ioc.Default.ConfigureServices(host.Services); } diff --git a/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs index 8d4312ba5..880bfd9bc 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs @@ -3,6 +3,7 @@ using System.Windows.Navigation; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Core.Resource; using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.ViewModel; namespace Flow.Launcher.Resources.Pages { @@ -11,6 +12,9 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { Settings = Ioc.Default.GetRequiredService(); + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page number + Ioc.Default.GetRequiredService().PageNum = 1; InitializeComponent(); } private Internationalization _translater => InternationalizationManager.Instance; diff --git a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs index 4a4679a4a..786b4d506 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs @@ -16,6 +16,9 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { Settings = Ioc.Default.GetRequiredService(); + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page number + Ioc.Default.GetRequiredService().PageNum = 2; InitializeComponent(); } diff --git a/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs index 38cb8642a..f59b65c1c 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs @@ -1,6 +1,7 @@ using System.Windows.Navigation; using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.ViewModel; namespace Flow.Launcher.Resources.Pages { @@ -9,6 +10,9 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { Settings = Ioc.Default.GetRequiredService(); + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page number + Ioc.Default.GetRequiredService().PageNum = 3; InitializeComponent(); } diff --git a/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs index fc6568935..4c83f3a83 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs @@ -1,5 +1,6 @@ using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.ViewModel; using System.Windows.Navigation; namespace Flow.Launcher.Resources.Pages @@ -9,6 +10,9 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { Settings = Ioc.Default.GetRequiredService(); + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page number + Ioc.Default.GetRequiredService().PageNum = 4; InitializeComponent(); } diff --git a/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs index 5327eb5c8..95d7ff1a0 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs @@ -4,6 +4,7 @@ using Flow.Launcher.Infrastructure.UserSettings; using Microsoft.Win32; using Flow.Launcher.Infrastructure; using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.ViewModel; namespace Flow.Launcher.Resources.Pages { @@ -16,6 +17,9 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { Settings = Ioc.Default.GetRequiredService(); + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page number + Ioc.Default.GetRequiredService().PageNum = 5; InitializeComponent(); } diff --git a/Flow.Launcher/ViewModel/WelcomeViewModel.cs b/Flow.Launcher/ViewModel/WelcomeViewModel.cs new file mode 100644 index 000000000..5eecabfde --- /dev/null +++ b/Flow.Launcher/ViewModel/WelcomeViewModel.cs @@ -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; + } + } + } +} diff --git a/Flow.Launcher/WelcomeWindow.xaml b/Flow.Launcher/WelcomeWindow.xaml index 73f423d0b..e7c483f89 100644 --- a/Flow.Launcher/WelcomeWindow.xaml +++ b/Flow.Launcher/WelcomeWindow.xaml @@ -6,6 +6,7 @@ xmlns:local="clr-namespace:Flow.Launcher" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:ui="http://schemas.modernwpf.com/2019" + xmlns:vm="clr-namespace:Flow.Launcher.ViewModel" Name="FlowWelcomeWindow" Title="{DynamicResource Welcome_Page1_Title}" Width="550" @@ -14,8 +15,10 @@ MinHeight="650" MaxWidth="550" MaxHeight="650" + d:DataContext="{d:DesignInstance Type=vm:WelcomeViewModel}" Activated="OnActivated" Background="{DynamicResource Color00B}" + Closed="Window_Closed" Foreground="{DynamicResource PopupTextColor}" MouseDown="window_MouseDown" WindowStartupLocation="CenterScreen" @@ -41,12 +44,12 @@ Grid.Column="0" Width="16" Height="16" - Margin="10,4,4,4" + Margin="10 4 4 4" RenderOptions.BitmapScalingMode="HighQuality" Source="/Images/app.png" /> + BorderThickness="0 1 0 0"> @@ -109,11 +112,11 @@ VerticalAlignment="Center"> @@ -122,25 +125,26 @@ Grid.Column="0" Width="100" Height="40" - Margin="20,5,0,5" + Margin="20 5 0 5" Click="BtnCancel_OnClick" Content="{DynamicResource Skip}" DockPanel.Dock="Right" FontSize="14" />