OrientationHelper.cs 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. namespace Terminal.Gui;
  2. /// <summary>
  3. /// Helper class for implementing <see cref="IOrientation"/>.
  4. /// </summary>
  5. public class OrientationHelper
  6. {
  7. private Orientation _orientation = Orientation.Vertical;
  8. private readonly IOrientation _owner;
  9. /// <summary>
  10. /// Initializes a new instance of the <see cref="OrientationHelper"/> class.
  11. /// </summary>
  12. /// <param name="owner"></param>
  13. public OrientationHelper (IOrientation owner)
  14. {
  15. _owner = owner;
  16. }
  17. /// <summary>
  18. /// Gets or sets the orientation of the View.
  19. /// </summary>
  20. public Orientation Orientation
  21. {
  22. get => _orientation;
  23. set
  24. {
  25. if (_orientation == value)
  26. {
  27. return;
  28. }
  29. var args = new CancelEventArgs<Orientation> (in _orientation, ref value);
  30. OrientationChanging?.Invoke (_owner, args);
  31. if (args.Cancel)
  32. {
  33. return;
  34. }
  35. if (_owner?.OnOrientationChanging (value, _orientation) ?? false)
  36. {
  37. return;
  38. }
  39. Orientation old = _orientation;
  40. if (_orientation != value)
  41. {
  42. _orientation = value;
  43. if (_owner is { })
  44. {
  45. _owner.Orientation = value;
  46. }
  47. }
  48. args = new CancelEventArgs<Orientation> (in old, ref _orientation);
  49. OrientationChanged?.Invoke (_owner, args);
  50. _owner?.OnOrientationChanged (old, _orientation);
  51. }
  52. }
  53. /// <summary>
  54. ///
  55. /// </summary>
  56. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
  57. /// <summary>
  58. ///
  59. /// </summary>
  60. /// <param name="currentOrientation"></param>
  61. /// <param name="newOrientation"></param>
  62. /// <returns></returns>
  63. protected bool OnOrientationChanging (Orientation currentOrientation, Orientation newOrientation)
  64. {
  65. return _owner?.OnOrientationChanging (currentOrientation, newOrientation) ?? false;
  66. }
  67. /// <summary>
  68. ///
  69. /// </summary>
  70. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanged;
  71. /// <summary>
  72. ///
  73. /// </summary>
  74. /// <param name="oldOrientation"></param>
  75. /// <param name="newOrientation"></param>
  76. /// <returns></returns>
  77. protected void OnOrientationChanged (Orientation oldOrientation, Orientation newOrientation)
  78. {
  79. _owner?.OnOrientationChanged (oldOrientation, newOrientation);
  80. }
  81. }