diff --git a/VPet.Solution/Models/GraphicsSettingModel.cs b/VPet.Solution/Models/SettingEditor/GraphicsSettingModel.cs similarity index 96% rename from VPet.Solution/Models/GraphicsSettingModel.cs rename to VPet.Solution/Models/SettingEditor/GraphicsSettingModel.cs index 515d8e7..9e67d3b 100644 --- a/VPet.Solution/Models/GraphicsSettingModel.cs +++ b/VPet.Solution/Models/SettingEditor/GraphicsSettingModel.cs @@ -1,9 +1,9 @@ using System.ComponentModel; using System.Windows; -namespace VPet.Solution.Models; +namespace VPet.Solution.Models.SettingEditor; -public class GraphicsSettingModel : ObservableClass +public class GraphicsSettingModel : ObservableClass { private double _zoomLevel = 1; diff --git a/VPet.Solution/Models/InteractiveSettingModel.cs b/VPet.Solution/Models/SettingEditor/InteractiveSettingModel.cs similarity index 99% rename from VPet.Solution/Models/InteractiveSettingModel.cs rename to VPet.Solution/Models/SettingEditor/InteractiveSettingModel.cs index 42ba843..50e0f61 100644 --- a/VPet.Solution/Models/InteractiveSettingModel.cs +++ b/VPet.Solution/Models/SettingEditor/InteractiveSettingModel.cs @@ -1,7 +1,7 @@ using HKW.HKWUtils.Observable; using VPet_Simulator.Core; -namespace VPet.Solution.Models; +namespace VPet.Solution.Models.SettingEditor; public class InteractiveSettingModel : ObservableClass { diff --git a/VPet.Solution/Models/SettingModel.cs b/VPet.Solution/Models/SettingEditor/SettingModel.cs similarity index 97% rename from VPet.Solution/Models/SettingModel.cs rename to VPet.Solution/Models/SettingEditor/SettingModel.cs index c158088..9ee6e71 100644 --- a/VPet.Solution/Models/SettingModel.cs +++ b/VPet.Solution/Models/SettingEditor/SettingModel.cs @@ -6,7 +6,7 @@ using System.Windows; using VPet.Solution.Properties; using VPet_Simulator.Windows.Interface; -namespace VPet.Solution.Models; +namespace VPet.Solution.Models.SettingEditor; public class SettingModel : ObservableClass { diff --git a/VPet.Solution/Models/SystemSettingModel.cs b/VPet.Solution/Models/SettingEditor/SystemSettingModel.cs similarity index 90% rename from VPet.Solution/Models/SystemSettingModel.cs rename to VPet.Solution/Models/SettingEditor/SystemSettingModel.cs index 91e412b..2386dd0 100644 --- a/VPet.Solution/Models/SystemSettingModel.cs +++ b/VPet.Solution/Models/SettingEditor/SystemSettingModel.cs @@ -1,6 +1,6 @@ -namespace VPet.Solution.Models; +namespace VPet.Solution.Models.SettingEditor; -public class SystemSettingModel : ObservableClass +public class SystemSettingModel : ObservableClass { /// /// 数据收集是否被禁止(当日) diff --git a/VPet.Solution/SimpleObservable/ObservableClass/ObservableClass.cs b/VPet.Solution/SimpleObservable/ObservableClass/ObservableClass.cs index b8e0794..f33b86d 100644 --- a/VPet.Solution/SimpleObservable/ObservableClass/ObservableClass.cs +++ b/VPet.Solution/SimpleObservable/ObservableClass/ObservableClass.cs @@ -23,6 +23,14 @@ public abstract class ObservableClass INotifyPropertyChangedX where TObject : ObservableClass { + public ObservableClass() + { + if (GetType() != typeof(TObject)) + throw new InvalidCastException( + $"Inconsistency between target type [{GetType().FullName}] and generic type [{typeof(TObject).FullName}]" + ); + } + #region OnPropertyChange /// /// 设置属性值 diff --git a/VPet.Solution/Utils/Expansions.cs b/VPet.Solution/Utils/Expansions.cs index 94d8aba..15e09ed 100644 --- a/VPet.Solution/Utils/Expansions.cs +++ b/VPet.Solution/Utils/Expansions.cs @@ -1,4 +1,5 @@ using System.Collections; +using System.ComponentModel; using System.Diagnostics; using System.Drawing; using System.Drawing.Imaging; @@ -321,6 +322,43 @@ public static class Extensions { return (T)(page.DataContext ??= new T()); } + + private static Dictionary _windowCloseStates = new(); + + public static void SetCloseState(this Window window, WindowCloseState state) + { + window.Closing -= WindowCloseState_Closing; + window.Closing += WindowCloseState_Closing; + _windowCloseStates[window] = state; + } + + public static void CloseX(this Window window) + { + _windowCloseStates.Remove(window); + window.Closing -= WindowCloseState_Closing; + window.Close(); + } + + private static void WindowCloseState_Closing(object sender, CancelEventArgs e) + { + if (sender is not Window window) + return; + if (_windowCloseStates.TryGetValue(window, out var state) is false) + return; + if (state is WindowCloseState.Close) + return; + e.Cancel = true; + window.Visibility = + state is WindowCloseState.Hidden ? Visibility.Hidden : Visibility.Collapsed; + return; + } +} + +public enum WindowCloseState +{ + Close, + Hidden, + Collapsed } /// diff --git a/VPet.Solution/VPet.Solution.csproj b/VPet.Solution/VPet.Solution.csproj index 3ff2d7a..425c9ce 100644 --- a/VPet.Solution/VPet.Solution.csproj +++ b/VPet.Solution/VPet.Solution.csproj @@ -95,27 +95,31 @@ - - - + + + - - - - - - + + + + + + + + SettingWindow.xaml + + CustomizedSettingPage.xaml - + DiagnosticSettingPage.xaml - + ModSettingPage.xaml - + SystemSettingPage.xaml @@ -130,19 +134,23 @@ MSBuild:Compile Designer - + + MSBuild:Compile + Designer + + Designer MSBuild:Compile - + Designer MSBuild:Compile - + Designer MSBuild:Compile - + Designer MSBuild:Compile @@ -154,13 +162,13 @@ App.xaml Code - - - - + + + + GraphicsSettingPage.xaml - + InteractiveSettingPage.xaml @@ -195,11 +203,11 @@ NativeStyles.xaml - + Designer MSBuild:Compile - + Designer MSBuild:Compile @@ -241,6 +249,10 @@ VPet-Simulator.Windows.Interface - + + + + + \ No newline at end of file diff --git a/VPet.Solution/ViewModels/MainWindowVM.cs b/VPet.Solution/ViewModels/MainWindowVM.cs index 9a71c46..5d52748 100644 --- a/VPet.Solution/ViewModels/MainWindowVM.cs +++ b/VPet.Solution/ViewModels/MainWindowVM.cs @@ -1,89 +1,9 @@ -using HKW.HKWUtils.Observable; -using LinePutScript; -using System; +using System; using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; -using VPet.Solution.Models; -using VPet_Simulator.Windows.Interface; namespace VPet.Solution.ViewModels; -public class MainWindowVM : ObservableClass -{ - public static MainWindowVM Current { get; private set; } - - private SettingModel _currentSettings; - public SettingModel CurrentSetting - { - get => _currentSettings; - set => SetProperty(ref _currentSettings, value); - } - - private readonly ObservableCollection _settings = new(); - - private IEnumerable _showSettings; - public IEnumerable ShowSettings - { - get => _showSettings; - set => SetProperty(ref _showSettings, value); - } - - private string _searchSetting; - public string SearchSetting - { - get => _searchSetting; - set => SetProperty(ref _searchSetting, value); - } - - public MainWindowVM() - { - Current = this; - ShowSettings = _settings; - - foreach (var s in LoadSettings()) - _settings.Add(s); - - PropertyChanged += MainWindowVM_PropertyChanged; - } - - private void MainWindowVM_PropertyChanged(object sender, PropertyChangedEventArgs e) - { - if (e.PropertyName == nameof(SearchSetting)) - { - if (string.IsNullOrWhiteSpace(SearchSetting)) - ShowSettings = _settings; - else - ShowSettings = _settings.Where( - s => s.Name.Contains(SearchSetting, StringComparison.OrdinalIgnoreCase) - ); - } - } - - public static IEnumerable LoadSettings() - { - foreach ( - var file in Directory - .EnumerateFiles(Environment.CurrentDirectory) - .Where( - (s) => - { - if (s.EndsWith(".lps") is false) - return false; - return Path.GetFileName(s).StartsWith("Setting"); - } - ) - ) - { - var setting = new Setting(File.ReadAllText(file)); - yield return new SettingModel(setting) - { - Name = Path.GetFileNameWithoutExtension(file), - FilePath = file - }; - } - } -} +public class MainWindowVM : ObservableClass { } diff --git a/VPet.Solution/ViewModels/CustomizedSettingPageVM.cs b/VPet.Solution/ViewModels/SettingEditor/CustomizedSettingPageVM.cs similarity index 76% rename from VPet.Solution/ViewModels/CustomizedSettingPageVM.cs rename to VPet.Solution/ViewModels/SettingEditor/CustomizedSettingPageVM.cs index b289aa8..7f38a71 100644 --- a/VPet.Solution/ViewModels/CustomizedSettingPageVM.cs +++ b/VPet.Solution/ViewModels/SettingEditor/CustomizedSettingPageVM.cs @@ -4,6 +4,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace VPet.Solution.ViewModels; +namespace VPet.Solution.ViewModels.SettingEditor; public class CustomizedSettingPageVM { } diff --git a/VPet.Solution/ViewModels/DiagnosticSettingPageVM.cs b/VPet.Solution/ViewModels/SettingEditor/DiagnosticSettingPageVM.cs similarity index 76% rename from VPet.Solution/ViewModels/DiagnosticSettingPageVM.cs rename to VPet.Solution/ViewModels/SettingEditor/DiagnosticSettingPageVM.cs index 30d8ed6..7d7b780 100644 --- a/VPet.Solution/ViewModels/DiagnosticSettingPageVM.cs +++ b/VPet.Solution/ViewModels/SettingEditor/DiagnosticSettingPageVM.cs @@ -4,6 +4,6 @@ using System.Linq; using System.Text; using System.Threading.Tasks; -namespace VPet.Solution.ViewModels; +namespace VPet.Solution.ViewModels.SettingEditor; public class DiagnosticSettingPageVM { } diff --git a/VPet.Solution/ViewModels/GraphicsSettingPageVM.cs b/VPet.Solution/ViewModels/SettingEditor/GraphicsSettingPageVM.cs similarity index 64% rename from VPet.Solution/ViewModels/GraphicsSettingPageVM.cs rename to VPet.Solution/ViewModels/SettingEditor/GraphicsSettingPageVM.cs index 8c9537b..a1d4f49 100644 --- a/VPet.Solution/ViewModels/GraphicsSettingPageVM.cs +++ b/VPet.Solution/ViewModels/SettingEditor/GraphicsSettingPageVM.cs @@ -5,8 +5,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using VPet.Solution.Models; +using VPet.Solution.Models.SettingEditor; -namespace VPet.Solution.ViewModels; +namespace VPet.Solution.ViewModels.SettingEditor; public class GraphicsSettingPageVM : ObservableClass { @@ -19,12 +20,12 @@ public class GraphicsSettingPageVM : ObservableClass public GraphicsSettingPageVM() { - MainWindowVM.Current.PropertyChangedX += Current_PropertyChangedX; + SettingWindowVM.Current.PropertyChangedX += Current_PropertyChangedX; } - private void Current_PropertyChangedX(MainWindowVM sender, PropertyChangedXEventArgs e) + private void Current_PropertyChangedX(SettingWindowVM sender, PropertyChangedXEventArgs e) { - if (e.PropertyName == nameof(MainWindowVM.CurrentSetting)) + if (e.PropertyName == nameof(SettingWindowVM.CurrentSetting)) { GraphicsSetting = sender.CurrentSetting.GraphicsSetting; } diff --git a/VPet.Solution/ViewModels/InteractiveSettingPageVM.cs b/VPet.Solution/ViewModels/SettingEditor/InteractiveSettingPageVM.cs similarity index 64% rename from VPet.Solution/ViewModels/InteractiveSettingPageVM.cs rename to VPet.Solution/ViewModels/SettingEditor/InteractiveSettingPageVM.cs index 31f5092..d368ebb 100644 --- a/VPet.Solution/ViewModels/InteractiveSettingPageVM.cs +++ b/VPet.Solution/ViewModels/SettingEditor/InteractiveSettingPageVM.cs @@ -4,8 +4,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using VPet.Solution.Models; +using VPet.Solution.Models.SettingEditor; -namespace VPet.Solution.ViewModels; +namespace VPet.Solution.ViewModels.SettingEditor; public class InteractiveSettingPageVM : ObservableClass { @@ -18,12 +19,12 @@ public class InteractiveSettingPageVM : ObservableClass +{ + public static SettingWindowVM Current { get; private set; } + + private SettingModel _currentSettings; + public SettingModel CurrentSetting + { + get => _currentSettings; + set => SetProperty(ref _currentSettings, value); + } + + private readonly ObservableCollection _settings = new(); + + private IEnumerable _showSettings; + public IEnumerable ShowSettings + { + get => _showSettings; + set => SetProperty(ref _showSettings, value); + } + + private string _searchSetting; + public string SearchSetting + { + get => _searchSetting; + set => SetProperty(ref _searchSetting, value); + } + + public SettingWindowVM() + { + Current = this; + ShowSettings = _settings; + + foreach (var s in LoadSettings()) + _settings.Add(s); + + PropertyChanged += MainWindowVM_PropertyChanged; + } + + private void MainWindowVM_PropertyChanged(object sender, PropertyChangedEventArgs e) + { + if (e.PropertyName == nameof(SearchSetting)) + { + if (string.IsNullOrWhiteSpace(SearchSetting)) + ShowSettings = _settings; + else + ShowSettings = _settings.Where( + s => s.Name.Contains(SearchSetting, StringComparison.OrdinalIgnoreCase) + ); + } + } + + public static IEnumerable LoadSettings() + { + foreach ( + var file in Directory + .EnumerateFiles(Environment.CurrentDirectory) + .Where( + (s) => + { + if (s.EndsWith(".lps") is false) + return false; + return Path.GetFileName(s).StartsWith("Setting"); + } + ) + ) + { + var setting = new Setting(File.ReadAllText(file)); + yield return new SettingModel(setting) + { + Name = Path.GetFileNameWithoutExtension(file), + FilePath = file + }; + } + } +} diff --git a/VPet.Solution/ViewModels/SystemSettingPageVM.cs b/VPet.Solution/ViewModels/SettingEditor/SystemSettingPageVM.cs similarity index 63% rename from VPet.Solution/ViewModels/SystemSettingPageVM.cs rename to VPet.Solution/ViewModels/SettingEditor/SystemSettingPageVM.cs index 427f061..2055e24 100644 --- a/VPet.Solution/ViewModels/SystemSettingPageVM.cs +++ b/VPet.Solution/ViewModels/SettingEditor/SystemSettingPageVM.cs @@ -5,8 +5,9 @@ using System.Linq; using System.Text; using System.Threading.Tasks; using VPet.Solution.Models; +using VPet.Solution.Models.SettingEditor; -namespace VPet.Solution.ViewModels; +namespace VPet.Solution.ViewModels.SettingEditor; public class SystemSettingPageVM : ObservableClass { @@ -19,12 +20,12 @@ public class SystemSettingPageVM : ObservableClass public SystemSettingPageVM() { - MainWindowVM.Current.PropertyChangedX += Current_PropertyChangedX; + SettingWindowVM.Current.PropertyChangedX += Current_PropertyChangedX; } - private void Current_PropertyChangedX(MainWindowVM sender, PropertyChangedXEventArgs e) + private void Current_PropertyChangedX(SettingWindowVM sender, PropertyChangedXEventArgs e) { - if (e.PropertyName == nameof(MainWindowVM.CurrentSetting)) + if (e.PropertyName == nameof(SettingWindowVM.CurrentSetting)) { SystemSetting = sender.CurrentSetting.SystemSetting; } diff --git a/VPet.Solution/Views/MainWindow.xaml b/VPet.Solution/Views/MainWindow.xaml index ed19f3f..f553737 100644 --- a/VPet.Solution/Views/MainWindow.xaml +++ b/VPet.Solution/Views/MainWindow.xaml @@ -15,122 +15,24 @@ MinWidth="400" MinHeight="200" d:DataContext="{d:DesignInstance Type=vm:MainWindowVM}" + WindowStartupLocation="CenterScreen" mc:Ignorable="d"> - - + + - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - +