FunctionsHelper.cs 25 KB

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