From fff4c110ae020937c4027c1582156b64ccc07874 Mon Sep 17 00:00:00 2001 From: xujie86 <167618598+xujie86@users.noreply.github.com> Date: Thu, 25 Sep 2025 07:16:03 +0800 Subject: [PATCH] Update MsgView.xaml.cs --- v2rayN/v2rayN/Views/MsgView.xaml.cs | 46 ++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/v2rayN/v2rayN/Views/MsgView.xaml.cs b/v2rayN/v2rayN/Views/MsgView.xaml.cs index 3cc1fdfa..a905c729 100644 --- a/v2rayN/v2rayN/Views/MsgView.xaml.cs +++ b/v2rayN/v2rayN/Views/MsgView.xaml.cs @@ -1,4 +1,5 @@ using System.Reactive.Disposables; +using System.Text; using System.Windows; using System.Windows.Threading; using ReactiveUI; @@ -7,6 +8,9 @@ namespace v2rayN.Views; public partial class MsgView { + private const int _maxLines = 320; + private const int _trimLines = 350; + public MsgView() { InitializeComponent(); @@ -47,12 +51,52 @@ public partial class MsgView private void ShowMsg(object msg) { + var incoming = msg?.ToString() ?? string.Empty; + var old = txtMsg.Text ?? string.Empty; + txtMsg.BeginChange(); - txtMsg.Text = msg.ToString(); + + if (incoming.Length >= old.Length && incoming.AsSpan(0, old.Length).SequenceEqual(old)) + { + var delta = incoming.AsSpan(old.Length); + if (!delta.IsEmpty) + txtMsg.AppendText(delta.ToString()); + } + else + { + // 兼容增量:如果不是全量覆盖场景,直接把 incoming 当作增量追加 + if (old.Length == 0) + { + txtMsg.Text = incoming; + } + else + { + txtMsg.AppendText(incoming); + } + } + + // 行数超过阈值才裁剪到 _maxLines + var lines = txtMsg.Text.Split(Environment.NewLine); + if (lines.Length > _trimLines) + { + var start = lines.Length - _maxLines; + if (start < 0) start = 0; + + var sb = new StringBuilder(); + for (int i = start; i < lines.Length; i++) + { + sb.Append(lines[i]); + if (i < lines.Length - 1) + sb.Append(Environment.NewLine); + } + txtMsg.Text = sb.ToString(); + } + if (togScrollToEnd.IsChecked ?? true) { txtMsg.ScrollToEnd(); } + txtMsg.EndChange(); }