Update MsgView.xaml.cs

This commit is contained in:
xujie86 2025-09-25 07:16:03 +08:00 committed by GitHub
parent fafdbb42ca
commit fff4c110ae
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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();
}