PreviewerWindowViewModel.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. using System.Collections.ObjectModel;
  2. using System.Diagnostics;
  3. using ReactiveUI;
  4. using Unit = System.Reactive.Unit;
  5. using Avalonia.Threading;
  6. namespace QuestPDF.Previewer
  7. {
  8. internal class PreviewerWindowViewModel : ReactiveObject
  9. {
  10. private ObservableCollection<DocumentSnapshot.PageSnapshot> _pages = new();
  11. public ObservableCollection<DocumentSnapshot.PageSnapshot> Pages
  12. {
  13. get => _pages;
  14. set => this.RaiseAndSetIfChanged(ref _pages, value);
  15. }
  16. private bool _documentContentHasLayoutOverflowIssues;
  17. public bool DocumentContentHasLayoutOverflowIssues
  18. {
  19. get => _documentContentHasLayoutOverflowIssues;
  20. set => this.RaiseAndSetIfChanged(ref _documentContentHasLayoutOverflowIssues, value);
  21. }
  22. private float _currentScroll;
  23. public float CurrentScroll
  24. {
  25. get => _currentScroll;
  26. set => this.RaiseAndSetIfChanged(ref _currentScroll, value);
  27. }
  28. private float _scrollViewportSize;
  29. public float ScrollViewportSize
  30. {
  31. get => _scrollViewportSize;
  32. set
  33. {
  34. this.RaiseAndSetIfChanged(ref _scrollViewportSize, value);
  35. VerticalScrollbarVisible = value < 1;
  36. }
  37. }
  38. private bool _verticalScrollbarVisible;
  39. public bool VerticalScrollbarVisible
  40. {
  41. get => _verticalScrollbarVisible;
  42. private set => Dispatcher.UIThread.Post(() => this.RaiseAndSetIfChanged(ref _verticalScrollbarVisible, value));
  43. }
  44. public ReactiveCommand<Unit, Unit> ShowPdfCommand { get; }
  45. public ReactiveCommand<Unit, Unit> ShowDocumentationCommand { get; }
  46. public ReactiveCommand<Unit, Unit> SponsorProjectCommand { get; }
  47. public PreviewerWindowViewModel()
  48. {
  49. CommunicationService.Instance.OnDocumentRefreshed += HandleUpdatePreview;
  50. ShowPdfCommand = ReactiveCommand.Create(ShowPdf);
  51. ShowDocumentationCommand = ReactiveCommand.Create(() => OpenLink("https://www.questpdf.com/api-reference/index.html"));
  52. SponsorProjectCommand = ReactiveCommand.Create(() => OpenLink("https://github.com/sponsors/QuestPDF"));
  53. }
  54. private void ShowPdf()
  55. {
  56. var filePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.pdf");
  57. Helpers.GeneratePdfFromDocumentSnapshots(filePath, Pages);
  58. OpenLink(filePath);
  59. }
  60. private void OpenLink(string path)
  61. {
  62. using var openBrowserProcess = new Process
  63. {
  64. StartInfo = new()
  65. {
  66. UseShellExecute = true,
  67. FileName = path
  68. }
  69. };
  70. openBrowserProcess.Start();
  71. }
  72. private void HandleUpdatePreview(DocumentSnapshot documentSnapshot)
  73. {
  74. var oldPages = Pages;
  75. Pages.Clear();
  76. Pages = new ObservableCollection<DocumentSnapshot.PageSnapshot>(documentSnapshot.Pages);
  77. DocumentContentHasLayoutOverflowIssues = documentSnapshot.DocumentContentHasLayoutOverflowIssues;
  78. foreach (var page in oldPages)
  79. page.Picture.Dispose();
  80. }
  81. }
  82. }