2024-03-17 18:37:06 -07:00
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
|
|
using Avalonia;
|
2024-02-06 03:07:17 -08:00
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Media;
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
namespace SourceGit.Views
|
|
|
|
|
{
|
|
|
|
|
public class NameHighlightedTextBlock : Control
|
|
|
|
|
{
|
2024-02-06 03:07:17 -08:00
|
|
|
|
public static readonly StyledProperty<string> TextProperty =
|
|
|
|
|
AvaloniaProperty.Register<NameHighlightedTextBlock, string>(nameof(Text));
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public string Text
|
|
|
|
|
{
|
2024-02-06 03:07:17 -08:00
|
|
|
|
get => GetValue(TextProperty);
|
|
|
|
|
set => SetValue(TextProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
|
|
|
|
|
TextBlock.FontFamilyProperty.AddOwner<NameHighlightedTextBlock>();
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public FontFamily FontFamily
|
|
|
|
|
{
|
2024-02-06 03:07:17 -08:00
|
|
|
|
get => GetValue(FontFamilyProperty);
|
|
|
|
|
set => SetValue(FontFamilyProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly StyledProperty<double> FontSizeProperty =
|
|
|
|
|
TextBlock.FontSizeProperty.AddOwner<NameHighlightedTextBlock>();
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public double FontSize
|
|
|
|
|
{
|
2024-02-06 03:07:17 -08:00
|
|
|
|
get => GetValue(FontSizeProperty);
|
|
|
|
|
set => SetValue(FontSizeProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly StyledProperty<IBrush> ForegroundProperty =
|
|
|
|
|
TextBlock.ForegroundProperty.AddOwner<NameHighlightedTextBlock>();
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public IBrush Foreground
|
|
|
|
|
{
|
2024-02-06 03:07:17 -08:00
|
|
|
|
get => GetValue(ForegroundProperty);
|
|
|
|
|
set => SetValue(ForegroundProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
static NameHighlightedTextBlock()
|
|
|
|
|
{
|
2024-02-06 03:07:17 -08:00
|
|
|
|
AffectsMeasure<NameHighlightedTextBlock>(TextProperty);
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public NameHighlightedTextBlock(string nameKey, params object[] args)
|
|
|
|
|
{
|
2024-02-28 02:55:23 -08:00
|
|
|
|
Text = App.Text(nameKey, args);
|
|
|
|
|
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center;
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
protected override Size MeasureOverride(Size availableSize)
|
|
|
|
|
{
|
2024-02-06 03:07:17 -08:00
|
|
|
|
var text = Text;
|
|
|
|
|
if (string.IsNullOrEmpty(text)) return base.MeasureOverride(availableSize);
|
|
|
|
|
|
|
|
|
|
var typeface = new Typeface(FontFamily, FontStyle.Normal, FontWeight.Normal, FontStretch.Normal);
|
|
|
|
|
var formatted = new FormattedText(
|
|
|
|
|
Text,
|
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
|
FlowDirection.LeftToRight,
|
|
|
|
|
typeface,
|
|
|
|
|
FontSize,
|
|
|
|
|
Foreground);
|
2024-02-06 03:12:40 -08:00
|
|
|
|
|
|
|
|
|
return new Size(formatted.Width - 16, formatted.Height);
|
2024-02-06 03:07:17 -08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
public override void Render(DrawingContext context)
|
|
|
|
|
{
|
2024-02-06 03:07:17 -08:00
|
|
|
|
var text = Text;
|
|
|
|
|
if (string.IsNullOrEmpty(text)) return;
|
|
|
|
|
|
|
|
|
|
var normalTypeface = new Typeface(FontFamily, FontStyle.Normal, FontWeight.Normal, FontStretch.Normal);
|
2024-02-17 18:41:41 -08:00
|
|
|
|
//var highlightTypeface = new Typeface(FontFamily, FontStyle.Normal, FontWeight.Bold, FontStretch.Normal);
|
2024-02-06 03:07:17 -08:00
|
|
|
|
var underlinePen = new Pen(Foreground, 1);
|
|
|
|
|
var offsetX = 0.0;
|
|
|
|
|
|
|
|
|
|
var parts = text.Split('$', StringSplitOptions.None);
|
|
|
|
|
var isName = false;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
foreach (var part in parts)
|
|
|
|
|
{
|
|
|
|
|
if (string.IsNullOrEmpty(part))
|
|
|
|
|
{
|
2024-02-06 03:07:17 -08:00
|
|
|
|
isName = !isName;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var formatted = new FormattedText(
|
|
|
|
|
part,
|
|
|
|
|
CultureInfo.CurrentCulture,
|
|
|
|
|
FlowDirection.LeftToRight,
|
2024-02-17 18:41:41 -08:00
|
|
|
|
//isName ? highlightTypeface : normalTypeface,
|
|
|
|
|
normalTypeface,
|
2024-02-06 03:07:17 -08:00
|
|
|
|
FontSize,
|
|
|
|
|
Foreground);
|
|
|
|
|
|
2024-03-17 18:37:06 -07:00
|
|
|
|
if (isName)
|
|
|
|
|
{
|
2024-02-06 03:07:17 -08:00
|
|
|
|
var lineY = formatted.Baseline + 2;
|
2024-02-17 18:41:41 -08:00
|
|
|
|
offsetX += 4;
|
|
|
|
|
context.DrawText(formatted, new Point(offsetX, 0));
|
2024-02-06 03:07:17 -08:00
|
|
|
|
context.DrawLine(underlinePen, new Point(offsetX, lineY), new Point(offsetX + formatted.Width, lineY));
|
2024-02-17 18:41:41 -08:00
|
|
|
|
offsetX += formatted.Width + 4;
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-02-17 18:41:41 -08:00
|
|
|
|
context.DrawText(formatted, new Point(offsetX, 0));
|
2024-02-06 03:12:40 -08:00
|
|
|
|
offsetX += formatted.Width;
|
2024-02-06 03:07:17 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isName = !isName;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-17 18:37:06 -07:00
|
|
|
|
}
|