Skip to content

Commit

Permalink
Merge branch 'main' into release
Browse files Browse the repository at this point in the history
  • Loading branch information
BlossomiShymae committed Aug 17, 2024
2 parents 9ba36a5 + 88149d1 commit e2bfac4
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Needlework.Net.Desktop/Needlework.Net.Desktop.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<AvaloniaXamlIlDebuggerLaunch>False</AvaloniaXamlIlDebuggerLaunch>
<ApplicationIcon>app.ico</ApplicationIcon>
<AssemblyName>NeedleworkDotNet</AssemblyName>
<AssemblyVersion>0.5.0.0</AssemblyVersion>
<AssemblyVersion>0.5.1.0</AssemblyVersion>
<FileVersion>$(AssemblyVersion)</FileVersion>
<AvaloniaXamlVerboseExceptions>False</AvaloniaXamlVerboseExceptions>
</PropertyGroup>
Expand Down
17 changes: 13 additions & 4 deletions Needlework.Net.Desktop/ViewModels/OperationViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,20 @@ private List<string> CreateTemplate(AvaloniaList<PropertyClassViewModel> request
{
var type = template[i];
if (!type.Contains("#")) continue;
if (requestClasses.Where(c => c.Id == type.Replace("#", string.Empty)).Any())

var foundClass = requestClasses.Where(c => c.Id == type.Replace("#", string.Empty));
if (foundClass.Any())
{
AvaloniaList<PropertyClassViewModel> classes = [.. requestClasses];
classes.Remove(rootClass);
template[i] = string.Join(string.Empty, CreateTemplate(classes));
if (foundClass.First().PropertyEnums.Any())
{
template[i] = string.Join(string.Empty, CreateTemplate([.. foundClass]));
}
else
{
AvaloniaList<PropertyClassViewModel> classes = [.. requestClasses];
classes.Remove(rootClass);
template[i] = string.Join(string.Empty, CreateTemplate(classes));
}
}
else
{
Expand Down
33 changes: 22 additions & 11 deletions Needlework.Net.Desktop/ViewModels/WebsocketViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ namespace Needlework.Net.Desktop.ViewModels
public partial class WebsocketViewModel : PageBase
{
public ObservableCollection<string> EventLog { get; } = [];
public SemaphoreSlim EventLogLock { get; } = new(1, 1);

[NotifyPropertyChangedFor(nameof(FilteredEventLog))]
[ObservableProperty] private string _search = string.Empty;
[ObservableProperty] private bool _isAttach = true;
Expand Down Expand Up @@ -94,25 +96,34 @@ private void OnDisconnection(DisconnectionInfo info)

private void OnMessage(EventMessage message)
{
Avalonia.Threading.Dispatcher.UIThread.Invoke(() =>
Avalonia.Threading.Dispatcher.UIThread.Invoke(async () =>
{
if (!IsAttach) return;

var line = $"{DateTime.Now:HH:mm:ss.fff} {message.Data?.EventType.ToUpper()} {message.Data?.Uri}";
Trace.WriteLine($"Message: {line}");
if (EventLog.Count < 1000)

await EventLogLock.WaitAsync();
try
{
EventLog.Add(line);
_events[line] = message;
if (EventLog.Count < 1000)
{
EventLog.Add(line);
_events[line] = message;
}
else
{
var key = EventLog[0];
EventLog.RemoveAt(0);
_events.Remove(key);

EventLog.Add(line);
_events[line] = message;
}
}
else
finally
{
var key = EventLog[0];
EventLog.RemoveAt(0);
_events.Remove(key);

EventLog.Add(line);
_events[line] = message;
EventLogLock.Release();
}
});
}
Expand Down
29 changes: 26 additions & 3 deletions Needlework.Net.Desktop/Views/WebsocketView.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
using Needlework.Net.Desktop.Extensions;
using Needlework.Net.Desktop.Messages;
using Needlework.Net.Desktop.ViewModels;
using System;
using TextMateSharp.Grammars;

namespace Needlework.Net.Desktop.Views;

public partial class WebsocketView : UserControl, IRecipient<ResponseUpdatedMessage>
{
private TextEditor? _responseEditor;
public WebsocketViewModel? _viewModel;
private ListBox? _viewer;

public WebsocketView()
{
Expand All @@ -29,9 +32,9 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);

var vm = (WebsocketViewModel)DataContext!;
var viewer = this.FindControl<ListBox>("EventViewer");
vm.EventLog.CollectionChanged += (s, e) => { if (vm.IsTail) viewer!.ScrollIntoView(vm.EventLog.Count - 1); };
_viewModel = (WebsocketViewModel)DataContext!;
_viewer = this.FindControl<ListBox>("EventViewer");
_viewModel.EventLog.CollectionChanged += EventLog_CollectionChanged; ;

_responseEditor = this.FindControl<TextEditor>("ResponseEditor");
_responseEditor?.ApplyJsonEditorSettings();
Expand All @@ -41,6 +44,26 @@ protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
OnBaseThemeChanged(Application.Current!.ActualThemeVariant);
}

private void EventLog_CollectionChanged(object? sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
Avalonia.Threading.Dispatcher.UIThread.Post(async () =>
{
if (_viewModel!.IsTail)
{
await _viewModel.EventLogLock.WaitAsync();
try
{
_viewer!.ScrollIntoView(_viewModel.EventLog.Count - 1);
}
catch (InvalidOperationException) { }
finally
{
_viewModel.EventLogLock.Release();
}
}
});
}

private void OnBaseThemeChanged(ThemeVariant currentTheme)
{

Expand Down

0 comments on commit e2bfac4

Please sign in to comment.