-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainViewModel.cs
More file actions
281 lines (227 loc) · 10.5 KB
/
MainViewModel.cs
File metadata and controls
281 lines (227 loc) · 10.5 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
271
272
273
274
275
276
277
278
279
280
281
using System.Collections.Frozen;
namespace DotPilot.Presentation;
public sealed class MainViewModel : ObservableObject
{
private const double IndentSize = 16d;
private const string DefaultDocumentTitle = "Select a file";
private const string DefaultDocumentPath = "Choose a repository item from the left sidebar.";
private const string DefaultDocumentStatus = "The file surface becomes active after you open a file.";
private const string DefaultInspectorArtifactsTitle = "Artifacts";
private const string DefaultInspectorLogsTitle = "Runtime log console";
private const string DefaultInspectorArtifactsSummary = "Generated files, plans, screenshots, and session outputs stay attached to the current workbench.";
private const string DefaultInspectorLogsSummary = "Runtime logs remain visible without leaving the main workbench.";
private const string DefaultLanguageLabel = "No document";
private const string DefaultRendererLabel = "Select a repository item";
private const string DefaultPreviewContent = "Open a file from the repository tree to inspect it here.";
private readonly FrozenDictionary<string, WorkbenchDocumentDescriptor> _documentsByPath;
private readonly IReadOnlyList<WorkbenchRepositoryNodeItem> _allRepositoryNodes;
private IReadOnlyList<WorkbenchRepositoryNodeItem> _filteredRepositoryNodes;
private string _repositorySearchText = string.Empty;
private WorkbenchRepositoryNodeItem? _selectedRepositoryNode;
private WorkbenchDocumentDescriptor? _selectedDocument;
private string _editablePreviewContent = DefaultPreviewContent;
private bool _isDiffReviewMode;
private bool _isLogConsoleVisible;
public MainViewModel(
IWorkbenchCatalog workbenchCatalog,
IRuntimeFoundationCatalog runtimeFoundationCatalog)
{
try
{
BrowserConsoleDiagnostics.Info("[DotPilot.Startup] MainViewModel constructor started.");
ArgumentNullException.ThrowIfNull(workbenchCatalog);
ArgumentNullException.ThrowIfNull(runtimeFoundationCatalog);
Snapshot = workbenchCatalog.GetSnapshot();
BrowserConsoleDiagnostics.Info(
$"[DotPilot.Startup] MainViewModel workbench snapshot loaded. Nodes={Snapshot.RepositoryNodes.Count}, Documents={Snapshot.Documents.Count}.");
RuntimeFoundation = runtimeFoundationCatalog.GetSnapshot();
BrowserConsoleDiagnostics.Info(
$"[DotPilot.Startup] MainViewModel runtime foundation snapshot loaded. Providers={RuntimeFoundation.Providers.Count}.");
EpicLabel = WorkbenchIssues.FormatIssueLabel(WorkbenchIssues.DesktopWorkbenchEpic);
_documentsByPath = Snapshot.Documents.ToFrozenDictionary(document => document.RelativePath, StringComparer.OrdinalIgnoreCase);
_allRepositoryNodes = Snapshot.RepositoryNodes
.Select(MapRepositoryNode)
.ToArray();
_filteredRepositoryNodes = _allRepositoryNodes;
_selectedDocument = Snapshot.Documents.Count > 0 ? Snapshot.Documents[0] : null;
_editablePreviewContent = _selectedDocument?.PreviewContent ?? DefaultPreviewContent;
var initialNode = _selectedDocument is null
? FindFirstOpenableNode(_allRepositoryNodes)
: FindNodeByRelativePath(_allRepositoryNodes, _selectedDocument.RelativePath);
if (initialNode is not null)
{
SetSelectedRepositoryNode(initialNode);
}
BrowserConsoleDiagnostics.Info("[DotPilot.Startup] MainViewModel constructor completed.");
}
catch (Exception exception)
{
BrowserConsoleDiagnostics.Error($"[DotPilot.Startup] MainViewModel constructor failed: {exception}");
throw;
}
}
public WorkbenchSnapshot Snapshot { get; }
public RuntimeFoundationSnapshot RuntimeFoundation { get; }
public string EpicLabel { get; }
public string PageTitle => Snapshot.SessionTitle;
public string WorkspaceName => Snapshot.WorkspaceName;
public string WorkspaceRoot => Snapshot.WorkspaceRoot;
public string SearchPlaceholder => Snapshot.SearchPlaceholder;
public string SessionStage => Snapshot.SessionStage;
public string SessionSummary => Snapshot.SessionSummary;
public IReadOnlyList<WorkbenchSessionEntry> SessionEntries => Snapshot.SessionEntries;
public IReadOnlyList<WorkbenchArtifactDescriptor> Artifacts => Snapshot.Artifacts;
public IReadOnlyList<WorkbenchLogEntry> Logs => Snapshot.Logs;
public IReadOnlyList<WorkbenchRepositoryNodeItem> FilteredRepositoryNodes
{
get => _filteredRepositoryNodes;
private set
{
if (ReferenceEquals(_filteredRepositoryNodes, value))
{
return;
}
_filteredRepositoryNodes = value;
RaisePropertyChanged();
RaisePropertyChanged(nameof(RepositoryResultSummary));
}
}
public string RepositoryResultSummary => $"{FilteredRepositoryNodes.Count} items";
public string RepositorySearchText
{
get => _repositorySearchText;
set
{
if (!SetProperty(ref _repositorySearchText, value))
{
return;
}
UpdateFilteredRepositoryNodes();
}
}
public WorkbenchRepositoryNodeItem? SelectedRepositoryNode
{
get => _selectedRepositoryNode;
set => SetSelectedRepositoryNode(value);
}
public string SelectedDocumentTitle => _selectedDocument?.Title ?? DefaultDocumentTitle;
public string SelectedDocumentPath => _selectedDocument?.RelativePath ?? DefaultDocumentPath;
public string SelectedDocumentStatus => _selectedDocument?.StatusSummary ?? DefaultDocumentStatus;
public string SelectedDocumentLanguage => _selectedDocument?.LanguageLabel ?? DefaultLanguageLabel;
public string SelectedDocumentRenderer => _selectedDocument?.RendererLabel ?? DefaultRendererLabel;
public bool SelectedDocumentIsReadOnly => _selectedDocument?.IsReadOnly ?? true;
public IReadOnlyList<WorkbenchDiffLine> SelectedDocumentDiffLines => _selectedDocument?.DiffLines ?? [];
public string EditablePreviewContent
{
get => _editablePreviewContent;
set => SetProperty(ref _editablePreviewContent, value);
}
public bool IsDiffReviewMode
{
get => _isDiffReviewMode;
set
{
if (!SetProperty(ref _isDiffReviewMode, value))
{
return;
}
RaisePropertyChanged(nameof(IsPreviewMode));
}
}
public bool IsPreviewMode => !IsDiffReviewMode;
public bool IsLogConsoleVisible
{
get => _isLogConsoleVisible;
set
{
if (!SetProperty(ref _isLogConsoleVisible, value))
{
return;
}
RaisePropertyChanged(nameof(IsArtifactsVisible));
RaisePropertyChanged(nameof(InspectorTitle));
RaisePropertyChanged(nameof(InspectorSummary));
}
}
public bool IsArtifactsVisible => !IsLogConsoleVisible;
public string InspectorTitle => IsLogConsoleVisible ? DefaultInspectorLogsTitle : DefaultInspectorArtifactsTitle;
public string InspectorSummary => IsLogConsoleVisible ? DefaultInspectorLogsSummary : DefaultInspectorArtifactsSummary;
private static WorkbenchRepositoryNodeItem MapRepositoryNode(WorkbenchRepositoryNode node)
{
var kindGlyph = node.IsDirectory ? "▾" : "•";
var indentMargin = new Thickness(node.Depth * IndentSize, 0d, 0d, 0d);
var automationId = PresentationAutomationIds.RepositoryNode(node.RelativePath);
var tapAutomationId = PresentationAutomationIds.RepositoryNodeTap(node.RelativePath);
return new(
node.RelativePath,
node.Name,
node.DisplayLabel,
node.IsDirectory,
node.CanOpen,
kindGlyph,
indentMargin,
automationId,
tapAutomationId);
}
private void UpdateFilteredRepositoryNodes()
{
var searchTerms = RepositorySearchText.Split(' ', StringSplitOptions.TrimEntries | StringSplitOptions.RemoveEmptyEntries);
FilteredRepositoryNodes = searchTerms.Length is 0
? _allRepositoryNodes
: _allRepositoryNodes
.Where(node => searchTerms.All(term =>
node.DisplayLabel.Contains(term, StringComparison.OrdinalIgnoreCase) ||
node.Name.Contains(term, StringComparison.OrdinalIgnoreCase)))
.ToArray();
if (_selectedRepositoryNode is null ||
!FilteredRepositoryNodes.Contains(_selectedRepositoryNode))
{
SetSelectedRepositoryNode(FindFirstOpenableNode(FilteredRepositoryNodes));
}
}
private static WorkbenchRepositoryNodeItem? FindFirstOpenableNode(IReadOnlyList<WorkbenchRepositoryNodeItem> nodes)
{
for (var index = 0; index < nodes.Count; index++)
{
if (nodes[index].CanOpen)
{
return nodes[index];
}
}
return nodes.Count > 0 ? nodes[0] : null;
}
private static WorkbenchRepositoryNodeItem? FindNodeByRelativePath(
IReadOnlyList<WorkbenchRepositoryNodeItem> nodes,
string relativePath)
{
for (var index = 0; index < nodes.Count; index++)
{
if (nodes[index].RelativePath.Equals(relativePath, StringComparison.OrdinalIgnoreCase))
{
return nodes[index];
}
}
return null;
}
private void SetSelectedRepositoryNode(WorkbenchRepositoryNodeItem? value)
{
if (!SetProperty(ref _selectedRepositoryNode, value, nameof(SelectedRepositoryNode)))
{
return;
}
if (value?.CanOpen != true ||
!_documentsByPath.TryGetValue(value.RelativePath, out var selectedDocument))
{
return;
}
_selectedDocument = selectedDocument;
EditablePreviewContent = selectedDocument.PreviewContent;
RaisePropertyChanged(nameof(SelectedDocumentTitle));
RaisePropertyChanged(nameof(SelectedDocumentPath));
RaisePropertyChanged(nameof(SelectedDocumentStatus));
RaisePropertyChanged(nameof(SelectedDocumentLanguage));
RaisePropertyChanged(nameof(SelectedDocumentRenderer));
RaisePropertyChanged(nameof(SelectedDocumentIsReadOnly));
RaisePropertyChanged(nameof(SelectedDocumentDiffLines));
}
}