Got Hover working for all Views (Button) and Border. Still a bit of a prototype.

This commit is contained in:
Tig
2024-04-06 09:09:42 -04:00
parent 40ae272944
commit ac667dae2c
4 changed files with 69 additions and 18 deletions

View File

@@ -212,7 +212,7 @@ public class Adornment : View
}
/// <inheritdoc/>
protected internal override bool OnMouseEnter (MouseEvent mouseEvent)
protected internal override bool? OnMouseEnter (MouseEvent mouseEvent)
{
// Invert Normal
if (Diagnostics.HasFlag (ViewDiagnosticFlags.MouseEnter) && ColorScheme != null)

View File

@@ -57,7 +57,7 @@ public class Border : Adornment
Application.GrabbingMouse += Application_GrabbingMouse;
Application.UnGrabbingMouse += Application_UnGrabbingMouse;
HighlightStyle = HighlightStyle.Pressed;
HighlightStyle |= HighlightStyle.Pressed;
Highlight += Border_Highlight;
}
@@ -73,6 +73,12 @@ public class Border : Adornment
/// <inheritdoc/>
public override void BeginInit ()
{
// TOOD: Hack - make Arragnement overidable
if ((Parent?.Arrangement & ViewArrangement.Movable) != 0)
{
HighlightStyle |= HighlightStyle.Hover;
}
base.BeginInit ();
#if SUBVIEW_BASED_BORDER
@@ -100,7 +106,7 @@ public class Border : Adornment
LayoutStarted += OnLayoutStarted;
}
#endif
}
}
#if SUBVIEW_BASED_BORDER
private void OnLayoutStarted (object sender, LayoutEventArgs e)
@@ -190,18 +196,30 @@ public class Border : Adornment
#region Mouse Support
private LineStyle _savedHighlightLineStyle;
private LineStyle? _savedHighlightLineStyle;
private void Border_Highlight (object sender, HighlightEventArgs e)
{
if (e.HighlightStyle == HighlightStyle)
if (e.HighlightStyle.HasFlag (HighlightStyle.Pressed))
{
_savedHighlightLineStyle = Parent?.BorderStyle ?? LineStyle;
if (!_savedHighlightLineStyle.HasValue)
{
_savedHighlightLineStyle = Parent?.BorderStyle ?? LineStyle;
}
LineStyle = LineStyle.Heavy;
}
else
else if (e.HighlightStyle.HasFlag (HighlightStyle.Hover))
{
LineStyle = _savedHighlightLineStyle;
if (!_savedHighlightLineStyle.HasValue)
{
_savedHighlightLineStyle = Parent?.BorderStyle ?? LineStyle;
}
LineStyle = LineStyle.Double;
}
if (e.HighlightStyle == HighlightStyle.None && _savedHighlightLineStyle.HasValue)
{
LineStyle = _savedHighlightLineStyle.Value;
}
Parent?.SetNeedsDisplay ();
e.Cancel = true;
@@ -321,7 +339,7 @@ public class Border : Adornment
}
}
#endregion Mouse Support
#endregion Mouse Support
/// <inheritdoc/>
public override void OnDrawContent (Rectangle contentArea)

View File

@@ -90,7 +90,17 @@ public partial class View
return false;
}
return OnMouseEnter (mouseEvent);
if (OnMouseEnter (mouseEvent) == true)
{
return true;
}
if (HighlightStyle.HasFlag(HighlightStyle.Hover))
{
SetHighlight (HighlightStyle.Hover);
}
return false;
}
/// <summary>
@@ -108,7 +118,7 @@ public partial class View
/// </remarks>
/// <param name="mouseEvent"></param>
/// <returns><see langword="true"/>, if the event was handled, <see langword="false"/> otherwise.</returns>
protected internal virtual bool OnMouseEnter (MouseEvent mouseEvent)
protected internal virtual bool? OnMouseEnter (MouseEvent mouseEvent)
{
var args = new MouseEventEventArgs (mouseEvent);
@@ -150,7 +160,17 @@ public partial class View
return false;
}
return OnMouseLeave (mouseEvent);
if (OnMouseEnter (mouseEvent) == true)
{
return true;
}
if (HighlightStyle.HasFlag (HighlightStyle.Hover))
{
SetHighlight (HighlightStyle.None);
}
return false;
}
/// <summary>
/// Called by <see cref="NewMouseEvent"/> when a mouse leaves <see cref="Bounds"/>. The view will
@@ -400,12 +420,28 @@ public partial class View
/// </remarks>
internal void SetHighlight (HighlightStyle style)
{
// TODO: Make the highlight colors configurable
// Enable override via virtual method and/or event
if (OnHighlight (style) == true)
{
return;
}
if (style.HasFlag (HighlightStyle.Hover))
{
if (_savedHighlightColorScheme is null && ColorScheme is { })
{
_savedHighlightColorScheme ??= ColorScheme;
var cs = new ColorScheme (ColorScheme)
{
Normal = new (ColorName.BrightRed, ColorName.Black),
};
ColorScheme = cs;
}
}
if (style.HasFlag (HighlightStyle.Pressed) || style.HasFlag (HighlightStyle.PressedOutside))
{
@@ -415,14 +451,9 @@ public partial class View
if (CanFocus)
{
// TODO: Make the inverted color configurable
var cs = new ColorScheme (ColorScheme)
{
// For Buttons etc...
Focus = new (ColorScheme.Normal.Foreground, ColorScheme.Focus.Background),
// For Adornments
Normal = new (ColorScheme.Focus.Foreground, ColorScheme.Normal.Background)
};
ColorScheme = cs;
}
@@ -437,7 +468,8 @@ public partial class View
}
}
}
else
if (style == HighlightStyle.None)
{
// Unhighlight
if (_savedHighlightColorScheme is { })

View File

@@ -54,6 +54,7 @@ public class Button : View
CanFocus = true;
AutoSize = true;
HighlightStyle |= HighlightStyle.Pressed;
HighlightStyle |= HighlightStyle.Hover;
// Override default behavior of View
AddCommand (Command.HotKey, () =>