Skip to content

Commit

Permalink
Merge pull request gui-cs#3571 from tig/v2_3570-TextView-ENTER
Browse files Browse the repository at this point in the history
Fixes gui-cs#3570 & gui-cs#3561. Fixes a bunch of `EventArgs` sins
  • Loading branch information
tig authored Jul 3, 2024
2 parents 13cbdab + 710bd7f commit a6bd533
Show file tree
Hide file tree
Showing 86 changed files with 1,603 additions and 2,067 deletions.
10 changes: 6 additions & 4 deletions Terminal.Gui/Application/Application.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ internal static void InternalInit (
SupportedCultures = GetSupportedCultures ();
_mainThreadId = Thread.CurrentThread.ManagedThreadId;
_initialized = true;
InitializedChanged?.Invoke (null, new (false, _initialized));
InitializedChanged?.Invoke (null, new (in _initialized));
}

private static void Driver_SizeChanged (object sender, SizeChangedEventArgs e) { OnSizeChanging (e); }
Expand Down Expand Up @@ -349,16 +349,18 @@ public static void Shutdown ()
// TODO: Throw an exception if Init hasn't been called.
ResetState ();
PrintJsonErrors ();
InitializedChanged?.Invoke (null, new (true, _initialized));
InitializedChanged?.Invoke (null, new (in _initialized));
}

#nullable enable
/// <summary>
/// This event is fired after the <see cref="Init"/> and <see cref="Shutdown"/> methods have been called.
/// This event is raised after the <see cref="Init"/> and <see cref="Shutdown"/> methods have been called.
/// </summary>
/// <remarks>
/// Intended to support unit tests that need to know when the application has been initialized.
/// </remarks>
public static event EventHandler<StateEventArgs<bool>> InitializedChanged;
public static event EventHandler<EventArgs<bool>>? InitializedChanged;
#nullable restore

#endregion Initialization (Init/Shutdown)

Expand Down
6 changes: 3 additions & 3 deletions Terminal.Gui/Drawing/Glyphs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ public class GlyphDefinitions
#region ----------------- Single Glyphs -----------------

/// <summary>Checked indicator (e.g. for <see cref="ListView"/> and <see cref="CheckBox"/>).</summary>
public Rune Checked { get; set; } = (Rune)'☑';
public Rune CheckStateChecked { get; set; } = (Rune)'☑';

/// <summary>Not Checked indicator (e.g. for <see cref="ListView"/> and <see cref="CheckBox"/>).</summary>
public Rune UnChecked { get; set; } = (Rune)'☐';
public Rune CheckStateUnChecked { get; set; } = (Rune)'☐';

/// <summary>Null Checked indicator (e.g. for <see cref="ListView"/> and <see cref="CheckBox"/>).</summary>
public Rune NullChecked { get; set; } = (Rune)'☒';
public Rune CheckStateNone { get; set; } = (Rune)'☒';

/// <summary>Selected indicator (e.g. for <see cref="ListView"/> and <see cref="RadioGroup"/>).</summary>
public Rune Selected { get; set; } = (Rune)'◉';
Expand Down
16 changes: 0 additions & 16 deletions Terminal.Gui/Drawing/ThicknessEventArgs.cs

This file was deleted.

26 changes: 0 additions & 26 deletions Terminal.Gui/Text/StringEventArgs.cs

This file was deleted.

26 changes: 13 additions & 13 deletions Terminal.Gui/View/Adornment/Adornment.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Terminal.Gui;
#nullable enable
using Terminal.Gui;
using Attribute = Terminal.Gui.Attribute;

/// <summary>
/// Adornments are a special form of <see cref="View"/> that appear outside the <see cref="View.Viewport"/>:
Expand Down Expand Up @@ -33,7 +35,7 @@ public Adornment (View parent)
/// Adornments are distinguished from typical View classes in that they are not sub-views, but have a parent/child
/// relationship with their containing View.
/// </remarks>
public View Parent { get; set; }
public View? Parent { get; set; }

#region Thickness

Expand All @@ -45,10 +47,10 @@ public Thickness Thickness
get => _thickness;
set
{
Thickness prev = _thickness;
Thickness current = _thickness;
_thickness = value;

if (prev != _thickness)
if (current != _thickness)
{
if (Parent?.IsInitialized == false)
{
Expand All @@ -61,21 +63,19 @@ public Thickness Thickness
Parent?.LayoutSubviews ();
}

OnThicknessChanged (prev);
OnThicknessChanged ();
}
}
}

/// <summary>Fired whenever the <see cref="Thickness"/> property changes.</summary>
public event EventHandler<ThicknessEventArgs> ThicknessChanged;
[CanBeNull]
public event EventHandler? ThicknessChanged;

/// <summary>Called whenever the <see cref="Thickness"/> property changes.</summary>
public void OnThicknessChanged (Thickness previousThickness)
public void OnThicknessChanged ()
{
ThicknessChanged?.Invoke (
this,
new () { Thickness = Thickness, PreviousThickness = previousThickness }
);
ThicknessChanged?.Invoke (this, EventArgs.Empty);
}

#endregion Thickness
Expand All @@ -88,7 +88,7 @@ public void OnThicknessChanged (Thickness previousThickness)
/// </summary>
public override View SuperView
{
get => null;
get => null!;
set => throw new InvalidOperationException (@"Adornments can not be Subviews or have SuperViews. Use Parent instead.");
}

Expand Down Expand Up @@ -137,7 +137,7 @@ public override Rectangle FrameToScreen ()
/// <inheritdoc/>
public override Point ScreenToFrame (in Point location)
{
return Parent.ScreenToFrame (new (location.X - Frame.X, location.Y - Frame.Y));
return Parent!.ScreenToFrame (new (location.X - Frame.X, location.Y - Frame.Y));
}

/// <summary>Does nothing for Adornment</summary>
Expand Down
7 changes: 4 additions & 3 deletions Terminal.Gui/View/Adornment/Border.cs
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,15 @@ public bool ShowTitle

private Color? _savedForeColor;

private void Border_Highlight (object sender, HighlightEventArgs e)
private void Border_Highlight (object sender, CancelEventArgs<HighlightStyle> e)
{
if (!Parent.Arrangement.HasFlag (ViewArrangement.Movable))
{
e.Cancel = true;
return;
}

if (e.HighlightStyle.HasFlag (HighlightStyle.Pressed))
if (e.NewValue.HasFlag (HighlightStyle.Pressed))
{
if (!_savedForeColor.HasValue)
{
Expand All @@ -252,7 +252,7 @@ private void Border_Highlight (object sender, HighlightEventArgs e)
}
#endif

if (e.HighlightStyle == HighlightStyle.None && _savedForeColor.HasValue)
if (e.NewValue == HighlightStyle.None && _savedForeColor.HasValue)
{
ColorScheme cs = new ColorScheme (ColorScheme)
{
Expand Down Expand Up @@ -299,6 +299,7 @@ protected internal override bool OnMouseEvent (MouseEvent mouseEvent)
_startGrabPoint = new (mouseEvent.Position.X + Frame.X, mouseEvent.Position.Y + Frame.Y);
_dragPosition = mouseEvent.Position;
Application.GrabMouse (this);

SetHighlight (HighlightStyle);
}

Expand Down
6 changes: 3 additions & 3 deletions Terminal.Gui/View/Adornment/Margin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ private void Margin_LayoutStarted (object? sender, LayoutEventArgs e)
}

private bool _pressed;
private void Margin_Highlight (object? sender, HighlightEventArgs e)
private void Margin_Highlight (object? sender, CancelEventArgs<HighlightStyle> e)
{
if (ShadowStyle != Gui.ShadowStyle.None)
{
if (_pressed && e.HighlightStyle == HighlightStyle.None)
if (_pressed && e.CurrentValue == HighlightStyle.None)
{
Thickness = new (Thickness.Left - 1, Thickness.Top, Thickness.Right + 1, Thickness.Bottom);

Expand All @@ -61,7 +61,7 @@ private void Margin_Highlight (object? sender, HighlightEventArgs e)
return;
}

if (!_pressed && (e.HighlightStyle.HasFlag (HighlightStyle.Pressed) /*|| e.HighlightStyle.HasFlag (HighlightStyle.PressedOutside)*/))
if (!_pressed && (e.CurrentValue.HasFlag (HighlightStyle.Pressed) /*|| e.HighlightStyle.HasFlag (HighlightStyle.PressedOutside)*/))
{
Thickness = new (Thickness.Left + 1, Thickness.Top, Thickness.Right - 1, Thickness.Bottom);
_pressed = true;
Expand Down
35 changes: 35 additions & 0 deletions Terminal.Gui/View/CancelEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#nullable enable
using System.ComponentModel;

namespace Terminal.Gui;

#pragma warning disable CS1711

/// <summary>
/// <see cref="EventArgs"/> for events that convey changes to a property of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of the value that was part of the change being canceled.</typeparam>
/// <remarks>
/// Events that use this class can be cancellable. Where applicable, the <see cref="CancelEventArgs.Cancel"/> property
/// should be set to
/// <see langword="true"/> to prevent the state change from occurring.
/// </remarks>
public class CancelEventArgs<T> : CancelEventArgs where T : notnull
{
/// <summary>Initializes a new instance of the <see cref="CancelEventArgs{T}"/> class.</summary>
/// <param name="currentValue">The current (old) value of the property.</param>
/// <param name="newValue">The value the property will be set to if the event is not cancelled.</param>
/// <param name="cancel">Whether the event should be canceled or not.</param>
/// <typeparam name="T">The type of the value for the change being canceled.</typeparam>
public CancelEventArgs (ref readonly T currentValue, ref T newValue, bool cancel = false) : base (cancel)
{
CurrentValue = currentValue;
NewValue = newValue;
}

/// <summary>The current value of the property.</summary>
public T CurrentValue { get; }

/// <summary>The value the property will be set to if the event is not cancelled.</summary>
public T NewValue { get; set; }
}
18 changes: 18 additions & 0 deletions Terminal.Gui/View/EventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#nullable enable
namespace Terminal.Gui;

#pragma warning disable CS1711
/// <summary>
/// <see cref="EventArgs"/> for events that convey changes to a property of type <typeparamref name="T"/>.
/// </summary>
/// <typeparam name="T">The type of the value that was part of the change being canceled.</typeparam>
public class EventArgs<T> : EventArgs where T : notnull
{
/// <summary>Initializes a new instance of the <see cref="EventArgs{T}"/> class.</summary>
/// <param name="currentValue">The current value of the property.</param>
/// <typeparam name="T">The type of the value.</typeparam>
public EventArgs (ref readonly T currentValue) { CurrentValue = currentValue; }

/// <summary>The current value of the property.</summary>
public T CurrentValue { get; }
}
30 changes: 30 additions & 0 deletions Terminal.Gui/View/HighlightStyle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
namespace Terminal.Gui;

/// <summary>
/// Describes the highlight style of a view.
/// </summary>
[Flags]
public enum HighlightStyle
{
/// <summary>
/// No highlight.
/// </summary>
None = 0,

#if HOVER
/// <summary>
/// The mouse is hovering over the view.
/// </summary>
Hover = 1,
#endif

/// <summary>
/// The mouse is pressed within the <see cref="View.Viewport"/>.
/// </summary>
Pressed = 2,

/// <summary>
/// The mouse is pressed but moved outside the <see cref="View.Viewport"/>.
/// </summary>
PressedOutside = 4
}
27 changes: 0 additions & 27 deletions Terminal.Gui/View/StateEventArgs.cs

This file was deleted.

Loading

0 comments on commit a6bd533

Please sign in to comment.