Update MsgView.axaml.cs

This commit is contained in:
xujie86 2025-09-25 06:57:58 +08:00 committed by GitHub
parent 6d45efc9e6
commit fafdbb42ca
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,12 +5,15 @@ using Avalonia.ReactiveUI;
using Avalonia.Threading; using Avalonia.Threading;
using ReactiveUI; using ReactiveUI;
using v2rayN.Desktop.Common; using v2rayN.Desktop.Common;
using System.Text;
namespace v2rayN.Desktop.Views; namespace v2rayN.Desktop.Views;
public partial class MsgView : ReactiveUserControl<MsgViewModel> public partial class MsgView : ReactiveUserControl<MsgViewModel>
{ {
private readonly ScrollViewer _scrollViewer; private readonly ScrollViewer _scrollViewer;
private const int _maxLines = 320; // 实际保留的行数
private const int _trimLines = 350; // 超过此阈值时裁剪
private int _lastShownLength = 0; private int _lastShownLength = 0;
public MsgView() public MsgView()
@ -32,8 +35,7 @@ public partial class MsgView : ReactiveUserControl<MsgViewModel>
switch (action) switch (action)
{ {
case EViewAction.DispatcherShowMsg: case EViewAction.DispatcherShowMsg:
if (obj is null) if (obj is null) return false;
return false;
Dispatcher.UIThread.Post(() => Dispatcher.UIThread.Post(() =>
ShowMsg(obj), ShowMsg(obj),
@ -46,16 +48,20 @@ public partial class MsgView : ReactiveUserControl<MsgViewModel>
private void ShowMsg(object msg) private void ShowMsg(object msg)
{ {
var newText = msg?.ToString() ?? string.Empty; var newText = msg?.ToString() ?? string.Empty;
txtMsg.Text += newText;
if (txtMsg.Text is { } old && newText.Length >= _lastShownLength && newText.AsSpan(0, _lastShownLength).SequenceEqual(old)) var lines = txtMsg.Text.Split(Environment.NewLine);
if (lines.Length > _trimLines)
{ {
var delta = newText.AsSpan(_lastShownLength); var sb = new StringBuilder();
if (!delta.IsEmpty) int start = lines.Length - _maxLines;
txtMsg.Text += delta.ToString(); for (int i = start; i < lines.Length; i++)
} {
else sb.Append(lines[i]);
{ if (i < lines.Length - 1)
txtMsg.Text = newText; sb.Append(Environment.NewLine);
}
txtMsg.Text = sb.ToString();
} }
_lastShownLength = txtMsg.Text.Length; _lastShownLength = txtMsg.Text.Length;
@ -70,6 +76,7 @@ public partial class MsgView : ReactiveUserControl<MsgViewModel>
{ {
ViewModel?.ClearMsg(); ViewModel?.ClearMsg();
txtMsg.Text = ""; txtMsg.Text = "";
_lastShownLength = 0;
} }
private void menuMsgViewSelectAll_Click(object? sender, RoutedEventArgs e) private void menuMsgViewSelectAll_Click(object? sender, RoutedEventArgs e)