Compare commits

...

3 Commits

Author SHA1 Message Date
2dust 879c736924 Update Utils.cs
https://github.com/2dust/v2rayN/commit/5fed566a324569da59252d8ee8179e22ff21a035
2026-06-07 15:49:31 +08:00
sparklelcm333 8147b39723 fix: 在窗口初始化前设置保存的尺寸,消除启动时偏右下偏移 (#9498)
* fix: 在窗口初始化前设置保存的尺寸,消除启动时偏右下偏移

WindowStartupLocation="CenterScreen"(来自 XAML)能正确包含 chrome 居中窗口,
但只能使用生效时的 Width/Height。旧代码在 OnLoaded 中设置保存的尺寸为时已晚
(CenterScreen 已应用),随后又用不含 chrome 的手工计算覆盖 Position,
导致窗口每次启动偏右下。

修复方式:在 Initialized 事件中设置保存的 Width/Height。
Avalonia 中 Initialized 在 Show() 过程中触发,早于 WindowStartupLocation 生效,
因此 CenterScreen 能用正确的保存尺寸居中。

同时移除 OnLoaded 中重复的 Width/Height 设置(Initialized 中已设置)。

* Update WindowBase.cs

---------

Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
2026-06-07 14:59:50 +08:00
2dust 5fed566a32 Add ReplaceLineBreaks extension and Fix bug
https://github.com/2dust/v2rayN/issues/9497
2026-06-07 14:49:39 +08:00
3 changed files with 36 additions and 16 deletions
+16
View File
@@ -118,4 +118,20 @@ public static class Extension
destination.Add(item); destination.Add(item);
} }
} }
/// <summary>
/// Replace all cross-platform newline characters with the specified string
/// </summary>
public static string ReplaceLineBreaks(this string input, string replacement)
{
if (string.IsNullOrEmpty(input))
{
return input;
}
// You must replace \r\n first, and then replace the single characters \r and \n.
return input.Replace("\r\n", replacement)
.Replace("\r", replacement)
.Replace("\n", replacement);
}
} }
+5 -5
View File
@@ -51,7 +51,7 @@ public class Utils
try try
{ {
str = str.Replace(Environment.NewLine, string.Empty); str = str.ReplaceLineBreaks(string.Empty);
return new List<string>(str.Split(',', StringSplitOptions.RemoveEmptyEntries)); return new List<string>(str.Split(',', StringSplitOptions.RemoveEmptyEntries));
} }
catch (Exception ex) catch (Exception ex)
@@ -114,9 +114,7 @@ public class Utils
} }
plainText = plainText.Trim() plainText = plainText.Trim()
.Replace(Environment.NewLine, "") .ReplaceLineBreaks("")
.Replace("\n", "")
.Replace("\r", "")
.Replace('_', '/') .Replace('_', '/')
.Replace('-', '+') .Replace('-', '+')
.Replace(" ", ""); .Replace(" ", "");
@@ -321,7 +319,9 @@ public class Utils
return text; return text;
} }
return text.Replace(",", ",").Replace(Environment.NewLine, ","); return text.Replace(",", ",")
.Replace(" ", "")
.ReplaceLineBreaks(",");
} }
public static List<string> GetEnumNames<TEnum>() where TEnum : Enum public static List<string> GetEnumNames<TEnum>() where TEnum : Enum
+15 -11
View File
@@ -4,6 +4,7 @@ public class WindowBase<TViewModel> : ReactiveWindow<TViewModel> where TViewMode
{ {
public WindowBase() public WindowBase()
{ {
Initialized += OnWindowInitialized;
Loaded += OnLoaded; Loaded += OnLoaded;
} }
@@ -12,30 +13,33 @@ public class WindowBase<TViewModel> : ReactiveWindow<TViewModel> where TViewMode
throw new NotImplementedException(); throw new NotImplementedException();
} }
protected virtual void OnLoaded(object? sender, RoutedEventArgs e) private void OnWindowInitialized(object? sender, EventArgs e)
{ {
try try
{ {
var sizeItem = ConfigHandler.GetWindowSizeItem(AppManager.Instance.Config, GetType().Name); var sizeItem = ConfigHandler.GetWindowSizeItem(AppManager.Instance.Config, GetType().Name);
if (sizeItem == null) if (sizeItem is null)
{ {
return; return;
} }
Width = sizeItem.Width; if (sizeItem.Width > 0 && !Width.Equals(sizeItem.Width))
Height = sizeItem.Height; {
Width = sizeItem.Width;
}
var workingArea = (Screens.ScreenFromWindow(this) ?? Screens.Primary).WorkingArea; if (sizeItem.Height > 0 && !Height.Equals(sizeItem.Height))
var scaling = (Utils.IsMacOS() ? null : VisualRoot?.RenderScaling) ?? 1.0; {
Height = sizeItem.Height;
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 { } catch { }
} }
protected virtual void OnLoaded(object? sender, RoutedEventArgs e)
{
}
protected override void OnClosed(EventArgs e) protected override void OnClosed(EventArgs e)
{ {
base.OnClosed(e); base.OnClosed(e);