mirror of
https://github.com/2dust/v2rayN.git
synced 2025-10-14 20:39:12 +00:00
Update MsgView.axaml.cs
This commit is contained in:
parent
d4d47b0d26
commit
1b9d7be553
1 changed files with 123 additions and 69 deletions
|
@ -1,102 +1,156 @@
|
||||||
using System.Reactive.Disposables;
|
using System.Collections.Concurrent;
|
||||||
using Avalonia.Controls;
|
using System.Reactive.Linq;
|
||||||
using Avalonia.Interactivity;
|
using System.Text;
|
||||||
using Avalonia.ReactiveUI;
|
using System.Text.RegularExpressions;
|
||||||
using Avalonia.Threading;
|
|
||||||
using ReactiveUI;
|
using ReactiveUI;
|
||||||
using v2rayN.Desktop.Common;
|
using ReactiveUI.Fody.Helpers;
|
||||||
|
|
||||||
namespace v2rayN.Desktop.Views;
|
namespace ServiceLib.ViewModels;
|
||||||
|
|
||||||
public partial class MsgView : ReactiveUserControl<MsgViewModel>
|
public class MsgViewModel : MyReactiveObject
|
||||||
{
|
{
|
||||||
private readonly ScrollViewer _scrollViewer;
|
private readonly ConcurrentQueue<string> _queueMsg = new();
|
||||||
private int _lastShownLength = 0;
|
private readonly int _numMaxMsg = 500;
|
||||||
|
private bool _lastMsgFilterNotAvailable;
|
||||||
|
private bool _blLockShow = false;
|
||||||
|
|
||||||
public MsgView()
|
private readonly Queue<string> _window = new();
|
||||||
|
private readonly int _maxLines = 350;
|
||||||
|
|
||||||
|
[Reactive]
|
||||||
|
public string MsgFilter { get; set; }
|
||||||
|
|
||||||
|
[Reactive]
|
||||||
|
public bool AutoRefresh { get; set; }
|
||||||
|
|
||||||
|
public MsgViewModel(Func<EViewAction, object?, Task<bool>>? updateView)
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
_config = AppManager.Instance.Config;
|
||||||
_scrollViewer = this.FindControl<ScrollViewer>("msgScrollViewer");
|
_updateView = updateView;
|
||||||
|
MsgFilter = _config.MsgUIItem.MainMsgFilter ?? string.Empty;
|
||||||
|
AutoRefresh = _config.MsgUIItem.AutoRefresh ?? true;
|
||||||
|
|
||||||
ViewModel = new MsgViewModel(UpdateViewHandler);
|
this.WhenAnyValue(x => x.MsgFilter)
|
||||||
|
.Subscribe(_ => DoMsgFilter());
|
||||||
|
|
||||||
this.WhenActivated(disposables =>
|
this.WhenAnyValue(x => x.AutoRefresh, y => y == true)
|
||||||
{
|
.Subscribe(_ => { _config.MsgUIItem.AutoRefresh = AutoRefresh; });
|
||||||
this.Bind(ViewModel, vm => vm.MsgFilter, v => v.cmbMsgFilter.Text).DisposeWith(disposables);
|
|
||||||
this.Bind(ViewModel, vm => vm.AutoRefresh, v => v.togAutoRefresh.IsChecked).DisposeWith(disposables);
|
AppEvents.SendMsgViewRequested
|
||||||
});
|
.AsObservable()
|
||||||
|
//.ObserveOn(RxApp.MainThreadScheduler)
|
||||||
|
.Subscribe(async content => await AppendQueueMsg(content));
|
||||||
}
|
}
|
||||||
|
|
||||||
private async Task<bool> UpdateViewHandler(EViewAction action, object? obj)
|
private async Task AppendQueueMsg(string msg)
|
||||||
{
|
{
|
||||||
switch (action)
|
//if (msg == Global.CommandClearMsg)
|
||||||
|
//{
|
||||||
|
// ClearMsg();
|
||||||
|
// return;
|
||||||
|
//}
|
||||||
|
if (AutoRefresh == false)
|
||||||
{
|
{
|
||||||
case EViewAction.DispatcherShowMsg:
|
return;
|
||||||
if (obj is null)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
Dispatcher.UIThread.Post(() =>
|
|
||||||
ShowMsg(obj),
|
|
||||||
DispatcherPriority.ApplicationIdle);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
return await Task.FromResult(true);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void ShowMsg(object msg)
|
_ = EnqueueQueueMsg(msg);
|
||||||
{
|
|
||||||
var newText = msg?.ToString() ?? string.Empty;
|
|
||||||
|
|
||||||
if (txtMsg.Text is { } old &&
|
if (_blLockShow)
|
||||||
newText.Length >= _lastShownLength &&
|
|
||||||
newText.AsSpan(0, _lastShownLength).SequenceEqual(old))
|
|
||||||
{
|
{
|
||||||
var delta = newText.AsSpan(_lastShownLength);
|
return;
|
||||||
if (!delta.IsEmpty)
|
}
|
||||||
txtMsg.Text += delta.ToString();
|
if (!_config.UiItem.ShowInTaskbar)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_blLockShow = true;
|
||||||
|
|
||||||
|
await Task.Delay(500);
|
||||||
|
|
||||||
|
var sb = new StringBuilder();
|
||||||
|
var needRebuild = false;
|
||||||
|
|
||||||
|
while (_queueMsg.TryDequeue(out var line))
|
||||||
|
{
|
||||||
|
_window.Enqueue(line);
|
||||||
|
if (_window.Count > _maxLines)
|
||||||
|
{
|
||||||
|
_window.Dequeue();
|
||||||
|
needRebuild = true;
|
||||||
|
}
|
||||||
|
if (!needRebuild)
|
||||||
|
{
|
||||||
|
sb.Append(line);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needRebuild)
|
||||||
|
{
|
||||||
|
var sbAll = new StringBuilder();
|
||||||
|
foreach (var s in _window)
|
||||||
|
{
|
||||||
|
sbAll.Append(s);
|
||||||
|
}
|
||||||
|
await _updateView?.Invoke(EViewAction.DispatcherShowMsg, sbAll.ToString());
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
txtMsg.Text = newText;
|
if (sb.Length > 0)
|
||||||
}
|
|
||||||
|
|
||||||
_lastShownLength = txtMsg.Text.Length;
|
|
||||||
|
|
||||||
if (togScrollToEnd.IsChecked ?? true)
|
|
||||||
{
|
{
|
||||||
Avalonia.Threading.Dispatcher.UIThread.Post(
|
await _updateView?.Invoke(EViewAction.DispatcherShowMsg, sb.ToString());
|
||||||
() => _scrollViewer?.ScrollToEnd(),
|
|
||||||
Avalonia.Threading.DispatcherPriority.Render);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_blLockShow = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task EnqueueQueueMsg(string msg)
|
||||||
|
{
|
||||||
|
//filter msg
|
||||||
|
if (MsgFilter.IsNotEmpty() && !_lastMsgFilterNotAvailable)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
if (!Regex.IsMatch(msg, MsgFilter))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
_queueMsg.Enqueue(ex.Message);
|
||||||
|
_lastMsgFilterNotAvailable = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 限制待处理队列规模,避免短时洪峰占用
|
||||||
|
if (_queueMsg.Count > _numMaxMsg)
|
||||||
|
{
|
||||||
|
for (int k = 0; k < _queueMsg.Count - _numMaxMsg; k++)
|
||||||
|
{
|
||||||
|
_queueMsg.TryDequeue(out _);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_queueMsg.Enqueue(msg);
|
||||||
|
if (!msg.EndsWith(Environment.NewLine))
|
||||||
|
{
|
||||||
|
_queueMsg.Enqueue(Environment.NewLine);
|
||||||
|
}
|
||||||
|
await Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
public void ClearMsg()
|
public void ClearMsg()
|
||||||
{
|
{
|
||||||
ViewModel?.ClearMsg();
|
_queueMsg.Clear();
|
||||||
txtMsg.Text = "";
|
_window.Clear();
|
||||||
_lastShownLength = 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void menuMsgViewSelectAll_Click(object? sender, RoutedEventArgs e)
|
private void DoMsgFilter()
|
||||||
{
|
{
|
||||||
txtMsg.Focus();
|
_config.MsgUIItem.MainMsgFilter = MsgFilter;
|
||||||
txtMsg.SelectAll();
|
_lastMsgFilterNotAvailable = false;
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue