qamp/Qrakhen.Qamp.Editor/Controls/Caret.cs

53 lines
1.4 KiB
C#

using System.Windows;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Threading;
namespace Qrakhen.Qamp.Editor.Controls;
public class Caret : Adorner
{
private readonly Pen _pen;
private readonly double _height;
private readonly Brush _brush;
private bool _blink;
private Point _cursorPosition;
private DispatcherTimer _timer;
public Caret(UIElement adornedElement, Brush brush, double height = 18, double thickness = 1.72) : base(adornedElement)
{
IsHitTestVisible = false;
_height = height;
_brush = brush;
_pen = new Pen(brush, 1.5);
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(428);
_timer.Tick += new EventHandler((s, e) => {
Toggle(_blink = !_blink);
});
_timer.Start();
}
public void Toggle(bool hide)
{
_pen.Brush = hide ? Brushes.Transparent : _brush;
}
public void UpdatePosition(Point newPosition)
{
_timer.Stop();
Toggle(_blink = false);
_cursorPosition = newPosition;
InvalidateVisual();
_timer.Start();
}
protected override void OnRender(DrawingContext drawingContext)
{
Point startPoint = new(_cursorPosition.X, _cursorPosition.Y);
Point endPoint = new(_cursorPosition.X, _cursorPosition.Y + _height);
drawingContext.DrawLine(_pen, startPoint, endPoint);
}
}