diff --git a/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs index a3b008206..1b1dd21e9 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs @@ -14,7 +14,7 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { Settings = Ioc.Default.GetRequiredService(); _viewModel = Ioc.Default.GetRequiredService(); diff --git a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs index a63edbcda..7408d8a14 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs @@ -16,7 +16,7 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { Settings = Ioc.Default.GetRequiredService(); _viewModel = Ioc.Default.GetRequiredService(); diff --git a/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs index 4a6610e61..bd14bf0f7 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs @@ -12,7 +12,7 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { Settings = Ioc.Default.GetRequiredService(); _viewModel = Ioc.Default.GetRequiredService(); diff --git a/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs index 1ee3284d2..b20e4ce0a 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs @@ -12,7 +12,7 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { Settings = Ioc.Default.GetRequiredService(); _viewModel = Ioc.Default.GetRequiredService(); diff --git a/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs index 6328c9914..84d298966 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs @@ -15,7 +15,7 @@ namespace Flow.Launcher.Resources.Pages protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { Settings = Ioc.Default.GetRequiredService(); _viewModel = Ioc.Default.GetRequiredService(); diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs index e57cba6d4..93515caec 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneAbout.xaml.cs @@ -1,21 +1,27 @@ using System.Windows.Navigation; using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.ViewModel; namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPaneAbout { private SettingsPaneAboutViewModel _viewModel = null!; + private SettingWindowViewModel _settingViewModel = null; protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); + _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; InitializeComponent(); } + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPaneAbout); base.OnNavigatedTo(e); } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs index 31653962d..fa3406bd9 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneGeneral.xaml.cs @@ -1,21 +1,27 @@ using System.Windows.Navigation; using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.ViewModel; namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPaneGeneral { private SettingsPaneGeneralViewModel _viewModel = null!; + private SettingWindowViewModel _settingViewModel = null; protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); + _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; InitializeComponent(); } + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPaneGeneral); base.OnNavigatedTo(e); } } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs index 8b757bd60..e590dc9a8 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneHotkey.xaml.cs @@ -1,21 +1,27 @@ using System.Windows.Navigation; using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.ViewModel; namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPaneHotkey { private SettingsPaneHotkeyViewModel _viewModel = null!; + private SettingWindowViewModel _settingViewModel = null; protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); + _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; InitializeComponent(); } + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPaneHotkey); base.OnNavigatedTo(e); } } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs index 5ddfe4650..aa6900dae 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPanePluginStore.xaml.cs @@ -11,16 +11,21 @@ namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPanePluginStore { private SettingsPanePluginStoreViewModel _viewModel = null!; + private SettingWindowViewModel _settingViewModel = null; protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); + _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; InitializeComponent(); } _viewModel.PropertyChanged += ViewModel_PropertyChanged; + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPanePluginStore); base.OnNavigatedTo(e); } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs index e9490804a..6424aabdf 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPanePlugins.xaml.cs @@ -11,16 +11,21 @@ namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPanePlugins { private SettingsPanePluginsViewModel _viewModel = null!; + private SettingWindowViewModel _settingViewModel = null; protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); + _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; InitializeComponent(); } _viewModel.PropertyChanged += ViewModel_PropertyChanged; + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPanePlugins); base.OnNavigatedTo(e); } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs index 258f2a4ad..c89a050f5 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneProxy.xaml.cs @@ -1,21 +1,27 @@ using System.Windows.Navigation; using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.ViewModel; namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPaneProxy { private SettingsPaneProxyViewModel _viewModel = null!; + private SettingWindowViewModel _settingViewModel = null; protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); + _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; InitializeComponent(); } + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPaneProxy); base.OnNavigatedTo(e); } } diff --git a/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs b/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs index cee8e4ae4..8759c8efc 100644 --- a/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs +++ b/Flow.Launcher/SettingPages/Views/SettingsPaneTheme.xaml.cs @@ -1,21 +1,27 @@ using System.Windows.Navigation; using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.SettingPages.ViewModels; +using Flow.Launcher.ViewModel; namespace Flow.Launcher.SettingPages.Views; public partial class SettingsPaneTheme { private SettingsPaneThemeViewModel _viewModel = null!; + private SettingWindowViewModel _settingViewModel = null; protected override void OnNavigatedTo(NavigationEventArgs e) { - if (!IsInitialized) + if (_viewModel == null) { _viewModel = Ioc.Default.GetRequiredService(); + _settingViewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; InitializeComponent(); } + // Sometimes the navigation is not triggered by button click, + // so we need to reset the page type + _settingViewModel.PageType = typeof(SettingsPaneTheme); base.OnNavigatedTo(e); } } diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index cf84317ac..14e4343e3 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -1,4 +1,5 @@ using System; +using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Input; @@ -18,6 +19,7 @@ public partial class SettingWindow #region Private Fields private readonly Settings _settings; + private readonly SettingWindowViewModel _viewModel; #endregion @@ -26,8 +28,8 @@ public partial class SettingWindow public SettingWindow() { _settings = Ioc.Default.GetRequiredService(); - var viewModel = Ioc.Default.GetRequiredService(); - DataContext = viewModel; + _viewModel = Ioc.Default.GetRequiredService(); + DataContext = _viewModel; InitializeComponent(); UpdatePositionAndState(); @@ -48,10 +50,37 @@ public partial class SettingWindow hwndTarget.RenderMode = RenderMode.SoftwareOnly; // Must use software only render mode here UpdatePositionAndState(); + + _viewModel.PropertyChanged += ViewModel_PropertyChanged; + } + + // Sometimes the navigation is not triggered by button click, + // so we need to update the selected item here + private void ViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e) + { + switch (e.PropertyName) + { + case nameof(SettingWindowViewModel.PageType): + var selectedIndex = _viewModel.PageType.Name switch + { + nameof(SettingsPaneGeneral) => 0, + nameof(SettingsPanePlugins) => 1, + nameof(SettingsPanePluginStore) => 2, + nameof(SettingsPaneTheme) => 3, + nameof(SettingsPaneHotkey) => 4, + nameof(SettingsPaneProxy) => 5, + nameof(SettingsPaneAbout) => 6, + _ => 0 + }; + NavView.SelectedItem = NavView.MenuItems[selectedIndex]; + break; + } } private void OnClosed(object sender, EventArgs e) { + _viewModel.PropertyChanged -= ViewModel_PropertyChanged; + // If app is exiting, settings save is not needed because main window closing event will handle this if (App.Exiting) return; // Save settings when window is closed diff --git a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs index 17a1a2b50..9e7eaf6e8 100644 --- a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs +++ b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs @@ -1,5 +1,7 @@ -using Flow.Launcher.Infrastructure.UserSettings; +using System; +using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; +using Flow.Launcher.SettingPages.Views; namespace Flow.Launcher.ViewModel; @@ -12,6 +14,20 @@ public partial class SettingWindowViewModel : BaseModel _settings = settings; } + private Type _pageType = typeof(SettingsPaneGeneral); + public Type PageType + { + get => _pageType; + set + { + if (_pageType != value) + { + _pageType = value; + OnPropertyChanged(); + } + } + } + public double SettingWindowWidth { get => _settings.SettingWindowWidth;