Repository navigation
Expand file tree
/
Copy pathIPScannerView.xaml.cs
More file actions
168 lines (133 loc) · 4.84 KB
/
Copy pathIPScannerView.xaml.cs
File metadata and controls
168 lines (133 loc) · 4.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
using System;
using System.Collections;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using NETworkManager.Controls;
using NETworkManager.Models.Network;
using NETworkManager.Utilities;
using NETworkManager.ViewModels;
using VisualTreeHelper = System.Windows.Media.VisualTreeHelper;
namespace NETworkManager.Views;
public partial class IPScannerView : IDragablzTabItem
{
private readonly IPScannerViewModel _viewModel;
public IPScannerView(Guid tabId, string hostOrIPRange = null)
{
InitializeComponent();
_viewModel = new IPScannerViewModel(tabId, hostOrIPRange);
DataContext = _viewModel;
Dispatcher.ShutdownStarted += Dispatcher_ShutdownStarted;
}
public void CloseTab()
{
_viewModel.OnClose();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
_viewModel.OnLoaded();
}
private void Dispatcher_ShutdownStarted(object sender, EventArgs e)
{
_viewModel.OnClose();
}
private void ContextMenu_Opened(object sender, RoutedEventArgs e)
{
if (sender is not ContextMenu menu)
return;
// Set DataContext to ViewModel
menu.DataContext = _viewModel;
// Append custom commands
var index = menu.Items.Count - 1;
var entryFound = false;
for (var i = 0; i < menu.Items.Count; i++)
{
if (menu.Items[i] is not MenuItem item)
continue;
if ((string)item.Tag != "CustomCommands")
continue;
index = i;
entryFound = true;
break;
}
if (!entryFound)
return;
// Clear existing items in custom commands
((MenuItem)menu.Items[index])?.Items.Clear();
// Add items to custom commands
foreach (var info in IPScannerViewModel.CustomCommands)
((MenuItem)menu.Items[index])?.Items.Add(new MenuItem
{
Header = info.Name,
Command = _viewModel.CustomCommandCommand,
CommandParameter = info.ID
});
}
private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
{
// Get the row from the sender
for (var visual = sender as Visual; visual != null; visual = VisualTreeHelper.GetParent(visual) as Visual)
{
if (visual is not DataGridRow row)
continue;
row.DetailsVisibility =
row.DetailsVisibility == Visibility.Visible ? Visibility.Collapsed : Visibility.Visible;
break;
}
}
private void DataGrid_OnSorting(object sender, DataGridSortingEventArgs e)
{
var column = e.Column;
var selectedComparer = -1;
switch (column.SortMemberPath)
{
case $"{nameof(PingInfo)}.{nameof(PingInfo.IPAddress)}":
selectedComparer = 0;
break;
case nameof(IPScannerHostInfo.MACAddress):
selectedComparer = 1;
break;
default:
return;
}
// Prevent the built-in sort from sorting
e.Handled = true;
// Get the direction
var direction = column.SortDirection != ListSortDirection.Ascending
? ListSortDirection.Ascending
: ListSortDirection.Descending;
// Update the sort direction
e.Column.SortDirection = direction;
// Get the view
var view = (ListCollectionView)CollectionViewSource.GetDefaultView(((DataGrid)sender).ItemsSource);
// Sort the view
view.CustomSort = new DataGridComparer(direction, selectedComparer);
}
public class DataGridComparer(ListSortDirection direction, int comparer = -1) : IComparer
{
public int Compare(object x, object y)
{
// No comparer selected
if (comparer == -1)
return 0;
// Get data from objects
if (x is not IPScannerHostInfo first || y is not IPScannerHostInfo second)
return 0;
// Compare the data
return comparer switch
{
// IP address
0 => direction == ListSortDirection.Ascending
? IPAddressHelper.CompareIPAddresses(first.PingInfo.IPAddress, second.PingInfo.IPAddress)
: IPAddressHelper.CompareIPAddresses(second.PingInfo.IPAddress, first.PingInfo.IPAddress),
// MAC address
1 => direction == ListSortDirection.Ascending
? MACAddressHelper.CompareMACAddresses(first.MACAddress, second.MACAddress)
: MACAddressHelper.CompareMACAddresses(second.MACAddress, first.MACAddress),
_ => 0
};
}
}
}