From dbe37c7865878f830b478612ec66edaa6eb0be55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Saliba?= Date: Fri, 3 Jan 2025 13:33:06 +0100 Subject: [PATCH] refactor: Now uses the new array-like collections initializers everywhere. fix: Improved the bandwidth chart logic (which was buggy and probably still is ?). --- Common.Tests/Helpers/DnsResolverTest.cs | 18 +-- Common/Net/IP/IPHelper.cs | 2 +- Common/Processes/ProcessHelper.cs | 2 +- Common/Security/EventLogAsyncReader.cs | 2 +- Common/UI/Themes/Dark.xaml | 3 + Common/UI/Themes/Light.xaml | 3 + Console/Helpers/ColorGenerator.cs | 17 +++ Console/UI/Controls/BandwidthDateTimePoint.cs | 6 +- Console/UI/Controls/BandwidthGraph.xaml | 5 +- Console/UI/Controls/BandwidthGraph.xaml.cs | 78 +++++------- Console/UI/Controls/BandwidthGraphTooltip.cs | 118 ++++++++++++++++++ Console/UI/Pages/Connections.xaml.cs | 29 ++--- Console/UI/Pages/Status.xaml.cs | 2 +- Console/UI/Pages/TimerBasedPage.cs | 2 +- .../ViewModels/GroupedMonitoredConnections.cs | 2 + Notifier/App.cs | 2 +- Notifier/AppDataSample.cs | 2 +- Notifier/Helpers/CurrentConn.cs | 2 +- .../UI/Windows/NotificationWindow.xaml.cs | 2 +- 19 files changed, 212 insertions(+), 85 deletions(-) create mode 100644 Console/Helpers/ColorGenerator.cs create mode 100644 Console/UI/Controls/BandwidthGraphTooltip.cs diff --git a/Common.Tests/Helpers/DnsResolverTest.cs b/Common.Tests/Helpers/DnsResolverTest.cs index 6bf2f59..b399853 100644 --- a/Common.Tests/Helpers/DnsResolverTest.cs +++ b/Common.Tests/Helpers/DnsResolverTest.cs @@ -14,14 +14,14 @@ public class DnsResolverTest : NUnitTestBase public void TestDnsResolverResolveIpAddresses() { // Hostname -> IP lookup: https://whatismyipaddress.com/hostname-ip - List ipList = new List - { + List ipList = + [ "52.200.121.83", // origin on ec2-52-200-121-83.compute-1.amazonaws.com "52.200.121.83", // duplicate "8.8.8.8", // dns.google "2001:4860:4860::8888", // dns.google Dns.GetHostAddresses("www.google.ch").FirstOrDefault().ToString() - }; + ]; WriteDebugOutput("Resolve first 3 entries:"); _ = ResolvedIPInformation.ResolveIpAddressAsync(ipList[0]).ConfigureAwait(true); @@ -34,13 +34,13 @@ public void TestDnsResolverResolveIpAddresses() Assert.That(ResolvedIPInformation.CachedIPHostEntryDict["8.8.8.8"].resolvedHost, Is.EqualTo("dns.google")); Assert.True(ResolvedIPInformation.CachedIPHostEntryDict.Values.Count == 4); - ipList = new List - { + ipList = + [ "2001:4860:4860::8888", // dns.google "172.217.5.195", // lax28s10-in-f195.1e100.net (www.google.ch) "23.211.5.15", // a23-211-5-15.deploy.static.akamaitechnologies.com "1.78.64.10", // sp1-78-64-10.msa.spmode.ne.jp - }; + ]; WriteDebugOutput("Resolve next 3 entries:"); _ = ResolvedIPInformation.ResolveIpAddressAsync(ipList[0]).ConfigureAwait(false); _ = ResolvedIPInformation.ResolveIpAddressAsync(ipList[1]).ConfigureAwait(false); @@ -57,11 +57,11 @@ public void TestDnsResolverResolveIpAddresses() //[Test, ManualTestCategory] public void TestDnsResolverResolveIpAddresses_unresolved() { - List ipList = new List - { + List ipList = + [ // unresolvable ips "1.9.1.9", // cdns01.tm.net.my - }; + ]; WriteDebugOutput("Unresolvabe IPs:"); _ = ResolvedIPInformation.ResolveIpAddressAsync("1.9.1.9").ConfigureAwait(false); diff --git a/Common/Net/IP/IPHelper.cs b/Common/Net/IP/IPHelper.cs index 420a9fb..efecc48 100644 --- a/Common/Net/IP/IPHelper.cs +++ b/Common/Net/IP/IPHelper.cs @@ -40,7 +40,7 @@ internal static int GetRealPort(uint _remotePort) return (int)(BitConverter.IsLittleEndian ? BinaryPrimitives.ReverseEndianness((ushort)_remotePort) : (ushort)_remotePort); } - internal static Dictionary TCP4MIBCACHE = new(); + internal static Dictionary TCP4MIBCACHE = []; public static string MergePorts(IEnumerable ports) { diff --git a/Common/Processes/ProcessHelper.cs b/Common/Processes/ProcessHelper.cs index 48dd2c7..7ed6f3a 100644 --- a/Common/Processes/ProcessHelper.cs +++ b/Common/Processes/ProcessHelper.cs @@ -142,7 +142,7 @@ public static Dictionary GetAllServicesByPidWMI() { // use WMI "Win32_Service" query to get service names by pid // https://docs.microsoft.com/en-us/windows/win32/cimwin32prov/win32-service - Dictionary dict = new Dictionary(); + Dictionary dict = []; using (var searcher = new ManagementObjectSearcher("SELECT ProcessId, Name, DisplayName, PathName FROM Win32_Service WHERE ProcessId != 0")) { using var results = searcher.Get(); diff --git a/Common/Security/EventLogAsyncReader.cs b/Common/Security/EventLogAsyncReader.cs index eb3596f..340f0f6 100644 --- a/Common/Security/EventLogAsyncReader.cs +++ b/Common/Security/EventLogAsyncReader.cs @@ -107,7 +107,7 @@ public EventLogAsyncReader(string eventLogName, Func pro private readonly Func _projection; public Func? PlaceHolderCreator { get; set; } - private readonly Dictionary filteredPagesMap = new Dictionary(); + private readonly Dictionary filteredPagesMap = []; public int Count => eventLog.Entries.Count; diff --git a/Common/UI/Themes/Dark.xaml b/Common/UI/Themes/Dark.xaml index b5c1bdf..f9c1dc1 100644 --- a/Common/UI/Themes/Dark.xaml +++ b/Common/UI/Themes/Dark.xaml @@ -28,13 +28,16 @@ + White + Gray + Black diff --git a/Common/UI/Themes/Light.xaml b/Common/UI/Themes/Light.xaml index 3e1c3f0..3567439 100644 --- a/Common/UI/Themes/Light.xaml +++ b/Common/UI/Themes/Light.xaml @@ -28,13 +28,16 @@ + Black + LightYellow + Black diff --git a/Console/Helpers/ColorGenerator.cs b/Console/Helpers/ColorGenerator.cs new file mode 100644 index 0000000..09ea78d --- /dev/null +++ b/Console/Helpers/ColorGenerator.cs @@ -0,0 +1,17 @@ +using System.Windows.Media; + +namespace Wokhan.WindowsFirewallNotifier.Console.Helpers; + +public static class ColorGenerator +{ + public static readonly List Variations = [ + Color.FromRgb(0x00, 0x3f, 0x5c), + Color.FromRgb(0x2f, 0x4b, 0x7c), + Color.FromRgb(0x66, 0x51, 0x91), + Color.FromRgb(0xa0, 0x51, 0x95), + Color.FromRgb(0xd4, 0x50, 0x87), + Color.FromRgb(0xf9, 0x5d, 0x6a), + Color.FromRgb(0xff, 0x7c, 0x43), + Color.FromRgb(0xff, 0xa6, 0x00) + ]; +} diff --git a/Console/UI/Controls/BandwidthDateTimePoint.cs b/Console/UI/Controls/BandwidthDateTimePoint.cs index 9e69b93..f8a53b0 100644 --- a/Console/UI/Controls/BandwidthDateTimePoint.cs +++ b/Console/UI/Controls/BandwidthDateTimePoint.cs @@ -1,3 +1,5 @@ -namespace Wokhan.WindowsFirewallNotifier.Console.UI.Controls; +using Wokhan.WindowsFirewallNotifier.Console.ViewModels; -public record BandwidthDateTimePoint(DateTime DateTime, ulong? BandwidthIn = null, ulong? BandwidthOut = null); \ No newline at end of file +namespace Wokhan.WindowsFirewallNotifier.Console.UI.Controls; + +public record BandwidthDateTimePoint(DateTime DateTime, ulong? BandwidthIn = null, ulong? BandwidthOut = null, GroupedMonitoredConnections? Source = null); \ No newline at end of file diff --git a/Console/UI/Controls/BandwidthGraph.xaml b/Console/UI/Controls/BandwidthGraph.xaml index 51433a4..4484784 100644 --- a/Console/UI/Controls/BandwidthGraph.xaml +++ b/Console/UI/Controls/BandwidthGraph.xaml @@ -9,6 +9,9 @@ xmlns:controls="clr-namespace:Wokhan.WindowsFirewallNotifier.Console.UI.Controls" mc:Ignorable="d" x:Name="me" d:DesignHeight="450" d:DesignWidth="800" ClipToBounds="True"> + + + @@ -37,6 +40,6 @@ - + diff --git a/Console/UI/Controls/BandwidthGraph.xaml.cs b/Console/UI/Controls/BandwidthGraph.xaml.cs index 212024b..3b971b9 100644 --- a/Console/UI/Controls/BandwidthGraph.xaml.cs +++ b/Console/UI/Controls/BandwidthGraph.xaml.cs @@ -1,4 +1,5 @@ using LiveChartsCore; +using LiveChartsCore.Drawing; using LiveChartsCore.Kernel; using LiveChartsCore.SkiaSharpView; using LiveChartsCore.SkiaSharpView.Drawing.Geometries; @@ -28,15 +29,15 @@ public partial class BandwidthGraph : UserControl, INotifyPropertyChanged private const int MAX_DURATION_SEC = 10; - private ObservableDictionary, ObservableCollection>> allSeries = new(); + private readonly ObservableDictionary, ObservableCollection>> allSeries = []; private DateTime datetime = DateTime.Now; private bool isXPanned; - private ObservableCollection seriesInTotal = new(); + private readonly ObservableCollection seriesInTotal = []; - private ObservableCollection seriesOutTotal = new(); + private readonly ObservableCollection seriesOutTotal = []; private Axis xAxis; private Axis yAxis; @@ -55,16 +56,16 @@ public BandwidthGraph() private void InitMiniGraph() { - MiniSeries = new List { - new LineSeries() { Name = "In", Stroke = new SolidColorPaint(SKColors.LightGreen), GeometryStroke = null, GeometryFill = null, Values = seriesInTotal, Mapping = logMapper }, - new LineSeries() { Name = "Out", Stroke = new SolidColorPaint(SKColors.OrangeRed), GeometryStroke = null, GeometryFill = null, Values = seriesOutTotal, Mapping = logMapper } - }; + MiniSeries = [ + new LineSeries() { Name = "In", Stroke = new SolidColorPaint(SKColors.LightGreen), GeometryStroke = null, GeometryFill = null, Values = seriesInTotal, Mapping = LogMapper }, + new LineSeries() { Name = "Out", Stroke = new SolidColorPaint(SKColors.OrangeRed), GeometryStroke = null, GeometryFill = null, Values = seriesOutTotal, Mapping = LogMapper } + ]; } private void InitAxes() { - var skAxisPaint = new SolidColorPaint(((SolidColorBrush)Application.Current.Resources[System.Windows.SystemColors.WindowTextBrushKey]).Color.ToSKColor()); + var skAxisPaint = new SolidColorPaint(((Color)Application.Current.Resources[SystemColors.WindowTextColorKey]).ToSKColor()); crosshairPaint = new SolidColorPaint(SKColors.Red) { StrokeThickness = 1 }; xAxis = (Axis)chart.XAxes.First(); @@ -77,14 +78,15 @@ private void InitAxes() yAxis = (Axis)chart.YAxes.First(); yAxis.MinLimit = 0; + yAxis.MinStep = 10; yAxis.TextSize = 10; - yAxis.MinStep = 1; yAxis.Labeler = (value) => value == 0 ? "0bps" : UnitFormatter.FormatValue(Math.Pow(10, value), "bps"); yAxis.LabelsPaint = skAxisPaint; yAxis.CrosshairPaint = crosshairPaint; var miniYAxis = miniChart.YAxes.First(); miniYAxis.MinLimit = 0; + miniYAxis.MinStep = 10; miniYAxis.TextSize = 10; miniYAxis.Padding = new LiveChartsCore.Drawing.Padding(0); miniYAxis.ShowSeparatorLines = false; @@ -123,31 +125,16 @@ public double CurrentStart } } public IEnumerable MiniSeries { get; private set; } - public ObservableCollection Series { get; } = new(); + public ObservableCollection Series { get; } = []; public double ThumbSize => (xAxis is not null ? (xAxis.MaxLimit - xAxis.MinLimit) * scrollArea.Track.ActualWidth / (xAxis.DataBounds.Max - xAxis.DataBounds.Min) : 0) ?? 0; - private string tooltipFormatter(ChartPoint arg) + private Coordinate LogMapper(BandwidthDateTimePoint dateTimePoint, int _) { - return $"{((LineSeries)arg.Context.Series).Tag} - In: {UnitFormatter.FormatValue(arg.PrimaryValue, "bps")} / Out: {UnitFormatter.FormatValue(arg.TertiaryValue, "bps")}"; + var value = dateTimePoint.BandwidthIn ?? dateTimePoint.BandwidthOut ?? 0; + return new Coordinate(dateTimePoint.DateTime.Ticks, value == 0 ? 0 : Math.Log10(value)); } - private Coordinate logMapper(BandwidthDateTimePoint dateTimePoint, int _) - { - var value = (long)(dateTimePoint.BandwidthIn ?? dateTimePoint.BandwidthOut)!; - - // Only points in the first series (IN bandwidth) have to store the secondary values for the legend - // We keep both in / out values since we don't want the log10 for them but the original values. - if (dateTimePoint.BandwidthIn is not null) - { - return new Coordinate(value == 0 ? 0 : Math.Log10(value), dateTimePoint.DateTime.Ticks, dateTimePoint.BandwidthIn ?? 0, dateTimePoint.BandwidthOut ?? 0, 0, 0, Error.Empty); - } - else - { - return new Coordinate(value == 0 ? 0 : Math.Log10(value), dateTimePoint.DateTime.Ticks); - } - } - - readonly DashEffect outDashEffect = new DashEffect(new[] { 2f, 2f }); + readonly DashEffect outDashEffect = new([2f, 2f]); public void UpdateGraph() { @@ -170,19 +157,18 @@ public void UpdateGraph() { seriesInValues = []; seriesOutValues = []; - + var color = connectionGroup.Key.Color.ToSKColor(); var inStroke = new SolidColorPaint(color) { StrokeThickness = 2 }; var outStroke = new SolidColorPaint(color) { StrokeThickness = 2, PathEffect = outDashEffect }; - - Series.Add(new LineSeries() { Tag = connectionGroup.Key.Path, Name = $"{connectionGroup.Key} - In", XToolTipLabelFormatter = tooltipFormatter, Fill = null, Stroke = inStroke, GeometryStroke = inStroke, Values = seriesInValues, LineSmoothness = 0, Mapping = logMapper }); - Series.Add(new LineSeries() { Tag = connectionGroup.Key.Path, Name = $"{connectionGroup.Key} - Out", IsVisibleAtLegend = false, IsHoverable = false, Fill = null, Stroke = outStroke, GeometryStroke = outStroke, Values = seriesOutValues, LineSmoothness = 0, Mapping = logMapper }); - + + Series.Add(new LineSeries() { Tag = connectionGroup.Key, Name = $"{connectionGroup.Key.FileName} ({connectionGroup.Key.ProcessId}) - In", Fill = null, Stroke = inStroke, GeometryStroke = inStroke, Values = seriesInValues, LineSmoothness = 0, Mapping = LogMapper }); + Series.Add(new LineSeries() { Name = $"{connectionGroup.Key.FileName} ({connectionGroup.Key.ProcessId}) - Out", Fill = null, Stroke = outStroke, GeometryStroke = outStroke, Values = seriesOutValues, LineSmoothness = 0, Mapping = LogMapper }); + allSeries.Add(connectionGroup.Key.Path, Tuple.Create(seriesInValues, seriesOutValues)); } - AddAndMergePoints(seriesInValues, connectionGroup.Key.InboundBandwidth, connectionGroup.Key.OutboundBandwidth); - AddAndMergePoints(seriesOutValues, bandwidthOut: connectionGroup.Key.OutboundBandwidth); + AddAndMergePoints(connectionGroup.Key, seriesInValues, seriesOutValues); totalIn += connectionGroup.Key.InboundBandwidth; totalOut += connectionGroup.Key.OutboundBandwidth; @@ -208,17 +194,19 @@ public void UpdateGraph() } } - private void AddAndMergePoints(ObservableCollection series, ulong? bandwidthIn = null, ulong? bandwidthOut = null) + + private void AddAndMergePoints(GroupedMonitoredConnections group, ObservableCollection seriesIn, ObservableCollection seriesOut) { - if (series.Count == 0 - || (bandwidthIn == 0 && (series[^1].BandwidthIn != 0 || bandwidthOut is not null and not 0)) - || (bandwidthOut == 0 && series[^1].BandwidthOut != 0)) + bool isOut = group.OutboundBandwidth != 0 || (seriesOut.Count > 0 && seriesOut[^1].BandwidthOut != 0); + if (isOut || group.OutboundBandwidth != 0 || group.InboundBandwidth != 0 || (seriesIn.Count > 0 && seriesIn[^1].BandwidthIn != 0)) + { + // Adding both inbound and outbound bandwidth as they will be used in the tooltip + seriesIn.Add(new BandwidthDateTimePoint(datetime, group.InboundBandwidth, group.OutboundBandwidth, group)); + } + + if (isOut) { - series.Add(new BandwidthDateTimePoint(datetime, bandwidthIn, bandwidthOut)); - //if (series.Count > 3 && series[^2].Value == sum && series[^3].Value == sum) - //{ - // series.RemoveAt(series.Count - 2); - //} + seriesOut.Add(new BandwidthDateTimePoint(datetime, BandwidthOut: group.OutboundBandwidth)); } } diff --git a/Console/UI/Controls/BandwidthGraphTooltip.cs b/Console/UI/Controls/BandwidthGraphTooltip.cs new file mode 100644 index 0000000..d1600f9 --- /dev/null +++ b/Console/UI/Controls/BandwidthGraphTooltip.cs @@ -0,0 +1,118 @@ +using LiveChartsCore; +using LiveChartsCore.Drawing; +using LiveChartsCore.Kernel; +using LiveChartsCore.Kernel.Sketches; +using LiveChartsCore.Measure; +using LiveChartsCore.SkiaSharpView; +using LiveChartsCore.SkiaSharpView.Drawing; +using LiveChartsCore.SkiaSharpView.Drawing.Geometries; +using LiveChartsCore.SkiaSharpView.Painting; +using LiveChartsCore.SkiaSharpView.VisualElements; +using LiveChartsCore.VisualElements; + +using SkiaSharp.Views.WPF; + +using System.Windows; +using System.Windows.Media; + +using Wokhan.Core; +using Wokhan.WindowsFirewallNotifier.Console.ViewModels; + + +namespace Wokhan.WindowsFirewallNotifier.Console.UI.Controls; + +/// +/// Modified from https://livecharts.dev/docs/winforms/2.0.0-rc1/CartesianChart.Tooltips#customize-default-tooltips +/// +public class BandwidthGraphTooltip : IChartTooltip +{ + private readonly StackPanel _stackPanel; + + private static readonly int s_zIndex = 10100; + + private SolidColorPaint _fontPaint = new(((Color)Application.Current.Resources[SystemColors.InfoTextColorKey]).ToSKColor()); + + public BandwidthGraphTooltip() + { + _stackPanel = new StackPanel + { + Padding = new Padding(5), + Orientation = ContainerOrientation.Vertical, + HorizontalAlignment = Align.Start, + VerticalAlignment = Align.Middle, + BackgroundPaint = new SolidColorPaint(((Color)Application.Current.Resources[SystemColors.InfoColorKey]).ToSKColor()) + }; + + _stackPanel.Animate(new Animation(EasingFunctions.BounceOut, TimeSpan.FromSeconds(1)), nameof(_stackPanel.X), nameof(_stackPanel.Y)); + } + + public void UpdateColors() + { + // Should update all visuals? + _fontPaint = new(((Color)Application.Current.Resources[SystemColors.InfoTextColorKey]).ToSKColor()); + _stackPanel.BackgroundPaint = new SolidColorPaint(((Color)Application.Current.Resources[SystemColors.InfoColorKey]).ToSKColor()); + } + + public void Show(IEnumerable foundPoints, Chart chart) + { + // clear the previous elements. + foreach (var child in _stackPanel.Children.ToArray()) + { + _ = _stackPanel.Children.Remove(child); + chart.RemoveVisual(child); + } + //_stackPanel.Children.Clear(); + + foreach (var point in foundPoints) + { + var bandwidthDateTimePoint = (BandwidthDateTimePoint)point.Context.DataSource!; + + // Ignore secondary (out) points as the bandwidth is already logged along the "In" series + if (bandwidthDateTimePoint.BandwidthIn is null) + { + continue; + } + + var sketch = ((IChartSeries)point.Context.Series).GetMiniaturesSketch(); + var relativePanel = sketch.AsDrawnControl(s_zIndex); + + var groupedConnections = bandwidthDateTimePoint.Source!; + + var label = new LabelVisual + { + Text = $"{groupedConnections.FileName} ({groupedConnections.ProcessId}) - In: {UnitFormatter.FormatValue(bandwidthDateTimePoint.BandwidthIn ?? 0, "bps")} / Out: {UnitFormatter.FormatValue(bandwidthDateTimePoint.BandwidthOut ?? 0, "bps")}", + Paint = _fontPaint, + TextSize = 12, + Padding = new Padding(8, 0, 0, 0), + ClippingMode = ClipMode.None, // required on tooltips + VerticalAlignment = Align.Start, + HorizontalAlignment = Align.Start + }; + + var sp = new StackPanel + { + Padding = new Padding(0, 4), + VerticalAlignment = Align.Middle, + HorizontalAlignment = Align.Middle, + Children = { relativePanel, label } + }; + + _stackPanel.Children.Add(sp); + } + + var size = _stackPanel.Measure(chart); + + var location = foundPoints.GetTooltipLocation(size, chart); + + _stackPanel.X = location.X; + _stackPanel.Y = location.Y; + + chart.AddVisual(_stackPanel); + } + + public void Hide(Chart chart) + { + //if (chart is null || _stackPanel is null) return; + chart.RemoveVisual(_stackPanel); + } +} \ No newline at end of file diff --git a/Console/UI/Pages/Connections.xaml.cs b/Console/UI/Pages/Connections.xaml.cs index 9eba640..b67da95 100644 --- a/Console/UI/Pages/Connections.xaml.cs +++ b/Console/UI/Pages/Connections.xaml.cs @@ -1,26 +1,15 @@ using CommunityToolkit.Mvvm.ComponentModel; -using HarfBuzzSharp; - -using LiveChartsCore.SkiaSharpView; -using LiveChartsCore.Themes; - -using SkiaSharp.Views.WPF; - using System.ComponentModel; -using System.Data; -using System.Drawing.Imaging; using System.Timers; using System.Windows; -using System.Windows.Data; using System.Windows.Media; using Wokhan.Collections; using Wokhan.WindowsFirewallNotifier.Common.Config; using Wokhan.WindowsFirewallNotifier.Common.Net.IP; - -using Wokhan.WindowsFirewallNotifier.Common.UI.Themes; +using Wokhan.WindowsFirewallNotifier.Console.Helpers; using Wokhan.WindowsFirewallNotifier.Console.ViewModels; namespace Wokhan.WindowsFirewallNotifier.Console.UI.Pages; @@ -191,13 +180,15 @@ private void AddOrUpdateConnection(Connection connectionInfo) internal void UpdateConnectionsColors() { // TODO: temporary improvement for dark themes colors. I'll have to rework this anyway. - Colors = Settings.Default.Theme switch - { - ThemeHelper.THEME_LIGHT => LiveChartsCore.Themes.ColorPalletes.FluentDesign.Select(c => c.AsSKColor().ToColor()).ToList(), - ThemeHelper.THEME_DARK => LiveChartsCore.Themes.ColorPalletes.FluentDesign.Select(c => c.AsSKColor().ToColor()).ToList(), - ThemeHelper.THEME_SYSTEM => [SystemColors.WindowTextColor], - _ => LiveChartsCore.Themes.ColorPalletes.FluentDesign.Select(c => c.AsSKColor().ToColor()).ToList(), - }; + //Colors = Settings.Default.Theme switch + //{ + // ThemeHelper.THEME_LIGHT => LiveChartsCore.Themes.ColorPalletes.FluentDesign.Select(c => c.AsSKColor().ToColor()).ToList(), + // ThemeHelper.THEME_DARK => LiveChartsCore.Themes.ColorPalletes.FluentDesign.Select(c => c.AsSKColor().ToColor()).ToList(), + // ThemeHelper.THEME_SYSTEM => [SystemColors.WindowTextColor], + // _ => LiveChartsCore.Themes.ColorPalletes.FluentDesign.Select(c => c.AsSKColor().ToColor()).ToList(), + //}; + + Colors = ColorGenerator.Variations; // TODO: check for concurrent access issue when switching themes while updating // I removed the lock but it could have been useful here... diff --git a/Console/UI/Pages/Status.xaml.cs b/Console/UI/Pages/Status.xaml.cs index e1821e5..3d42cdf 100644 --- a/Console/UI/Pages/Status.xaml.cs +++ b/Console/UI/Pages/Status.xaml.cs @@ -14,7 +14,7 @@ namespace Wokhan.WindowsFirewallNotifier.Console.UI.Pages; public partial class Status : Page { - public ObservableCollection Messages { get; } = new(); + public ObservableCollection Messages { get; } = []; public FirewallStatusWrapper StatusWrapper { get; private set; } = new(); diff --git a/Console/UI/Pages/TimerBasedPage.cs b/Console/UI/Pages/TimerBasedPage.cs index dee0908..3df7176 100644 --- a/Console/UI/Pages/TimerBasedPage.cs +++ b/Console/UI/Pages/TimerBasedPage.cs @@ -11,7 +11,7 @@ public partial class TimerBasedPage : Page { private readonly System.Timers.Timer timer; - public virtual List Intervals { get; } = new List { 0.5, 1, 5, 10 }; + public virtual List Intervals { get; } = [0.5, 1, 5, 10]; public virtual bool IsTrackingEnabled { diff --git a/Console/ViewModels/GroupedMonitoredConnections.cs b/Console/ViewModels/GroupedMonitoredConnections.cs index e281bdb..26e3ad5 100644 --- a/Console/ViewModels/GroupedMonitoredConnections.cs +++ b/Console/ViewModels/GroupedMonitoredConnections.cs @@ -8,6 +8,7 @@ public partial class GroupedMonitoredConnections : ObservableObject, IComparable public string FileName { get; init; } public string Path { get; init; } public string? ProductName { get; init; } + public uint ProcessId { get; init; } private SolidColorBrush? _brush; public SolidColorBrush Brush => _brush ??= new SolidColorBrush(Color); @@ -26,6 +27,7 @@ public GroupedMonitoredConnections(MonitoredConnection connection, Color color) Path = connection.Path!; FileName = connection.FileName!; ProductName = connection.ProductName; + ProcessId = connection.Pid; Color = color; connection.Color = color; diff --git a/Notifier/App.cs b/Notifier/App.cs index 2a4cfe3..33854c0 100644 --- a/Notifier/App.cs +++ b/Notifier/App.cs @@ -34,7 +34,7 @@ public sealed class App : Application, IDisposable private static ActivityWindow activityWindow; private EventLog eventLog; - public ObservableCollection Connections { get; } = new ObservableCollection(); + public ObservableCollection Connections { get; } = []; /// /// Main entrypoint of the application. diff --git a/Notifier/AppDataSample.cs b/Notifier/AppDataSample.cs index 079ddb8..6c0c643 100644 --- a/Notifier/AppDataSample.cs +++ b/Notifier/AppDataSample.cs @@ -24,5 +24,5 @@ internal DemoCurrentConn() : base() } internal static CurrentConn DemoConnection = new DemoCurrentConn(); - public IList Connections { get; } = new List { DemoConnection, DemoConnection }; + public IList Connections { get; } = [DemoConnection, DemoConnection]; } diff --git a/Notifier/Helpers/CurrentConn.cs b/Notifier/Helpers/CurrentConn.cs index bb369ed..3b3dbc9 100644 --- a/Notifier/Helpers/CurrentConn.cs +++ b/Notifier/Helpers/CurrentConn.cs @@ -16,7 +16,7 @@ public partial class CurrentConn : LoggedConnection, INotifyPropertyChanged //TODO: rename as it's not something "current" public string CurrentLocalUserOwner { get; set; }// => this.GetOrSetAsyncValue(() => ProcessHelper.GetLocalUserOwner(Pid), NotifyPropertyChanged, nameof(_currentLocalUserOwner)); - public SortedSet LocalPortArray { get; } = new SortedSet(); + public SortedSet LocalPortArray { get; } = []; [ObservableProperty] private int _tentativesCounter = 1; diff --git a/Notifier/UI/Windows/NotificationWindow.xaml.cs b/Notifier/UI/Windows/NotificationWindow.xaml.cs index e0f52ec..42da17c 100644 --- a/Notifier/UI/Windows/NotificationWindow.xaml.cs +++ b/Notifier/UI/Windows/NotificationWindow.xaml.cs @@ -507,7 +507,7 @@ private bool createRule(CurrentConn activeConn, bool createWithAdvancedOptions, } private static WinForms::NotifyIcon _tempNotifyIcon; - private readonly List _tempRules = new(); + private readonly List _tempRules = []; private void CreateTempRuleNotifyIcon(CustomRule newRule) { if (!_tempRules.Contains(newRule))