2024-02-06 03:07:17 -08:00
|
|
|
|
using Avalonia;
|
|
|
|
|
using Avalonia.Controls;
|
|
|
|
|
using Avalonia.Media;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
|
|
|
|
namespace SourceGit.Views {
|
|
|
|
|
public class NameHighlightedTextBlock : Control {
|
|
|
|
|
|
|
|
|
|
public static readonly StyledProperty<string> TextProperty =
|
|
|
|
|
AvaloniaProperty.Register<NameHighlightedTextBlock, string>(nameof(Text));
|
|
|
|
|
|
|
|
|
|
public string Text {
|
|
|
|
|
get => GetValue(TextProperty);
|
|
|
|
|
set => SetValue(TextProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly StyledProperty<FontFamily> FontFamilyProperty =
|
|
|
|
|
TextBlock.FontFamilyProperty.AddOwner<NameHighlightedTextBlock>();
|
|
|
|
|
|
|
|
|
|
public FontFamily FontFamily {
|
|
|
|
|
get => GetValue(FontFamilyProperty);
|
|
|
|
|
set => SetValue(FontFamilyProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly StyledProperty<double> FontSizeProperty =
|
|
|
|
|
TextBlock.FontSizeProperty.AddOwner<NameHighlightedTextBlock>();
|
|
|
|
|
|
|
|
|
|
public double FontSize {
|
|
|
|
|
get => GetValue(FontSizeProperty);
|
|
|
|
|
set => SetValue(FontSizeProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static readonly StyledProperty<IBrush> ForegroundProperty =
|
|
|
|
|
TextBlock.ForegroundProperty.AddOwner<NameHighlightedTextBlock>();
|
|
|
|
|
|
|
|
|
|
public IBrush Foreground {
|
|
|
|
|
get => GetValue(ForegroundProperty);
|
|
|
|
|
set => SetValue(ForegroundProperty, value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static NameHighlightedTextBlock() {
|
|
|
|
|
AffectsMeasure<NameHighlightedTextBlock>(TextProperty);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-28 02:55:23 -08:00
|
|
|
|
public NameHighlightedTextBlock(string nameKey, params object[] args) {
|
|
|
|
|
Text = App.Text(nameKey, args);
|
|
|
|
|
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center;
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-06 03:07:17 -08:00
|
|
|
|
protected override Size MeasureOverride(Size availableSize) {
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Render(DrawingContext context) {
|
|
|
|
|
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;
|
|
|
|
|
foreach (var part in parts) {
|
|
|
|
|
if (string.IsNullOrEmpty(part)) {
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
if (isName) {
|
|
|
|
|
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-02-06 03:07:17 -08: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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|