Repository navigation
Expand file tree
/
Copy pathInputSuggestions.cs
More file actions
128 lines (113 loc) · 5.55 KB
/
Copy pathInputSuggestions.cs
File metadata and controls
128 lines (113 loc) · 5.55 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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Windows.Data;
using NETworkManager.Controls;
using NETworkManager.Localization.Resources;
using NETworkManager.Settings;
namespace NETworkManager;
/// <summary>
/// Entry of an input with profiles and history (e.g. the ports of the Port Scanner): a profile (with a description,
/// e.g. its ports) or an entry of the history.
/// </summary>
/// <param name="Text">Text inserted into the input: name of the profile or the history entry.</param>
/// <param name="Description">Description of the profile (e.g. its ports), null for a history entry.</param>
/// <param name="Group">Group in the list (profiles, history).</param>
public sealed record InputSuggestion(string Text, string Description, string Group)
{
/// <summary>
/// The text, e.g. kept in an editable ComboBox when the entry is removed from the list (see
/// <see cref="ComboBoxHistoryBehavior" />).
/// </summary>
public override string ToString()
{
return Text;
}
}
/// <summary>
/// History and profiles for an input (an editable ComboBox like the ports of the Port Scanner or the DNS server of
/// DNS Lookup): the history first (recently used, at most the number of history entries, a single profile name is
/// not added), then the profiles, grouped, and the names of the profiles for the Tab completion.
/// Updated when the profiles or the history change (weak events, the list doesn't keep a closed tab or dialog
/// alive).
/// </summary>
public sealed class InputSuggestions
{
private readonly ObservableRangeCollection<InputSuggestion> _suggestions = [];
private readonly Func<IEnumerable<(string Name, string Description)>> _getProfiles;
private readonly string _profilesGroup;
private readonly ObservableCollection<string> _history;
/// <summary>
/// Creates the list with the current profiles and history.
/// </summary>
/// <param name="profiles">Collection of the profiles (to update the list when it changes).</param>
/// <param name="getProfiles">Gets the names and descriptions of the profiles, in the order shown.</param>
/// <param name="profilesGroup">Name of the group of the profiles.</param>
/// <param name="history">History of the input.</param>
private InputSuggestions(INotifyCollectionChanged profiles,
Func<IEnumerable<(string Name, string Description)>> getProfiles, string profilesGroup,
ObservableCollection<string> history)
{
_getProfiles = getProfiles;
_profilesGroup = profilesGroup;
_history = history;
View = new CollectionViewSource { Source = _suggestions }.View;
View.GroupDescriptions.Add(new PropertyGroupDescription(nameof(InputSuggestion.Group)));
CollectionChangedEventManager.AddHandler(profiles, OnSourceChanged);
CollectionChangedEventManager.AddHandler(history, OnSourceChanged);
Refresh();
}
/// <summary>
/// Creates the list for the ports of the Port Scanner: the port profiles (sorted by name, with their ports) and
/// the ports history.
/// </summary>
public static InputSuggestions ForPortScannerPorts()
{
return new InputSuggestions(SettingsManager.Current.PortScanner_PortProfiles,
() => SettingsManager.Current.PortScanner_PortProfiles
.OrderBy(x => x.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => (x.Name, x.Ports)),
Strings.PortProfiles, SettingsManager.Current.PortScanner_PortHistory);
}
/// <summary>
/// Creates the list for the DNS server of DNS Lookup: the DNS servers (Windows DNS first, then sorted by name,
/// with their servers) and the DNS server history.
/// </summary>
public static InputSuggestions ForDNSLookupDNSServers()
{
return new InputSuggestions(SettingsManager.Current.DNSLookup_DNSServers,
() => SettingsManager.Current.DNSLookup_DNSServers
.OrderByDescending(x => x.UseWindowsDNSServer)
.ThenBy(x => x.Name, StringComparer.OrdinalIgnoreCase)
.Select(x => (x.Name, x.UseWindowsDNSServer
? null
: string.Join("; ", x.Servers.Select(server => server.Port == 53 ? server.Server : server.ToString())))),
Strings.DNSServers, SettingsManager.Current.DNSLookup_DNSServerHistory);
}
/// <summary>
/// Gets the view of the entries, grouped (history, profiles).
/// </summary>
public ICollectionView View { get; }
/// <summary>
/// Gets the names of the profiles, for the Tab completion of an entry (see
/// <see cref="ComboBoxListCompletionBehavior" />).
/// </summary>
public ObservableRangeCollection<string> ProfileNames { get; } = [];
private void OnSourceChanged(object sender, NotifyCollectionChangedEventArgs e)
{
Refresh();
}
private void Refresh()
{
var profiles = _getProfiles().ToList();
ProfileNames.ReplaceAll(profiles.Select(x => x.Name));
// History first: the recently used inputs (few, usually custom ones) are visible without scrolling below the
// profiles; the groups are shown in the order of their first entry
_suggestions.ReplaceAll(_history
.Select(x => new InputSuggestion(x, null, Strings.History))
.Concat(profiles.Select(x => new InputSuggestion(x.Name, x.Description, _profilesGroup))));
}
}