diff --git a/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs b/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs index cc07caaa2..77febde9d 100644 --- a/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs +++ b/Flow.Launcher/CustomQueryHotkeySetting.xaml.cs @@ -10,14 +10,14 @@ namespace Flow.Launcher { public partial class CustomQueryHotkeySetting : Window { - public Settings Settings { get; } + private readonly Settings _settings; private bool update; private CustomPluginHotkey updateCustomHotkey; public CustomQueryHotkeySetting(Settings settings) { - Settings = settings; + _settings = settings; InitializeComponent(); } @@ -30,13 +30,13 @@ namespace Flow.Launcher { if (!update) { - Settings.CustomPluginHotkeys ??= new ObservableCollection(); + _settings.CustomPluginHotkeys ??= new ObservableCollection(); var pluginHotkey = new CustomPluginHotkey { Hotkey = HotkeyControl.CurrentHotkey.ToString(), ActionKeyword = tbAction.Text }; - Settings.CustomPluginHotkeys.Add(pluginHotkey); + _settings.CustomPluginHotkeys.Add(pluginHotkey); HotKeyMapper.SetCustomQueryHotkey(pluginHotkey); } @@ -55,7 +55,7 @@ namespace Flow.Launcher public void UpdateItem(CustomPluginHotkey item) { - updateCustomHotkey = Settings.CustomPluginHotkeys.FirstOrDefault(o => + updateCustomHotkey = _settings.CustomPluginHotkeys.FirstOrDefault(o => o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey); if (updateCustomHotkey == null) { diff --git a/Flow.Launcher/CustomShortcutSetting.xaml.cs b/Flow.Launcher/CustomShortcutSetting.xaml.cs index fcee0c961..e180f6570 100644 --- a/Flow.Launcher/CustomShortcutSetting.xaml.cs +++ b/Flow.Launcher/CustomShortcutSetting.xaml.cs @@ -1,15 +1,11 @@ using System.Windows; using System.Windows.Input; -using CommunityToolkit.Mvvm.DependencyInjection; -using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.SettingPages.ViewModels; namespace Flow.Launcher { public partial class CustomShortcutSetting : Window { - public Settings Settings { get; } = Ioc.Default.GetRequiredService(); - private readonly SettingsPaneHotkeyViewModel _hotkeyVm; public string Key { get; set; } = string.Empty; public string Value { get; set; } = string.Empty; diff --git a/Flow.Launcher/Resources/Controls/HotkeyDisplay.xaml.cs b/Flow.Launcher/Resources/Controls/HotkeyDisplay.xaml.cs index 9b19ffd86..bc167184b 100644 --- a/Flow.Launcher/Resources/Controls/HotkeyDisplay.xaml.cs +++ b/Flow.Launcher/Resources/Controls/HotkeyDisplay.xaml.cs @@ -42,14 +42,11 @@ namespace Flow.Launcher.Resources.Controls private static void keyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { - var control = d as UserControl; - if (null == control) return; // This should not be possible + if (d is not UserControl) return; // This should not be possible - var newValue = e.NewValue as string; - if (null == newValue) return; + if (e.NewValue is not string newValue) return; - if (d is not HotkeyDisplay hotkeyDisplay) - return; + if (d is not HotkeyDisplay hotkeyDisplay) return; hotkeyDisplay.Values.Clear(); foreach (var key in newValue.Split('+')) diff --git a/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs index 64f52edff..a3b008206 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage1.xaml.cs @@ -1,29 +1,35 @@ using System.Collections.Generic; using System.Windows.Navigation; -using Flow.Launcher.Infrastructure.UserSettings; -using Flow.Launcher.Core.Resource; using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Core.Resource; +using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.ViewModel; namespace Flow.Launcher.Resources.Pages { public partial class WelcomePage1 { + public Settings Settings { get; private set; } + private WelcomeViewModel _viewModel; + protected override void OnNavigatedTo(NavigationEventArgs e) { - Settings = Ioc.Default.GetRequiredService(); + if (!IsInitialized) + { + Settings = Ioc.Default.GetRequiredService(); + _viewModel = Ioc.Default.GetRequiredService(); + InitializeComponent(); + } // Sometimes the navigation is not triggered by button click, // so we need to reset the page number - Ioc.Default.GetRequiredService().PageNum = 1; - InitializeComponent(); + _viewModel.PageNum = 1; + base.OnNavigatedTo(e); } private readonly Internationalization _translater = Ioc.Default.GetRequiredService(); public List Languages => _translater.LoadAvailableLanguages(); - public Settings Settings { get; set; } - public string CustomLanguage { get diff --git a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs index 786b4d506..a63edbcda 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage2.xaml.cs @@ -1,25 +1,31 @@ -using Flow.Launcher.Helper; -using Flow.Launcher.Infrastructure.Hotkey; -using Flow.Launcher.Infrastructure.UserSettings; +using System.Windows.Media; using System.Windows.Navigation; using CommunityToolkit.Mvvm.Input; -using Flow.Launcher.ViewModel; -using System.Windows.Media; using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Helper; +using Flow.Launcher.Infrastructure.Hotkey; +using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.ViewModel; namespace Flow.Launcher.Resources.Pages { public partial class WelcomePage2 { - public Settings Settings { get; set; } + public Settings Settings { get; private set; } + private WelcomeViewModel _viewModel; protected override void OnNavigatedTo(NavigationEventArgs e) { - Settings = Ioc.Default.GetRequiredService(); + if (!IsInitialized) + { + Settings = Ioc.Default.GetRequiredService(); + _viewModel = Ioc.Default.GetRequiredService(); + InitializeComponent(); + } // Sometimes the navigation is not triggered by button click, // so we need to reset the page number - Ioc.Default.GetRequiredService().PageNum = 2; - InitializeComponent(); + _viewModel.PageNum = 2; + base.OnNavigatedTo(e); } [RelayCommand] diff --git a/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs index f59b65c1c..4a6610e61 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage3.xaml.cs @@ -7,15 +7,21 @@ namespace Flow.Launcher.Resources.Pages { public partial class WelcomePage3 { + public Settings Settings { get; private set; } + private WelcomeViewModel _viewModel; + protected override void OnNavigatedTo(NavigationEventArgs e) { - Settings = Ioc.Default.GetRequiredService(); + if (!IsInitialized) + { + Settings = Ioc.Default.GetRequiredService(); + _viewModel = Ioc.Default.GetRequiredService(); + InitializeComponent(); + } // Sometimes the navigation is not triggered by button click, // so we need to reset the page number - Ioc.Default.GetRequiredService().PageNum = 3; - InitializeComponent(); + _viewModel.PageNum = 3; + base.OnNavigatedTo(e); } - - public Settings Settings { get; set; } } } diff --git a/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs index 4c83f3a83..1ee3284d2 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage4.xaml.cs @@ -1,21 +1,27 @@ -using CommunityToolkit.Mvvm.DependencyInjection; +using System.Windows.Navigation; +using CommunityToolkit.Mvvm.DependencyInjection; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.ViewModel; -using System.Windows.Navigation; namespace Flow.Launcher.Resources.Pages { public partial class WelcomePage4 { + public Settings Settings { get; private set; } + private WelcomeViewModel _viewModel; + protected override void OnNavigatedTo(NavigationEventArgs e) { - Settings = Ioc.Default.GetRequiredService(); + if (!IsInitialized) + { + Settings = Ioc.Default.GetRequiredService(); + _viewModel = Ioc.Default.GetRequiredService(); + InitializeComponent(); + } // Sometimes the navigation is not triggered by button click, // so we need to reset the page number - Ioc.Default.GetRequiredService().PageNum = 4; - InitializeComponent(); + _viewModel.PageNum = 4; + base.OnNavigatedTo(e); } - - public Settings Settings { get; set; } } } diff --git a/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs b/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs index 95d7ff1a0..6328c9914 100644 --- a/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs +++ b/Flow.Launcher/Resources/Pages/WelcomePage5.xaml.cs @@ -1,54 +1,74 @@ -using System.Windows; +using System; +using System.Windows; using System.Windows.Navigation; -using Flow.Launcher.Infrastructure.UserSettings; -using Microsoft.Win32; -using Flow.Launcher.Infrastructure; using CommunityToolkit.Mvvm.DependencyInjection; +using Flow.Launcher.Helper; +using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.ViewModel; namespace Flow.Launcher.Resources.Pages { public partial class WelcomePage5 { - private const string StartupPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; - public Settings Settings { get; set; } - public bool HideOnStartup { get; set; } + public Settings Settings { get; private set; } + private WelcomeViewModel _viewModel; protected override void OnNavigatedTo(NavigationEventArgs e) { - Settings = Ioc.Default.GetRequiredService(); + if (!IsInitialized) + { + Settings = Ioc.Default.GetRequiredService(); + _viewModel = Ioc.Default.GetRequiredService(); + InitializeComponent(); + } // Sometimes the navigation is not triggered by button click, // so we need to reset the page number - Ioc.Default.GetRequiredService().PageNum = 5; - InitializeComponent(); + _viewModel.PageNum = 5; + base.OnNavigatedTo(e); } private void OnAutoStartupChecked(object sender, RoutedEventArgs e) { - SetStartup(); - } - private void OnAutoStartupUncheck(object sender, RoutedEventArgs e) - { - RemoveStartup(); + ChangeAutoStartup(true); } - private void RemoveStartup() + private void OnAutoStartupUncheck(object sender, RoutedEventArgs e) { - using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); - key?.DeleteValue(Constant.FlowLauncher, false); - Settings.StartFlowLauncherOnSystemStartup = false; + ChangeAutoStartup(false); } - private void SetStartup() + + private void ChangeAutoStartup(bool value) { - using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); - key?.SetValue(Constant.FlowLauncher, Constant.ExecutablePath); - Settings.StartFlowLauncherOnSystemStartup = true; + Settings.StartFlowLauncherOnSystemStartup = value; + try + { + if (value) + { + if (Settings.UseLogonTaskForStartup) + { + AutoStartup.ChangeToViaLogonTask(); + } + else + { + AutoStartup.ChangeToViaRegistry(); + } + } + else + { + AutoStartup.DisableViaLogonTaskAndRegistry(); + } + } + catch (Exception e) + { + App.API.ShowMsg(App.API.GetTranslation("setAutoStartFailed"), e.Message); + } } private void OnHideOnStartupChecked(object sender, RoutedEventArgs e) { Settings.HideOnStartup = true; } + private void OnHideOnStartupUnchecked(object sender, RoutedEventArgs e) { Settings.HideOnStartup = false; @@ -59,6 +79,5 @@ namespace Flow.Launcher.Resources.Pages var window = Window.GetWindow(this); window.Close(); } - } } diff --git a/Flow.Launcher/SelectBrowserWindow.xaml.cs b/Flow.Launcher/SelectBrowserWindow.xaml.cs index 849056ab1..bea5b4352 100644 --- a/Flow.Launcher/SelectBrowserWindow.xaml.cs +++ b/Flow.Launcher/SelectBrowserWindow.xaml.cs @@ -10,13 +10,14 @@ namespace Flow.Launcher [INotifyPropertyChanged] public partial class SelectBrowserWindow : Window { - public Settings Settings { get; } + private readonly Settings _settings; private int selectedCustomBrowserIndex; public int SelectedCustomBrowserIndex { - get => selectedCustomBrowserIndex; set + get => selectedCustomBrowserIndex; + set { selectedCustomBrowserIndex = value; OnPropertyChanged(nameof(CustomBrowser)); @@ -27,9 +28,9 @@ namespace Flow.Launcher public CustomBrowserViewModel CustomBrowser => CustomBrowsers[SelectedCustomBrowserIndex]; public SelectBrowserWindow(Settings settings) { - Settings = settings; - CustomBrowsers = new ObservableCollection(Settings.CustomBrowserList.Select(x => x.Copy())); - SelectedCustomBrowserIndex = Settings.CustomBrowserIndex; + _settings = settings; + CustomBrowsers = new ObservableCollection(_settings.CustomBrowserList.Select(x => x.Copy())); + SelectedCustomBrowserIndex = _settings.CustomBrowserIndex; InitializeComponent(); } @@ -40,8 +41,8 @@ namespace Flow.Launcher private void btnDone_Click(object sender, RoutedEventArgs e) { - Settings.CustomBrowserList = CustomBrowsers.ToList(); - Settings.CustomBrowserIndex = SelectedCustomBrowserIndex; + _settings.CustomBrowserList = CustomBrowsers.ToList(); + _settings.CustomBrowserIndex = SelectedCustomBrowserIndex; Close(); } diff --git a/Flow.Launcher/SelectFileManagerWindow.xaml.cs b/Flow.Launcher/SelectFileManagerWindow.xaml.cs index 946052e32..bf94ddacb 100644 --- a/Flow.Launcher/SelectFileManagerWindow.xaml.cs +++ b/Flow.Launcher/SelectFileManagerWindow.xaml.cs @@ -12,13 +12,14 @@ namespace Flow.Launcher [INotifyPropertyChanged] public partial class SelectFileManagerWindow : Window { - public Settings Settings { get; } + private readonly Settings _settings; private int selectedCustomExplorerIndex; public int SelectedCustomExplorerIndex { - get => selectedCustomExplorerIndex; set + get => selectedCustomExplorerIndex; + set { selectedCustomExplorerIndex = value; OnPropertyChanged(nameof(CustomExplorer)); @@ -29,9 +30,9 @@ namespace Flow.Launcher public CustomExplorerViewModel CustomExplorer => CustomExplorers[SelectedCustomExplorerIndex]; public SelectFileManagerWindow(Settings settings) { - Settings = settings; - CustomExplorers = new ObservableCollection(Settings.CustomExplorerList.Select(x => x.Copy())); - SelectedCustomExplorerIndex = Settings.CustomExplorerIndex; + _settings = settings; + CustomExplorers = new ObservableCollection(_settings.CustomExplorerList.Select(x => x.Copy())); + SelectedCustomExplorerIndex = _settings.CustomExplorerIndex; InitializeComponent(); } @@ -42,8 +43,8 @@ namespace Flow.Launcher private void btnDone_Click(object sender, RoutedEventArgs e) { - Settings.CustomExplorerList = CustomExplorers.ToList(); - Settings.CustomExplorerIndex = SelectedCustomExplorerIndex; + _settings.CustomExplorerList = CustomExplorers.ToList(); + _settings.CustomExplorerIndex = SelectedCustomExplorerIndex; Close(); } diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs index 6b56caf5e..c8b83c730 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs @@ -17,6 +17,7 @@ namespace Flow.Launcher.SettingPages.ViewModels; public partial class SettingsPaneGeneralViewModel : BaseModel { public Settings Settings { get; } + private readonly Updater _updater; private readonly IPortable _portable; private readonly Internationalization _translater; @@ -78,7 +79,7 @@ public partial class SettingsPaneGeneralViewModel : BaseModel { try { - if (UseLogonTaskForStartup) + if (value) { AutoStartup.ChangeToViaLogonTask(); } diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPanePluginStoreViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPanePluginStoreViewModel.cs index fd2c8e09f..68c69f841 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPanePluginStoreViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPanePluginStoreViewModel.cs @@ -2,7 +2,6 @@ using System.Linq; using System.Threading.Tasks; using CommunityToolkit.Mvvm.Input; -using Flow.Launcher.Infrastructure; using Flow.Launcher.Plugin; using Flow.Launcher.ViewModel;