From 7a9ee6e9e21f5fdfa7a2dac9d5ce7a97a5a502d5 Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 1 Jul 2025 19:39:27 +0800 Subject: [PATCH] Each window can remember its size --- v2rayN/ServiceLib/Handler/ConfigHandler.cs | 31 +++++++++++ v2rayN/ServiceLib/Models/ConfigItems.cs | 13 +++-- v2rayN/ServiceLib/Models/IPAPIInfo.cs | 2 +- v2rayN/ServiceLib/Models/ProfileItem.cs | 2 +- v2rayN/v2rayN.Desktop/Base/WindowBase.cs | 52 +++++++++++++++++++ .../Views/AddServer2Window.axaml.cs | 4 +- .../Views/AddServerWindow.axaml.cs | 4 +- .../Views/DNSSettingWindow.axaml.cs | 4 +- .../Views/GlobalHotkeySettingWindow.axaml.cs | 4 +- .../v2rayN.Desktop/Views/MainWindow.axaml.cs | 20 ++++--- .../Views/OptionSettingWindow.axaml.cs | 4 +- .../Views/RoutingRuleDetailsWindow.axaml.cs | 4 +- .../Views/RoutingRuleSettingWindow.axaml.cs | 4 +- .../Views/RoutingSettingWindow.axaml.cs | 4 +- .../Views/SubEditWindow.axaml.cs | 4 +- .../Views/SubSettingWindow.axaml.cs | 4 +- v2rayN/v2rayN/App.xaml | 2 +- v2rayN/v2rayN/Base/WindowBase.cs | 42 +++++++++++++++ v2rayN/v2rayN/Views/AddServer2Window.xaml | 5 +- v2rayN/v2rayN/Views/AddServerWindow.xaml | 5 +- v2rayN/v2rayN/Views/DNSSettingWindow.xaml | 5 +- .../Views/GlobalHotkeySettingWindow.xaml | 5 +- v2rayN/v2rayN/Views/MainWindow.xaml | 9 ++-- v2rayN/v2rayN/Views/MainWindow.xaml.cs | 22 +++----- v2rayN/v2rayN/Views/OptionSettingWindow.xaml | 7 ++- .../Views/RoutingRuleDetailsWindow.xaml | 5 +- .../Views/RoutingRuleSettingWindow.xaml | 5 +- v2rayN/v2rayN/Views/RoutingSettingWindow.xaml | 5 +- v2rayN/v2rayN/Views/StatusBarView.xaml | 6 +-- v2rayN/v2rayN/Views/SubEditWindow.xaml | 5 +- v2rayN/v2rayN/Views/SubSettingWindow.xaml | 5 +- 31 files changed, 212 insertions(+), 81 deletions(-) create mode 100644 v2rayN/v2rayN.Desktop/Base/WindowBase.cs create mode 100644 v2rayN/v2rayN/Base/WindowBase.cs diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index cc8f1efc..5fb5471e 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -101,6 +101,7 @@ public class ConfigHandler EnableAutoAdjustMainLvColWidth = true }; config.UiItem.MainColumnItem ??= new(); + config.UiItem.WindowSizeItem ??= new(); if (config.UiItem.CurrentLanguage.IsNullOrEmpty()) { @@ -2174,4 +2175,34 @@ public class ConfigHandler } #endregion Regional Presets + + #region UIItem + + public static WindowSizeItem? GetWindowSizeItem(Config config, string typeName) + { + var sizeItem = config?.UiItem?.WindowSizeItem?.FirstOrDefault(t => t.TypeName == typeName); + if (sizeItem == null || sizeItem.Width <= 0 || sizeItem.Height <= 0) + { + return null; + } + + return sizeItem; + } + + public static int SaveWindowSizeItem(Config config, string typeName, double width, double height) + { + var sizeItem = config?.UiItem?.WindowSizeItem?.FirstOrDefault(t => t.TypeName == typeName); + if (sizeItem == null) + { + sizeItem = new WindowSizeItem { TypeName = typeName }; + config.UiItem.WindowSizeItem.Add(sizeItem); + } + + sizeItem.Width = width; + sizeItem.Height = height; + + return 0; + } + + #endregion UIItem } diff --git a/v2rayN/ServiceLib/Models/ConfigItems.cs b/v2rayN/ServiceLib/Models/ConfigItems.cs index 08ce284b..bca32a53 100644 --- a/v2rayN/ServiceLib/Models/ConfigItems.cs +++ b/v2rayN/ServiceLib/Models/ConfigItems.cs @@ -89,8 +89,6 @@ public class UIItem { public bool EnableAutoAdjustMainLvColWidth { get; set; } public bool EnableUpdateSubOnlyRemarksExist { get; set; } - public double MainWidth { get; set; } - public double MainHeight { get; set; } public double MainGirdHeight1 { get; set; } public double MainGirdHeight2 { get; set; } public EGirdOrientation MainGirdOrientation { get; set; } = EGirdOrientation.Vertical; @@ -103,9 +101,10 @@ public class UIItem public bool DoubleClick2Activate { get; set; } public bool AutoHideStartup { get; set; } public bool Hide2TrayWhenClose { get; set; } - public List MainColumnItem { get; set; } public bool ShowInTaskbar { get; set; } public bool MacOSShowInDock { get; set; } + public List MainColumnItem { get; set; } + public List WindowSizeItem { get; set; } } [Serializable] @@ -246,3 +245,11 @@ public class Fragment4RayItem public string? Length { get; set; } public string? Interval { get; set; } } + +[Serializable] +public class WindowSizeItem +{ + public string TypeName { get; set; } + public double Width { get; set; } + public double Height { get; set; } +} diff --git a/v2rayN/ServiceLib/Models/IPAPIInfo.cs b/v2rayN/ServiceLib/Models/IPAPIInfo.cs index 7ba16727..86cfe111 100644 --- a/v2rayN/ServiceLib/Models/IPAPIInfo.cs +++ b/v2rayN/ServiceLib/Models/IPAPIInfo.cs @@ -4,7 +4,7 @@ internal class IPAPIInfo { public string? ip { get; set; } public string? clientIp { get; set; } - public string? ip_addr { get; set; } + public string? ip_addr { get; set; } public string? query { get; set; } public string? country { get; set; } public string? country_name { get; set; } diff --git a/v2rayN/ServiceLib/Models/ProfileItem.cs b/v2rayN/ServiceLib/Models/ProfileItem.cs index 9a11d002..bc4358b5 100644 --- a/v2rayN/ServiceLib/Models/ProfileItem.cs +++ b/v2rayN/ServiceLib/Models/ProfileItem.cs @@ -4,7 +4,7 @@ using SQLite; namespace ServiceLib.Models; [Serializable] -public class ProfileItem: ReactiveObject +public class ProfileItem : ReactiveObject { public ProfileItem() { diff --git a/v2rayN/v2rayN.Desktop/Base/WindowBase.cs b/v2rayN/v2rayN.Desktop/Base/WindowBase.cs new file mode 100644 index 00000000..ecdeb24f --- /dev/null +++ b/v2rayN/v2rayN.Desktop/Base/WindowBase.cs @@ -0,0 +1,52 @@ +using Avalonia; +using Avalonia.Interactivity; +using Avalonia.ReactiveUI; + +namespace v2rayN.Desktop.Base; + +public class WindowBase : ReactiveWindow where TViewModel : class +{ + public WindowBase() + { + Loaded += OnLoaded; + } + + private void ReactiveWindowBase_Closed(object? sender, EventArgs e) + { + throw new NotImplementedException(); + } + + protected virtual void OnLoaded(object? sender, RoutedEventArgs e) + { + try + { + var sizeItem = ConfigHandler.GetWindowSizeItem(AppHandler.Instance.Config, GetType().Name); + if (sizeItem == null) + { + return; + } + + Width = sizeItem.Width; + Height = sizeItem.Height; + + var workingArea = (Screens.ScreenFromWindow(this) ?? Screens.Primary).WorkingArea; + var scaling = VisualRoot is not null ? VisualRoot.RenderScaling : 1.0; + + var x = workingArea.X + ((workingArea.Width - (Width * scaling)) / 2); + var y = workingArea.Y + ((workingArea.Height - (Height * scaling)) / 2); + + Position = new PixelPoint((int)x, (int)y); + } + catch { } + } + + protected override void OnClosed(EventArgs e) + { + base.OnClosed(e); + try + { + ConfigHandler.SaveWindowSizeItem(AppHandler.Instance.Config, GetType().Name, Width, Height); + } + catch { } + } +} diff --git a/v2rayN/v2rayN.Desktop/Views/AddServer2Window.axaml.cs b/v2rayN/v2rayN.Desktop/Views/AddServer2Window.axaml.cs index 79b6ce7d..3c96871f 100644 --- a/v2rayN/v2rayN.Desktop/Views/AddServer2Window.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/AddServer2Window.axaml.cs @@ -1,12 +1,12 @@ using System.Reactive.Disposables; using Avalonia.Interactivity; -using Avalonia.ReactiveUI; using ReactiveUI; +using v2rayN.Desktop.Base; using v2rayN.Desktop.Common; namespace v2rayN.Desktop.Views; -public partial class AddServer2Window : ReactiveWindow +public partial class AddServer2Window : WindowBase { public AddServer2Window() { diff --git a/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs index d7cf39e2..64c43f6c 100644 --- a/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/AddServerWindow.axaml.cs @@ -1,12 +1,12 @@ using System.Reactive.Disposables; using Avalonia.Controls; using Avalonia.Interactivity; -using Avalonia.ReactiveUI; using ReactiveUI; +using v2rayN.Desktop.Base; namespace v2rayN.Desktop.Views; -public partial class AddServerWindow : ReactiveWindow +public partial class AddServerWindow : WindowBase { public AddServerWindow() { diff --git a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs index c687e153..347093f9 100644 --- a/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/DNSSettingWindow.axaml.cs @@ -1,11 +1,11 @@ using System.Reactive.Disposables; using Avalonia.Interactivity; -using Avalonia.ReactiveUI; using ReactiveUI; +using v2rayN.Desktop.Base; namespace v2rayN.Desktop.Views; -public partial class DNSSettingWindow : ReactiveWindow +public partial class DNSSettingWindow : WindowBase { private static Config _config; diff --git a/v2rayN/v2rayN.Desktop/Views/GlobalHotkeySettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/GlobalHotkeySettingWindow.axaml.cs index f73a49d9..a9f79765 100644 --- a/v2rayN/v2rayN.Desktop/Views/GlobalHotkeySettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/GlobalHotkeySettingWindow.axaml.cs @@ -3,13 +3,13 @@ using System.Text; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; -using Avalonia.ReactiveUI; using ReactiveUI; +using v2rayN.Desktop.Base; using v2rayN.Desktop.Handler; namespace v2rayN.Desktop.Views; -public partial class GlobalHotkeySettingWindow : ReactiveWindow +public partial class GlobalHotkeySettingWindow : WindowBase { private readonly List _textBoxKeyEventItem = new(); diff --git a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs index 6aa89db6..63646201 100644 --- a/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/MainWindow.axaml.cs @@ -5,18 +5,18 @@ using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Controls.Notifications; using Avalonia.Input; using Avalonia.Interactivity; -using Avalonia.ReactiveUI; using Avalonia.Threading; using DialogHostAvalonia; using MsBox.Avalonia.Enums; using ReactiveUI; using Splat; +using v2rayN.Desktop.Base; using v2rayN.Desktop.Common; using v2rayN.Desktop.Handler; namespace v2rayN.Desktop.Views; -public partial class MainWindow : ReactiveWindow +public partial class MainWindow : WindowBase { private static Config _config; private WindowNotificationManager? _manager; @@ -154,7 +154,6 @@ public partial class MainWindow : ReactiveWindow } menuAddServerViaScan.IsVisible = false; - RestoreUI(); AddHelpMenuItem(); MessageBus.Current.Listen(EMsgCommand.AppExit.ToString()).Subscribe(StorageUI); } @@ -436,14 +435,14 @@ public partial class MainWindow : ReactiveWindow _config.UiItem.ShowInTaskbar = bl; } + protected override void OnLoaded(object? sender, RoutedEventArgs e) + { + base.OnLoaded(sender, e); + RestoreUI(); + } + private void RestoreUI() { - if (_config.UiItem.MainWidth > 0 && _config.UiItem.MainHeight > 0) - { - Width = _config.UiItem.MainWidth; - Height = _config.UiItem.MainHeight; - } - if (_config.UiItem.MainGirdHeight1 > 0 && _config.UiItem.MainGirdHeight2 > 0) { if (_config.UiItem.MainGirdOrientation == EGirdOrientation.Horizontal) @@ -461,8 +460,7 @@ public partial class MainWindow : ReactiveWindow private void StorageUI(string? n = null) { - _config.UiItem.MainWidth = this.Width; - _config.UiItem.MainHeight = this.Height; + ConfigHandler.SaveWindowSizeItem(_config, GetType().Name, Width, Height); if (_config.UiItem.MainGirdOrientation == EGirdOrientation.Horizontal) { diff --git a/v2rayN/v2rayN.Desktop/Views/OptionSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/OptionSettingWindow.axaml.cs index fd319d92..f417e498 100644 --- a/v2rayN/v2rayN.Desktop/Views/OptionSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/OptionSettingWindow.axaml.cs @@ -1,11 +1,11 @@ using System.Reactive.Disposables; using Avalonia.Controls; -using Avalonia.ReactiveUI; using ReactiveUI; +using v2rayN.Desktop.Base; namespace v2rayN.Desktop.Views; -public partial class OptionSettingWindow : ReactiveWindow +public partial class OptionSettingWindow : WindowBase { private static Config _config; diff --git a/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs index c6eead9f..35bc24a4 100644 --- a/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/RoutingRuleDetailsWindow.axaml.cs @@ -1,12 +1,12 @@ using System.Reactive.Disposables; using Avalonia.Controls; using Avalonia.Interactivity; -using Avalonia.ReactiveUI; using ReactiveUI; +using v2rayN.Desktop.Base; namespace v2rayN.Desktop.Views; -public partial class RoutingRuleDetailsWindow : ReactiveWindow +public partial class RoutingRuleDetailsWindow : WindowBase { public RoutingRuleDetailsWindow() { diff --git a/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs index 1cae4c0b..37b9a1a8 100644 --- a/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/RoutingRuleSettingWindow.axaml.cs @@ -2,14 +2,14 @@ using System.Reactive.Disposables; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; -using Avalonia.ReactiveUI; using MsBox.Avalonia.Enums; using ReactiveUI; +using v2rayN.Desktop.Base; using v2rayN.Desktop.Common; namespace v2rayN.Desktop.Views; -public partial class RoutingRuleSettingWindow : ReactiveWindow +public partial class RoutingRuleSettingWindow : WindowBase { public RoutingRuleSettingWindow() { diff --git a/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs index 2f187f8e..0e6f69cc 100644 --- a/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/RoutingSettingWindow.axaml.cs @@ -2,14 +2,14 @@ using System.Reactive.Disposables; using Avalonia.Controls; using Avalonia.Input; using Avalonia.Interactivity; -using Avalonia.ReactiveUI; using MsBox.Avalonia.Enums; using ReactiveUI; +using v2rayN.Desktop.Base; using v2rayN.Desktop.Common; namespace v2rayN.Desktop.Views; -public partial class RoutingSettingWindow : ReactiveWindow +public partial class RoutingSettingWindow : WindowBase { private bool _manualClose = false; diff --git a/v2rayN/v2rayN.Desktop/Views/SubEditWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/SubEditWindow.axaml.cs index 6c0f192e..b2fcf442 100644 --- a/v2rayN/v2rayN.Desktop/Views/SubEditWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/SubEditWindow.axaml.cs @@ -1,12 +1,12 @@ using System.Reactive.Disposables; using Avalonia; using Avalonia.Interactivity; -using Avalonia.ReactiveUI; using ReactiveUI; +using v2rayN.Desktop.Base; namespace v2rayN.Desktop.Views; -public partial class SubEditWindow : ReactiveWindow +public partial class SubEditWindow : WindowBase { public SubEditWindow() { diff --git a/v2rayN/v2rayN.Desktop/Views/SubSettingWindow.axaml.cs b/v2rayN/v2rayN.Desktop/Views/SubSettingWindow.axaml.cs index 7882449e..9ff0bcbd 100644 --- a/v2rayN/v2rayN.Desktop/Views/SubSettingWindow.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/SubSettingWindow.axaml.cs @@ -1,16 +1,16 @@ using System.Reactive.Disposables; using Avalonia.Controls; using Avalonia.Interactivity; -using Avalonia.ReactiveUI; using DialogHostAvalonia; using DynamicData; using MsBox.Avalonia.Enums; using ReactiveUI; +using v2rayN.Desktop.Base; using v2rayN.Desktop.Common; namespace v2rayN.Desktop.Views; -public partial class SubSettingWindow : ReactiveWindow +public partial class SubSettingWindow : WindowBase { private bool _manualClose = false; diff --git a/v2rayN/v2rayN/App.xaml b/v2rayN/v2rayN/App.xaml index 49a31268..e6b7e3af 100644 --- a/v2rayN/v2rayN/App.xaml +++ b/v2rayN/v2rayN/App.xaml @@ -215,7 +215,7 @@ - +