qamp/Qrakhen.Qamp.Editor/Controls/LineRenderer.xaml.cs

61 lines
1.8 KiB
C#

using Qrakhen.Qamp.Memory;
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Qrakhen.Qamp.Editor.Controls;
public partial class LineRenderer : UserControl
{
public static readonly DependencyProperty LineBufferProperty =
DependencyProperty.Register(
nameof(LineBuffer),
typeof(LineBuffer),
typeof(LineRenderer),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));
public LineBuffer LineBuffer {
get => (LineBuffer)GetValue(LineBufferProperty);
set => SetValue(LineBufferProperty, value);
}
private readonly Typeface _typeface = new(new FontFamily("Consolas"), FontStyles.Normal, FontWeights.Normal, FontStretches.Normal);
private const double FontSize = 14;
private const double LineHeight = 18;
public LineRenderer()
{
Height = LineHeight;
SnapsToDevicePixels = true;
}
protected override void OnRender(DrawingContext drawingContext)
{
base.OnRender(drawingContext);
if (string.IsNullOrEmpty(LineBuffer.Cached))
return;
var formattedText = new FormattedText(
LineBuffer.Cached,
System.Globalization.CultureInfo.CurrentCulture,
FlowDirection.LeftToRight,
_typeface,
FontSize,
Brushes.White,
VisualTreeHelper.GetDpi(this).PixelsPerDip
);
drawingContext.DrawText(formattedText, new Point(2, 0));
}
}