-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMainWindow.WorkspaceModeSwitch.cs
More file actions
270 lines (235 loc) · 9.11 KB
/
Copy pathMainWindow.WorkspaceModeSwitch.cs
File metadata and controls
270 lines (235 loc) · 9.11 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Copyright 2026 Ari Sulistiono
// SPDX-License-Identifier: Apache-2.0
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
namespace ArIED61850Tester;
public partial class MainWindow
{
private const string WorkspaceModeSwitchTag = "ARSAS_WORKSPACE_MODE_SWITCH";
private static readonly bool WorkspaceModeSwitchRegistered = RegisterWorkspaceModeSwitch();
private Button? _workspaceFatButton;
private IoListTestingWindow? _loadedIoFatWindow;
private static bool RegisterWorkspaceModeSwitch()
{
EventManager.RegisterClassHandler(
typeof(MainWindow),
FrameworkElement.LoadedEvent,
new RoutedEventHandler(WorkspaceModeSwitch_Loaded));
return true;
}
private static void WorkspaceModeSwitch_Loaded(object sender, RoutedEventArgs e)
{
if (sender is MainWindow window)
window.InstallWorkspaceModeSwitch();
}
private void InstallWorkspaceModeSwitch()
{
if (Content is not Grid root)
return;
var header = root.Children.OfType<Grid>().FirstOrDefault(child => Grid.GetRow(child) == 0);
if (header == null || header.Children.OfType<FrameworkElement>()
.Any(child => Equals(child.Tag, WorkspaceModeSwitchTag)))
return;
var shell = new Border
{
Tag = WorkspaceModeSwitchTag,
Background = WorkspaceBrush("#E7ECF5"),
BorderBrush = WorkspaceBrush("#D5DEEB"),
BorderThickness = new Thickness(1),
CornerRadius = new CornerRadius(16),
Padding = new Thickness(4),
Margin = new Thickness(10, 0, 10, 0),
HorizontalAlignment = HorizontalAlignment.Right,
VerticalAlignment = VerticalAlignment.Center,
ToolTip = "Switch between Engineering and IO List FAT workspaces"
};
Grid.SetColumn(shell, 1);
var modes = new StackPanel { Orientation = Orientation.Horizontal };
modes.Children.Add(new Border
{
Background = TryFindResource("Accent") as Brush ?? WorkspaceBrush("#2563EB"),
CornerRadius = new CornerRadius(12),
Padding = new Thickness(12, 7, 12, 7),
Child = new TextBlock
{
Text = "ENGINEERING",
Foreground = Brushes.White,
FontSize = 10.5,
FontWeight = FontWeights.Bold,
VerticalAlignment = VerticalAlignment.Center
}
});
_workspaceFatButton = new Button
{
Content = "IO LIST FAT",
Style = TryFindResource("SoftButton") as Style,
Padding = new Thickness(12, 7, 12, 7),
Margin = new Thickness(4, 0, 0, 0),
FontSize = 10.5,
FontWeight = FontWeights.Bold,
Cursor = Cursors.Hand,
ToolTip = "Open or return to the IO List FAT workspace"
};
_workspaceFatButton.Click += OpenOrResumeIoFatWorkspace_Click;
modes.Children.Add(_workspaceFatButton);
var menuButton = new Button
{
Content = "▾",
Style = TryFindResource("SoftButton") as Style,
Padding = new Thickness(8, 7, 8, 7),
Margin = new Thickness(2, 0, 0, 0),
FontSize = 10.5,
FontWeight = FontWeights.Bold,
Cursor = Cursors.Hand,
ToolTip = "Load another IO List workbook or portable ARSAS project"
};
menuButton.Click += OpenIoFatWorkspaceMenu_Click;
modes.Children.Add(menuButton);
shell.Child = modes;
header.Children.Add(shell);
UpdateIoFatWorkspaceModeState();
}
internal void RegisterLoadedIoFatWindow(IoListTestingWindow window)
{
ArgumentNullException.ThrowIfNull(window);
if (ReferenceEquals(_loadedIoFatWindow, window))
return;
if (_loadedIoFatWindow != null)
_loadedIoFatWindow.Closed -= LoadedIoFatWindow_Closed;
_loadedIoFatWindow = window;
_loadedIoFatWindow.Closed += LoadedIoFatWindow_Closed;
UpdateIoFatWorkspaceModeState();
}
internal void ShowEngineeringWorkspaceFromFat(IoListTestingWindow window)
{
if (!ReferenceEquals(_loadedIoFatWindow, window))
RegisterLoadedIoFatWindow(window);
window.Storage?.ScheduleSave();
window.Hide();
IsEnabled = true;
Show();
if (WindowState == WindowState.Minimized)
WindowState = WindowState.Normal;
Activate();
SetStatus($"Engineering workspace active · IO List FAT project '{window.Project.ProjectName}' remains loaded.");
UpdateIoFatWorkspaceModeState();
}
private void OpenOrResumeIoFatWorkspace_Click(object sender, RoutedEventArgs e)
{
if (ShowLoadedIoFatWorkspace())
return;
if (sender is Button anchor)
OpenIoFatWorkspaceMenu(anchor);
}
private bool ShowLoadedIoFatWorkspace()
{
var window = _loadedIoFatWindow;
if (window == null || !window.IsLoaded)
return false;
SetStatus($"Returning to loaded IO List FAT project '{window.Project.ProjectName}'.");
IsEnabled = false;
Hide();
window.Show();
if (window.WindowState == WindowState.Minimized)
window.WindowState = WindowState.Normal;
window.Activate();
return true;
}
private void OpenIoFatWorkspaceMenu_Click(object sender, RoutedEventArgs e)
{
if (sender is Button anchor)
OpenIoFatWorkspaceMenu(anchor);
}
private void OpenIoFatWorkspaceMenu(Button anchor)
{
var menu = new ContextMenu
{
PlacementTarget = anchor,
Placement = PlacementMode.Bottom,
VerticalOffset = 5,
StaysOpen = false
};
if (_loadedIoFatWindow is { IsLoaded: true } loaded)
{
var resume = new MenuItem
{
Header = $"Continue loaded FAT project · {loaded.Project.ProjectName}",
FontWeight = FontWeights.SemiBold
};
resume.Click += (_, _) => ShowLoadedIoFatWorkspace();
menu.Items.Add(resume);
menu.Items.Add(new Separator());
}
var importWorkbook = new MenuItem
{
Header = _loadedIoFatWindow == null
? "Import IO List Excel workbook"
: "Import another IO List Excel workbook"
};
importWorkbook.Click += (_, _) => QueueIoFatWorkspaceReplacement(
() => OpenIoListTesting_Click(this, new RoutedEventArgs()));
var openProject = new MenuItem
{
Header = _loadedIoFatWindow == null
? "Open portable .arsas project"
: "Open another portable .arsas project"
};
openProject.Click += (_, _) => QueueIoFatWorkspaceReplacement(
() => OpenIoListPackage_Click(this, new RoutedEventArgs()));
menu.Items.Add(importWorkbook);
menu.Items.Add(openProject);
menu.IsOpen = true;
}
private void QueueIoFatWorkspaceReplacement(Action openReplacement)
{
ArgumentNullException.ThrowIfNull(openReplacement);
var loaded = _loadedIoFatWindow;
if (loaded == null || !loaded.IsLoaded)
{
Dispatcher.BeginInvoke(openReplacement, DispatcherPriority.ContextIdle);
return;
}
var answer = MessageBox.Show(
this,
$"IO List FAT project '{loaded.Project.ProjectName}' is still loaded.\n\nSave and close that workspace before loading another project?",
"Load another IO List FAT project",
MessageBoxButton.YesNo,
MessageBoxImage.Question,
MessageBoxResult.No);
if (answer != MessageBoxResult.Yes)
return;
loaded.Close();
if (ReferenceEquals(_loadedIoFatWindow, loaded))
return;
Dispatcher.BeginInvoke(openReplacement, DispatcherPriority.ContextIdle);
}
private void LoadedIoFatWindow_Closed(object? sender, EventArgs e)
{
if (sender is IoListTestingWindow window)
window.Closed -= LoadedIoFatWindow_Closed;
if (ReferenceEquals(_loadedIoFatWindow, sender))
_loadedIoFatWindow = null;
IsEnabled = true;
UpdateIoFatWorkspaceModeState();
}
private void UpdateIoFatWorkspaceModeState()
{
if (_workspaceFatButton == null)
return;
var loaded = _loadedIoFatWindow is { IsLoaded: true };
_workspaceFatButton.Content = loaded ? "IO LIST FAT · LOADED" : "IO LIST FAT";
_workspaceFatButton.ToolTip = loaded
? "Return instantly to the loaded IO List FAT workspace"
: "Open an IO List FAT workbook or portable ARSAS project";
}
private static SolidColorBrush WorkspaceBrush(string hex)
{
var brush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(hex));
brush.Freeze();
return brush;
}
}