using System;
using OpenNest.Desktop.Rendering;
namespace OpenNest.Desktop.Interaction;
///
/// The WinForms plate view's hover tooltip, toolkit-neutral: each pointer move (in Select, no
/// button held) hides the tip and restarts a timer; when the timer elapses
/// the part under the point last moved to, if any, has its drawing name shown beside the
/// pointer. The host owns the timer: it calls , and
/// , and redraws when they return true.
///
public sealed class PartHoverTip
{
/// How long the pointer must rest before the tip shows, as the WinForms hover timer.
public static readonly TimeSpan Delay = TimeSpan.FromSeconds(1);
private bool pending;
/// The part whose tip is shown, or null.
public Part Part { get; private set; }
/// Where the pointer rested, in screen pixels.
public ScreenPoint Anchor { get; private set; }
/// The tip's text, the part's drawing name, or null while no tip shows.
public string Text => Part?.BaseDrawing.Name;
/// Whether a move started the delay and the timer has not elapsed since.
public bool IsPending => pending;
/// The pointer moved: the tip hides and the delay starts again.
/// Whether a shown tip was hidden, so the view must redraw.
public bool Move(ScreenPoint at)
{
Anchor = at;
pending = true;
return Hide();
}
/// The pointer left, pressed a button or the view changed: no tip until the next move.
/// Whether a shown tip was hidden.
public bool Clear()
{
pending = false;
return Hide();
}
///
/// The delay elapsed: shows the tip for the part finds at the
/// anchor. Nothing happens unless a move is pending.
///
/// Whether a tip is now shown.
public bool Elapse(Func partAt)
{
ArgumentNullException.ThrowIfNull(partAt);
if (!pending)
return false;
pending = false;
Part = partAt(Anchor);
return Part != null;
}
///
/// Where the tip's box goes for text of the given size, as WinForms places it: above and to
/// the right of the anchor, flipped to the left when it would pass the view's right edge and
/// below when it would pass the top. The box is the text plus a 3 by 2 pixel padding.
///
public static (double X, double Y, double Width, double Height) Place(
ScreenPoint anchor, double textWidth, double textHeight, double viewWidth)
{
var x = anchor.X + 16;
var y = anchor.Y - textHeight - 6;
if (x + textWidth + 8 > viewWidth)
x = anchor.X - textWidth - 8;
if (y < 0)
y = anchor.Y + 20;
return (x, y, textWidth + 6, textHeight + 4);
}
private bool Hide()
{
if (Part == null)
return false;
Part = null;
return true;
}
}