App.axaml.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. using System.Runtime;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.ApplicationLifetimes;
  5. using Avalonia.Markup.Xaml;
  6. using PicView.Avalonia.Interfaces;
  7. using PicView.Avalonia.Navigation;
  8. using PicView.Avalonia.UI;
  9. using PicView.Avalonia.ViewModels;
  10. using PicView.Avalonia.Win32.Views;
  11. using PicView.Core.Config;
  12. using PicView.Core.FileHandling;
  13. using PicView.Core.Localization;
  14. using PicView.Core.ProcessHandling;
  15. using PicView.WindowsNT;
  16. using PicView.WindowsNT.FileHandling;
  17. using PicView.WindowsNT.Lockscreen;
  18. using PicView.WindowsNT.Taskbar;
  19. using PicView.WindowsNT.Wallpaper;
  20. using Dispatcher = Avalonia.Threading.Dispatcher;
  21. namespace PicView.Avalonia.Win32;
  22. public class App : Application, IPlatformSpecificService
  23. {
  24. private WinMainWindow? _mainWindow;
  25. private ExifWindow? _exifWindow;
  26. private SettingsWindow? _settingsWindow;
  27. private KeybindingsWindow? _keybindingsWindow;
  28. private AboutWindow? _aboutWindow;
  29. private MainViewModel? _vm;
  30. private TaskbarProgress? _taskbarProgress;
  31. public override void Initialize()
  32. {
  33. ProfileOptimization.SetProfileRoot(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/"));
  34. ProfileOptimization.StartProfile("ProfileOptimization");
  35. AvaloniaXamlLoader.Load(this);
  36. }
  37. public override async void OnFrameworkInitializationCompleted()
  38. {
  39. base.OnFrameworkInitializationCompleted();
  40. if (ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  41. {
  42. return;
  43. }
  44. bool settingsExists;
  45. try
  46. {
  47. settingsExists = await SettingsHelper.LoadSettingsAsync().ConfigureAwait(false);
  48. await TranslationHelper.LoadLanguage(SettingsHelper.Settings.UIProperties.UserLanguage);
  49. }
  50. catch (TaskCanceledException)
  51. {
  52. return;
  53. }
  54. await Dispatcher.UIThread.InvokeAsync(() =>
  55. {
  56. _mainWindow = new WinMainWindow();
  57. desktop.MainWindow = _mainWindow;
  58. });
  59. _vm = new MainViewModel(this);
  60. await Dispatcher.UIThread.InvokeAsync(() =>
  61. {
  62. _mainWindow.DataContext = _vm;
  63. StartUpHelper.Start(_vm, settingsExists, desktop, _mainWindow);
  64. });
  65. }
  66. #region Interface Implementations
  67. public void SetTaskbarProgress(ulong progress, ulong maximum)
  68. {
  69. if (_taskbarProgress is null)
  70. {
  71. var handle = _mainWindow?.TryGetPlatformHandle()?.Handle;
  72. // Ensure the handle is valid before proceeding
  73. if (handle == IntPtr.Zero || handle is null)
  74. {
  75. return;
  76. }
  77. _taskbarProgress = new TaskbarProgress(handle.Value);
  78. }
  79. _taskbarProgress.SetProgress(progress, maximum);
  80. }
  81. public void StopTaskbarProgress()
  82. {
  83. var handle = _mainWindow?.TryGetPlatformHandle()?.Handle;
  84. // Ensure the handle is valid before proceeding
  85. if (handle == IntPtr.Zero || handle is null)
  86. {
  87. return;
  88. }
  89. _taskbarProgress?.StopProgress();
  90. _taskbarProgress = null;
  91. }
  92. public void SetCursorPos(int x, int y)
  93. {
  94. NativeMethods.SetCursorPos(x, y);
  95. }
  96. public List<string> GetFiles(FileInfo fileInfo)
  97. {
  98. var files = FileListHelper.RetrieveFiles(fileInfo);
  99. return FileListManager.SortIEnumerable(files, this);
  100. }
  101. public int CompareStrings(string str1, string str2)
  102. {
  103. return NativeMethods.StrCmpLogicalW(str1, str2);
  104. }
  105. public void OpenWith(string path)
  106. {
  107. ProcessHelper.OpenWith(path);
  108. }
  109. public void LocateOnDisk(string path)
  110. {
  111. var folder = Path.GetDirectoryName(path);
  112. FileExplorer.OpenFolderAndSelectFile(folder, path);
  113. }
  114. public void ShowFileProperties(string path)
  115. {
  116. FileExplorer.ShowFileProperties(path);
  117. }
  118. public void ShowAboutWindow()
  119. {
  120. if (Dispatcher.UIThread.CheckAccess())
  121. {
  122. Set();
  123. }
  124. else
  125. {
  126. Dispatcher.UIThread.InvokeAsync(Set);
  127. }
  128. return;
  129. void Set()
  130. {
  131. if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  132. {
  133. return;
  134. }
  135. if (_aboutWindow is null)
  136. {
  137. _aboutWindow = new AboutWindow
  138. {
  139. DataContext = _vm,
  140. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  141. };
  142. _aboutWindow.Show(desktop.MainWindow);
  143. _aboutWindow.Closing += (s, e) => _aboutWindow = null;
  144. }
  145. else
  146. {
  147. _aboutWindow.Activate();
  148. }
  149. _ = FunctionsHelper.CloseMenus();
  150. }
  151. }
  152. public void ShowExifWindow()
  153. {
  154. if (Dispatcher.UIThread.CheckAccess())
  155. {
  156. Set();
  157. }
  158. else
  159. {
  160. Dispatcher.UIThread.InvokeAsync(Set);
  161. }
  162. return;
  163. void Set()
  164. {
  165. if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  166. {
  167. return;
  168. }
  169. if (_exifWindow is null)
  170. {
  171. _exifWindow = new ExifWindow
  172. {
  173. DataContext = _vm,
  174. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  175. };
  176. _exifWindow.Show(desktop.MainWindow);
  177. _exifWindow.Closing += (s, e) => _exifWindow = null;
  178. }
  179. else
  180. {
  181. _exifWindow.Activate();
  182. }
  183. _ = FunctionsHelper.CloseMenus();
  184. }
  185. }
  186. public void ShowKeybindingsWindow()
  187. {
  188. if (Dispatcher.UIThread.CheckAccess())
  189. {
  190. Set();
  191. }
  192. else
  193. {
  194. Dispatcher.UIThread.InvokeAsync(Set);
  195. }
  196. return;
  197. void Set()
  198. {
  199. if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  200. {
  201. return;
  202. }
  203. if (_keybindingsWindow is null)
  204. {
  205. _keybindingsWindow = new KeybindingsWindow
  206. {
  207. DataContext = _vm,
  208. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  209. };
  210. _keybindingsWindow.Show(desktop.MainWindow);
  211. _keybindingsWindow.Closing += (s, e) => _keybindingsWindow = null;
  212. }
  213. else
  214. {
  215. _keybindingsWindow.Activate();
  216. }
  217. _ = FunctionsHelper.CloseMenus();
  218. }
  219. }
  220. public void ShowSettingsWindow()
  221. {
  222. if (Dispatcher.UIThread.CheckAccess())
  223. {
  224. Set();
  225. }
  226. else
  227. {
  228. Dispatcher.UIThread.InvokeAsync(Set);
  229. }
  230. return;
  231. void Set()
  232. {
  233. if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  234. {
  235. return;
  236. }
  237. if (_settingsWindow is null)
  238. {
  239. _settingsWindow = new SettingsWindow
  240. {
  241. DataContext = _vm,
  242. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  243. };
  244. _settingsWindow.Show(desktop.MainWindow);
  245. _settingsWindow.Closing += (s, e) => _settingsWindow = null;
  246. }
  247. else
  248. {
  249. _settingsWindow.Activate();
  250. }
  251. _= FunctionsHelper.CloseMenus();
  252. }
  253. }
  254. public void ShowEffectsWindow()
  255. {
  256. // TODO: Implement ShowEffectsWindow
  257. }
  258. public void ShowResizeWindow()
  259. {
  260. // TODO: Implement ShowResizeWindow
  261. }
  262. public void Print(string path)
  263. {
  264. ProcessHelper.Print(path);
  265. }
  266. public void SetAsWallpaper(string path, int wallpaperStyle)
  267. {
  268. var style = (WallpaperHelper.WallpaperStyle)wallpaperStyle;
  269. WallpaperHelper.SetDesktopWallpaper(path, style);
  270. }
  271. public void SetAsLockScreen(string path)
  272. {
  273. // TODO: Run a new instance with admin rights and execute SetLockScreenImage
  274. LockscreenHelper.SetLockScreenImage(path);
  275. }
  276. public bool CopyFile(string path)
  277. {
  278. return Win32Clipboard.CopyFileToClipboard(false, path);
  279. }
  280. public bool CutFile(string path)
  281. {
  282. return Win32Clipboard.CopyFileToClipboard(true, path);
  283. }
  284. public async Task<bool> ExtractWithLocalSoftwareAsync(string path, string tempDirectory)
  285. {
  286. return await ArchiveExtractionHelper.ExtractWithLocalSoftwareAsync(path, tempDirectory);
  287. }
  288. public void DisableScreensaver()
  289. {
  290. NativeMethods.DisableScreensaver();
  291. }
  292. public void EnableScreensaver()
  293. {
  294. NativeMethods.EnableScreensaver();
  295. }
  296. #endregion
  297. }