|
1 | 1 | using System; |
| 2 | +using System.Drawing; |
2 | 3 | using System.Windows.Forms; |
| 4 | +using System.Threading.Tasks; |
3 | 5 |
|
4 | | -using Tanji.Properties; |
5 | | -using Tanji.Core.Infrastructure.ViewModels; |
6 | | - |
| 6 | +using Microsoft.Extensions.Logging; |
7 | 7 | using Microsoft.Extensions.DependencyInjection; |
8 | 8 |
|
9 | 9 | using CommunityToolkit.Mvvm.ComponentModel; |
10 | 10 |
|
| 11 | +using Tanji.Properties; |
| 12 | +using Tanji.Core.Infrastructure.Models; |
| 13 | +using Tanji.Core.Infrastructure.Services; |
| 14 | +using Tanji.Core.Infrastructure.ViewModels; |
| 15 | + |
11 | 16 | namespace Tanji.Views; |
12 | 17 |
|
13 | 18 | public partial class PacketLoggerView : Form |
14 | 19 | { |
15 | | - public PacketLoggerView() |
| 20 | + private readonly ILogger<PacketLoggerView> _logger; |
| 21 | + private IPacketLogHandlerService _packetLogHandler; |
| 22 | + private readonly PacketLoggerViewModel _packetLoggerViewModel; |
| 23 | + |
| 24 | + public PacketLoggerView(ILogger<PacketLoggerView> logger, |
| 25 | + PacketLoggerViewModel packetLoggerViewModel, |
| 26 | + IPacketLogHandlerService packetLogHandler) |
16 | 27 | { |
| 28 | + _logger = logger; |
| 29 | + _packetLogHandler = packetLogHandler; |
| 30 | + _packetLoggerViewModel = packetLoggerViewModel; |
| 31 | + |
17 | 32 | InitializeComponent(); |
18 | 33 | Icon = Resources.Tanji_256; |
| 34 | + |
| 35 | + _ = RenderPacketLogsAsync(); |
| 36 | + } |
| 37 | + |
| 38 | + private async Task RenderPacketLogsAsync() |
| 39 | + { |
| 40 | + while (await _packetLogHandler.WaitForPacketLogsAsync()) |
| 41 | + { |
| 42 | + PacketLog pLog = await _packetLogHandler.ReadPacketLogAsync(); |
| 43 | + |
| 44 | + // Suspend Text Drawing |
| 45 | + loggerVw.SuspendPaint(); |
| 46 | + |
| 47 | + if (pLog.Repetitions < 2) |
| 48 | + { |
| 49 | + int start = loggerVw.TextLength; |
| 50 | + loggerVw.AppendText(pLog.ToString()); |
| 51 | + foreach ((int length, Color highlight) in pLog.GetHighlights()) |
| 52 | + { |
| 53 | + HighlightText(start, length, highlight); |
| 54 | + start += length; |
| 55 | + } |
| 56 | + } |
| 57 | + else |
| 58 | + { |
| 59 | + int previousLineIndex = loggerVw.Lines.Length - 2; |
| 60 | + string previousLine = loggerVw.Lines[previousLineIndex]; |
| 61 | + int previousLineFirstCharIndex = loggerVw.Find(previousLine, RichTextBoxFinds.Reverse | RichTextBoxFinds.NoHighlight); |
| 62 | + |
| 63 | + HighlightText(previousLineFirstCharIndex, previousLine.Length, _packetLoggerViewModel.PacketLoggingOptions.RepeatedHighlight); |
| 64 | + loggerVw.SelectedText = $"--------------- x{pLog.Repetitions}"; |
| 65 | + } |
| 66 | + |
| 67 | + // Resume Text Drawing |
| 68 | + loggerVw.ResumePaint(); |
| 69 | + |
| 70 | + if (_packetLoggerViewModel.PacketLoggingOptions.IsAutoScrolling) |
| 71 | + { |
| 72 | + loggerVw.ScrollToBottom(); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private void HighlightText(int start, int length, Color highlight) |
| 78 | + { |
| 79 | + loggerVw.SelectionStart = start; |
| 80 | + loggerVw.SelectionLength = length; |
| 81 | + loggerVw.SelectionColor = highlight; |
19 | 82 | } |
20 | 83 |
|
21 | 84 | protected override void OnLoad(EventArgs e) |
|
0 commit comments