IOrientation.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. 
  2. namespace Terminal.Gui;
  3. using System;
  4. /// <summary>
  5. /// Implement this interface to provide orientation support.
  6. /// </summary>
  7. public interface IOrientation
  8. {
  9. /// <summary>
  10. /// Gets or sets the orientation of the View.
  11. /// </summary>
  12. Orientation Orientation { get; set; }
  13. /// <summary>
  14. /// Raised when <see cref="Orientation"/> is changing. Can be cancelled.
  15. /// </summary>
  16. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
  17. /// <summary>
  18. /// Called when <see cref="Orientation"/> is changing.
  19. /// </summary>
  20. /// <param name="currentOrientation">The current orienation.</param>
  21. /// <param name="newOrientation">The new orienation.</param>
  22. /// <returns><see langword="true"/> to cancel the change.</returns>
  23. public bool OnOrientationChanging (Orientation currentOrientation, Orientation newOrientation) { return false; }
  24. /// <summary>
  25. ///
  26. /// </summary>
  27. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanged;
  28. /// <summary>
  29. /// Called when <see cref="Orientation"/> has been changed.
  30. /// </summary>
  31. /// <param name="oldOrientation"></param>
  32. /// <param name="newOrientation"></param>
  33. /// <returns></returns>
  34. public void OnOrientationChanged (Orientation oldOrientation, Orientation newOrientation) { return; }
  35. }
  36. /// <summary>
  37. /// Helper class for implementing <see cref="IOrientation"/>.
  38. /// </summary>
  39. public class OrientationHelper
  40. {
  41. private Orientation _orientation = Orientation.Vertical;
  42. private readonly IOrientation _owner;
  43. /// <summary>
  44. /// Initializes a new instance of the <see cref="OrientationHelper"/> class.
  45. /// </summary>
  46. /// <param name="owner"></param>
  47. public OrientationHelper (IOrientation owner)
  48. {
  49. _owner = owner;
  50. }
  51. /// <summary>
  52. /// Gets or sets the orientation of the View.
  53. /// </summary>
  54. public Orientation Orientation
  55. {
  56. get => _orientation;
  57. set
  58. {
  59. var args = new CancelEventArgs<Orientation> (in _orientation, ref value);
  60. OrientationChanging?.Invoke (_owner, args);
  61. if (args.Cancel)
  62. {
  63. return;
  64. }
  65. if (_owner?.OnOrientationChanging (value, _orientation) ?? false)
  66. {
  67. return;
  68. }
  69. Orientation old = _orientation;
  70. if (_orientation != value)
  71. {
  72. _orientation = value;
  73. _owner.Orientation = value;
  74. }
  75. args = new CancelEventArgs<Orientation> (in old, ref _orientation);
  76. OrientationChanged?.Invoke (_owner, args);
  77. _owner?.OnOrientationChanged (old, _orientation);
  78. }
  79. }
  80. /// <summary>
  81. ///
  82. /// </summary>
  83. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
  84. /// <summary>
  85. ///
  86. /// </summary>
  87. /// <param name="currentOrientation"></param>
  88. /// <param name="newOrientation"></param>
  89. /// <returns></returns>
  90. protected bool OnOrientationChanging (Orientation currentOrientation, Orientation newOrientation)
  91. {
  92. return _owner?.OnOrientationChanging (currentOrientation, newOrientation) ?? false;
  93. }
  94. /// <summary>
  95. ///
  96. /// </summary>
  97. public event EventHandler<CancelEventArgs<Orientation>> OrientationChanged;
  98. /// <summary>
  99. ///
  100. /// </summary>
  101. /// <param name="oldOrientation"></param>
  102. /// <param name="newOrientation"></param>
  103. /// <returns></returns>
  104. protected void OnOrientationChanged (Orientation oldOrientation, Orientation newOrientation)
  105. {
  106. _owner?.OnOrientationChanged (oldOrientation, newOrientation);
  107. }
  108. }