From e3573f32d52b3e1e007516d4c818da5851a1cae0 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Fri, 25 Apr 2025 08:42:41 +0800 Subject: [PATCH] Improve code quality --- Flow.Launcher/SettingWindow.xaml.cs | 39 ++++++++++++++----- .../ViewModel/SettingWindowViewModel.cs | 8 ---- Flow.Launcher/WelcomeWindow.xaml.cs | 10 +++-- 3 files changed, 36 insertions(+), 21 deletions(-) diff --git a/Flow.Launcher/SettingWindow.xaml.cs b/Flow.Launcher/SettingWindow.xaml.cs index b4a3ae47a..cd3e5414f 100644 --- a/Flow.Launcher/SettingWindow.xaml.cs +++ b/Flow.Launcher/SettingWindow.xaml.cs @@ -1,6 +1,6 @@ using System; using System.Windows; -using System.Windows.Forms; +using System.Windows.Controls; using System.Windows.Input; using System.Windows.Interop; using CommunityToolkit.Mvvm.DependencyInjection; @@ -9,35 +9,45 @@ using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.SettingPages.Views; using Flow.Launcher.ViewModel; using ModernWpf.Controls; -using TextBox = System.Windows.Controls.TextBox; +using Screen = System.Windows.Forms.Screen; namespace Flow.Launcher; public partial class SettingWindow { + #region Private Fields + private readonly Settings _settings; - private readonly SettingWindowViewModel _viewModel; + + #endregion + + #region Constructor public SettingWindow() { - var viewModel = Ioc.Default.GetRequiredService(); _settings = Ioc.Default.GetRequiredService(); + var viewModel = Ioc.Default.GetRequiredService(); DataContext = viewModel; - _viewModel = viewModel; - InitializePosition(); InitializeComponent(); + + UpdatePositionAndState(); } + #endregion + + #region Window Events + private void OnLoaded(object sender, RoutedEventArgs e) { RefreshMaximizeRestoreButton(); + // Fix (workaround) for the window freezes after lock screen (Win+L) or sleep // https://stackoverflow.com/questions/4951058/software-rendering-mode-wpf HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource; HwndTarget hwndTarget = hwndSource.CompositionTarget; hwndTarget.RenderMode = RenderMode.SoftwareOnly; // Must use software only render mode here - InitializePosition(); + UpdatePositionAndState(); } private void OnClosed(object sender, EventArgs e) @@ -45,7 +55,7 @@ public partial class SettingWindow _settings.SettingWindowState = WindowState; _settings.SettingWindowTop = Top; _settings.SettingWindowLeft = Left; - _viewModel.Save(); + _settings.Save(); App.API.SavePluginSettings(); } @@ -104,7 +114,11 @@ public partial class SettingWindow RefreshMaximizeRestoreButton(); } - public void InitializePosition() + #endregion + + #region Window Position + + public void UpdatePositionAndState() { var previousTop = _settings.SettingWindowTop; var previousLeft = _settings.SettingWindowLeft; @@ -119,6 +133,7 @@ public partial class SettingWindow Top = previousTop.Value; Left = previousLeft.Value; } + WindowState = _settings.SettingWindowState; } @@ -155,6 +170,10 @@ public partial class SettingWindow return top; } + #endregion + + #region Navigation View Events + private void NavigationView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) { if (args.IsSettingsSelected) @@ -201,4 +220,6 @@ public partial class SettingWindow { NavView.SelectedItem ??= NavView.MenuItems[0]; /* Set First Page */ } + + #endregion } diff --git a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs index 0fe4557a7..17a1a2b50 100644 --- a/Flow.Launcher/ViewModel/SettingWindowViewModel.cs +++ b/Flow.Launcher/ViewModel/SettingWindowViewModel.cs @@ -12,14 +12,6 @@ public partial class SettingWindowViewModel : BaseModel _settings = settings; } - /// - /// Save Flow settings. Plugins settings are not included. - /// - public void Save() - { - _settings.Save(); - } - public double SettingWindowWidth { get => _settings.SettingWindowWidth; diff --git a/Flow.Launcher/WelcomeWindow.xaml.cs b/Flow.Launcher/WelcomeWindow.xaml.cs index 637f9448d..d23bd4937 100644 --- a/Flow.Launcher/WelcomeWindow.xaml.cs +++ b/Flow.Launcher/WelcomeWindow.xaml.cs @@ -2,16 +2,17 @@ using System.Windows; using System.Windows.Input; using System.Windows.Controls; -using Flow.Launcher.Resources.Pages; -using ModernWpf.Media.Animation; using CommunityToolkit.Mvvm.DependencyInjection; -using Flow.Launcher.ViewModel; using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.Resources.Pages; +using Flow.Launcher.ViewModel; +using ModernWpf.Media.Animation; namespace Flow.Launcher { public partial class WelcomeWindow : Window { + private readonly Settings _settings; private readonly WelcomeViewModel _viewModel; private readonly NavigationTransitionInfo _forwardTransitionInfo = new SlideNavigationTransitionInfo() @@ -25,6 +26,7 @@ namespace Flow.Launcher public WelcomeWindow() { + _settings = Ioc.Default.GetRequiredService(); _viewModel = Ioc.Default.GetRequiredService(); DataContext = _viewModel; InitializeComponent(); @@ -97,7 +99,7 @@ namespace Flow.Launcher private void Window_Closed(object sender, EventArgs e) { // Save settings when window is closed - Ioc.Default.GetRequiredService().Save(); + _settings.Save(); } } }