From d91b0afb9a00c1df80b2cf0b7470d16f884b339a Mon Sep 17 00:00:00 2001 From: 2dust <31833384+2dust@users.noreply.github.com> Date: Tue, 3 Sep 2024 16:03:26 +0800 Subject: [PATCH] Improved log message display --- v2rayN/ServiceLib/Enums/EViewAction.cs | 3 +- v2rayN/ServiceLib/Handler/ConfigHandler.cs | 30 +++-- v2rayN/ServiceLib/Models/Config.cs | 1 + v2rayN/ServiceLib/Models/ConfigItems.cs | 8 +- v2rayN/ServiceLib/ViewModels/MsgViewModel.cs | 112 +++++++++++++++++++ v2rayN/v2rayN.Desktop/Views/MsgView.axaml.cs | 98 ++++------------ v2rayN/v2rayN/Views/MsgView.xaml | 10 +- v2rayN/v2rayN/Views/MsgView.xaml.cs | 97 +++++----------- 8 files changed, 191 insertions(+), 168 deletions(-) create mode 100644 v2rayN/ServiceLib/ViewModels/MsgViewModel.cs diff --git a/v2rayN/ServiceLib/Enums/EViewAction.cs b/v2rayN/ServiceLib/Enums/EViewAction.cs index 9992daa3..acacbd6a 100644 --- a/v2rayN/ServiceLib/Enums/EViewAction.cs +++ b/v2rayN/ServiceLib/Enums/EViewAction.cs @@ -39,6 +39,7 @@ DispatcherRefreshServersBiz, DispatcherRefreshIcon, DispatcherCheckUpdate, - DispatcherCheckUpdateFinished, + DispatcherCheckUpdateFinished, + DispatcherShowMsg, } } \ No newline at end of file diff --git a/v2rayN/ServiceLib/Handler/ConfigHandler.cs b/v2rayN/ServiceLib/Handler/ConfigHandler.cs index 601bec23..40fd4370 100644 --- a/v2rayN/ServiceLib/Handler/ConfigHandler.cs +++ b/v2rayN/ServiceLib/Handler/ConfigHandler.cs @@ -124,20 +124,16 @@ namespace ServiceLib.Handler mtu = 9000, }; } - if (config.guiItem == null) + config.guiItem ??= new() { - config.guiItem = new() - { - enableStatistics = false, - }; - } - if (config.uiItem == null) + enableStatistics = false, + }; + config.msgUIItem ??= new(); + + config.uiItem ??= new UIItem() { - config.uiItem = new UIItem() - { - enableAutoAdjustMainLvColWidth = true - }; - } + enableAutoAdjustMainLvColWidth = true + }; if (config.uiItem.mainColumnItem == null) { config.uiItem.mainColumnItem = new(); @@ -174,11 +170,11 @@ namespace ServiceLib.Handler } config.mux4RayItem ??= new() - { - concurrency = 8, - xudpConcurrency = 16, - xudpProxyUDP443 = "reject" - }; + { + concurrency = 8, + xudpConcurrency = 16, + xudpProxyUDP443 = "reject" + }; if (config.mux4SboxItem == null) { diff --git a/v2rayN/ServiceLib/Models/Config.cs b/v2rayN/ServiceLib/Models/Config.cs index 1c943547..4062914c 100644 --- a/v2rayN/ServiceLib/Models/Config.cs +++ b/v2rayN/ServiceLib/Models/Config.cs @@ -38,6 +38,7 @@ public GrpcItem grpcItem { get; set; } public RoutingBasicItem routingBasicItem { get; set; } public GUIItem guiItem { get; set; } + public MsgUIItem msgUIItem { get; set; } public UIItem uiItem { get; set; } public ConstItem constItem { get; set; } public SpeedTestItem speedTestItem { get; set; } diff --git a/v2rayN/ServiceLib/Models/ConfigItems.cs b/v2rayN/ServiceLib/Models/ConfigItems.cs index 7ddcf376..b07c1442 100644 --- a/v2rayN/ServiceLib/Models/ConfigItems.cs +++ b/v2rayN/ServiceLib/Models/ConfigItems.cs @@ -107,6 +107,13 @@ public bool enableLog { get; set; } = true; } + [Serializable] + public class MsgUIItem + { + public string? mainMsgFilter { get; set; } + public bool? autoRefresh { get; set; } + } + [Serializable] public class UIItem { @@ -126,7 +133,6 @@ public bool enableDragDropSort { get; set; } public bool doubleClick2Activate { get; set; } public bool autoHideStartup { get; set; } - public string mainMsgFilter { get; set; } public List mainColumnItem { get; set; } public bool showInTaskbar { get; set; } } diff --git a/v2rayN/ServiceLib/ViewModels/MsgViewModel.cs b/v2rayN/ServiceLib/ViewModels/MsgViewModel.cs new file mode 100644 index 00000000..53e6c438 --- /dev/null +++ b/v2rayN/ServiceLib/ViewModels/MsgViewModel.cs @@ -0,0 +1,112 @@ +锘縰sing ReactiveUI; +using ReactiveUI.Fody.Helpers; +using Splat; +using System.Collections.Concurrent; +using System.Diagnostics; +using System.Text.RegularExpressions; + +namespace ServiceLib.ViewModels +{ + public class MsgViewModel : MyReactiveObject + { + private ConcurrentQueue _queueMsg = new(); + private int _numMaxMsg = 500; + private string _lastMsgFilter = string.Empty; + private bool _lastMsgFilterNotAvailable; + private bool _blLockShow = false; + + [Reactive] + public string MsgFilter { get; set; } + + [Reactive] + public bool AutoRefresh { get; set; } + + public MsgViewModel(Func>? updateView) + { + _config = LazyConfig.Instance.Config; + _updateView = updateView; + _noticeHandler = Locator.Current.GetService(); + + MessageBus.Current.Listen(Global.CommandSendMsgView).Subscribe(async x => await AppendQueueMsg(x)); + + MsgFilter = _config.msgUIItem.mainMsgFilter ?? string.Empty; + AutoRefresh = _config.msgUIItem.autoRefresh ?? true; + + this.WhenAnyValue( + x => x.MsgFilter) + .Subscribe(c => _config.msgUIItem.mainMsgFilter = MsgFilter); + + this.WhenAnyValue( + x => x.AutoRefresh, + y => y == true) + .Subscribe(c => { _config.msgUIItem.autoRefresh = AutoRefresh; }); + } + + private async Task AppendQueueMsg(string msg) + { + //if (msg == Global.CommandClearMsg) + //{ + // ClearMsg(); + // return; + //} + if (AutoRefresh == false) + { + return; + } + _ = EnqueueQueueMsg(msg); + + if (_blLockShow) + { + return; + } + + _blLockShow = true; + + await Task.Delay(100); + var txt = string.Join("", _queueMsg.ToArray()); + await _updateView?.Invoke(EViewAction.DispatcherShowMsg, txt); + + _blLockShow = false; + } + + private async Task EnqueueQueueMsg(string msg) + { + //filter msg + if (MsgFilter != _lastMsgFilter) _lastMsgFilterNotAvailable = false; + if (!Utils.IsNullOrEmpty(MsgFilter) && !_lastMsgFilterNotAvailable) + { + try + { + if (!Regex.IsMatch(msg, MsgFilter)) + { + return; + } + } + catch (Exception) + { + _lastMsgFilterNotAvailable = true; + } + } + _lastMsgFilter = MsgFilter; + + //Enqueue + 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); + } + } + + public void ClearMsg() + { + _queueMsg.Clear(); + } + } +} \ No newline at end of file diff --git a/v2rayN/v2rayN.Desktop/Views/MsgView.axaml.cs b/v2rayN/v2rayN.Desktop/Views/MsgView.axaml.cs index a35e335e..8d233993 100644 --- a/v2rayN/v2rayN.Desktop/Views/MsgView.axaml.cs +++ b/v2rayN/v2rayN.Desktop/Views/MsgView.axaml.cs @@ -1,104 +1,54 @@ -using Avalonia.Controls; using Avalonia.Interactivity; +using Avalonia.ReactiveUI; using Avalonia.Threading; using ReactiveUI; -using System.Collections.Concurrent; -using System.Text.RegularExpressions; +using System.Reactive.Disposables; using v2rayN.Desktop.Common; namespace v2rayN.Desktop.Views { - public partial class MsgView : UserControl + public partial class MsgView : ReactiveUserControl { - private static Config? _config; - private ConcurrentQueue _queueMsg = new(); - private int _numMaxMsg = 500; - - private string lastMsgFilter = string.Empty; - private bool lastMsgFilterNotAvailable; - public MsgView() { InitializeComponent(); - _config = LazyConfig.Instance.Config; - MessageBus.Current.Listen(Global.CommandSendMsgView).Subscribe(x => DelegateAppendText(x)); - //Global.PresetMsgFilters.ForEach(it => - //{ - // cmbMsgFilter.Items.Add(it); - //}); - if (!_config.uiItem.mainMsgFilter.IsNullOrEmpty()) - { - cmbMsgFilter.Text = _config.uiItem.mainMsgFilter; - } - cmbMsgFilter.TextChanged += (s, e) => - { - _config.uiItem.mainMsgFilter = cmbMsgFilter.Text?.ToString(); - }; - } - private void DelegateAppendText(string msg) - { - Dispatcher.UIThread.Post(() => AppendText(msg), DispatcherPriority.ApplicationIdle); - } + ViewModel = new MsgViewModel(UpdateViewHandler); - public void AppendText(string msg) - { - if (msg == Global.CommandClearMsg) + this.WhenActivated(disposables => { - ClearMsg(); - return; - } - if (togAutoRefresh.IsChecked == false) - { - return; - } + this.Bind(ViewModel, vm => vm.MsgFilter, v => v.cmbMsgFilter.Text).DisposeWith(disposables); + this.Bind(ViewModel, vm => vm.AutoRefresh, v => v.togAutoRefresh.IsChecked).DisposeWith(disposables); + }); + } - var MsgFilter = cmbMsgFilter.Text?.ToString(); - if (MsgFilter != lastMsgFilter) lastMsgFilterNotAvailable = false; - if (!Utils.IsNullOrEmpty(MsgFilter) && !lastMsgFilterNotAvailable) + private async Task UpdateViewHandler(EViewAction action, object? obj) + { + switch (action) { - try - { - if (!Regex.IsMatch(msg, MsgFilter)) - { - return; - } - } - catch (Exception) - { - lastMsgFilterNotAvailable = true; - } - } - lastMsgFilter = MsgFilter; + case EViewAction.DispatcherShowMsg: + if (obj is null) return false; - ShowMsg(msg); - - if (togScrollToEnd.IsChecked ?? true) - { - txtMsg.CaretIndex = int.MaxValue; + Dispatcher.UIThread.Post(() => + ShowMsg(obj), + DispatcherPriority.ApplicationIdle); + break; } + return await Task.FromResult(true); } - private void ShowMsg(string msg) + private void ShowMsg(object msg) { - if (_queueMsg.Count > _numMaxMsg) - { - for (int k = 0; k < _queueMsg.Count - _numMaxMsg; k++) - { - _queueMsg.TryDequeue(out _); - } - } - _queueMsg.Enqueue(msg); - if (!msg.EndsWith(Environment.NewLine)) + txtMsg.Text = msg.ToString(); + if (togScrollToEnd.IsChecked ?? true) { - _queueMsg.Enqueue(Environment.NewLine); + txtMsg.CaretIndex = int.MaxValue; } - txtMsg.Text = string.Join("", _queueMsg.ToArray()); } public void ClearMsg() { - _queueMsg.Clear(); + ViewModel?.ClearMsg(); txtMsg.Clear(); } diff --git a/v2rayN/v2rayN/Views/MsgView.xaml b/v2rayN/v2rayN/Views/MsgView.xaml index de8de857..18445008 100644 --- a/v2rayN/v2rayN/Views/MsgView.xaml +++ b/v2rayN/v2rayN/Views/MsgView.xaml @@ -1,13 +1,16 @@ - + Style="{StaticResource DefComboBox}" />