Skip to content

Commit

Permalink
Preparing V2.7 (slightly updating the design).
Browse files Browse the repository at this point in the history
feat: updated the Adapters page and logic to rely on ObservableObject.
fix: modified the bandwidth computation for the Adapters page, still off compared to the task manager's one though :thinking_face:
wokhan committed Jul 14, 2024
1 parent d82368f commit e4da03f
Showing 8 changed files with 127 additions and 118 deletions.
8 changes: 6 additions & 2 deletions Console/App.xaml
Original file line number Diff line number Diff line change
@@ -24,9 +24,13 @@

<system:Double x:Key="ConsoleSizeHeight">600</system:Double>
<system:Double x:Key="ConsoleSizeWidth">900</system:Double>

<Style TargetType="DataGrid">
<Setter Property="Background" Value="Transparent" />
</Style>

<Style TargetType="DataGridColumnHeader">
<!--<Setter Property="Background" Value="WhiteSmoke" />-->
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderThickness" Value="0,0,1,1" />
<Setter Property="BorderBrush" Value="LightGray" />
<Setter Property="Padding" Value="5" />
@@ -224,7 +228,7 @@
<Setter Property="ContentTemplate">
<Setter.Value>
<DataTemplate>
<Grid Background="{DynamicResource {x:Static SystemColors.MenuBarBrushKey}}">
<Grid Background="Transparent">
<ContentControl Margin="10" Content="{Binding}" />
</Grid>
</DataTemplate>
2 changes: 1 addition & 1 deletion Console/Console.csproj
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@
<!-- TODO: add this to other projects as well? -->
<FileVersion>2.6.$([System.DateTime]::UtcNow.Date.Subtract($([System.DateTime]::Parse("2020-01-01"))).TotalDays).$([System.Math]::Floor($([MSBuild]::Divide($([System.DateTime]::UtcNow.TimeOfDay.TotalSeconds), 1.32))))</FileVersion>
<!-- Version impacts location of the user.config location, therefore remove the last part (seconds) -->
<Version>2.6.$([System.DateTime]::UtcNow.Date.Subtract($([System.DateTime]::Parse("2020-01-01"))).TotalDays)</Version>
<Version>2.7.$([System.DateTime]::UtcNow.Date.Subtract($([System.DateTime]::Parse("2020-01-01"))).TotalDays)</Version>
<UseWPF>true</UseWPF>
<AssemblyName>WFN</AssemblyName>
<RootNamespace>Wokhan.WindowsFirewallNotifier.Console</RootNamespace>
78 changes: 42 additions & 36 deletions Console/UI/Pages/AdapterInfo.xaml
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@
<Page.Resources>
<Converters:SecondsToDateTimeConverter x:Key="secondsConverter" />
<Style x:Key="Card" TargetType="Border">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" />
<Setter Property="Background" Value="#33AAAAAA" />
<Setter Property="CornerRadius" Value="5" />
<Setter Property="Padding" Value="10" />
<Setter Property="Margin" Value="5" />
@@ -31,17 +31,17 @@
</Grid>
</ContentControl>
<ScrollViewer>
<ItemsControl ItemsSource="{Binding}" DataContext="{Binding AllInterfaces}" d:DataContext="{d:DesignInstance Type=dummydata:ExposedInterfaceViewDummy,IsDesignTimeCreatable=True,CreateList=True}" Grid.IsSharedSizeScope="True">
<ItemsControl ItemsSource="{Binding}" DataContext="{Binding AllInterfaces}" d:DataContext="{d:DesignInstance Type=dummydata:ExposedInterfaceViewDummy,IsDesignTimeCreatable=True,CreateList=True}">
<ItemsControl.ItemTemplate>
<DataTemplate DataType="ExposedInterfaceView">
<Expander IsExpanded="True" Background="{DynamicResource {x:Static SystemColors.ControlBrushKey}}" Padding="5" Margin="10">
<Expander IsExpanded="True" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch" Background="Transparent">
<Expander.Header>
<Grid TextElement.FontWeight="Bold" Width="{Binding ActualWidth,RelativeSource={RelativeSource AncestorType=Expander}}">
<Grid Margin="10" Width="{Binding ActualWidth,RelativeSource={RelativeSource AncestorType=Expander}}">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="status" />
<ColumnDefinition Width="*" SharedSizeGroup="name"/>
<ColumnDefinition Width="Auto" SharedSizeGroup="bandwidthUP" />
<ColumnDefinition Width="Auto" SharedSizeGroup="bandwidthDOWN" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<ContentControl>
<ContentControl.Style>
@@ -55,15 +55,16 @@
</Style>
</ContentControl.Style>
</ContentControl>
<TextBlock Grid.Column="1" Text="{Binding Information.Name}" />
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Grid.Column="2" Margin="20,0,10,0">
<ContentControl Margin="0,0,5,0" Content="{StaticResource Image_DataUp}"/>
<TextBlock Text="{Binding Statistics.BytesSent,Converter={StaticResource unitFormatter},ConverterParameter='bps'}" Margin="0,0,10,0" />
</StackPanel>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" Grid.Column="3" Margin="10,0,10,0">
<ContentControl Margin="0,0,5,0" Content="{StaticResource Image_DataDown}"/>
<TextBlock Text="{Binding Statistics.BytesReceived,Converter={StaticResource unitFormatter},ConverterParameter='bps'}" />
</StackPanel>
<TextBlock Grid.Column="1" TextElement.FontWeight="Bold" Text="{Binding Information.Name}" HorizontalAlignment="Left" VerticalAlignment="Center" />
<Rectangle Grid.Column="2" Margin="10,0,0,0" HorizontalAlignment="Stretch" Height="2" Fill="Gray" />
<Border Grid.Column="3" Margin="0,0,50,0" Padding="20,5" BorderBrush="Gray" BorderThickness="2" CornerRadius="10">
<StackPanel Orientation="Horizontal">
<ContentControl Margin="0,0,5,0" Width="16" Content="{StaticResource Image_DataDown}"/>
<TextBlock Text="{Binding Bandwidth.In,Mode=OneWay,Converter={StaticResource unitFormatter},ConverterParameter='bps'}" />
<ContentControl Margin="20,0,5,0" Width="16" Content="{StaticResource Image_DataUp}"/>
<TextBlock Text="{Binding Bandwidth.Out,Mode=OneWay,Converter={StaticResource unitFormatter},ConverterParameter='bps'}" />
</StackPanel>
</Border>
</Grid>
</Expander.Header>
<StackPanel>
@@ -90,51 +91,56 @@
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Description" />
<TextBlock Grid.Column="1" Text="{Binding Information.Description}" TextTrimming="CharacterEllipsis" ToolTip="{Binding Information.Description}" />
<TextBlock Grid.Row="1" Text="Interface type" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Information.NetworkInterfaceType}" />
<TextBlock Grid.Row="2" Text="Speed" />
<TextBlock Grid.Row="2" Text="Max speed" />
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Information.Speed,Converter={StaticResource unitFormatter},ConverterParameter='bps'}" />
<TextBlock Grid.Row="3" Text="MAC address" />
<TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding MAC,Mode=OneTime}" />
</Grid>
</Border>
<Border Style="{StaticResource Card}">
<Grid >
<Grid HorizontalAlignment="Stretch">
<Grid.Resources>
<Style TargetType="TextBlock">
<Setter Property="Margin" Value="4" />
</Style>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" MinWidth="90" />
<ColumnDefinition Width="Auto" MinWidth="90" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Label FontWeight="Bold" Content="Detailed statistics" />
<Label Grid.Column="1" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="Incoming" />
<TextBlock Grid.Row="1" Text="Non-unicast packets" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Statistics.NonUnicastPacketsReceived}" />
<TextBlock Grid.Row="2" Text="Unicast packets" />
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Statistics.UnicastPacketsReceived}" />
<TextBlock Grid.Row="3" Text="Discarded packets" />
<TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding Statistics.IncomingPacketsDiscarded}" />
<TextBlock Grid.Row="4" Text="Erroneous packets" />
<TextBlock Grid.Row="4" Grid.Column="1" Text="{Binding Statistics.IncomingPacketsWithErrors}" />
<Label Grid.Column="2" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="Outgoing" Grid.ColumnSpan="2" />
<TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding Statistics.NonUnicastPacketsSent}" />
<TextBlock Grid.Row="2" Grid.Column="2" Text="{Binding Statistics.UnicastPacketsSent}" />
<TextBlock Grid.Row="3" Grid.Column="2" Text="{Binding Statistics.OutgoingPacketsDiscarded}" />
<TextBlock Grid.Row="4" Grid.Column="2" Text="{Binding Statistics.OutgoingPacketsWithErrors}" />
<Label Grid.Column="1" VerticalContentAlignment="Center" Content="Received" />
<Label Grid.Column="2" VerticalContentAlignment="Center" Content="Sent" />
<TextBlock Grid.Row="1" Text="Total Bytes (current session)" />
<TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Statistics.BytesReceived,Converter={StaticResource unitFormatter},ConverterParameter='B'}" />
<TextBlock Grid.Row="1" Grid.Column="2" Text="{Binding Statistics.BytesSent,Converter={StaticResource unitFormatter},ConverterParameter='B'}" />
<TextBlock Grid.Row="2" Text="Non-unicast packets" />
<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Statistics.NonUnicastPacketsReceived}" />
<TextBlock Grid.Row="2" Grid.Column="2" Text="{Binding Statistics.NonUnicastPacketsSent}" />
<TextBlock Grid.Row="3" Text="Unicast packets" />
<TextBlock Grid.Row="3" Grid.Column="1" Text="{Binding Statistics.UnicastPacketsReceived}" />
<TextBlock Grid.Row="3" Grid.Column="2" Text="{Binding Statistics.UnicastPacketsSent}" />
<TextBlock Grid.Row="4" Text="Discarded packets" />
<TextBlock Grid.Row="4" Grid.Column="1" Text="{Binding Statistics.IncomingPacketsDiscarded}" />
<TextBlock Grid.Row="4" Grid.Column="2" Text="{Binding Statistics.OutgoingPacketsDiscarded}" />
<TextBlock Grid.Row="5" Text="Erroneous packets" />
<TextBlock Grid.Row="5" Grid.Column="1" Text="{Binding Statistics.IncomingPacketsWithErrors}" />
<TextBlock Grid.Row="5" Grid.Column="2" Text="{Binding Statistics.OutgoingPacketsWithErrors}" />
</Grid>
</Border>
</UniformGrid>
25 changes: 13 additions & 12 deletions Console/UI/Pages/Connections.xaml
Original file line number Diff line number Diff line change
@@ -15,13 +15,6 @@
mc:Ignorable="d"
d:DesignHeight="400" d:DesignWidth="800"
Title="Connections">
<!--<Page.Resources>
<CollectionViewSource x:Key="connectionsView" Source="{Binding GroupedConnections}">
<CollectionViewSource.SortDescriptions>
<componentmodel:SortDescription PropertyName="FileName" Direction="Ascending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</Page.Resources>-->
<DockPanel>
<controls:AdminPanel Caption="Information provided here might be inaccurate for non-admin users." />
<ContentControl Style="{StaticResource ToolBarPanel}" Panel.ZIndex="0">
@@ -50,7 +43,15 @@
<RowDefinition Height="4" x:Name="separatorRow" />
<RowDefinition Height="*" x:Name="secondRow" />
</Grid.RowDefinitions>
<DataGrid x:Name="connections" VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.IsVirtualizingWhenGrouping="True" BorderThickness="0" ScrollViewer.HorizontalScrollBarVisibility="Disabled" ColumnWidth="*" GridLinesVisibility="None" SelectionMode="Single" IsReadOnly="True" d:DataContext="{d:DesignInstance Type=viewmodels:ConnectionDummy,IsDesignTimeCreatable=True,CreateList=True}" ItemsSource="{Binding ConnectionsView}" AutoGenerateColumns="False" HeadersVisibility="Column" Grid.ColumnSpan="3" Visibility="{Binding IsChecked,ElementName=toggleShowConnections,Mode=OneWay,Converter={StaticResource valueToVisibility}}" IsVisibleChanged="Components_VisibilityChanged">
<DataGrid x:Name="connections"
VirtualizingPanel.VirtualizationMode="Recycling" VirtualizingPanel.IsVirtualizingWhenGrouping="True"
AutoGenerateColumns="False" HeadersVisibility="Column" ScrollViewer.HorizontalScrollBarVisibility="Disabled" GridLinesVisibility="None"
BorderThickness="0"
SelectionMode="Single" IsReadOnly="True"
ItemsSource="{Binding ConnectionsView}"
d:DataContext="{d:DesignInstance Type=viewmodels:ConnectionDummy,IsDesignTimeCreatable=True,CreateList=True}"
Grid.ColumnSpan="3"
Visibility="{Binding IsChecked,ElementName=toggleShowConnections,Mode=OneWay,Converter={StaticResource valueToVisibility}}" IsVisibleChanged="Components_VisibilityChanged">
<DataGrid.GroupStyle>
<GroupStyle>
<GroupStyle.ContainerStyle>
@@ -87,8 +88,8 @@
</Style>
</ContentControl.Style>
</ContentControl>
<TextBlock Grid.Column="1" Text="{Binding Name.InboundBandwidth, Mode=OneWay, Converter={StaticResource unitFormatter}, ConverterParameter='bps'}" />
<ContentControl Grid.Column="2" Margin="20,0,5,0" Width="16">
<TextBlock Text="{Binding Name.InboundBandwidth, Mode=OneWay, Converter={StaticResource unitFormatter}, ConverterParameter='bps'}" />
<ContentControl Margin="20,0,5,0" Width="16">
<ContentControl.Style>
<Style TargetType="ContentControl">
<Setter Property="Content" Value="{StaticResource Image_DataUp}" />
@@ -100,7 +101,7 @@
</Style>
</ContentControl.Style>
</ContentControl>
<TextBlock Grid.Column="3" Text="{Binding Name.OutboundBandwidth, Mode=OneWay, Converter={StaticResource unitFormatter}, ConverterParameter='bps'}" />
<TextBlock Text="{Binding Name.OutboundBandwidth, Mode=OneWay, Converter={StaticResource unitFormatter}, ConverterParameter='bps'}" />
</StackPanel>
</Border>
</Grid>
@@ -261,7 +262,7 @@
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTextColumn Header="Remote port" Binding="{Binding TargetPort, Mode=OneTime}" />
<DataGridTextColumn Header="Remote port" Binding="{Binding TargetPort, Mode=OneTime}" Width="Auto" />
<DataGridTemplateColumn Header="Down" Width="Auto" MinWidth="50">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
25 changes: 10 additions & 15 deletions Console/UI/Pages/Connections.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,10 +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;
@@ -26,37 +31,27 @@ public partial class Connections : TimerBasedPage
private const double ConnectionTimeoutDying = 2000.0; //milliseconds
private const double ConnectionTimeoutNew = 1000.0; //milliseconds

private readonly object locker = new();
private readonly object uisynclocker = new();

//TODO: let the user pick a color palette for the bandwidth graph & connection
private List<Color>? Colors;

public GroupedObservableCollection<GroupedMonitoredConnections, MonitoredConnection> GroupedConnections { get; init; }

public CollectionView ConnectionsView { get; init; }
public GroupedObservableCollectionView<GroupedMonitoredConnections, MonitoredConnection> ConnectionsView { get; init; }

[ObservableProperty]
private string _textFilter = String.Empty;

partial void OnTextFilterChanged(string? value) => ResetTextFilter();
partial void OnTextFilterChanged(string value) => ResetTextFilter();

public Connections()
{
UpdateConnectionsColors();

Func<MonitoredConnection, GroupedMonitoredConnections> keyGetter = (MonitoredConnection connection) => GroupedConnections!.Keys.FirstOrDefault(group => group.Path == connection.Path) ?? new GroupedMonitoredConnections(connection, Colors![GroupedConnections!.Count % Colors.Count]);
GroupedMonitoredConnections keyGetter(MonitoredConnection connection) => GroupedConnections!.Keys.FirstOrDefault(group => group.Path == connection.Path) ?? new GroupedMonitoredConnections(connection, Colors![GroupedConnections!.Count % Colors.Count]);

GroupedConnections = new(keyGetter);

//ConnectionsView = new GroupedObservableCollectionsView2<GroupedMonitoredConnections, MonitoredConnection>(GroupedConnections, keyGetter);
ConnectionsView = new GroupedObservableCollectionView<GroupedMonitoredConnections, MonitoredConnection>(GroupedConnections, keyGetter);
//GroupedConnections.CollectionChanged += GroupedConnections_CollectionChanged;
//BindingOperations.EnableCollectionSynchronization(GroupedConnections, uisynclocker);

//ConnectionsViewSource.Source = GroupedConnections;
//ConnectionsViewSource.SortDescriptions.Add(new SortDescription(nameof(GroupedMonitoredConnections.FileName), ListSortDirection.Ascending));
//ConnectionsViewSource.GroupDescriptions.Add(new PropertyGroupDescription("Key"));//<GroupedMonitoredConnections, MonitoredConnection>(group => group.Path));
ConnectionsView = new(GroupedConnections, keyGetter);
ConnectionsView.SortDescriptions.Add(new SortDescription("FileName", ListSortDirection.Ascending));

Settings.Default.PropertyChanged += SettingsChanged;

38 changes: 7 additions & 31 deletions Console/UI/Windows/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -18,14 +18,14 @@
ResizeMode="CanResizeWithGrip">
<Grid>
<Grid.Background>
<ImageBrush ImageSource="/WFN;component/Resources/Backgrounds/Blue-Abstract.jpg" Opacity="0.1" />
<ImageBrush ImageSource="/WFN;component/Resources/Backgrounds/Blue-Abstract.jpg" Opacity="0.17" />
</Grid.Background>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition />
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<uicontrols:CustomWindowChrome CanClose="True" Height="{DynamicResource {x:Static SystemParameters.CaptionHeightKey}}" Background="{DynamicResource {x:Static SystemColors.ActiveCaptionBrushKey}}">
<uicontrols:CustomWindowChrome CanClose="True" Height="{DynamicResource {x:Static SystemParameters.CaptionHeightKey}}">
<Button Click="OpenOptions" ToolTip="settings">
<Path VerticalAlignment="Stretch" MaxHeight="18" Stretch="Uniform" Fill="#FFF12EA1" Data="F1 M 38,23.5C 38.8643,23.5 39.7109,23.5756 40.5337,23.7206L 42.6275,18.5381L 48.1901,20.787L 46.0964,25.9692C 47.6473,27.0149 48.9851,28.3527 50.0308,29.9036L 55.213,27.8099L 57.4619,33.3725L 52.2794,35.4664C 52.4244,36.2891 52.5,37.1357 52.5,38C 52.5,38.8643 52.4244,39.7109 52.2794,40.5337L 57.4619,42.6275L 55.213,48.1901L 50.0308,46.0964C 49.0795,47.5073 47.8865,48.7418 46.5112,49.7405L 48.7844,54.8462L 43.3041,57.2891L 41.0307,52.1828C 40.0533,52.3906 39.0394,52.5 38,52.5C 37.1357,52.5 36.2891,52.4244 35.4664,52.2794L 33.3725,57.462L 27.8099,55.213L 29.9036,50.0309C 28.3527,48.9851 27.0149,47.6473 25.9691,46.0964L 20.787,48.1901L 18.538,42.6275L 23.7206,40.5336C 23.5756,39.7109 23.5,38.8643 23.5,38C 23.5,37.1357 23.5756,36.2891 23.7206,35.4664L 18.538,33.3725L 20.787,27.8099L 25.9691,29.9036C 26.9205,28.4927 28.1135,27.2582 29.4889,26.2594L 27.2157,21.1537L 32.6959,18.7109L 34.9694,23.8172C 35.9468,23.6094 36.9606,23.5 38,23.5 Z M 38,28C 32.4771,28 28,32.4772 28,38C 28,43.5229 32.4771,48 38,48C 43.5228,48 48,43.5229 48,38C 48,32.4772 43.5228,28 38,28 Z " />
</Button>
@@ -38,16 +38,6 @@
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Border Grid.Column="1" Background="{DynamicResource TabItemOverBackground}">
<Border.RenderTransform>
<TransformGroup>
<TranslateTransform X="{Binding ActualWidth,ElementName=widthIndicator}" />
</TransformGroup>
</Border.RenderTransform>
<Border.Effect>
<DropShadowEffect Direction="180" Color="White" ShadowDepth="1" />
</Border.Effect>
</Border>
<TabControl x:Name="menuGrid" Background="Transparent" Grid.ColumnSpan="2" HorizontalContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" BorderThickness="0" TabStripPlacement="Left" Grid.IsSharedSizeScope="True">
<TabControl.Resources>
<Style x:Key="Separator" TargetType="TabItem">
@@ -60,29 +50,15 @@
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Grid ClipToBounds="True" Margin="5,0,1,0">
<Grid Margin="0,0,-2,0">
<Label x:Name="Label" BorderThickness="4,0,0,0" BorderBrush="Transparent" Height="40" Margin="0,3,0,3" Padding="10,5,15,5" Content="{TemplateBinding Header}" ext:CustomAdorner.Image="{Binding Path=(ext:CustomAdorner.Image),RelativeSource={x:Static RelativeSource.TemplatedParent}}" />
</Grid>
</Grid>
<Border x:Name="Container" CornerRadius="20" Margin="5">
<Label Height="40" Margin="5,0" Padding="10,5,15,5" Content="{TemplateBinding Header}" ext:CustomAdorner.Image="{Binding Path=(ext:CustomAdorner.Image),RelativeSource={x:Static RelativeSource.TemplatedParent}}" />
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Label" Property="Background" Value="{DynamicResource TabItemOverBackground}" />
<Setter TargetName="Label" Property="BorderBrush" Value="{DynamicResource AccentColorBrush}" />
<Setter TargetName="Label" Property="ext:CustomAdorner.BackgroundEffect">
<Setter.Value>
<DropShadowEffect Direction="-90" ShadowDepth="0" Color="LightGray" />
</Setter.Value>
</Setter>
<Setter TargetName="Container" Property="Background" Value="{DynamicResource TabItemOverBackground}" />
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Label" Property="Background" Value="{DynamicResource TabItemOverBackground}" />
<Setter TargetName="Label" Property="BorderBrush" Value="{DynamicResource AccentColorBrush}" />
<Setter TargetName="Label" Property="ext:CustomAdorner.BackgroundEffect">
<Setter.Value>
<DropShadowEffect Direction="-90" ShadowDepth="1" Color="LightGray" />
</Setter.Value>
</Setter>
<Setter TargetName="Container" Property="Background" Value="{DynamicResource AccentColorBrush}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
64 changes: 45 additions & 19 deletions Console/ViewModels/ExposedInterfaceView.cs
Original file line number Diff line number Diff line change
@@ -1,36 +1,62 @@
using System.ComponentModel;
using CommunityToolkit.Mvvm.ComponentModel;
using System.Diagnostics;
using System.Net.NetworkInformation;

namespace Wokhan.WindowsFirewallNotifier.Console.ViewModels;

public partial class ExposedInterfaceView : INotifyPropertyChanged
public partial class ExposedInterfaceView : ObservableObject
{
public event PropertyChangedEventHandler? PropertyChanged;
public string MAC { get; init; }

public void NotifyPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
[ObservableProperty]
private NetworkInterface _information;

public string MAC => String.Join(":", Information.GetPhysicalAddress().GetAddressBytes().Select(b => b.ToString("X2")));
[ObservableProperty]
public IPInterfaceStatistics _statistics;

public NetworkInterface Information { get; private set; }
[ObservableProperty]
private IPInterfaceProperties _properties;

public IPInterfaceStatistics Statistics => Information.GetIPStatistics();
[ObservableProperty]
private ComputedBandwidth _bandwidth = new();

private readonly int delay = 400; // Arbitrary delay, must be lower than the Adapters page timer
private readonly Stopwatch sw = new();
private async Task ComputeBandwidth()
{
var initial = Information.GetIPStatistics();
sw.Restart();
await Task.Delay(delay);
var final = Information.GetIPStatistics();
sw.Stop();

var inb = AdjustBandwidth(initial.BytesReceived, final.BytesReceived, sw.ElapsedMilliseconds);
var outb = AdjustBandwidth(initial.BytesSent, final.BytesSent, sw.ElapsedMilliseconds);

Bandwidth = new ComputedBandwidth(inb, outb);
}

private static double AdjustBandwidth(long bytes1, long bytes2, long elapsedms)
{
return Math.Max(0, (bytes2 - bytes1) * 8 / elapsedms * 1000);
}

public IPInterfaceProperties Properties => Information.GetIPProperties();
public record ComputedBandwidth(double In = 0, double Out = 0);

public ExposedInterfaceView(NetworkInterface inter)
public ExposedInterfaceView(NetworkInterface netInterface)
{
this.Information = inter;
this.MAC = String.Join(":", netInterface.GetPhysicalAddress().GetAddressBytes().Select(b => b.ToString("X2")));

UpdateInner(netInterface);
}

internal void UpdateInner(NetworkInterface inter)
internal void UpdateInner(NetworkInterface netInterface)
{
this.Information = inter;
NotifyPropertyChanged(nameof(Information));
NotifyPropertyChanged(nameof(Statistics));
NotifyPropertyChanged(nameof(Properties));
NotifyPropertyChanged(nameof(MAC));
Information = netInterface;

_ = ComputeBandwidth();

Statistics = Information.GetIPStatistics();
Properties = Information.GetIPProperties();
}
}
5 changes: 3 additions & 2 deletions WindowsFirewallNotifier.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29201.188
# Visual Studio Version 17
VisualStudioVersion = 17.10.34902.84
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Notifier", "Notifier\Notifier.csproj", "{B0777CD1-8F34-4370-8D9F-C4F896AD542B}"
EndProject
@@ -12,6 +12,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{8868B2A5-29B2-4774-B32D-913A242232BC}"
ProjectSection(SolutionItems) = preProject
.editorconfig = .editorconfig
.gitignore = .gitignore
EndProjectSection
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Common.Tests", "Common.Tests\Common.Tests.csproj", "{999EE8FD-93FF-44F0-AC66-4DE6AF6303D2}"

0 comments on commit e4da03f

Please sign in to comment.