-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathMainViewModel.cs
More file actions
119 lines (104 loc) · 4.21 KB
/
MainViewModel.cs
File metadata and controls
119 lines (104 loc) · 4.21 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
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MainViewModel.cs" company="OxyPlot">
// Copyright (c) 2014 OxyPlot contributors
// </copyright>
// <summary>
// Represents the view-model for the main window.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ExampleBrowser
{
using ExampleLibrary;
using OxyPlot;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
/// <summary>
/// Represents the view-model for the main window.
/// </summary>
public class MainViewModel : INotifyPropertyChanged
{
private ExampleInfo example;
private Renderer selectedRenderer;
private PlotModel model;
/// <summary>
/// Initializes a new instance of the <see cref="MainViewModel" /> class.
/// </summary>
public MainViewModel()
{
this.model = new PlotModel() { Title = "Example Browser", Subtitle = "Select an example from the list" };
this.Categories = Examples.GetList()
.GroupBy(e => e.Category)
.Select(g => new Category(g.Key, g.ToList()))
.OrderBy(c => c.Key)
.ToList();
}
public IReadOnlyList<Renderer> Renderers { get; } = Enum.GetValues<Renderer>();
public Renderer SelectedRenderer
{
get => this.selectedRenderer;
set
{
if (this.selectedRenderer != value)
{
this.selectedRenderer = value;
this.CoerceSelectedRenderer();
this.OnPropertyChanged(nameof(this.SelectedRenderer));
}
}
}
public List<Category> Categories { get; }
/// <summary>
/// Gets the plot model.
/// </summary>
public ExampleInfo Example
{
get => this.example;
set
{
if (value == null)
{
// many listboxes are bound to this, so ignore null assignments and hope that doesn't break anything
return;
}
if (this.example != value)
{
this.example = value;
this.OnPropertyChanged(nameof(this.Example));
this.CoerceExample();
}
}
}
public PlotModel CanvasModel => this.SelectedRenderer == Renderer.Canvas ? this.model : null;
public PlotModel SkiaSharpModel => this.SelectedRenderer == Renderer.SkiaSharp ? this.model : null;
public PlotModel SkiaSharpDoubleBufferedModel => this.SelectedRenderer == Renderer.SkiaSharpDoubleBuffered ? this.model : null;
public PlotModel SkiaSharpPictureRecorderModel => this.SelectedRenderer == Renderer.SkiaSharpRecorder ? this.model : null;
public void ChangeExample(ExampleInfo example)
{
this.Example = example;
}
public event PropertyChangedEventHandler PropertyChanged;
private void CoerceSelectedRenderer()
{
((IPlotModel)this.model)?.AttachPlotView(null);
this.OnPropertyChanged(nameof(this.CanvasModel));
this.OnPropertyChanged(nameof(this.SkiaSharpModel));
this.OnPropertyChanged(nameof(this.SkiaSharpDoubleBufferedModel));
this.OnPropertyChanged(nameof(this.SkiaSharpPictureRecorderModel));
}
private void CoerceExample()
{
this.model = this.example?.PlotModel;
this.model?.InvalidatePlot(true);
this.OnPropertyChanged(nameof(this.CanvasModel));
this.OnPropertyChanged(nameof(this.SkiaSharpModel));
this.OnPropertyChanged(nameof(this.SkiaSharpDoubleBufferedModel));
this.OnPropertyChanged(nameof(this.SkiaSharpPictureRecorderModel));
}
private void OnPropertyChanged(string propertyName)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}