ValidatingDrawingContext.cs 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Reactive.Disposables;
  5. using Perspex.Media.Imaging;
  6. namespace Perspex.Media
  7. {
  8. public class ValidatingDrawingContext : IDrawingContext
  9. {
  10. private readonly IDrawingContext _base;
  11. public ValidatingDrawingContext(IDrawingContext @base)
  12. {
  13. _base = @base;
  14. }
  15. public void Dispose()
  16. {
  17. _base.Dispose();
  18. }
  19. public Matrix CurrentTransform => _base.CurrentTransform;
  20. public void DrawImage(IBitmap source, double opacity, Rect sourceRect, Rect destRect)
  21. {
  22. _base.DrawImage(source, opacity, sourceRect, destRect);
  23. }
  24. public void DrawLine(Pen pen, Point p1, Point p2)
  25. {
  26. _base.DrawLine(pen, p1, p2);
  27. }
  28. public void DrawGeometry(Brush brush, Pen pen, Geometry geometry)
  29. {
  30. _base.DrawGeometry(brush, pen, geometry);
  31. }
  32. public void DrawRectangle(Pen pen, Rect rect, float cornerRadius = 0)
  33. {
  34. _base.DrawRectangle(pen, rect, cornerRadius);
  35. }
  36. public void DrawText(Brush foreground, Point origin, FormattedText text)
  37. {
  38. _base.DrawText(foreground, origin, text);
  39. }
  40. public void FillRectangle(Brush brush, Rect rect, float cornerRadius = 0)
  41. {
  42. _base.FillRectangle(brush, rect, cornerRadius);
  43. }
  44. Stack<IDisposable> _stateStack = new Stack<IDisposable>();
  45. IDisposable Transform(IDisposable disposable)
  46. {
  47. _stateStack.Push(disposable);
  48. return Disposable.Create(() =>
  49. {
  50. var current = _stateStack.Peek();
  51. if (current != disposable)
  52. throw new InvalidOperationException("Invalid push/pop order");
  53. current.Dispose();
  54. _stateStack.Pop();
  55. });
  56. }
  57. public IDisposable PushClip(Rect clip) => Transform(_base.PushClip(clip));
  58. public IDisposable PushOpacity(double opacity) => Transform(_base.PushOpacity(opacity));
  59. public IDisposable PushTransform(Matrix matrix) => Transform(_base.PushTransform(matrix));
  60. }
  61. }