-
Notifications
You must be signed in to change notification settings - Fork 189
Expand file tree
/
Copy pathLibraryConfig.cs
More file actions
348 lines (283 loc) · 10.8 KB
/
LibraryConfig.cs
File metadata and controls
348 lines (283 loc) · 10.8 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
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
339
340
341
342
343
344
345
346
347
348
/*
Copyright (c) 2018, John Lewin
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
The views and conclusions contained in the software and documentation are those
of the authors and should not be interpreted as representing official policies,
either expressed or implied, of the FreeBSD Project.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using MatterHackers.Agg;
using MatterHackers.Agg.Image;
using MatterHackers.Agg.Platform;
using MatterHackers.Agg.UI;
using MatterHackers.MatterControl.CustomWidgets;
using MatterHackers.MatterControl.PrintQueue;
namespace MatterHackers.MatterControl.Library
{
public interface ILibraryContext
{
event EventHandler<ContainerChangedEventArgs> ContainerChanged;
event EventHandler<ContainerChangedEventArgs> ContentChanged;
ILibraryContainer ActiveContainer { get; set; }
}
public class ContainerChangedEventArgs : EventArgs
{
public ContainerChangedEventArgs(ILibraryContainer activeContainer, ILibraryContainer previousContainer)
{
this.ActiveContainer = activeContainer;
this.PreviousContainer = previousContainer;
}
public ILibraryContainer ActiveContainer { get; }
public ILibraryContainer PreviousContainer { get; }
}
public class LibraryConfig : ILibraryContext
{
public Dictionary<string, IContentProvider> ContentProviders = new Dictionary<string, IContentProvider>();
private static ImageBuffer defaultFolderIcon = StaticData.Instance.LoadIcon(Path.Combine("Library", "folder.png")).SetPreMultiply();
public static ImageBuffer DefaultItemIcon { get; } = StaticData.Instance.LoadIcon(Path.Combine("Library", "file.png"));
private ILibraryContainer activeContainer;
private SafeList<ILibraryContainerLink> libraryProviders;
public LibraryConfig()
{
libraryProviders = new SafeList<ILibraryContainerLink>();
this.RootLibaryContainer = new RootLibraryContainer(libraryProviders);
this.ActiveContainer = this.RootLibaryContainer;
}
public event EventHandler<ContainerChangedEventArgs> ContainerChanged;
public event EventHandler<ContainerChangedEventArgs> ContentChanged;
public ILibraryContainer ActiveContainer
{
get => activeContainer;
set
{
if (activeContainer == value)
{
return;
}
var newContainer = value;
var eventArgs = new ContainerChangedEventArgs(newContainer, activeContainer);
if (activeContainer != null)
{
activeContainer.Deactivate();
activeContainer.ContentChanged -= ActiveContainer_ContentChanged;
if (activeContainer.CustomSearch is ICustomSearch customSearch)
{
customSearch.ClearFilter();
}
// If the new container is an ancestor of the active container we need to Dispose everyone up to that point
if (activeContainer.Ancestors().Where(p => p == newContainer).Any())
{
var context = activeContainer;
while (context != newContainer)
{
context.Dispose();
context = context.Parent;
}
}
}
activeContainer = newContainer;
activeContainer.Activate();
activeContainer.ContentChanged += ActiveContainer_ContentChanged;
ContainerChanged?.Invoke(this, eventArgs);
}
}
public BundledPartsCollectionContainer BundledPartsCollectionContainer { get; internal set; }
public SafeList<LibraryAction> MenuExtensions { get; } = new SafeList<LibraryAction>();
public PlatingHistoryContainer PlatingHistory { get; internal set; }
public ILibraryContainer RootLibaryContainer { get; }
public ImageBuffer EnsureCorrectThumbnailSizing(ImageBuffer originalThumbnail, int thumbWidth, int thumbHeight, Action<ImageBuffer> resizedImage)
{
var processingImage = originalThumbnail;
// Resize canvas to target as fallback
if (processingImage.Width < thumbWidth || processingImage.Height < thumbHeight)
{
processingImage = LibraryListView.ResizeCanvas(processingImage, thumbWidth, thumbHeight);
}
else if (processingImage.Width > thumbWidth || processingImage.Height > thumbHeight)
{
processingImage = LibraryProviderHelpers.ResizeImage(processingImage, thumbWidth, thumbHeight);
}
if (GuiWidget.DeviceScale != 1
&& processingImage.Width != thumbWidth * GuiWidget.DeviceScale)
{
processingImage = processingImage.CreateScaledImage(GuiWidget.DeviceScale);
}
originalThumbnail.ImageChanged += (s, e) =>
{
// this happens when we get an updated image from a web request
UiThread.RunOnIdle(() =>
{
resizedImage?.Invoke(originalThumbnail.CreateScaledImage(GuiWidget.DeviceScale));
});
};
return processingImage;
}
public IContentProvider GetContentProvider(ILibraryItem item)
{
string contentType = (item as ILibraryAssetStream)?.ContentType ?? (item as ILibraryObject3D)?.ContentType;
if (contentType == null)
{
return null;
}
return GetContentProvider(contentType);
}
public IContentProvider GetContentProvider(string contentType)
{
IContentProvider provider;
ContentProviders.TryGetValue(contentType, out provider);
return provider;
}
public bool IsContentFileType(string fileName)
{
string fileExtensionLower = Path.GetExtension(fileName).ToLower().Trim('.');
return !string.IsNullOrEmpty(fileExtensionLower)
&& (ApplicationSettings.LibraryFilterFileExtensions.Contains(fileExtensionLower)
|| ApplicationController.Instance.Library.ContentProviders.Keys.Contains(fileExtensionLower));
}
public bool IsMeshFileType(string fileName)
{
string fileExtensionLower = Path.GetExtension(fileName).ToLower().Trim('.');
return !string.IsNullOrEmpty(fileExtensionLower)
&& ApplicationSettings.ValidFileExtensions.Contains(fileExtensionLower);
}
public async Task LoadItemThumbnail(Action<ImageBuffer> thumbnailListenerIn, Action<MeshContentProvider> buildThumbnail, ILibraryItem libraryItem, ILibraryContainer libraryContainer, int thumbWidth, int thumbHeight, ThemeConfig theme)
{
async void setItemThumbnail(ImageBuffer icon)
{
var thumbnailListener = thumbnailListenerIn;
if (icon != null)
{
if (icon.Width == 0)
{
return;
}
icon = await Task.Run(() => this.EnsureCorrectThumbnailSizing(icon, thumbWidth, thumbHeight, (image) =>
{
setItemThumbnail(image);
}));
// Invoke callback on UI thread to ensure Invalidate() works correctly
UiThread.RunOnIdle(() =>
{
thumbnailListener?.Invoke(icon);
});
}
}
// Load from cache via LibraryID
var thumbnail = await Task.Run(() => ApplicationController.Instance.Thumbnails.LoadCachedImage(libraryItem, thumbWidth, thumbHeight));
if (thumbnail != null)
{
setItemThumbnail(thumbnail);
return;
}
if (thumbnail == null && libraryContainer != null)
{
try
{
// Ask the container - allows the container to provide its own interpretation of the item thumbnail
thumbnail = await libraryContainer.GetThumbnail(libraryItem, thumbWidth, thumbHeight);
}
catch
{
}
}
if (thumbnail == null && libraryItem is IThumbnail)
{
// If the item provides its own thumbnail, try to collect it
thumbnail = await (libraryItem as IThumbnail).GetThumbnail(thumbWidth, thumbHeight);
}
if (thumbnail == null)
{
// Ask content provider - allows type specific thumbnail creation
var contentProvider = ApplicationController.Instance.Library.GetContentProvider(libraryItem);
if (contentProvider != null)
{
// Before we have a thumbnail set to the content specific thumbnail
thumbnail = contentProvider.DefaultImage;
if (contentProvider is MeshContentProvider meshContentProvider)
{
// Show default image immediately while thumbnail is being generated
setItemThumbnail(thumbnail);
buildThumbnail?.Invoke(meshContentProvider);
}
else
{
// Show processing image
setItemThumbnail(theme.GeneratingThumbnailIcon);
// Ask the provider for a content specific thumbnail
thumbnail = await contentProvider.GetThumbnail(libraryItem, thumbWidth, thumbHeight);
}
}
}
if (thumbnail == null)
{
// Use the listview defaults
thumbnail = ((libraryItem is ILibraryContainerLink) ? defaultFolderIcon : DefaultItemIcon).AlphaToPrimaryAccent();
}
// TODO: Resolve and implement
// Allow the container to draw an overlay - use signal interface or add method to interface?
// var iconWithOverlay = ActiveContainer.DrawOverlay()
setItemThumbnail(thumbnail);
}
public void RegisterContainer(ILibraryContainerLink containerItem)
{
libraryProviders.Add(containerItem);
libraryProviders.Modify((list) =>
{
list.Sort(SortOnName);
});
}
public void RegisterCreator(ILibraryObject3D libraryItem)
{
this.RootLibaryContainer.Items.Add(libraryItem);
}
public void RegisterCreator(ILibraryAssetStream libraryItem)
{
this.RootLibaryContainer.Items.Add(libraryItem);
}
/// <summary>
/// Notifies listeners that the ActiveContainer Changed
/// </summary>
internal void NotifyContainerChanged()
{
this.OnContainerChanged(this.ActiveContainer);
}
private void ActiveContainer_ContentChanged(object sender, EventArgs args)
{
this.OnContainerChanged(this.ActiveContainer);
}
private void OnContainerChanged(ILibraryContainer container)
{
ContentChanged?.Invoke(this, new ContainerChangedEventArgs(container, null));
}
private int SortOnName(ILibraryContainerLink x, ILibraryContainerLink y)
{
if (x != null && x.Name != null
&& y != null && y.Name != null)
{
return string.Compare(x.Name, y.Name, StringComparison.OrdinalIgnoreCase);
}
return 0;
}
}
}