Browse Source

One class per file

Tig 1 year ago
parent
commit
329cd2f06c
1 changed files with 94 additions and 0 deletions
  1. 94 0
      Terminal.Gui/View/Orientation/OrientationHelper.cs

+ 94 - 0
Terminal.Gui/View/Orientation/OrientationHelper.cs

@@ -0,0 +1,94 @@
+namespace Terminal.Gui;
+
+/// <summary>
+///     Helper class for implementing <see cref="IOrientation"/>.
+/// </summary>
+public class OrientationHelper
+{
+    private Orientation _orientation = Orientation.Vertical;
+    private readonly IOrientation _owner;
+
+    /// <summary>
+    ///     Initializes a new instance of the <see cref="OrientationHelper"/> class.
+    /// </summary>
+    /// <param name="owner"></param>
+    public OrientationHelper (IOrientation owner)
+    {
+        _owner = owner;
+    }
+
+    /// <summary>
+    ///     Gets or sets the orientation of the View.
+    /// </summary>
+    public Orientation Orientation
+    {
+        get => _orientation;
+        set
+        {
+            if (_orientation == value)
+            {
+                return;
+            }
+
+            var args = new CancelEventArgs<Orientation> (in _orientation, ref value);
+            OrientationChanging?.Invoke (_owner, args);
+            if (args.Cancel)
+            {
+                return;
+            }
+
+            if (_owner?.OnOrientationChanging (value, _orientation) ?? false)
+            {
+                return;
+            }
+
+            Orientation old = _orientation;
+            if (_orientation != value)
+            {
+                _orientation = value;
+
+                if (_owner is { })
+                {
+                    _owner.Orientation = value;
+                }
+            }
+
+            args = new CancelEventArgs<Orientation> (in old, ref _orientation);
+            OrientationChanged?.Invoke (_owner, args);
+
+            _owner?.OnOrientationChanged (old, _orientation);
+        }
+    }
+
+    /// <summary>
+    /// 
+    /// </summary>
+    public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
+
+    /// <summary>
+    /// 
+    /// </summary>
+    /// <param name="currentOrientation"></param>
+    /// <param name="newOrientation"></param>
+    /// <returns></returns>
+    protected bool OnOrientationChanging (Orientation currentOrientation, Orientation newOrientation)
+    {
+        return _owner?.OnOrientationChanging (currentOrientation, newOrientation) ?? false;
+    }
+
+    /// <summary>
+    /// 
+    /// </summary>
+    public event EventHandler<CancelEventArgs<Orientation>> OrientationChanged;
+
+    /// <summary>
+    /// 
+    /// </summary>
+    /// <param name="oldOrientation"></param>
+    /// <param name="newOrientation"></param>
+    /// <returns></returns>
+    protected void OnOrientationChanged (Orientation oldOrientation, Orientation newOrientation)
+    {
+        _owner?.OnOrientationChanged (oldOrientation, newOrientation);
+    }
+}