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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using Sysprogs.Core.Tools;
using Sysprogs.Core.Trees;
using Sysprogs.Core.Trees.Presentable;
using Sysprogs.GUI.Portable.Controls.Basic;
using Sysprogs.GUI.Portable.Dialogs;
using Sysprogs.GUI.Portable.Services;
using Sysprogs.PropertyEngine;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
#if AVALONIA
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Interactivity;
#else
using System.Windows;
using System.Windows.Controls;
#endif
namespace VisualGDB.WPF.CMake
{
public partial class CMakeSourceSynchronizationDialog : UserControl
{
readonly PortableGUIService _Service;
CMakeSourceSynchronizationDialog(PortableGUIService service, ModelImpl model)
{
Model = model;
_Service = service;
InitializeComponent();
}
public ModelImpl Model { get; }
public class ModelImpl : NotifyPropertyChangedImpl, IModalViewModelWithResult<bool>
{
private AdvancedBuildSystemProperties.IListProperty _SourceList;
private string _BaseDirectory;
private string[] _Extensions = new string[] { ".c", ".cpp", ".cc" };
public PresentableTreeWithFilters<NodeBase> Tree { get; } = new PresentableTreeWithFilters<NodeBase>();
public enum NodeIconType { File, Folder }
public class NodeBase : PresentableTreeNodeWithPublicChildren<NodeBase>
{
protected Dictionary<string, NodeBase> _ChildrenByName = new Dictionary<string, NodeBase>(StringComparer.OrdinalIgnoreCase);
public NodeBase Parent { get; private set; }
public NodeIconType Icon { get; }
public NodeBase(string name, NodeIconType icon, NodeBase parent = null)
: base(name)
{
Icon = icon;
Parent = parent;
}
private bool? _IsChecked;
public bool? IsChecked
{
get => _IsChecked;
set => UpdateIsChecked(value, true);
}
private void UpdateIsChecked(bool? value, bool byUser)
{
if (_IsChecked == value)
return;
_IsChecked = value;
OnPropertyChanged(nameof(IsChecked));
foreach (var child in _ChildrenByName.Values)
child.UpdateIsChecked(value, false);
Parent?.RecomputeCheckState();
}
void RecomputeCheckState()
{
if (_ChildrenByName.Count == 0)
return;
bool? accumulatedState = GetCheckStateFromChildren();
if (_IsChecked != accumulatedState)
{
_IsChecked = accumulatedState;
OnPropertyChanged(nameof(IsChecked));
Parent?.RecomputeCheckState();
}
}
private bool? GetCheckStateFromChildren()
{
bool isFirst = true;
bool? accumulatedState = null;
foreach (var child in _ChildrenByName.Values)
{
if (isFirst)
{
accumulatedState = child.IsChecked;
isFirst = false;
}
else if (accumulatedState != child.IsChecked)
{
accumulatedState = null;
break;
}
}
return accumulatedState;
}
protected virtual bool? ComputeInitialCheckState() => GetCheckStateFromChildren();
public void ApplyInitialCheckState()
{
foreach (var child in _ChildrenByName.Values)
child.ApplyInitialCheckState();
var state = ComputeInitialCheckState();
if (state != _IsChecked)
{
_IsChecked = state;
OnPropertyChanged(nameof(IsChecked));
}
}
protected override void LoadInitialChildren(IPresentableTreeNodeChildren<NodeBase> targetCollection)
{
base.LoadInitialChildren(targetCollection);
foreach (var child in _ChildrenByName.Values.OrderBy(c => c.Name, StringComparer.OrdinalIgnoreCase))
targetCollection.Add(child);
}
public override bool HasChildren => _ChildrenByName.Count > 0;
public FileNode ProvideFileNode(string[] components, int level)
{
if (level >= components.Length)
return null;
string name = components[level];
if (level == components.Length - 1)
{
if (!_ChildrenByName.TryGetValue(name, out NodeBase node))
{
var fileNode = new FileNode(name, this);
_ChildrenByName[name] = fileNode;
return fileNode;
}
if (node is FileNode fileNode2)
return fileNode2;
return null;
}
else
{
if (!_ChildrenByName.TryGetValue(name, out NodeBase node))
{
var folderNode = new FolderNode(name, this);
_ChildrenByName[name] = folderNode;
return folderNode.ProvideFileNode(components, level + 1);
}
return node.ProvideFileNode(components, level + 1);
}
}
public virtual string WarningText { get; }
}
public class FolderNode : NodeBase
{
public FolderNode(string name, NodeBase parent = null) : base(name, NodeIconType.Folder, parent)
{
}
protected override bool? ComputeInitialCheckState() => base.ComputeInitialCheckState();
}
public class FileNode : NodeBase
{
private AdvancedBuildSystemProperties.IListPropertyToken _Token;
private string _PhysicalPath;
public FileNode(string name, NodeBase parent = null) : base(name, NodeIconType.File, parent)
{
}
public void AttachCMakeToken(AdvancedBuildSystemProperties.IListPropertyToken token, string baseDirectory)
{
_Token = token;
try
{
var fn = Path.Combine(baseDirectory, token.Value);
if (File.Exists(fn))
AttachPhysicalFile(fn);
}
catch { }
}
public void AttachPhysicalFile(string fullPath)
{
_PhysicalPath = fullPath;
}
protected override bool? ComputeInitialCheckState()
{
if (_Token != null)
return true;
else
return false;
}
public void CommitChanges(List<string> newFiles)
{
if (IsChecked == false && _Token != null)
_Token.Value = null;
else if (IsChecked == true && _Token == null && _PhysicalPath != null)
newFiles.Add(_PhysicalPath);
}
public override string WarningText
{
get
{
if (_Token != null && string.IsNullOrEmpty(_PhysicalPath))
return $"Missing {_Token.Value}";
return base.WarningText;
}
}
}
public enum NodeFilterMode
{
ShowAll,
CheckedOnly,
UncheckedOnly,
MissingOnly,
}
public class NodeTypeFilter : NotifyPropertyChangedImpl, IPresentableTreeFilter<NodeBase>
{
public bool ShowAllNodes => Mode == NodeFilterMode.ShowAll;
NodeFilterMode _Mode;
public NodeFilterMode Mode
{
get => _Mode;
set
{
_Mode = value;
OnPropertyChanged(nameof(Mode));
FilterChanged?.Invoke(this, EventArgs.Empty);
}
}
public event EventHandler FilterChanged;
public AdvancedNodeFilteringResult ApplyFilterToNode(NodeBase node)
{
}
}
public NodeTypeFilter Filter { get; }
public ModelImpl(AdvancedBuildSystemProperties.IListProperty sourceList, string baseDirectory)
{
_SourceList = sourceList;
_BaseDirectory = baseDirectory;
var topLevelFolder = new FolderNode(baseDirectory);
foreach (var token in sourceList.AllTokens)
{
string path = token.Value;
string[] components = path.Replace('\\', '/').Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var fileNode = topLevelFolder.ProvideFileNode(components, 0);
fileNode?.AttachCMakeToken(token, baseDirectory);
}
ProcessPhysicalFiles(baseDirectory, topLevelFolder);
topLevelFolder.ApplyInitialCheckState();
Tree.Nodes.Add(topLevelFolder);
Tree.AddFilter(Filter = new NodeTypeFilter());
}
private void ProcessPhysicalFiles(string baseDirectory, FolderNode topLevelFolder)
{
string[] files = Directory.GetFiles(baseDirectory, "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
string extension = Path.GetExtension(file);
if (_Extensions.Contains(extension, StringComparer.OrdinalIgnoreCase))
{
string relativePath = PortablePath.GetRelativePath(baseDirectory, file);
if (!string.IsNullOrEmpty(relativePath))
{
string[] components = relativePath.Replace('\\', '/').Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
var fileNode = topLevelFolder.ProvideFileNode(components, 0);
fileNode?.AttachPhysicalFile(file);
}
}
}
}
public bool CompleteOrThrow(ModalDialogButtonClickedEventArgs args)
{
if (args.CommonButton == CommonBarButton.OK)
{
List<string> newFiles = new List<string>();
foreach (var node in Tree.Nodes.GetAllNodesRecursively().OfType<FileNode>())
node.CommitChanges(newFiles);
foreach (var file in newFiles)
_SourceList.AddToken(null, StatementPlacementDirection.After, PortablePath.GetRelativePath(_BaseDirectory, file).Replace('\\', '/'));
}
return true;
}
public UserControl ConfigureWindowAndCreateControl(PortableGUIService service, ModalContentWindow window, BottomButtonBar bar)
{
return new CMakeSourceSynchronizationDialog(service, this);
}
}
}
}