Improve code quality

This commit is contained in:
Jack251970 2025-04-24 19:48:42 +08:00
parent d867875cde
commit d01c67f433
12 changed files with 121 additions and 83 deletions

View file

@ -10,14 +10,14 @@ namespace Flow.Launcher
{ {
public partial class CustomQueryHotkeySetting : Window public partial class CustomQueryHotkeySetting : Window
{ {
public Settings Settings { get; } private readonly Settings _settings;
private bool update; private bool update;
private CustomPluginHotkey updateCustomHotkey; private CustomPluginHotkey updateCustomHotkey;
public CustomQueryHotkeySetting(Settings settings) public CustomQueryHotkeySetting(Settings settings)
{ {
Settings = settings; _settings = settings;
InitializeComponent(); InitializeComponent();
} }
@ -30,13 +30,13 @@ namespace Flow.Launcher
{ {
if (!update) if (!update)
{ {
Settings.CustomPluginHotkeys ??= new ObservableCollection<CustomPluginHotkey>(); _settings.CustomPluginHotkeys ??= new ObservableCollection<CustomPluginHotkey>();
var pluginHotkey = new CustomPluginHotkey var pluginHotkey = new CustomPluginHotkey
{ {
Hotkey = HotkeyControl.CurrentHotkey.ToString(), ActionKeyword = tbAction.Text Hotkey = HotkeyControl.CurrentHotkey.ToString(), ActionKeyword = tbAction.Text
}; };
Settings.CustomPluginHotkeys.Add(pluginHotkey); _settings.CustomPluginHotkeys.Add(pluginHotkey);
HotKeyMapper.SetCustomQueryHotkey(pluginHotkey); HotKeyMapper.SetCustomQueryHotkey(pluginHotkey);
} }
@ -55,7 +55,7 @@ namespace Flow.Launcher
public void UpdateItem(CustomPluginHotkey item) public void UpdateItem(CustomPluginHotkey item)
{ {
updateCustomHotkey = Settings.CustomPluginHotkeys.FirstOrDefault(o => updateCustomHotkey = _settings.CustomPluginHotkeys.FirstOrDefault(o =>
o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey); o.ActionKeyword == item.ActionKeyword && o.Hotkey == item.Hotkey);
if (updateCustomHotkey == null) if (updateCustomHotkey == null)
{ {

View file

@ -1,15 +1,11 @@
using System.Windows; using System.Windows;
using System.Windows.Input; using System.Windows.Input;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.SettingPages.ViewModels; using Flow.Launcher.SettingPages.ViewModels;
namespace Flow.Launcher namespace Flow.Launcher
{ {
public partial class CustomShortcutSetting : Window public partial class CustomShortcutSetting : Window
{ {
public Settings Settings { get; } = Ioc.Default.GetRequiredService<Settings>();
private readonly SettingsPaneHotkeyViewModel _hotkeyVm; private readonly SettingsPaneHotkeyViewModel _hotkeyVm;
public string Key { get; set; } = string.Empty; public string Key { get; set; } = string.Empty;
public string Value { get; set; } = string.Empty; public string Value { get; set; } = string.Empty;

View file

@ -42,14 +42,11 @@ namespace Flow.Launcher.Resources.Controls
private static void keyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) private static void keyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{ {
var control = d as UserControl; if (d is not UserControl) return; // This should not be possible
if (null == control) return; // This should not be possible
var newValue = e.NewValue as string; if (e.NewValue is not string newValue) return;
if (null == newValue) return;
if (d is not HotkeyDisplay hotkeyDisplay) if (d is not HotkeyDisplay hotkeyDisplay) return;
return;
hotkeyDisplay.Values.Clear(); hotkeyDisplay.Values.Clear();
foreach (var key in newValue.Split('+')) foreach (var key in newValue.Split('+'))

View file

@ -1,29 +1,35 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Windows.Navigation; using System.Windows.Navigation;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Core.Resource;
using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Core.Resource;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.ViewModel; using Flow.Launcher.ViewModel;
namespace Flow.Launcher.Resources.Pages namespace Flow.Launcher.Resources.Pages
{ {
public partial class WelcomePage1 public partial class WelcomePage1
{ {
public Settings Settings { get; private set; }
private WelcomeViewModel _viewModel;
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
Settings = Ioc.Default.GetRequiredService<Settings>(); if (!IsInitialized)
{
Settings = Ioc.Default.GetRequiredService<Settings>();
_viewModel = Ioc.Default.GetRequiredService<WelcomeViewModel>();
InitializeComponent();
}
// Sometimes the navigation is not triggered by button click, // Sometimes the navigation is not triggered by button click,
// so we need to reset the page number // so we need to reset the page number
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 1; _viewModel.PageNum = 1;
InitializeComponent(); base.OnNavigatedTo(e);
} }
private readonly Internationalization _translater = Ioc.Default.GetRequiredService<Internationalization>(); private readonly Internationalization _translater = Ioc.Default.GetRequiredService<Internationalization>();
public List<Language> Languages => _translater.LoadAvailableLanguages(); public List<Language> Languages => _translater.LoadAvailableLanguages();
public Settings Settings { get; set; }
public string CustomLanguage public string CustomLanguage
{ {
get get

View file

@ -1,25 +1,31 @@
using Flow.Launcher.Helper; using System.Windows.Media;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.UserSettings;
using System.Windows.Navigation; using System.Windows.Navigation;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.ViewModel;
using System.Windows.Media;
using CommunityToolkit.Mvvm.DependencyInjection; 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 namespace Flow.Launcher.Resources.Pages
{ {
public partial class WelcomePage2 public partial class WelcomePage2
{ {
public Settings Settings { get; set; } public Settings Settings { get; private set; }
private WelcomeViewModel _viewModel;
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
Settings = Ioc.Default.GetRequiredService<Settings>(); if (!IsInitialized)
{
Settings = Ioc.Default.GetRequiredService<Settings>();
_viewModel = Ioc.Default.GetRequiredService<WelcomeViewModel>();
InitializeComponent();
}
// Sometimes the navigation is not triggered by button click, // Sometimes the navigation is not triggered by button click,
// so we need to reset the page number // so we need to reset the page number
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 2; _viewModel.PageNum = 2;
InitializeComponent(); base.OnNavigatedTo(e);
} }
[RelayCommand] [RelayCommand]

View file

@ -7,15 +7,21 @@ namespace Flow.Launcher.Resources.Pages
{ {
public partial class WelcomePage3 public partial class WelcomePage3
{ {
public Settings Settings { get; private set; }
private WelcomeViewModel _viewModel;
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
Settings = Ioc.Default.GetRequiredService<Settings>(); if (!IsInitialized)
{
Settings = Ioc.Default.GetRequiredService<Settings>();
_viewModel = Ioc.Default.GetRequiredService<WelcomeViewModel>();
InitializeComponent();
}
// Sometimes the navigation is not triggered by button click, // Sometimes the navigation is not triggered by button click,
// so we need to reset the page number // so we need to reset the page number
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 3; _viewModel.PageNum = 3;
InitializeComponent(); base.OnNavigatedTo(e);
} }
public Settings Settings { get; set; }
} }
} }

View file

@ -1,21 +1,27 @@
using CommunityToolkit.Mvvm.DependencyInjection; using System.Windows.Navigation;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.ViewModel; using Flow.Launcher.ViewModel;
using System.Windows.Navigation;
namespace Flow.Launcher.Resources.Pages namespace Flow.Launcher.Resources.Pages
{ {
public partial class WelcomePage4 public partial class WelcomePage4
{ {
public Settings Settings { get; private set; }
private WelcomeViewModel _viewModel;
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
Settings = Ioc.Default.GetRequiredService<Settings>(); if (!IsInitialized)
{
Settings = Ioc.Default.GetRequiredService<Settings>();
_viewModel = Ioc.Default.GetRequiredService<WelcomeViewModel>();
InitializeComponent();
}
// Sometimes the navigation is not triggered by button click, // Sometimes the navigation is not triggered by button click,
// so we need to reset the page number // so we need to reset the page number
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 4; _viewModel.PageNum = 4;
InitializeComponent(); base.OnNavigatedTo(e);
} }
public Settings Settings { get; set; }
} }
} }

View file

@ -1,54 +1,74 @@
using System.Windows; using System;
using System.Windows;
using System.Windows.Navigation; using System.Windows.Navigation;
using Flow.Launcher.Infrastructure.UserSettings;
using Microsoft.Win32;
using Flow.Launcher.Infrastructure;
using CommunityToolkit.Mvvm.DependencyInjection; using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.ViewModel; using Flow.Launcher.ViewModel;
namespace Flow.Launcher.Resources.Pages namespace Flow.Launcher.Resources.Pages
{ {
public partial class WelcomePage5 public partial class WelcomePage5
{ {
private const string StartupPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; public Settings Settings { get; private set; }
public Settings Settings { get; set; } private WelcomeViewModel _viewModel;
public bool HideOnStartup { get; set; }
protected override void OnNavigatedTo(NavigationEventArgs e) protected override void OnNavigatedTo(NavigationEventArgs e)
{ {
Settings = Ioc.Default.GetRequiredService<Settings>(); if (!IsInitialized)
{
Settings = Ioc.Default.GetRequiredService<Settings>();
_viewModel = Ioc.Default.GetRequiredService<WelcomeViewModel>();
InitializeComponent();
}
// Sometimes the navigation is not triggered by button click, // Sometimes the navigation is not triggered by button click,
// so we need to reset the page number // so we need to reset the page number
Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 5; _viewModel.PageNum = 5;
InitializeComponent(); base.OnNavigatedTo(e);
} }
private void OnAutoStartupChecked(object sender, RoutedEventArgs e) private void OnAutoStartupChecked(object sender, RoutedEventArgs e)
{ {
SetStartup(); ChangeAutoStartup(true);
}
private void OnAutoStartupUncheck(object sender, RoutedEventArgs e)
{
RemoveStartup();
} }
private void RemoveStartup() private void OnAutoStartupUncheck(object sender, RoutedEventArgs e)
{ {
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); ChangeAutoStartup(false);
key?.DeleteValue(Constant.FlowLauncher, false);
Settings.StartFlowLauncherOnSystemStartup = false;
} }
private void SetStartup()
private void ChangeAutoStartup(bool value)
{ {
using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); Settings.StartFlowLauncherOnSystemStartup = value;
key?.SetValue(Constant.FlowLauncher, Constant.ExecutablePath); try
Settings.StartFlowLauncherOnSystemStartup = true; {
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) private void OnHideOnStartupChecked(object sender, RoutedEventArgs e)
{ {
Settings.HideOnStartup = true; Settings.HideOnStartup = true;
} }
private void OnHideOnStartupUnchecked(object sender, RoutedEventArgs e) private void OnHideOnStartupUnchecked(object sender, RoutedEventArgs e)
{ {
Settings.HideOnStartup = false; Settings.HideOnStartup = false;
@ -59,6 +79,5 @@ namespace Flow.Launcher.Resources.Pages
var window = Window.GetWindow(this); var window = Window.GetWindow(this);
window.Close(); window.Close();
} }
} }
} }

View file

@ -10,13 +10,14 @@ namespace Flow.Launcher
[INotifyPropertyChanged] [INotifyPropertyChanged]
public partial class SelectBrowserWindow : Window public partial class SelectBrowserWindow : Window
{ {
public Settings Settings { get; } private readonly Settings _settings;
private int selectedCustomBrowserIndex; private int selectedCustomBrowserIndex;
public int SelectedCustomBrowserIndex public int SelectedCustomBrowserIndex
{ {
get => selectedCustomBrowserIndex; set get => selectedCustomBrowserIndex;
set
{ {
selectedCustomBrowserIndex = value; selectedCustomBrowserIndex = value;
OnPropertyChanged(nameof(CustomBrowser)); OnPropertyChanged(nameof(CustomBrowser));
@ -27,9 +28,9 @@ namespace Flow.Launcher
public CustomBrowserViewModel CustomBrowser => CustomBrowsers[SelectedCustomBrowserIndex]; public CustomBrowserViewModel CustomBrowser => CustomBrowsers[SelectedCustomBrowserIndex];
public SelectBrowserWindow(Settings settings) public SelectBrowserWindow(Settings settings)
{ {
Settings = settings; _settings = settings;
CustomBrowsers = new ObservableCollection<CustomBrowserViewModel>(Settings.CustomBrowserList.Select(x => x.Copy())); CustomBrowsers = new ObservableCollection<CustomBrowserViewModel>(_settings.CustomBrowserList.Select(x => x.Copy()));
SelectedCustomBrowserIndex = Settings.CustomBrowserIndex; SelectedCustomBrowserIndex = _settings.CustomBrowserIndex;
InitializeComponent(); InitializeComponent();
} }
@ -40,8 +41,8 @@ namespace Flow.Launcher
private void btnDone_Click(object sender, RoutedEventArgs e) private void btnDone_Click(object sender, RoutedEventArgs e)
{ {
Settings.CustomBrowserList = CustomBrowsers.ToList(); _settings.CustomBrowserList = CustomBrowsers.ToList();
Settings.CustomBrowserIndex = SelectedCustomBrowserIndex; _settings.CustomBrowserIndex = SelectedCustomBrowserIndex;
Close(); Close();
} }

View file

@ -12,13 +12,14 @@ namespace Flow.Launcher
[INotifyPropertyChanged] [INotifyPropertyChanged]
public partial class SelectFileManagerWindow : Window public partial class SelectFileManagerWindow : Window
{ {
public Settings Settings { get; } private readonly Settings _settings;
private int selectedCustomExplorerIndex; private int selectedCustomExplorerIndex;
public int SelectedCustomExplorerIndex public int SelectedCustomExplorerIndex
{ {
get => selectedCustomExplorerIndex; set get => selectedCustomExplorerIndex;
set
{ {
selectedCustomExplorerIndex = value; selectedCustomExplorerIndex = value;
OnPropertyChanged(nameof(CustomExplorer)); OnPropertyChanged(nameof(CustomExplorer));
@ -29,9 +30,9 @@ namespace Flow.Launcher
public CustomExplorerViewModel CustomExplorer => CustomExplorers[SelectedCustomExplorerIndex]; public CustomExplorerViewModel CustomExplorer => CustomExplorers[SelectedCustomExplorerIndex];
public SelectFileManagerWindow(Settings settings) public SelectFileManagerWindow(Settings settings)
{ {
Settings = settings; _settings = settings;
CustomExplorers = new ObservableCollection<CustomExplorerViewModel>(Settings.CustomExplorerList.Select(x => x.Copy())); CustomExplorers = new ObservableCollection<CustomExplorerViewModel>(_settings.CustomExplorerList.Select(x => x.Copy()));
SelectedCustomExplorerIndex = Settings.CustomExplorerIndex; SelectedCustomExplorerIndex = _settings.CustomExplorerIndex;
InitializeComponent(); InitializeComponent();
} }
@ -42,8 +43,8 @@ namespace Flow.Launcher
private void btnDone_Click(object sender, RoutedEventArgs e) private void btnDone_Click(object sender, RoutedEventArgs e)
{ {
Settings.CustomExplorerList = CustomExplorers.ToList(); _settings.CustomExplorerList = CustomExplorers.ToList();
Settings.CustomExplorerIndex = SelectedCustomExplorerIndex; _settings.CustomExplorerIndex = SelectedCustomExplorerIndex;
Close(); Close();
} }

View file

@ -17,6 +17,7 @@ namespace Flow.Launcher.SettingPages.ViewModels;
public partial class SettingsPaneGeneralViewModel : BaseModel public partial class SettingsPaneGeneralViewModel : BaseModel
{ {
public Settings Settings { get; } public Settings Settings { get; }
private readonly Updater _updater; private readonly Updater _updater;
private readonly IPortable _portable; private readonly IPortable _portable;
private readonly Internationalization _translater; private readonly Internationalization _translater;
@ -78,7 +79,7 @@ public partial class SettingsPaneGeneralViewModel : BaseModel
{ {
try try
{ {
if (UseLogonTaskForStartup) if (value)
{ {
AutoStartup.ChangeToViaLogonTask(); AutoStartup.ChangeToViaLogonTask();
} }

View file

@ -2,7 +2,6 @@
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Plugin; using Flow.Launcher.Plugin;
using Flow.Launcher.ViewModel; using Flow.Launcher.ViewModel;