FunctionsHelper.cs 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086
  1. using System.Runtime.InteropServices;
  2. using Avalonia;
  3. using Avalonia.Controls.ApplicationLifetimes;
  4. using Avalonia.Threading;
  5. using PicView.Avalonia.Clipboard;
  6. using PicView.Avalonia.ColorManagement;
  7. using PicView.Avalonia.Crop;
  8. using PicView.Avalonia.FileSystem;
  9. using PicView.Avalonia.Gallery;
  10. using PicView.Avalonia.ImageHandling;
  11. using PicView.Avalonia.ImageTransformations;
  12. using PicView.Avalonia.Navigation;
  13. using PicView.Avalonia.SettingsManagement;
  14. using PicView.Avalonia.ViewModels;
  15. using PicView.Avalonia.WindowBehavior;
  16. using PicView.Core.Config;
  17. using PicView.Core.FileHandling;
  18. using PicView.Core.ImageDecoding;
  19. using PicView.Core.ProcessHandling;
  20. namespace PicView.Avalonia.UI;
  21. public static class FunctionsHelper
  22. {
  23. public static MainViewModel? Vm;
  24. public static Task<Func<Task>> GetFunctionByName(string functionName)
  25. {
  26. // Remember to have exact matching names, or it will be null
  27. return Task.FromResult<Func<Task>>(functionName switch
  28. {
  29. // Navigation values
  30. "Next" => Next,
  31. "Prev" => Prev,
  32. "NextFolder" => NextFolder,
  33. "PrevFolder" => PrevFolder,
  34. "Up" => Up,
  35. "Down" => Down,
  36. "Last" => Last,
  37. "First" => First,
  38. "Next10" => Next10,
  39. "Prev10" => Prev10,
  40. "Next100" => Next100,
  41. "Prev100" => Prev100,
  42. // Rotate
  43. "RotateLeft" => RotateLeft,
  44. "RotateRight" => RotateRight,
  45. // Scroll
  46. "ScrollUp" => ScrollUp,
  47. "ScrollDown" => ScrollDown,
  48. "ScrollToTop" => ScrollToTop,
  49. "ScrollToBottom" => ScrollToBottom,
  50. // Zoom
  51. "ZoomIn" => ZoomIn,
  52. "ZoomOut" => ZoomOut,
  53. "ResetZoom" => ResetZoom,
  54. "ChangeCtrlZoom" => ChangeCtrlZoom,
  55. // Toggles
  56. "ToggleScroll" => ToggleScroll,
  57. "ToggleLooping" => ToggleLooping,
  58. "ToggleGallery" => ToggleGallery,
  59. // Scale Window
  60. "AutoFitWindow" => AutoFitWindow,
  61. "AutoFitWindowAndStretch" => AutoFitWindowAndStretch,
  62. "NormalWindow" => NormalWindow,
  63. "NormalWindowAndStretch" => NormalWindowAndStretch,
  64. // Window functions
  65. "Fullscreen" => Fullscreen,
  66. "ToggleFullscreen" => ToggleFullscreen,
  67. "SetTopMost" => SetTopMost,
  68. "Close" => Close,
  69. "ToggleInterface" => ToggleInterface,
  70. "NewWindow" => NewWindow,
  71. "Center" => Center,
  72. // Windows
  73. "AboutWindow" => AboutWindow,
  74. "EffectsWindow" => EffectsWindow,
  75. "ImageInfoWindow" => ImageInfoWindow,
  76. "ResizeWindow" => ResizeWindow,
  77. "SettingsWindow" => SettingsWindow,
  78. "KeybindingsWindow" => KeybindingsWindow,
  79. // Open functions
  80. "Open" => Open,
  81. "OpenWith" => OpenWith,
  82. "OpenInExplorer" => OpenInExplorer,
  83. "Save" => Save,
  84. "SaveAs" => SaveAs,
  85. "Print" => Print,
  86. "Reload" => Reload,
  87. // Copy functions
  88. "CopyFile" => CopyFile,
  89. "CopyFilePath" => CopyFilePath,
  90. "CopyImage" => CopyImage,
  91. "CopyBase64" => CopyBase64,
  92. "DuplicateFile" => DuplicateFile,
  93. "CutFile" => CutFile,
  94. "Paste" => Paste,
  95. // File functions
  96. "DeleteFile" => DeleteFile,
  97. "DeleteFilePermanently" => DeleteFilePermanently,
  98. "Rename" => Rename,
  99. "ShowFileProperties" => ShowFileProperties,
  100. // Image functions
  101. "ResizeImage" => ResizeImage,
  102. "Crop" => Crop,
  103. "Flip" => Flip,
  104. "OptimizeImage" => OptimizeImage,
  105. "Stretch" => Stretch,
  106. // Set stars
  107. "Set0Star" => Set0Star,
  108. "Set1Star" => Set1Star,
  109. "Set2Star" => Set2Star,
  110. "Set3Star" => Set3Star,
  111. "Set4Star" => Set4Star,
  112. "Set5Star" => Set5Star,
  113. // Background and lock screen image
  114. "SetAsLockScreen" => SetAsLockScreen,
  115. "SetAsLockscreenCentered" => SetAsLockscreenCentered,
  116. "SetAsWallpaper" => SetAsWallpaper,
  117. "SetAsWallpaperFitted" => SetAsWallpaperFitted,
  118. "SetAsWallpaperStretched" => SetAsWallpaperStretched,
  119. "SetAsWallpaperFilled" => SetAsWallpaperFilled,
  120. "SetAsWallpaperCentered" => SetAsWallpaperCentered,
  121. "SetAsWallpaperTiled" => SetAsWallpaperTiled,
  122. // Misc
  123. "ChangeBackground" => ChangeBackground,
  124. "SideBySide" => SideBySide,
  125. "GalleryClick" => GalleryClick,
  126. "Slideshow" => Slideshow,
  127. "ColorPicker" => ColorPicker,
  128. "Restart" => Restart,
  129. _ => null
  130. });
  131. }
  132. #region Functions
  133. #region Menus
  134. public static Task CloseMenus()
  135. {
  136. if (Vm is null)
  137. {
  138. return Task.CompletedTask;
  139. }
  140. UIHelper.CloseMenus(Vm);
  141. return Task.CompletedTask;
  142. }
  143. public static Task ToggleFileMenu()
  144. {
  145. if (Vm is null)
  146. {
  147. return Task.CompletedTask;
  148. }
  149. UIHelper.ToggleFileMenu(Vm);
  150. return Task.CompletedTask;
  151. }
  152. public static Task ToggleImageMenu()
  153. {
  154. if (Vm is null)
  155. {
  156. return Task.CompletedTask;
  157. }
  158. UIHelper.ToggleImageMenu(Vm);
  159. return Task.CompletedTask;
  160. }
  161. public static Task ToggleSettingsMenu()
  162. {
  163. if (Vm is null)
  164. {
  165. return Task.CompletedTask;
  166. }
  167. UIHelper.ToggleSettingsMenu(Vm);
  168. return Task.CompletedTask;
  169. }
  170. public static Task ToggleToolsMenu()
  171. {
  172. if (Vm is null)
  173. {
  174. return Task.CompletedTask;
  175. }
  176. UIHelper.ToggleToolsMenu(Vm);
  177. return Task.CompletedTask;
  178. }
  179. #endregion Menus
  180. #region Navigation, zoom and rotation
  181. public static async Task Next()
  182. {
  183. await NavigationHelper.Iterate(next: true, Vm);
  184. }
  185. public static async Task NextFolder()
  186. {
  187. await NavigationHelper.GoToNextFolder(true, Vm);
  188. }
  189. public static async Task Last()
  190. {
  191. await NavigationHelper.NavigateFirstOrLast(last: true, Vm);
  192. }
  193. public static async Task Prev()
  194. {
  195. await NavigationHelper.Iterate(next: false, Vm);
  196. }
  197. public static async Task PrevFolder()
  198. {
  199. await NavigationHelper.GoToNextFolder(false, Vm);
  200. }
  201. public static async Task First()
  202. {
  203. await NavigationHelper.NavigateFirstOrLast(last: false, Vm);
  204. }
  205. public static async Task Next10()
  206. {
  207. await NavigationHelper.Next10(Vm).ConfigureAwait(false);
  208. }
  209. public static async Task Next100()
  210. {
  211. await NavigationHelper.Next100(Vm).ConfigureAwait(false);
  212. }
  213. public static async Task Prev10()
  214. {
  215. await NavigationHelper.Prev10(Vm).ConfigureAwait(false);
  216. }
  217. public static async Task Prev100()
  218. {
  219. await NavigationHelper.Prev100(Vm).ConfigureAwait(false);
  220. }
  221. public static async Task Up()
  222. {
  223. await UIHelper.NavigateUp(Vm);
  224. }
  225. public static async Task RotateRight()
  226. {
  227. await Rotation.RotateRight(Vm);
  228. }
  229. public static async Task RotateLeft()
  230. {
  231. await Rotation.RotateLeft(Vm);
  232. }
  233. public static async Task Down()
  234. {
  235. await UIHelper.NavigateDown(Vm);
  236. }
  237. public static async Task ScrollDown()
  238. {
  239. await Dispatcher.UIThread.InvokeAsync(() =>
  240. {
  241. Vm.ImageViewer.ImageScrollViewer.LineDown();
  242. });
  243. }
  244. public static async Task ScrollUp()
  245. {
  246. await Dispatcher.UIThread.InvokeAsync(() =>
  247. {
  248. Vm.ImageViewer.ImageScrollViewer.LineUp();
  249. });
  250. }
  251. public static async Task ScrollToTop()
  252. {
  253. await Dispatcher.UIThread.InvokeAsync(() =>
  254. {
  255. Vm.ImageViewer.ImageScrollViewer.ScrollToHome();
  256. });
  257. }
  258. public static async Task ScrollToBottom()
  259. {
  260. await Dispatcher.UIThread.InvokeAsync(() =>
  261. {
  262. Vm.ImageViewer.ImageScrollViewer.ScrollToEnd();
  263. });
  264. }
  265. public static async Task ZoomIn()
  266. {
  267. if (Vm is null)
  268. {
  269. return;
  270. }
  271. await Dispatcher.UIThread.InvokeAsync(Vm.ImageViewer.ZoomIn);
  272. }
  273. public static async Task ZoomOut()
  274. {
  275. if (Vm is null)
  276. {
  277. return;
  278. }
  279. await Dispatcher.UIThread.InvokeAsync(Vm.ImageViewer.ZoomOut);
  280. }
  281. public static async Task ResetZoom()
  282. {
  283. if (Vm is null)
  284. {
  285. return;
  286. }
  287. await Dispatcher.UIThread.InvokeAsync(() => Vm.ImageViewer.ResetZoom(true));
  288. }
  289. #endregion
  290. #region Toggle UI functions
  291. public static async Task ToggleScroll()
  292. {
  293. await SettingsUpdater.ToggleScroll(Vm).ConfigureAwait(false);
  294. }
  295. public static async Task ChangeCtrlZoom()
  296. {
  297. await SettingsUpdater.ToggleCtrlZoom(Vm);
  298. }
  299. public static async Task ToggleLooping()
  300. {
  301. await SettingsUpdater.ToggleLooping(Vm);
  302. }
  303. public static async Task ToggleInterface()
  304. {
  305. await HideInterfaceLogic.ToggleUI(Vm);
  306. }
  307. public static async Task ToggleSubdirectories()
  308. {
  309. await SettingsUpdater.ToggleSubdirectories(vm: Vm);
  310. }
  311. public static async Task ToggleBottomToolbar()
  312. {
  313. if (Vm is null)
  314. {
  315. return;
  316. }
  317. await HideInterfaceLogic.ToggleBottomToolbar(Vm);
  318. }
  319. public static async Task ToggleTaskbarProgress()
  320. {
  321. await SettingsUpdater.ToggleTaskbarProgress(Vm);
  322. }
  323. #endregion
  324. #region Gallery functions
  325. public static async Task ToggleGallery()
  326. {
  327. await GalleryFunctions.ToggleGallery(Vm).ConfigureAwait(false);
  328. }
  329. public static async Task OpenCloseBottomGallery()
  330. {
  331. await GalleryFunctions.OpenCloseBottomGallery(Vm).ConfigureAwait(false);
  332. }
  333. public static async Task CloseGallery()
  334. {
  335. await GalleryFunctions.CloseGallery(Vm);
  336. }
  337. public static async Task GalleryClick()
  338. {
  339. await GalleryNavigation.GalleryClick(Vm);
  340. }
  341. #endregion
  342. #region Windows and window functions
  343. public static async Task ShowStartUpMenu()
  344. {
  345. await Dispatcher.UIThread.InvokeAsync(() =>
  346. {
  347. ErrorHandling.ShowStartUpMenu(Vm);
  348. });
  349. }
  350. public static async Task Close()
  351. {
  352. if (Vm is null)
  353. {
  354. return;
  355. }
  356. await UIHelper.Close(Vm);
  357. }
  358. public static async Task Center()
  359. {
  360. // TODO: scroll to center when the gallery is open
  361. await Dispatcher.UIThread.InvokeAsync(() =>
  362. {
  363. WindowFunctions.CenterWindowOnScreen();
  364. });
  365. }
  366. public static async Task NewWindow()
  367. {
  368. await Task.Run(ProcessHelper.StartNewProcess);
  369. }
  370. public static Task AboutWindow()
  371. {
  372. Vm?.PlatformService?.ShowAboutWindow();
  373. return Task.CompletedTask;
  374. }
  375. public static Task KeybindingsWindow()
  376. {
  377. Vm?.PlatformService?.ShowKeybindingsWindow();
  378. return Task.CompletedTask;
  379. }
  380. public static Task EffectsWindow()
  381. {
  382. Vm?.PlatformService?.ShowEffectsWindow();
  383. return Task.CompletedTask;
  384. }
  385. public static Task ImageInfoWindow()
  386. {
  387. Vm.PlatformService.ShowExifWindow();
  388. return Task.CompletedTask;
  389. }
  390. public static Task ResizeWindow()
  391. {
  392. Vm?.PlatformService?.ShowSingleImageResizeWindow();
  393. return Task.CompletedTask;
  394. }
  395. public static Task SettingsWindow()
  396. {
  397. Vm?.PlatformService.ShowSettingsWindow();
  398. return Task.CompletedTask;
  399. }
  400. #endregion Windows
  401. #region Image Scaling and Window Behavior
  402. public static async Task Stretch()
  403. {
  404. await WindowFunctions.Stretch(Vm);
  405. }
  406. public static async Task AutoFitWindow()
  407. {
  408. await WindowFunctions.ToggleAutoFit(Vm);
  409. }
  410. public static async Task AutoFitWindowAndStretch()
  411. {
  412. await WindowFunctions.AutoFitAndStretch(Vm);
  413. }
  414. public static async Task NormalWindow()
  415. {
  416. await WindowFunctions.NormalWindow(Vm);
  417. }
  418. public static async Task NormalWindowAndStretch()
  419. {
  420. await WindowFunctions.NormalWindowStretch(Vm);
  421. }
  422. public static async Task ToggleFullscreen()
  423. {
  424. if (Vm is null)
  425. {
  426. return;
  427. }
  428. await WindowFunctions.ToggleFullscreen(Vm);
  429. }
  430. public static Task Fullscreen()
  431. {
  432. if (Vm is null)
  433. {
  434. return Task.CompletedTask;
  435. }
  436. WindowFunctions.Fullscreen(Vm, Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime);
  437. return Task.CompletedTask;
  438. }
  439. public static async Task SetTopMost()
  440. {
  441. if (Vm is null)
  442. {
  443. return;
  444. }
  445. await WindowFunctions.ToggleTopMost(Vm);
  446. }
  447. #endregion
  448. #region File funnctions
  449. public static async Task OpenLastFile()
  450. {
  451. if (Vm is null)
  452. {
  453. return;
  454. }
  455. await FileHistoryNavigation.OpenLastFileAsync(Vm).ConfigureAwait(false);
  456. }
  457. public static async Task OpenPreviousFileHistoryEntry()
  458. {
  459. if (Vm is null)
  460. {
  461. return;
  462. }
  463. await FileHistoryNavigation.PrevAsync(Vm).ConfigureAwait(false);
  464. }
  465. public static async Task OpenNextFileHistoryEntry()
  466. {
  467. if (Vm is null)
  468. {
  469. return;
  470. }
  471. await FileHistoryNavigation.NextAsync(Vm).ConfigureAwait(false);
  472. }
  473. public static async Task Print()
  474. {
  475. await Task.Run(() =>
  476. {
  477. Vm?.PlatformService?.Print(Vm.FileInfo?.FullName);
  478. });
  479. }
  480. public static async Task Open()
  481. {
  482. await FilePicker.SelectAndLoadFile(Vm);
  483. }
  484. public static Task OpenWith()
  485. {
  486. Vm?.PlatformService?.OpenWith(Vm.FileInfo?.FullName);
  487. return Task.CompletedTask;
  488. }
  489. public static Task OpenInExplorer()
  490. {
  491. Vm?.PlatformService?.LocateOnDisk(Vm.FileInfo?.FullName);
  492. return Task.CompletedTask;
  493. }
  494. public static async Task Save()
  495. {
  496. await FileSaverHelper.SaveCurrentFile(Vm);
  497. }
  498. public static async Task SaveAs()
  499. {
  500. await FileSaverHelper.SaveFileAs(Vm);
  501. }
  502. public static async Task DeleteFile()
  503. {
  504. if (Vm is null)
  505. {
  506. return;
  507. }
  508. await FileManager.DeleteFile(true, Vm);
  509. }
  510. public static async Task DeleteFilePermanently()
  511. {
  512. if (Vm is null)
  513. {
  514. return;
  515. }
  516. await FileManager.DeleteFile(false, Vm);
  517. }
  518. public static async Task Rename()
  519. {
  520. await Dispatcher.UIThread.InvokeAsync(() =>
  521. {
  522. UIHelper.GetEditableTitlebar.SelectFileName();
  523. });
  524. }
  525. public static async Task ShowFileProperties()
  526. {
  527. await Task.Run(() => Vm?.PlatformService?.ShowFileProperties(Vm.FileInfo?.FullName));
  528. }
  529. #endregion
  530. #region Copy and Paste functions
  531. public static async Task CopyFile()
  532. {
  533. await ClipboardHelper.CopyFileToClipboard(Vm?.FileInfo?.FullName, Vm);
  534. }
  535. public static async Task CopyFilePath()
  536. {
  537. await ClipboardHelper.CopyTextToClipboard(Vm?.FileInfo?.FullName);
  538. }
  539. public static async Task CopyImage()
  540. {
  541. if (Vm is null)
  542. {
  543. return;
  544. }
  545. await ClipboardHelper.CopyImageToClipboard(Vm);
  546. }
  547. public static async Task CopyBase64()
  548. {
  549. if (Vm is null)
  550. {
  551. return;
  552. }
  553. await ClipboardHelper.CopyBase64ToClipboard(Vm.FileInfo?.FullName, vm: Vm);
  554. }
  555. public static async Task DuplicateFile()
  556. {
  557. await NavigationHelper.DuplicateAndNavigate(Vm).ConfigureAwait(false);
  558. }
  559. public static async Task CutFile()
  560. {
  561. if (Vm is null)
  562. {
  563. return;
  564. }
  565. await ClipboardHelper.CutFile(Vm.FileInfo.FullName, Vm);
  566. }
  567. public static async Task Paste()
  568. {
  569. if (Vm is null)
  570. {
  571. return;
  572. }
  573. await ClipboardHelper.Paste(Vm);
  574. }
  575. #endregion
  576. #region Image Functions
  577. public static async Task ChangeBackground()
  578. {
  579. if (Vm is null)
  580. {
  581. return;
  582. }
  583. BackgroundManager.ChangeBackground(Vm);
  584. await SettingsHelper.SaveSettingsAsync();
  585. }
  586. public static async Task SideBySide()
  587. {
  588. await SettingsUpdater.ToggleSideBySide(Vm);
  589. }
  590. public static async Task Reload()
  591. {
  592. if (Vm is null)
  593. {
  594. return;
  595. }
  596. await ErrorHandling.ReloadAsync(Vm);
  597. }
  598. public static Task ResizeImage()
  599. {
  600. Vm?.PlatformService?.ShowSingleImageResizeWindow();
  601. return Task.CompletedTask;
  602. }
  603. public static async Task Crop()
  604. {
  605. await Dispatcher.UIThread.InvokeAsync(() => CropFunctions.Init(Vm));
  606. }
  607. public static Task Flip()
  608. {
  609. ImageControl.Flip(Vm);
  610. return Task.CompletedTask;
  611. }
  612. public static async Task OptimizeImage()
  613. {
  614. await ImageHelper.OptimizeImage(Vm);
  615. }
  616. public static async Task Slideshow()
  617. {
  618. await Navigation.Slideshow.StartSlideshow(Vm);
  619. }
  620. public static Task ColorPicker()
  621. {
  622. return Task.CompletedTask;
  623. }
  624. #endregion
  625. #region Sorting
  626. public static async Task SortFilesByName()
  627. {
  628. if (Vm?.PlatformService is null)
  629. {
  630. return;
  631. }
  632. await FileListManager.UpdateFileList(Vm.PlatformService, Vm, FileListHelper.SortFilesBy.Name);
  633. }
  634. public static async Task SortFilesByCreationTime()
  635. {
  636. if (Vm?.PlatformService is null)
  637. {
  638. return;
  639. }
  640. await FileListManager.UpdateFileList(Vm?.PlatformService, Vm, FileListHelper.SortFilesBy.CreationTime);
  641. }
  642. public static async Task SortFilesByLastAccessTime()
  643. {
  644. if (Vm?.PlatformService is null)
  645. {
  646. return;
  647. }
  648. await FileListManager.UpdateFileList(Vm?.PlatformService, Vm, FileListHelper.SortFilesBy.LastAccessTime);
  649. }
  650. public static async Task SortFilesByLastWriteTime()
  651. {
  652. if (Vm?.PlatformService is null)
  653. {
  654. return;
  655. }
  656. await FileListManager.UpdateFileList(Vm?.PlatformService, Vm, FileListHelper.SortFilesBy.LastWriteTime);
  657. }
  658. public static async Task SortFilesBySize()
  659. {
  660. if (Vm?.PlatformService is null)
  661. {
  662. return;
  663. }
  664. await FileListManager.UpdateFileList(Vm?.PlatformService, Vm, FileListHelper.SortFilesBy.FileSize);
  665. }
  666. public static async Task SortFilesByExtension()
  667. {
  668. if (Vm?.PlatformService is null)
  669. {
  670. return;
  671. }
  672. await FileListManager.UpdateFileList(Vm?.PlatformService, Vm, FileListHelper.SortFilesBy.Extension);
  673. }
  674. public static async Task SortFilesRandomly()
  675. {
  676. if (Vm?.PlatformService is null)
  677. {
  678. return;
  679. }
  680. await FileListManager.UpdateFileList(Vm?.PlatformService, Vm, FileListHelper.SortFilesBy.Random);
  681. }
  682. public static async Task SortFilesAscending()
  683. {
  684. if (Vm?.PlatformService is null)
  685. {
  686. return;
  687. }
  688. await FileListManager.UpdateFileList(Vm?.PlatformService, Vm, ascending: true);
  689. }
  690. public static async Task SortFilesDescending()
  691. {
  692. if (Vm?.PlatformService is null)
  693. {
  694. return;
  695. }
  696. await FileListManager.UpdateFileList(Vm?.PlatformService, Vm, ascending: false);
  697. }
  698. #endregion Sorting
  699. #region Rating
  700. public static async Task Set0Star()
  701. {
  702. if (Vm is null)
  703. {
  704. return;
  705. }
  706. await Task.Run(() => { EXIFHelper.SetEXIFRating(Vm.FileInfo.FullName, 0); });
  707. Vm.EXIFRating = 0;
  708. }
  709. public static async Task Set1Star()
  710. {
  711. if (Vm is null)
  712. {
  713. return;
  714. }
  715. await Task.Run(() => { EXIFHelper.SetEXIFRating(Vm.FileInfo.FullName, 1); });
  716. Vm.EXIFRating = 1;
  717. }
  718. public static async Task Set2Star()
  719. {
  720. if (Vm is null)
  721. {
  722. return;
  723. }
  724. await Task.Run(() => { EXIFHelper.SetEXIFRating(Vm.FileInfo.FullName, 2); });
  725. Vm.EXIFRating = 2;
  726. }
  727. public static async Task Set3Star()
  728. {
  729. if (Vm is null)
  730. {
  731. return;
  732. }
  733. await Task.Run(() => { EXIFHelper.SetEXIFRating(Vm.FileInfo.FullName, 3); });
  734. Vm.EXIFRating = 3;
  735. }
  736. public static async Task Set4Star()
  737. {
  738. if (Vm is null)
  739. {
  740. return;
  741. }
  742. await Task.Run(() => { EXIFHelper.SetEXIFRating(Vm.FileInfo.FullName, 4); });
  743. Vm.EXIFRating = 4;
  744. }
  745. public static async Task Set5Star()
  746. {
  747. if (Vm is null)
  748. {
  749. return;
  750. }
  751. await Task.Run(() => { EXIFHelper.SetEXIFRating(Vm.FileInfo.FullName, 5); });
  752. Vm.EXIFRating = 5;
  753. }
  754. #endregion
  755. #region Open GPS link
  756. public static async Task OpenGoogleMaps()
  757. {
  758. if (Vm is null)
  759. {
  760. return;
  761. }
  762. if (string.IsNullOrEmpty(Vm.GoogleLink))
  763. {
  764. return;
  765. }
  766. await Task.Run(() => ProcessHelper.OpenLink(Vm.GoogleLink));
  767. }
  768. public static async Task OpenBingMaps()
  769. {
  770. if (Vm is null)
  771. {
  772. return;
  773. }
  774. if (string.IsNullOrEmpty(Vm.BingLink))
  775. {
  776. return;
  777. }
  778. await Task.Run(() => ProcessHelper.OpenLink(Vm.BingLink));
  779. }
  780. #endregion
  781. #region Wallpaper and lockscreen image
  782. public static async Task SetAsWallpaper()
  783. {
  784. if (Vm is null)
  785. {
  786. return;
  787. }
  788. if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
  789. {
  790. await SetAsWallpaperFilled();
  791. }
  792. // TODO: Add setting wallpaper support for macOS
  793. }
  794. public static async Task SetAsWallpaperTiled()
  795. {
  796. if (Vm is null)
  797. {
  798. return;
  799. }
  800. await Task.Run(() => Vm.PlatformService.SetAsWallpaper(Vm.FileInfo.FullName, 0));
  801. }
  802. public static async Task SetAsWallpaperCentered()
  803. {
  804. if (Vm is null)
  805. {
  806. return;
  807. }
  808. await Task.Run(() => Vm.PlatformService.SetAsWallpaper(Vm.FileInfo.FullName, 1));
  809. }
  810. public static async Task SetAsWallpaperStretched()
  811. {
  812. if (Vm is null)
  813. {
  814. return;
  815. }
  816. await Task.Run(() => Vm.PlatformService.SetAsWallpaper(Vm.FileInfo.FullName, 2));
  817. }
  818. public static async Task SetAsWallpaperFitted()
  819. {
  820. if (Vm is null)
  821. {
  822. return;
  823. }
  824. await Task.Run(() => Vm.PlatformService.SetAsWallpaper(Vm.FileInfo.FullName, 3));
  825. }
  826. public static async Task SetAsWallpaperFilled()
  827. {
  828. if (Vm is null)
  829. {
  830. return;
  831. }
  832. await Task.Run(() => Vm.PlatformService.SetAsWallpaper(Vm.FileInfo.FullName, 4));
  833. }
  834. public static async Task SetAsLockscreenCentered()
  835. {
  836. if (Vm is null)
  837. {
  838. return;
  839. }
  840. await Task.Run(() => Vm.PlatformService.SetAsLockScreen(Vm.FileInfo.FullName));
  841. }
  842. public static async Task SetAsLockScreen()
  843. {
  844. if (Vm is null)
  845. {
  846. return;
  847. }
  848. await Task.Run(() => Vm.PlatformService.SetAsLockScreen(Vm.FileInfo.FullName));
  849. }
  850. #endregion
  851. #region Other settings
  852. public static async Task ResetSettings()
  853. {
  854. await SettingsUpdater.ResetSettings(Vm);
  855. }
  856. public static async Task Restart()
  857. {
  858. var openFile = string.Empty;
  859. var getFromArgs = false;
  860. if (Vm?.FileInfo is not null)
  861. {
  862. if (Vm.FileInfo.Exists)
  863. {
  864. openFile = Vm.FileInfo.FullName;
  865. }
  866. else
  867. {
  868. getFromArgs = true;
  869. }
  870. }
  871. else
  872. {
  873. getFromArgs = true;
  874. }
  875. if (getFromArgs)
  876. {
  877. var args = Environment.GetCommandLineArgs();
  878. if (args is not null && args.Length > 0)
  879. {
  880. openFile = args[1];
  881. }
  882. }
  883. ProcessHelper.RestartApp(openFile);
  884. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  885. {
  886. Environment.Exit(0);
  887. return;
  888. }
  889. await Dispatcher.UIThread.InvokeAsync(() =>
  890. {
  891. desktop.MainWindow?.Close();
  892. });
  893. }
  894. public static async Task ToggleUsingTouchpad()
  895. {
  896. if (Vm is null)
  897. {
  898. return;
  899. }
  900. await SettingsUpdater.ToggleUsingTouchpad(Vm);
  901. }
  902. #endregion
  903. #endregion
  904. #if DEBUG
  905. public static async Task Invalidate()
  906. {
  907. Vm?.ImageViewer?.MainImage?.InvalidateVisual();
  908. //Vm?.ImageViewer?.InvalidateVisual();
  909. }
  910. #endif
  911. }