PreviewerWindowViewModel.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using QuestPDF.Fluent;
  2. using System.Diagnostics;
  3. using ReactiveUI;
  4. using QuestPDF.Infrastructure;
  5. using Unit = System.Reactive.Unit;
  6. using Avalonia.Threading;
  7. namespace QuestPDF.Previewer
  8. {
  9. internal class PreviewerWindowViewModel : ReactiveObject
  10. {
  11. public DocumentRenderer DocumentRenderer { get; } = new();
  12. private IDocument? _document;
  13. public IDocument? Document
  14. {
  15. get => _document;
  16. set
  17. {
  18. this.RaiseAndSetIfChanged(ref _document, value);
  19. UpdateDocument(value);
  20. }
  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. HotReloadManager.UpdateApplicationRequested += InvalidateDocument;
  50. ShowPdfCommand = ReactiveCommand.Create(ShowPdf);
  51. ShowDocumentationCommand = ReactiveCommand.Create(() => OpenLink("https://www.questpdf.com/documentation/api-reference.html"));
  52. SponsorProjectCommand = ReactiveCommand.Create(() => OpenLink("https://github.com/sponsors/QuestPDF"));
  53. }
  54. public void UnregisterHotReloadHandler()
  55. {
  56. HotReloadManager.UpdateApplicationRequested -= InvalidateDocument;
  57. }
  58. private void InvalidateDocument(object? sender, EventArgs e)
  59. {
  60. UpdateDocument(Document);
  61. }
  62. private Task UpdateDocument(IDocument? document)
  63. {
  64. return Task.Run(() => DocumentRenderer.UpdateDocument(document));
  65. }
  66. private void ShowPdf()
  67. {
  68. var filePath = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid():N}.pdf");
  69. try
  70. {
  71. Document?.GeneratePdf(filePath);
  72. }
  73. catch (Exception exception)
  74. {
  75. new ExceptionDocument(exception).GeneratePdf(filePath);
  76. }
  77. OpenLink(filePath);
  78. }
  79. private void OpenLink(string path)
  80. {
  81. var openBrowserProcess = new Process
  82. {
  83. StartInfo = new()
  84. {
  85. UseShellExecute = true,
  86. FileName = path
  87. }
  88. };
  89. openBrowserProcess.Start();
  90. }
  91. }
  92. }