* Add avaloniaEdit for test * Adjust avaloniaEdit * Optimize and improve message function * Update build-linux.yml * Update MsgView.axaml * Update MsgView.axaml.cs --------- Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
92 lines
2.5 KiB
C#
92 lines
2.5 KiB
C#
using System.Reactive.Disposables;
|
|
using Avalonia.Interactivity;
|
|
using Avalonia.ReactiveUI;
|
|
using Avalonia.Threading;
|
|
using ReactiveUI;
|
|
using v2rayN.Desktop.Common;
|
|
|
|
namespace v2rayN.Desktop.Views;
|
|
|
|
public partial class MsgView : ReactiveUserControl<MsgViewModel>
|
|
{
|
|
private const int MaxLines = 350;
|
|
private const int KeepLines = 320;
|
|
|
|
public MsgView()
|
|
{
|
|
InitializeComponent();
|
|
ViewModel = new MsgViewModel(UpdateViewHandler);
|
|
|
|
this.WhenActivated(disposables =>
|
|
{
|
|
this.Bind(ViewModel, vm => vm.MsgFilter, v => v.cmbMsgFilter.Text).DisposeWith(disposables);
|
|
this.Bind(ViewModel, vm => vm.AutoRefresh, v => v.togAutoRefresh.IsChecked).DisposeWith(disposables);
|
|
});
|
|
}
|
|
|
|
private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
|
|
{
|
|
switch (action)
|
|
{
|
|
case EViewAction.DispatcherShowMsg:
|
|
if (obj is null)
|
|
return false;
|
|
|
|
Dispatcher.UIThread.Post(() => ShowMsg(obj),
|
|
DispatcherPriority.ApplicationIdle);
|
|
break;
|
|
}
|
|
return await Task.FromResult(true);
|
|
}
|
|
|
|
private void ShowMsg(object msg)
|
|
{
|
|
txtMsg.AppendText(msg.ToString());
|
|
|
|
if (txtMsg.Document.LineCount > MaxLines)
|
|
{
|
|
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)
|
|
{
|
|
txtMsg.ScrollToEnd();
|
|
}
|
|
}
|
|
|
|
public void ClearMsg()
|
|
{
|
|
txtMsg.Text = string.Empty;
|
|
txtMsg.AppendText("----- Message cleared -----\n");
|
|
}
|
|
|
|
private void menuMsgViewSelectAll_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
Dispatcher.UIThread.Post(() =>
|
|
{
|
|
txtMsg.TextArea.Focus();
|
|
txtMsg.SelectAll();
|
|
}, DispatcherPriority.Render);
|
|
}
|
|
|
|
private async void menuMsgViewCopy_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
var data = txtMsg.SelectedText.TrimEx();
|
|
await AvaUtils.SetClipboardData(this, data);
|
|
}
|
|
|
|
private async void menuMsgViewCopyAll_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
var data = txtMsg.Text.TrimEx();
|
|
await AvaUtils.SetClipboardData(this, data);
|
|
}
|
|
|
|
private void menuMsgViewClear_Click(object? sender, RoutedEventArgs e)
|
|
{
|
|
ClearMsg();
|
|
}
|
|
}
|