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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
|
/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Markup.Xaml.Styling;
using Avalonia.Platform;
using Avalonia.Styling;
using Avalonia.Themes.Fluent;
using Emgu.CV;
using UVtools.Core;
using UVtools.Core.FileFormats;
using UVtools.Core.Managers;
using UVtools.Core.SystemOS;
using UVtools.WPF.Extensions;
using UVtools.WPF.Structures;
using Bitmap = Avalonia.Media.Imaging.Bitmap;
namespace UVtools.WPF;
#nullable enable
public class App : Application
{
public enum ApplicationTheme
{
[Description("Fluent light")]
FluentLight,
[Description("Fluent dark")]
FluentDark,
[Description("Default light")]
DefaultLight,
[Description("Default dark")]
DefaultDark
}
//public static ThemeSelector ThemeSelector { get; set; }
public static MainWindow MainWindow = null!;
public static FileFormat? SlicerFile = null;
public static AppVersionChecker VersionChecker { get; } = new();
public static StyleInclude DataGridFluent => new(CreateAssemblyUri("/Styles"))
{
Source = new Uri("avares://Avalonia.Controls.DataGrid/Themes/Fluent.xaml")
};
public static StyleInclude DataGridDefault => new(CreateAssemblyUri("/Styles"))
{
Source = new Uri("avares://Avalonia.Controls.DataGrid/Themes/Default.xaml")
};
public static readonly StyleInclude AppStyleLight = new(CreateAssemblyUri("/Assets/Styles"))
{
Source = CreateAssemblyUri("/Assets/Styles/StylesLight.xaml")
};
public static readonly StyleInclude AppStyleDark = new(CreateAssemblyUri("/Assets/Styles"))
{
Source = CreateAssemblyUri("/Assets/Styles/StylesDark.xaml")
};
public static FluentTheme Fluent = new(CreateAssemblyUri("/Styles"));
public static Styles DefaultLight = new()
{
new StyleInclude(new Uri($"resm:Styles?assembly={AssemblyName}"))
{
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/AccentColors.xaml")
},
new StyleInclude(new Uri($"resm:Styles?assembly={AssemblyName}"))
{
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/Base.xaml")
},
new StyleInclude(new Uri($"resm:Styles?assembly={AssemblyName}"))
{
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseLight.xaml")
},
new StyleInclude(new Uri($"resm:Styles?assembly={AssemblyName}"))
{
Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseLight.xaml")
},
new StyleInclude(new Uri($"resm:Styles?assembly={AssemblyName}"))
{
Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml")
}
};
public static Styles DefaultDark = new()
{
new StyleInclude(new Uri($"resm:Styles?assembly={AssemblyName}"))
{
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/AccentColors.xaml")
},
new StyleInclude(new Uri($"resm:Styles?assembly={AssemblyName}"))
{
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/Base.xaml")
},
new StyleInclude(new Uri($"resm:Styles?assembly={AssemblyName}"))
{
Source = new Uri("avares://Avalonia.Themes.Fluent/Accents/BaseDark.xaml")
},
new StyleInclude(new Uri($"resm:Styles?assembly={AssemblyName}"))
{
Source = new Uri("avares://Avalonia.Themes.Default/Accents/BaseDark.xaml")
},
new StyleInclude(new Uri($"resm:Styles?assembly={AssemblyName}"))
{
Source = new Uri("avares://Avalonia.Themes.Default/DefaultTheme.xaml")
}
};
public static void ApplyTheme()
{
switch (UserSettings.Instance.General.Theme)
{
case ApplicationTheme.FluentLight:
{
if (Fluent.Mode != FluentThemeMode.Light)
{
Fluent.Mode = FluentThemeMode.Light;
}
Current!.Styles[0] = Fluent;
Current.Styles[1] = DataGridFluent;
Current.Styles[2] = AppStyleLight;
break;
}
case ApplicationTheme.FluentDark:
{
if (Fluent.Mode != FluentThemeMode.Dark)
{
Fluent.Mode = FluentThemeMode.Dark;
}
Current!.Styles[0] = Fluent;
Current.Styles[1] = DataGridFluent;
Current.Styles[2] = AppStyleDark;
break;
}
case ApplicationTheme.DefaultLight:
Current!.Styles[0] = DefaultLight;
Current.Styles[1] = DataGridDefault;
Current.Styles[2] = AppStyleLight;
break;
case ApplicationTheme.DefaultDark:
Current!.Styles[0] = DefaultDark;
Current.Styles[1] = DataGridDefault;
Current.Styles[2] = AppStyleDark;
break;
}
}
public override void Initialize()
{
Styles.Insert(0, Fluent);
Styles.Insert(1, DataGridFluent);
Styles.Insert(2, AppStyleLight);
AvaloniaXamlLoader.Load(this);
}
public override async void OnFrameworkInitializationCompleted()
{
if (Design.IsDesignMode)
{
SlicerFile = new ChituboxFile
{
LayerHeight = 0.05f,
Resolution = new (1440, 2560),
Display = new (68.04f, 120.96f),
DisplayMirror = FlipDirection.Horizontally,
MachineZ = 155,
BottomLayerCount = 3,
MachineName = "Epax X1"
};
}
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
UserSettings.Load();
UserSettings.SetVersion();
MaterialManager.Load();
OperationProfiles.Load();
SuggestionManager.Load();
/*ThemeSelector = ThemeSelector.Create(Path.Combine(ApplicationPath, "Assets", "Themes"));
ThemeSelector.LoadSelectedTheme(Path.Combine(UserSettings.SettingsFolder, "selected.theme"));
if (ThemeSelector.SelectedTheme.Name == "UVtoolsDark" || ThemeSelector.SelectedTheme.Name == "Light")
{
foreach (var theme in ThemeSelector.Themes)
{
if (theme.Name != "UVtoolsLight") continue;
theme.ApplyTheme();
break;
}
}*/
if (!CvInvoke.Init())
{
Console.WriteLine("UVtools can not init OpenCV library\n" +
"Please build or install this dependencies in order to run UVtools\n" +
"Check manual or page at 'Requirements' section for help");
}
if (UserSettings.Instance.General.Theme != ApplicationTheme.FluentLight)
{
ApplyTheme();
}
MainWindow = new MainWindow();
try
{
if(!CvInvoke.Init())
await MainWindow.MessageBoxError("UVtools can not init OpenCV library\n" +
"Please build or install this dependencies in order to run UVtools\n" +
"Check manual or page at 'Requirements' section for help",
"UVtools can not run");
}
catch (Exception e)
{
await MainWindow.MessageBoxError("UVtools can not run due lack of dependencies from cvextern/OpenCV\n" +
"Please build or install this dependencies in order to run UVtools\n" +
"Check manual or page at 'Requirements' section for help\n\n" +
"Additional information:\n" +
$"{e}", "UVtools can not run");
return;
}
desktop.MainWindow = MainWindow;
//desktop.Exit += (sender, e) => ThemeSelector.SaveSelectedTheme(Path.Combine(UserSettings.SettingsFolder, "selected.theme"));
}
base.OnFrameworkInitializationCompleted();
}
#region Utilities
public static string ApplicationPath => AppContext.BaseDirectory;
public static readonly string AppExecutable = Environment.ProcessPath!;
public static readonly string AppExecutableQuoted = $"\"{AppExecutable}\"";
public static void NewInstance(string filePath)
{
try
{
if (File.Exists(AppExecutable)) // Direct execute
{
SystemAware.StartProcess(AppExecutable, $"\"{filePath}\"");
}
else
{
SystemAware.StartProcess("dotnet", $"UVtools.dll \"{filePath}\"");
}
}
catch (Exception e)
{
Debug.WriteLine(e);
}
}
public static Uri CreateAssemblyUri(string url)
{
Uri uri;
// Allow for assembly overrides
if (url.StartsWith("avares://"))
{
uri = new Uri(url);
}
else
{
uri = new Uri($"avares://{AssemblyName}{url}");
}
return uri;
}
public static Stream GetAsset(string url)
{
return AvaloniaLocator.Current.GetService<IAssetLoader>()?.Open(CreateAssemblyUri(url))!;
}
public static Bitmap GetBitmapFromAsset(string url) => new(GetAsset(url));
public static string? GetPrusaSlicerDirectory(bool isSuperSlicer = false)
{
var slicerFolder = isSuperSlicer ? "SuperSlicer" : "PrusaSlicer";
if (OperatingSystem.IsWindows())
{
return Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
slicerFolder);
}
if (OperatingSystem.IsLinux())
{
var folder1 = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
".config",
slicerFolder);
if (Directory.Exists(folder1)) return folder1;
return Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
$".{slicerFolder}");
}
if (OperatingSystem.IsMacOS())
{
return Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile),
"Library",
"Application Support",
slicerFolder);
}
return null;
}
#endregion
#region Assembly properties
public static Assembly WpfAssembly => Assembly.GetExecutingAssembly();
public static string AssemblyVersion => WpfAssembly.GetName().Version?.ToString()!;
public static string AssemblyName => Assembly.GetExecutingAssembly().GetName().Name!;
public static string AssemblyTitle
{
get
{
var attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyTitleAttribute), false);
if (attributes.Length > 0)
{
var titleAttribute = (AssemblyTitleAttribute)attributes[0];
if (titleAttribute.Title != string.Empty)
{
return titleAttribute.Title;
}
}
return Path.GetFileNameWithoutExtension(WpfAssembly.Location);
}
}
public static string AssemblyDescription
{
get
{
var attributes = WpfAssembly.GetCustomAttributes(typeof(AssemblyDescriptionAttribute), false);
if (attributes.Length == 0)
{
return string.Empty;
}
var description = ((AssemblyDescriptionAttribute)attributes[0]).Description + $"{Environment.NewLine}{Environment.NewLine}Available File Formats:";
return FileFormat.AvailableFormats.SelectMany(fileFormat => fileFormat.FileExtensions).Aggregate(description, (current, fileExtension) => current + $"{Environment.NewLine}- {fileExtension.Description} (.{fileExtension.Extension})");
}
}
public static string AssemblyProduct
{
get
{
var attributes = WpfAssembly.GetCustomAttributes(typeof(AssemblyProductAttribute), false);
return attributes.Length == 0 ? string.Empty : ((AssemblyProductAttribute)attributes[0]).Product;
}
}
public static string AssemblyCopyright
{
get
{
var attributes = WpfAssembly.GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
return attributes.Length == 0 ? string.Empty : ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
}
}
public static string AssemblyCompany
{
get
{
var attributes = WpfAssembly.GetCustomAttributes(typeof(AssemblyCompanyAttribute), false);
return attributes.Length == 0 ? string.Empty : ((AssemblyCompanyAttribute)attributes[0]).Company;
}
}
#endregion
}
|