forked from MaterialDesignInXAML/MaterialDesignInXamlToolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIconPackViewModel.cs
More file actions
188 lines (163 loc) · 6.26 KB
/
IconPackViewModel.cs
File metadata and controls
188 lines (163 loc) · 6.26 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using MaterialDesignThemes.Wpf;
using MaterialDesignDemo.Domain;
using Microsoft.Win32;
using BluwolfIcons;
using System.Windows.Media.Imaging;
using System.IO;
namespace MaterialDesignDemo.Domain
{
public class IconPackViewModel : ViewModelBase
{
private readonly Lazy<IEnumerable<PackIconKindGroup>> _packIconKinds;
private readonly ISnackbarMessageQueue _snackbarMessageQueue;
public IconPackViewModel(ISnackbarMessageQueue snackbarMessageQueue)
{
_snackbarMessageQueue = snackbarMessageQueue ?? throw new ArgumentNullException(nameof(snackbarMessageQueue));
OpenDotComCommand = new AnotherCommandImplementation(OpenDotCom);
SearchCommand = new AnotherCommandImplementation(Search);
CopyToClipboardCommand = new AnotherCommandImplementation(CopyToClipboard);
_packIconKinds = new Lazy<IEnumerable<PackIconKindGroup>>(() =>
Enum.GetNames(typeof(PackIconKind))
.GroupBy(k => (PackIconKind)Enum.Parse(typeof(PackIconKind), k))
.Select(g => new PackIconKindGroup(g))
.OrderBy(x => x.Kind)
.ToList());
var helper = new PaletteHelper();
if (helper.GetThemeManager() is { } themeManager)
{
themeManager.ThemeChanged += ThemeManager_ThemeChanged;
}
SetDefaultIconColors();
}
private void ThemeManager_ThemeChanged(object? sender, ThemeChangedEventArgs e)
=> SetDefaultIconColors();
public ICommand OpenDotComCommand { get; }
public ICommand SearchCommand { get; }
public ICommand CopyToClipboardCommand { get; }
private IEnumerable<PackIconKindGroup>? _kinds;
private PackIconKindGroup? _group;
private string? _kind;
private PackIconKind _packIconKind;
public IEnumerable<PackIconKindGroup> Kinds
{
get => _kinds ??= _packIconKinds.Value;
set => SetProperty(ref _kinds, value);
}
public PackIconKindGroup? Group
{
get => _group;
set
{
if (SetProperty(ref _group, value))
{
Kind = value?.Kind;
}
}
}
public string? Kind
{
get => _kind;
set
{
if (SetProperty(ref _kind, value))
{
PackIconKind = value != null ? (PackIconKind)Enum.Parse(typeof(PackIconKind), value) : default;
}
}
}
public PackIconKind PackIconKind
{
get => _packIconKind;
set => SetProperty(ref _packIconKind, value);
}
private void OpenDotCom(object obj)
=> Link.OpenInBrowser("https://materialdesignicons.com/");
private async void Search(object obj)
{
var text = obj as string;
if (string.IsNullOrWhiteSpace(text))
{
Kinds = _packIconKinds.Value;
}
else
{
Kinds = await Task.Run(() => _packIconKinds.Value
.Where(x => x.Aliases.Any(a => a.IndexOf(text, StringComparison.CurrentCultureIgnoreCase) >= 0))
.ToList());
}
}
private void CopyToClipboard(object obj)
{
var toBeCopied = $"<materialDesign:PackIcon Kind=\"{obj}\" />";
Clipboard.SetDataObject(toBeCopied);
_snackbarMessageQueue.Enqueue(toBeCopied + " copied to clipboard");
}
private void SetDefaultIconColors()
{
var helper = new PaletteHelper();
ITheme theme = helper.GetTheme();
GeneratedIconBackground = theme.Paper;
GeneratedIconForeground = theme.PrimaryMid.Color;
}
private Color _generatedIconBackground;
public Color GeneratedIconBackground
{
get => _generatedIconBackground;
set => SetProperty(ref _generatedIconBackground, value);
}
private Color _generatedIconForeground;
public Color GeneratedIconForeground
{
get => _generatedIconForeground;
set => SetProperty(ref _generatedIconForeground, value);
}
private ICommand? _saveIconCommand;
public ICommand SaveIconCommand => _saveIconCommand ??= new AnotherCommandImplementation(OnSaveIcon);
private void OnSaveIcon(object _)
{
var saveDialog = new SaveFileDialog
{
DefaultExt = ".ico",
Title = "Save Icon (.ico)",
Filter = "Icon Files|*.ico|All Files|*",
CheckPathExists = true,
OverwritePrompt = true,
RestoreDirectory = true
};
if (saveDialog.ShowDialog() != true) return;
var icon = new Icon();
//TODO: Make this size list configurable
foreach (var size in new[] { 256, 128, 64, 48, 32, 24, 16 })
{
RenderTargetBitmap bmp = RenderImage(size);
icon.Images.Add(new BmpIconImage(bmp));
}
icon.Save(saveDialog.FileName);
RenderTargetBitmap RenderImage(int size)
{
var packIcon = new PackIcon
{
Kind = PackIconKind,
Background = new SolidColorBrush(GeneratedIconBackground),
Foreground = new SolidColorBrush(GeneratedIconForeground),
Width = size,
Height = size,
Style = (Style)Application.Current.FindResource(typeof(PackIcon))
};
packIcon.Measure(new Size(size, size));
packIcon.Arrange(new Rect(0, 0, size, size));
packIcon.UpdateLayout();
RenderTargetBitmap bmp = new(size, size, 96, 96, PixelFormats.Pbgra32);
bmp.Render(packIcon);
return bmp;
}
}
}
}