Update MsgView.axaml.cs

This commit is contained in:
JieXu 2025-09-26 13:34:08 +08:00 committed by GitHub
parent 004c695b6a
commit 10b18efcaf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -9,10 +9,12 @@ namespace v2rayN.Desktop.Views;
public partial class MsgView : ReactiveUserControl<MsgViewModel> public partial class MsgView : ReactiveUserControl<MsgViewModel>
{ {
private const int MaxLines = 350;
private const int KeepLines = 320;
public MsgView() public MsgView()
{ {
InitializeComponent(); InitializeComponent();
ViewModel = new MsgViewModel(UpdateViewHandler); ViewModel = new MsgViewModel(UpdateViewHandler);
this.WhenActivated(disposables => this.WhenActivated(disposables =>
@ -30,8 +32,7 @@ public partial class MsgView : ReactiveUserControl<MsgViewModel>
if (obj is null) if (obj is null)
return false; return false;
Dispatcher.UIThread.Post(() => Dispatcher.UIThread.Post(() => ShowMsg(obj),
ShowMsg(obj),
DispatcherPriority.ApplicationIdle); DispatcherPriority.ApplicationIdle);
break; break;
} }
@ -40,29 +41,35 @@ public partial class MsgView : ReactiveUserControl<MsgViewModel>
private void ShowMsg(object msg) private void ShowMsg(object msg)
{ {
if (txtMsg.Document.LineCount > ViewModel?.NumMaxMsg) txtMsg.AppendText(msg.ToString());
if (txtMsg.Document.LineCount > MaxLines)
{ {
ClearMsg(); var lc = txtMsg.Document.LineCount;
var cutLineNumber = lc - KeepLines;
var cutLine = txtMsg.Document.GetLineByNumber(cutLineNumber);
txtMsg.Document.Remove(0, cutLine.Offset);
} }
if (togScrollToEnd.IsChecked ?? true) if (togScrollToEnd.IsChecked ?? true)
{ {
//txtMsg.ScrollToLine(txtMsg.Document.LineCount);
txtMsg.ScrollToEnd(); txtMsg.ScrollToEnd();
} }
txtMsg.AppendText(msg.ToString());
} }
public void ClearMsg() public void ClearMsg()
{ {
txtMsg.Clear(); txtMsg.Text = string.Empty;
txtMsg.AppendText("----- Message cleared -----\n"); txtMsg.AppendText("----- Message cleared -----\n");
} }
private void menuMsgViewSelectAll_Click(object? sender, RoutedEventArgs e) private void menuMsgViewSelectAll_Click(object? sender, RoutedEventArgs e)
{ {
txtMsg.Focus(); Dispatcher.UIThread.Post(() =>
{
txtMsg.TextArea.Focus();
txtMsg.SelectAll(); txtMsg.SelectAll();
}, DispatcherPriority.Render);
} }
private async void menuMsgViewCopy_Click(object? sender, RoutedEventArgs e) private async void menuMsgViewCopy_Click(object? sender, RoutedEventArgs e)