FunctionsHelper.cs 25 KB

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