Initial commit. Added interface. Testing with RadioGroup

This commit is contained in:
Tig
2024-07-22 10:58:25 -06:00
parent e8644f3948
commit 43ed4b59eb
6 changed files with 202 additions and 66 deletions

View File

@@ -0,0 +1,127 @@

namespace Terminal.Gui;
using System;
/// <summary>
/// Implement this interface to provide orientation support.
/// </summary>
public interface IOrientation
{
/// <summary>
/// Gets or sets the orientation of the View.
/// </summary>
Orientation Orientation { get; set; }
/// <summary>
/// Raised when <see cref="Orientation"/> is changing. Can be cancelled.
/// </summary>
public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
/// <summary>
/// Called when <see cref="Orientation"/> is changing.
/// </summary>
/// <param name="currentOrientation">The current orienation.</param>
/// <param name="newOrientation">The new orienation.</param>
/// <returns><see langword="true"/> to cancel the change.</returns>
public bool OnOrientationChanging (Orientation currentOrientation, Orientation newOrientation) { return false; }
/// <summary>
///
/// </summary>
public event EventHandler<CancelEventArgs<Orientation>> OrientationChanged;
/// <summary>
/// Called when <see cref="Orientation"/> has been changed.
/// </summary>
/// <param name="oldOrientation"></param>
/// <param name="newOrientation"></param>
/// <returns></returns>
public void OnOrientationChanged (Orientation oldOrientation, Orientation newOrientation) { return; }
}
/// <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
{
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;
_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);
}
}

View File

@@ -1,19 +0,0 @@
namespace Terminal.Gui;
/// <summary><see cref="EventArgs"/> for <see cref="Orientation"/> events.</summary>
public class OrientationEventArgs : EventArgs
{
/// <summary>Constructs a new instance.</summary>
/// <param name="orientation">the new orientation</param>
public OrientationEventArgs (Orientation orientation)
{
Orientation = orientation;
Cancel = false;
}
/// <summary>If set to true, the orientation change operation will be canceled, if applicable.</summary>
public bool Cancel { get; set; }
/// <summary>The new orientation.</summary>
public Orientation Orientation { get; set; }
}

View File

@@ -1,14 +1,14 @@
namespace Terminal.Gui;
/// <summary>Displays a group of labels each with a selected indicator. Only one of those can be selected at a given time.</summary>
public class RadioGroup : View, IDesignable
public class RadioGroup : View, IDesignable, IOrientation
{
private int _cursor;
private List<(int pos, int length)> _horizontal;
private int _horizontalSpace = 2;
private Orientation _orientation = Orientation.Vertical;
private List<string> _radioLabels = [];
private int _selected;
private readonly OrientationHelper _orientationHelper;
/// <summary>
/// Initializes a new instance of the <see cref="RadioGroup"/> class.
@@ -103,6 +103,13 @@ public class RadioGroup : View, IDesignable
return true;
});
_orientationHelper = new OrientationHelper (this);
_orientationHelper.OrientationChanging += (sender, e) => OrientationChanging?.Invoke (this, e);
_orientationHelper.OrientationChanged += (sender, e) => OrientationChanged?.Invoke (this, e);
//OrientationChanging += (sender, e) => OnOrientationChanging (e.CurrentValue, e.NewValue);
//OrientationChanged += (sender, e) => OnOrientationChanged (e.CurrentValue, e.NewValue);
SetupKeyBindings ();
LayoutStarted += RadioGroup_LayoutStarted;
@@ -142,15 +149,15 @@ public class RadioGroup : View, IDesignable
int viewportX = e.MouseEvent.Position.X;
int viewportY = e.MouseEvent.Position.Y;
int pos = _orientation == Orientation.Horizontal ? viewportX : viewportY;
int pos = Orientation == Orientation.Horizontal ? viewportX : viewportY;
int rCount = _orientation == Orientation.Horizontal
int rCount = Orientation == Orientation.Horizontal
? _horizontal.Last ().pos + _horizontal.Last ().length
: _radioLabels.Count;
if (pos < rCount)
{
int c = _orientation == Orientation.Horizontal
int c = Orientation == Orientation.Horizontal
? _horizontal.FindIndex (x => x.pos <= viewportX && x.pos + x.length - 2 >= viewportX)
: viewportY;
@@ -173,7 +180,7 @@ public class RadioGroup : View, IDesignable
get => _horizontalSpace;
set
{
if (_horizontalSpace != value && _orientation == Orientation.Horizontal)
if (_horizontalSpace != value && Orientation == Orientation.Horizontal)
{
_horizontalSpace = value;
UpdateTextFormatterText ();
@@ -182,16 +189,6 @@ public class RadioGroup : View, IDesignable
}
}
/// <summary>
/// Gets or sets the <see cref="Orientation"/> for this <see cref="RadioGroup"/>. The default is
/// <see cref="Orientation.Vertical"/>.
/// </summary>
public Orientation Orientation
{
get => _orientation;
set => OnOrientationChanged (value);
}
/// <summary>
/// The radio labels to display. A key binding will be added for each radio enabling the user to select
/// and/or focus the radio label using the keyboard. See <see cref="View.HotKey"/> for details on how HotKeys work.
@@ -323,30 +320,45 @@ public class RadioGroup : View, IDesignable
}
}
/// <summary>Called when the view orientation has changed. Invokes the <see cref="OrientationChanged"/> event.</summary>
/// <param name="newOrientation"></param>
/// <returns>True of the event was cancelled.</returns>
public virtual bool OnOrientationChanged (Orientation newOrientation)
/// <summary>
/// Gets or sets the <see cref="Orientation"/> for this <see cref="RadioGroup"/>. The default is
/// <see cref="Orientation.Vertical"/>.
/// </summary>
public Orientation Orientation
{
var args = new OrientationEventArgs (newOrientation);
OrientationChanged?.Invoke (this, args);
if (!args.Cancel)
{
_orientation = newOrientation;
SetupKeyBindings ();
SetContentSize ();
}
return args.Cancel;
get => _orientationHelper.Orientation;
set => _orientationHelper.Orientation = value;
}
#region IOrientation
/// <inheritdoc />
public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
/// <inheritdoc />
public bool OnOrientationChanging (Orientation currentOrientation, Orientation newOrientation)
{
return false;
}
/// <inheritdoc />
public event EventHandler<CancelEventArgs<Orientation>> OrientationChanged;
/// <summary>Called when <see cref="Orientation"/> has changed.</summary>
/// <param name="oldOrientation"></param>
/// <param name="newOrientation"></param>
public void OnOrientationChanged (Orientation oldOrientation, Orientation newOrientation)
{
SetupKeyBindings ();
SetContentSize ();
}
#endregion IOrientation
// TODO: This should be cancelable
/// <summary>Called whenever the current selected item changes. Invokes the <see cref="SelectedItemChanged"/> event.</summary>
/// <param name="selectedItem"></param>
/// <param name="previousSelectedItem"></param>
public virtual void OnSelectedItemChanged (int selectedItem, int previousSelectedItem)
{
{
if (_selected == selectedItem)
{
return;
@@ -355,12 +367,6 @@ public class RadioGroup : View, IDesignable
SelectedItemChanged?.Invoke (this, new (selectedItem, previousSelectedItem));
}
/// <summary>
/// Fired when the view orientation has changed. Can be cancelled by setting
/// <see cref="OrientationEventArgs.Cancel"/> to true.
/// </summary>
public event EventHandler<OrientationEventArgs> OrientationChanged;
/// <inheritdoc/>
public override Point? PositionCursor ()
{
@@ -429,7 +435,7 @@ public class RadioGroup : View, IDesignable
private void SetContentSize ()
{
switch (_orientation)
switch (Orientation)
{
case Orientation.Vertical:
var width = 0;
@@ -469,3 +475,27 @@ public class RadioGroup : View, IDesignable
return true;
}
}
public class RadioGroupHorizontal : RadioGroup, IOrientation
{
private bool _preventOrientationChange = false;
public RadioGroupHorizontal () : base ()
{
Orientation = Orientation.Horizontal;
_preventOrientationChange = true;
OrientationChanging += RadioGroupHorizontal_OrientationChanging;
}
private void RadioGroupHorizontal_OrientationChanging (object sender, CancelEventArgs<Orientation> e)
{
//e.Cancel = _preventOrientationChange;
}
/// <inheritdoc />
bool IOrientation.OnOrientationChanging (Orientation currrentOrientation, Orientation newOrientation)
{
return _preventOrientationChange;
}
}

View File

@@ -310,17 +310,16 @@ public class Slider<T> : View
#region Events
/// <summary>
/// Fired when the slider orientation has changed. Can be cancelled by setting
/// <see cref="OrientationEventArgs.Cancel"/> to true.
/// Fired when the slider orientation has changed. Can be cancelled.
/// </summary>
public event EventHandler<OrientationEventArgs> OrientationChanged;
public event EventHandler<CancelEventArgs<Orientation>> OrientationChanged;
/// <summary>Called when the slider orientation has changed. Invokes the <see cref="OrientationChanged"/> event.</summary>
/// <param name="newOrientation"></param>
/// <returns>True of the event was cancelled.</returns>
public virtual bool OnOrientationChanged (Orientation newOrientation)
{
var args = new OrientationEventArgs (newOrientation);
var args = new CancelEventArgs<Orientation> (in _config._sliderOrientation, ref newOrientation);
OrientationChanged?.Invoke (this, args);
if (!args.Cancel)

View File

@@ -74,7 +74,7 @@ public class ExpanderButton : Button
/// <returns>True of the event was cancelled.</returns>
protected virtual bool OnOrientationChanging (Orientation newOrientation)
{
var args = new OrientationEventArgs (newOrientation);
var args = new CancelEventArgs<Orientation> (in _orientation, ref newOrientation);
OrientationChanging?.Invoke (this, args);
if (!args.Cancel)
@@ -105,10 +105,9 @@ public class ExpanderButton : Button
}
/// <summary>
/// Fired when the orientation has changed. Can be cancelled by setting
/// <see cref="OrientationEventArgs.Cancel"/> to true.
/// Fired when the orientation has changed. Can be cancelled.
/// </summary>
public event EventHandler<OrientationEventArgs> OrientationChanging;
public event EventHandler<CancelEventArgs<Orientation>> OrientationChanging;
/// <summary>
/// The glyph to display when the view is collapsed.