App.axaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  1. using System.Runtime;
  2. using Avalonia;
  3. using Avalonia.Controls;
  4. using Avalonia.Controls.ApplicationLifetimes;
  5. using Avalonia.Markup.Xaml;
  6. using Avalonia.Media.Imaging;
  7. using Avalonia.Threading;
  8. using Clowd.Clipboard;
  9. using PicView.Avalonia.ColorManagement;
  10. using PicView.Avalonia.Interfaces;
  11. using PicView.Avalonia.Navigation;
  12. using PicView.Avalonia.StartUp;
  13. using PicView.Avalonia.UI;
  14. using PicView.Avalonia.ViewModels;
  15. using PicView.Avalonia.Win32.Views;
  16. using PicView.Avalonia.WindowBehavior;
  17. using PicView.Core.FileHandling;
  18. using PicView.Core.Localization;
  19. using PicView.Core.ProcessHandling;
  20. using PicView.Core.WindowsNT;
  21. using PicView.Core.WindowsNT.FileHandling;
  22. using PicView.Core.WindowsNT.Taskbar;
  23. using PicView.Core.WindowsNT.Wallpaper;
  24. using Dispatcher = Avalonia.Threading.Dispatcher;
  25. using Win32Clipboard = PicView.Core.WindowsNT.Copy.Win32Clipboard;
  26. namespace PicView.Avalonia.Win32;
  27. public partial class App : Application, IPlatformSpecificService
  28. {
  29. private WinMainWindow? _mainWindow;
  30. private ExifWindow? _exifWindow;
  31. private SettingsWindow? _settingsWindow;
  32. private KeybindingsWindow? _keybindingsWindow;
  33. private AboutWindow? _aboutWindow;
  34. private SingleImageResizeWindow? _singleImageResizeWindow;
  35. private BatchResizeWindow? _batchResizeWindow;
  36. private EffectsWindow? _effectsWindow;
  37. private MainViewModel? _vm;
  38. private TaskbarProgress? _taskbarProgress;
  39. public override void Initialize()
  40. {
  41. ProfileOptimization.SetProfileRoot(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Config/"));
  42. ProfileOptimization.StartProfile("ProfileOptimization");
  43. AvaloniaXamlLoader.Load(this);
  44. }
  45. public override async void OnFrameworkInitializationCompleted()
  46. {
  47. try
  48. {
  49. base.OnFrameworkInitializationCompleted();
  50. if (ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  51. {
  52. return;
  53. }
  54. bool settingsExists;
  55. try
  56. {
  57. settingsExists = await LoadSettingsAsync().ConfigureAwait(false);
  58. }
  59. catch (TaskCanceledException)
  60. {
  61. return;
  62. }
  63. TranslationManager.Init();
  64. await Dispatcher.UIThread.InvokeAsync(() =>
  65. {
  66. ThemeManager.DetermineTheme(Current, settingsExists);
  67. _mainWindow = new WinMainWindow();
  68. desktop.MainWindow = _mainWindow;
  69. },DispatcherPriority.Send);
  70. _vm = new MainViewModel(this);
  71. await Dispatcher.UIThread.InvokeAsync(() =>
  72. {
  73. _mainWindow.DataContext = _vm;
  74. StartUpHelper.Start(_vm, settingsExists, desktop, _mainWindow);
  75. },DispatcherPriority.Send);
  76. }
  77. catch (Exception e)
  78. {
  79. #if DEBUG
  80. Console.WriteLine(e);
  81. #endif
  82. }
  83. }
  84. #region Interface Implementations
  85. public void SetTaskbarProgress(ulong progress, ulong maximum)
  86. {
  87. if (_taskbarProgress is null)
  88. {
  89. var handle = _mainWindow?.TryGetPlatformHandle()?.Handle;
  90. // Ensure the handle is valid before proceeding
  91. if (handle == IntPtr.Zero || handle is null)
  92. {
  93. return;
  94. }
  95. _taskbarProgress = new TaskbarProgress(handle.Value);
  96. }
  97. _taskbarProgress.SetProgress(progress, maximum);
  98. }
  99. public void StopTaskbarProgress()
  100. {
  101. var handle = _mainWindow?.TryGetPlatformHandle()?.Handle;
  102. // Ensure the handle is valid before proceeding
  103. if (handle == IntPtr.Zero || handle is null)
  104. {
  105. return;
  106. }
  107. _taskbarProgress?.StopProgress();
  108. _taskbarProgress = null;
  109. }
  110. public void SetCursorPos(int x, int y)
  111. {
  112. NativeMethods.SetCursorPos(x, y);
  113. }
  114. public List<string> GetFiles(FileInfo fileInfo)
  115. {
  116. var files = FileListHelper.RetrieveFiles(fileInfo);
  117. return FileListManager.SortIEnumerable(files, this);
  118. }
  119. public int CompareStrings(string str1, string str2)
  120. {
  121. return NativeMethods.StrCmpLogicalW(str1, str2);
  122. }
  123. public void OpenWith(string path)
  124. {
  125. ProcessHelper.OpenWith(path);
  126. }
  127. public void LocateOnDisk(string path)
  128. {
  129. var folder = Path.GetDirectoryName(path);
  130. FileExplorer.OpenFolderAndSelectFile(folder, path);
  131. }
  132. public void ShowFileProperties(string path)
  133. {
  134. FileExplorer.ShowFileProperties(path);
  135. }
  136. public void ShowAboutWindow()
  137. {
  138. if (Dispatcher.UIThread.CheckAccess())
  139. {
  140. Set();
  141. }
  142. else
  143. {
  144. Dispatcher.UIThread.InvokeAsync(Set);
  145. }
  146. return;
  147. void Set()
  148. {
  149. if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  150. {
  151. return;
  152. }
  153. if (_aboutWindow is null)
  154. {
  155. _aboutWindow = new AboutWindow
  156. {
  157. DataContext = _vm,
  158. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  159. };
  160. _aboutWindow.Show(desktop.MainWindow);
  161. _aboutWindow.Closing += (s, e) => _aboutWindow = null;
  162. }
  163. else
  164. {
  165. if (_aboutWindow.WindowState == WindowState.Minimized)
  166. {
  167. WindowFunctions.ShowMinimizedWindow(_aboutWindow);
  168. }
  169. else
  170. {
  171. _aboutWindow.Show();
  172. }
  173. }
  174. _ = FunctionsMapper.CloseMenus();
  175. }
  176. }
  177. public void ShowExifWindow()
  178. {
  179. if (Dispatcher.UIThread.CheckAccess())
  180. {
  181. Set();
  182. }
  183. else
  184. {
  185. Dispatcher.UIThread.InvokeAsync(Set);
  186. }
  187. return;
  188. void Set()
  189. {
  190. if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  191. {
  192. return;
  193. }
  194. if (_exifWindow is null)
  195. {
  196. _exifWindow = new ExifWindow
  197. {
  198. DataContext = _vm,
  199. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  200. };
  201. _exifWindow.Show(desktop.MainWindow);
  202. _exifWindow.Closing += (s, e) => _exifWindow = null;
  203. }
  204. else
  205. {
  206. if (_exifWindow.WindowState == WindowState.Minimized)
  207. {
  208. WindowFunctions.ShowMinimizedWindow(_exifWindow);
  209. }
  210. else
  211. {
  212. _exifWindow.Show();
  213. }
  214. }
  215. _ = FunctionsMapper.CloseMenus();
  216. }
  217. }
  218. public void ShowKeybindingsWindow()
  219. {
  220. if (Dispatcher.UIThread.CheckAccess())
  221. {
  222. Set();
  223. }
  224. else
  225. {
  226. Dispatcher.UIThread.InvokeAsync(Set);
  227. }
  228. return;
  229. void Set()
  230. {
  231. if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  232. {
  233. return;
  234. }
  235. if (_keybindingsWindow is null)
  236. {
  237. _keybindingsWindow = new KeybindingsWindow
  238. {
  239. DataContext = _vm,
  240. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  241. };
  242. _keybindingsWindow.Show(desktop.MainWindow);
  243. _keybindingsWindow.Closing += (s, e) => _keybindingsWindow = null;
  244. }
  245. else
  246. {
  247. if (_keybindingsWindow.WindowState == WindowState.Minimized)
  248. {
  249. WindowFunctions.ShowMinimizedWindow(_keybindingsWindow);
  250. }
  251. else
  252. {
  253. _keybindingsWindow.Show();
  254. }
  255. }
  256. _ = FunctionsMapper.CloseMenus();
  257. }
  258. }
  259. public void ShowSettingsWindow()
  260. {
  261. if (Dispatcher.UIThread.CheckAccess())
  262. {
  263. Set();
  264. }
  265. else
  266. {
  267. Dispatcher.UIThread.InvokeAsync(Set);
  268. }
  269. return;
  270. void Set()
  271. {
  272. if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  273. {
  274. return;
  275. }
  276. if (_settingsWindow is null)
  277. {
  278. _settingsWindow = new SettingsWindow
  279. {
  280. DataContext = _vm,
  281. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  282. };
  283. _settingsWindow.Show(desktop.MainWindow);
  284. _settingsWindow.Closing += (s, e) => _settingsWindow = null;
  285. }
  286. else
  287. {
  288. if (_settingsWindow.WindowState == WindowState.Minimized)
  289. {
  290. WindowFunctions.ShowMinimizedWindow(_settingsWindow);
  291. }
  292. else
  293. {
  294. _settingsWindow.Show();
  295. }
  296. }
  297. _= FunctionsMapper.CloseMenus();
  298. }
  299. }
  300. public void ShowSingleImageResizeWindow()
  301. {
  302. if (Dispatcher.UIThread.CheckAccess())
  303. {
  304. Set();
  305. }
  306. else
  307. {
  308. Dispatcher.UIThread.InvokeAsync(Set);
  309. }
  310. return;
  311. void Set()
  312. {
  313. if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  314. {
  315. return;
  316. }
  317. if (_singleImageResizeWindow is null)
  318. {
  319. _singleImageResizeWindow = new SingleImageResizeWindow
  320. {
  321. DataContext = _vm,
  322. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  323. };
  324. _singleImageResizeWindow.Show(desktop.MainWindow);
  325. _singleImageResizeWindow.Closing += (s, e) => _singleImageResizeWindow = null;
  326. }
  327. else
  328. {
  329. if (_singleImageResizeWindow.WindowState == WindowState.Minimized)
  330. {
  331. WindowFunctions.ShowMinimizedWindow(_singleImageResizeWindow);
  332. }
  333. else
  334. {
  335. _singleImageResizeWindow.Show();
  336. }
  337. }
  338. _= FunctionsMapper.CloseMenus();
  339. }
  340. }
  341. public void ShowBatchResizeWindow()
  342. {
  343. if (Dispatcher.UIThread.CheckAccess())
  344. {
  345. Set();
  346. }
  347. else
  348. {
  349. Dispatcher.UIThread.InvokeAsync(Set);
  350. }
  351. return;
  352. void Set()
  353. {
  354. if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  355. {
  356. return;
  357. }
  358. if (_batchResizeWindow is null)
  359. {
  360. _batchResizeWindow = new BatchResizeWindow
  361. {
  362. DataContext = _vm,
  363. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  364. };
  365. _batchResizeWindow.Show(desktop.MainWindow);
  366. _batchResizeWindow.Closing += (s, e) => _batchResizeWindow = null;
  367. }
  368. else
  369. {
  370. if (_batchResizeWindow.WindowState == WindowState.Minimized)
  371. {
  372. WindowFunctions.ShowMinimizedWindow(_batchResizeWindow);
  373. }
  374. else
  375. {
  376. _batchResizeWindow.Show();
  377. }
  378. }
  379. _= FunctionsMapper.CloseMenus();
  380. }
  381. }
  382. public void ShowEffectsWindow()
  383. {
  384. if (Dispatcher.UIThread.CheckAccess())
  385. {
  386. Set();
  387. }
  388. else
  389. {
  390. Dispatcher.UIThread.InvokeAsync(Set);
  391. }
  392. return;
  393. void Set()
  394. {
  395. if (Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  396. {
  397. return;
  398. }
  399. if (_effectsWindow is null)
  400. {
  401. _effectsWindow = new EffectsWindow
  402. {
  403. DataContext = _vm,
  404. WindowStartupLocation = WindowStartupLocation.CenterOwner,
  405. };
  406. _effectsWindow.Show(desktop.MainWindow);
  407. _effectsWindow.Closing += (s, e) => _effectsWindow = null;
  408. }
  409. else
  410. {
  411. if (_effectsWindow.WindowState == WindowState.Minimized)
  412. {
  413. WindowFunctions.ShowMinimizedWindow(_effectsWindow);
  414. }
  415. else
  416. {
  417. _effectsWindow.Show();
  418. }
  419. }
  420. _= FunctionsMapper.CloseMenus();
  421. }
  422. }
  423. public void Print(string path)
  424. {
  425. ProcessHelper.Print(path);
  426. }
  427. public void SetAsWallpaper(string path, int wallpaperStyle)
  428. {
  429. var style = (WallpaperHelper.WallpaperStyle)wallpaperStyle;
  430. WallpaperHelper.SetDesktopWallpaper(path, style);
  431. }
  432. public bool SetAsLockScreen(string path)
  433. {
  434. return false;
  435. // return LockscreenHelper.SetLockScreenImage(path);
  436. }
  437. public bool CopyFile(string path)
  438. {
  439. return Win32Clipboard.CopyFileToClipboard(false, path);
  440. }
  441. public bool CutFile(string path)
  442. {
  443. return Win32Clipboard.CopyFileToClipboard(true, path);
  444. }
  445. public async Task CopyImageToClipboard(Bitmap bitmap)
  446. {
  447. await ClipboardAvalonia.SetImageAsync(bitmap).ConfigureAwait(false);
  448. }
  449. public async Task<Bitmap?> GetImageFromClipboard()
  450. {
  451. return await ClipboardAvalonia.GetImageAsync().ConfigureAwait(false);
  452. }
  453. public async Task<bool> ExtractWithLocalSoftwareAsync(string path, string tempDirectory)
  454. {
  455. return await ArchiveExtractionHelper.ExtractWithLocalSoftwareAsync(path, tempDirectory);
  456. }
  457. public string DefaultJsonKeyMap()
  458. {
  459. return WindowsKeybindings.DefaultKeybindings;
  460. }
  461. public void DisableScreensaver()
  462. {
  463. NativeMethods.DisableScreensaver();
  464. }
  465. public void EnableScreensaver()
  466. {
  467. NativeMethods.EnableScreensaver();
  468. }
  469. #endregion
  470. }