Welcome to mirror list, hosted at ThFree Co, Russian Federation.

App.axaml.cs « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: b946e10384b939fbcb7159b3bffa1408d9cc5134 (plain)
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
/*
 *                     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.Diagnostics;
using System.Globalization;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Markup.Xaml;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.ThemeManager;
using Emgu.CV;
using UVtools.Core.FileFormats;
using UVtools.WPF.Extensions;
using UVtools.WPF.Structures;

namespace UVtools.WPF
{
    public class App : Application
    {
        public static IThemeSelector? ThemeSelector { get; set; }
        public static MainWindow MainWindow;
        public static FileFormat SlicerFile = null;

        public static AppVersionChecker VersionChecker { get; } = new AppVersionChecker();

        public override void Initialize()
        {
            AvaloniaXamlLoader.Load(this);
        }

        public override async void OnFrameworkInitializationCompleted()
        {
            if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
            {
                CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;
                CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.InvariantCulture;

                UserSettings.Load();
                UserSettings.SetVersion();

                ThemeSelector = Avalonia.ThemeManager.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;
                    }
                }

                MainWindow = new MainWindow();

                try
                {
                    CvInvoke.CheckLibraryLoaded();
                }
                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 void NewInstance(string filePath)
        {
            try
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    var info = new ProcessStartInfo("UVtools.exe", $"\"{filePath}\"")
                    {
                        UseShellExecute = true
                    };
                    Process.Start(info).Dispose();
                }
                else if(File.Exists("UVtools"))
                {
                    Process.Start("UVtools", $"\"{filePath}\"").Dispose();
                }
                else
                {
                    Process.Start("dotnet", $"UVtools.dll \"{filePath}\"").Dispose();
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
        }

        public static void OpenBrowser(string url)
        {
            try
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
                {
                    Process.Start(new ProcessStartInfo(url) { UseShellExecute = true }).Dispose();
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    Process.Start("xdg-open", url).Dispose();
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    Process.Start("open", url).Dispose();
                }
                else
                {
                    // throw 
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
            
        }

        public static void StartProcess(string name)
        {
            try
            {
                using (Process.Start(new ProcessStartInfo(name)
                {
                    UseShellExecute = true
                })) { }
            }
            catch (Exception e)
            {
                Debug.WriteLine(e);
            }
            
        }

        public static Stream GetAsset(string url)
        {
            Uri uri;

            // Allow for assembly overrides
            if (url.StartsWith("avares://"))
            {
                uri = new Uri(url);
            }
            else
            {
                var assemblyName = Assembly.GetEntryAssembly().GetName().Name;
                uri = new Uri($"avares://{assemblyName}{url}");
            }

            var res = AvaloniaLocator.Current.GetService<IAssetLoader>().Open(uri);
            return res;
        }

        public static Bitmap GetBitmapFromAsset(string url) => new Bitmap(GetAsset(url));

        public static string ApplicationPath => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

        public static string GetPrusaSlicerDirectory()
        {
            if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                return $"{Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)}{Path.DirectorySeparatorChar}PrusaSlicer";
            }

            if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
            {
                return $"{Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)}{Path.DirectorySeparatorChar}.PrusaSlicer";
            }

            if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
            {
                return string.Format("{0}{1}Library{1}Application Support{1}PrusaSlicer",
                    Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), Path.DirectorySeparatorChar);
            }

            return null;
        }

        #endregion
    }
}