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

80 lines
2.4 KiB
C#

using Qrakhen.Qamp.Memory;
using System;
using System.Collections.Generic;
using System.ComponentModel;
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, OnLineBufferChanged));
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 = TextHelper.GetText(
LineBuffer.Cached,
FontSize,
Brushes.White,
VisualTreeHelper.GetDpi(this).PixelsPerDip
);
drawingContext.DrawText(formattedText, new Point(2, 0));
}
private static void OnLineBufferChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is LineRenderer renderer) {
if (e.OldValue is LineBuffer oldBuffer && oldBuffer != null) {
oldBuffer.PropertyChanged -= renderer.OnLineBufferPropertyChanged;
}
if (e.NewValue is LineBuffer newBuffer && newBuffer != null) {
newBuffer.PropertyChanged += renderer.OnLineBufferPropertyChanged;
}
renderer.InvalidateVisual();
}
}
private void OnLineBufferPropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(LineBuffer.Cached))
InvalidateVisual();
}
}