forked from miroiu/nodify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApplicationViewModel.cs
76 lines (70 loc) · 2.54 KB
/
ApplicationViewModel.cs
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
using System;
using System.Linq;
using System.Windows.Input;
namespace Nodify.Calculator
{
public class ApplicationViewModel : ObservableObject
{
public NodifyObservableCollection<EditorViewModel> Editors { get; } = new NodifyObservableCollection<EditorViewModel>();
public ApplicationViewModel()
{
AddEditorCommand = new DelegateCommand(() => Editors.Add(new EditorViewModel
{
Name = $"Editor {Editors.Count + 1}"
}));
CloseEditorCommand = new DelegateCommand<Guid>(
id => Editors.RemoveOne(editor => editor.Id == id),
_ => Editors.Count > 0 && SelectedEditor != null);
Editors.WhenAdded((editor) =>
{
if (AutoSelectNewEditor || Editors.Count == 1)
{
SelectedEditor = editor;
}
editor.OnOpenInnerCalculator += OnOpenInnerCalculator;
})
.WhenRemoved((editor) =>
{
editor.OnOpenInnerCalculator -= OnOpenInnerCalculator;
var childEditors = Editors.Where(ed => ed.Parent == editor).ToList();
childEditors.ForEach(ed => Editors.Remove(ed));
});
Editors.Add(new EditorViewModel
{
Name = $"Editor {Editors.Count + 1}"
});
}
private void OnOpenInnerCalculator(EditorViewModel parentEditor, CalculatorViewModel calculator)
{
var editor = Editors.FirstOrDefault(e => e.Calculator == calculator);
if (editor != null)
{
SelectedEditor = editor;
}
else
{
var childEditor = new EditorViewModel
{
Parent = parentEditor,
Calculator = calculator,
Name = $"[Inner] Editor {Editors.Count + 1}"
};
Editors.Add(childEditor);
}
}
public ICommand AddEditorCommand { get; }
public ICommand CloseEditorCommand { get; }
private EditorViewModel? _selectedEditor;
public EditorViewModel? SelectedEditor
{
get => _selectedEditor;
set => SetProperty(ref _selectedEditor, value);
}
private bool _autoSelectNewEditor = true;
public bool AutoSelectNewEditor
{
get => _autoSelectNewEditor;
set => SetProperty(ref _autoSelectNewEditor , value);
}
}
}