-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainWindow.FieldPresentationFix.cs
More file actions
168 lines (142 loc) · 5.9 KB
/
Copy pathMainWindow.FieldPresentationFix.cs
File metadata and controls
168 lines (142 loc) · 5.9 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.Globalization;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Threading;
namespace ArIED61850Tester;
/// <summary>
/// Field-facing presentation fixes for the Engineering workspace.
/// Keeps source/evidence values untouched while making the live workspace easier to read.
/// </summary>
internal static class MainWindowFieldPresentationFix
{
private const string IedTimestampHeader = "IED Timestamp";
[ModuleInitializer]
internal static void Register()
{
EventManager.RegisterClassHandler(
typeof(MainWindow),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(OnMainWindowLoaded));
}
private static void OnMainWindowLoaded(object sender, RoutedEventArgs e)
{
if (sender is not MainWindow window)
return;
if (window.FindName("MainTabs") is TabControl tabs)
{
tabs.SelectionChanged -= MainTabs_SelectionChanged;
tabs.SelectionChanged += MainTabs_SelectionChanged;
}
Apply(window);
window.Dispatcher.BeginInvoke(
DispatcherPriority.Loaded,
new Action(() => Apply(window)));
}
private static void MainTabs_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (sender is not TabControl tabs || !ReferenceEquals(e.Source, tabs))
return;
if (Window.GetWindow(tabs) is MainWindow window)
{
window.Dispatcher.BeginInvoke(
DispatcherPriority.Loaded,
new Action(() => Apply(window)));
}
}
private static void Apply(MainWindow window)
{
ApplyIedTimestampColumns(window);
ApplyDarkCommandHeaderContrast(window);
}
private static void ApplyIedTimestampColumns(MainWindow window)
{
foreach (var grid in VisualDescendants<DataGrid>(window))
{
foreach (var column in grid.Columns.OfType<DataGridTextColumn>())
{
if (!string.Equals(column.Header?.ToString(), IedTimestampHeader, StringComparison.OrdinalIgnoreCase))
continue;
if (column.Binding is Binding existing &&
ReferenceEquals(existing.Converter, RoundedIedTimestampConverter.Instance))
{
continue;
}
column.Binding = new Binding("DeviceTimestamp")
{
Mode = BindingMode.OneWay,
Converter = RoundedIedTimestampConverter.Instance
};
var style = new Style(typeof(TextBlock), column.ElementStyle);
style.Setters.Add(new Setter(
FrameworkElement.ToolTipProperty,
new Binding("DeviceTimestamp")
{
Mode = BindingMode.OneWay,
Converter = FullIedTimestampTooltipConverter.Instance
}));
style.Setters.Add(new Setter(ToolTipService.ShowDurationProperty, 60000));
column.ElementStyle = style;
}
}
}
private static void ApplyDarkCommandHeaderContrast(MainWindow window)
{
if (window.FindName("CommandPanelExpander") is not Expander expander || expander.Header is not DependencyObject header)
return;
expander.Foreground = Brushes.White;
foreach (var text in VisualDescendants<TextBlock>(header).Prepend(header as TextBlock).OfType<TextBlock>())
text.Foreground = Brushes.White;
}
private static IEnumerable<T> VisualDescendants<T>(DependencyObject root) where T : DependencyObject
{
if (root is T self)
yield return self;
var count = VisualTreeHelper.GetChildrenCount(root);
for (var index = 0; index < count; index++)
{
var child = VisualTreeHelper.GetChild(root, index);
foreach (var descendant in VisualDescendants<T>(child))
yield return descendant;
}
}
private sealed class RoundedIedTimestampConverter : IValueConverter
{
internal static readonly RoundedIedTimestampConverter Instance = new();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
=> Iec61850TimestampPresentation.FormatMilliseconds(value?.ToString());
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> Binding.DoNothing;
}
private sealed class FullIedTimestampTooltipConverter : IValueConverter
{
internal static readonly FullIedTimestampTooltipConverter Instance = new();
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var text = value?.ToString()?.Trim() ?? string.Empty;
if (text.Length == 0 || text == "-")
return text.Length == 0 ? "-" : text;
if (DateTime.TryParse(
text,
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces | DateTimeStyles.RoundtripKind,
out var dateTime))
{
return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fffffff", CultureInfo.InvariantCulture);
}
if (DateTimeOffset.TryParse(
text,
CultureInfo.InvariantCulture,
DateTimeStyles.AllowWhiteSpaces,
out var dateTimeOffset))
{
return dateTimeOffset.ToString("yyyy-MM-dd HH:mm:ss.fffffff zzz", CultureInfo.InvariantCulture);
}
return text;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
=> Binding.DoNothing;
}
}