Skip to content

Commit

Permalink
Using Queues to Improve Message Display
Browse files Browse the repository at this point in the history
  • Loading branch information
2dust committed Sep 2, 2024
1 parent b02ad6c commit 6879c75
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 18 deletions.
4 changes: 1 addition & 3 deletions v2rayN/v2rayN.Desktop/Views/MsgView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,12 @@
<TextBlock
Margin="8,0"
VerticalAlignment="Center"
IsVisible="False"
Text="{x:Static resx:ResUI.TbAutoScrollToEnd}" />
<ToggleSwitch
x:Name="togScrollToEnd"
Margin="8,0"
HorizontalAlignment="Left"
IsChecked="True"
IsVisible="False" />
IsChecked="True" />
</WrapPanel>
<TextBox
Name="txtMsg"
Expand Down
23 changes: 12 additions & 11 deletions v2rayN/v2rayN.Desktop/Views/MsgView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ namespace v2rayN.Desktop.Views
public partial class MsgView : UserControl
{
private static Config? _config;
private ConcurrentQueue<string> _queueMsg = new();
private int _numMaxMsg = 500;

private string lastMsgFilter = string.Empty;
private bool lastMsgFilterNotAvailable;
private ConcurrentBag<string> _lstMsg = [];

public MsgView()
{
Expand Down Expand Up @@ -74,30 +75,30 @@ public void AppendText(string msg)

if (togScrollToEnd.IsChecked ?? true)
{
txtMsg.CaretIndex = int.MaxValue;
}
}

private void ShowMsg(string msg)
{
if (_lstMsg.Count > 999)
if (_queueMsg.Count > _numMaxMsg)
{
ClearMsg();
for (int k = 0; k < _queueMsg.Count - _numMaxMsg; k++)
{
_queueMsg.TryDequeue(out _);
}
}
_queueMsg.Enqueue(msg);
if (!msg.EndsWith(Environment.NewLine))
{
_lstMsg.Add(Environment.NewLine);
_queueMsg.Enqueue(Environment.NewLine);
}
_lstMsg.Add(msg);
// if (!msg.EndsWith(Environment.NewLine))
// {
// _lstMsg.Add(Environment.NewLine);
// }
this.txtMsg.Text = string.Join("", _lstMsg);
txtMsg.Text = string.Join("", _queueMsg.ToArray());
}

public void ClearMsg()
{
_lstMsg.Clear();
_queueMsg.Clear();
txtMsg.Clear();
}

Expand Down
16 changes: 12 additions & 4 deletions v2rayN/v2rayN/Views/MsgView.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ReactiveUI;
using System.Collections.Concurrent;
using System.Reactive.Linq;
using System.Text.RegularExpressions;
using System.Windows.Threading;
Expand All @@ -8,6 +9,8 @@ namespace v2rayN.Views
public partial class MsgView
{
private static Config? _config;
private ConcurrentQueue<string> _queueMsg = new();
private int _numMaxMsg = 500;

private string lastMsgFilter = string.Empty;
private bool lastMsgFilterNotAvailable;
Expand Down Expand Up @@ -80,19 +83,24 @@ public void AppendText(string msg)

private void ShowMsg(string msg)
{
if (txtMsg.LineCount > 999)
if (_queueMsg.Count > _numMaxMsg)
{
ClearMsg();
for (int k = 0; k < _queueMsg.Count - _numMaxMsg; k++)
{
_queueMsg.TryDequeue(out _);
}
}
this.txtMsg.AppendText(msg);
_queueMsg.Enqueue(msg);
if (!msg.EndsWith(Environment.NewLine))
{
this.txtMsg.AppendText(Environment.NewLine);
_queueMsg.Enqueue(Environment.NewLine);
}
txtMsg.Text = string.Join("", _queueMsg.ToArray());
}

public void ClearMsg()
{
_queueMsg.Clear();
txtMsg.Clear();
}

Expand Down

0 comments on commit 6879c75

Please sign in to comment.