chore: use file-scoped namespace

This commit is contained in:
NaBian committed 2022-03-13 20:28:58 +08:00
1 parent 99d2fafb72
commit bbf1b7d4c6
748 files changed
+57929 -58678

No files matched your search

@@ -8,203 +8,202 @@ using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class HighlightTextBlock : TextBlock
{
public class HighlightTextBlock : TextBlock
public static readonly DependencyProperty SourceTextProperty =
DependencyProperty.Register("SourceText", typeof(string), typeof(HighlightTextBlock),
new PropertyMetadata(null, OnSourceTextChanged));
public static readonly DependencyProperty QueriesTextProperty =
DependencyProperty.Register("QueriesText", typeof(string), typeof(HighlightTextBlock),
new PropertyMetadata(null, OnQueriesTextChanged));
public static readonly DependencyProperty HighlightBrushProperty =
DependencyProperty.Register("HighlightBrush", typeof(Brush), typeof(HighlightTextBlock));
public static readonly DependencyProperty HighlightTextBrushProperty =
DependencyProperty.Register("HighlightTextBrush", typeof(Brush), typeof(HighlightTextBlock));
private static void OnSourceTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
((HighlightTextBlock) d).RefreshInlines();
private static void OnQueriesTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
((HighlightTextBlock) d).RefreshInlines();
/// <summary>
/// Replace the original property Text for binding text.
/// </summary>
/// <remarks>
/// Don't use the <see cref="TextBlock.Text"/> property!
/// Because the <see cref="TextBlock.Text"/> has some unique behaviors
/// that is not needed at <see cref="HighlightTextBlock"/> at <see cref="TextBlock"/>,
/// which will cause some unexpected issue.
/// </remarks>
public string SourceText
{
public static readonly DependencyProperty SourceTextProperty =
DependencyProperty.Register("SourceText", typeof(string), typeof(HighlightTextBlock),
new PropertyMetadata(null, OnSourceTextChanged));
get => (string) GetValue(SourceTextProperty);
set => SetValue(SourceTextProperty, value);
}
public static readonly DependencyProperty QueriesTextProperty =
DependencyProperty.Register("QueriesText", typeof(string), typeof(HighlightTextBlock),
new PropertyMetadata(null, OnQueriesTextChanged));
/// <summary>
/// Gets or sets the text that need to be highlighted.
/// It can be an array of text separated by spaces.
/// </summary>
public string QueriesText
{
get => (string) GetValue(QueriesTextProperty);
set => SetValue(QueriesTextProperty, value);
}
public static readonly DependencyProperty HighlightBrushProperty =
DependencyProperty.Register("HighlightBrush", typeof(Brush), typeof(HighlightTextBlock));
/// <summary>
/// Gets or sets the <see cref="Brush"/> of the background of the highlight text.
/// </summary>
public Brush HighlightBrush
{
get => (Brush) GetValue(HighlightBrushProperty);
set => SetValue(HighlightBrushProperty, value);
}
public static readonly DependencyProperty HighlightTextBrushProperty =
DependencyProperty.Register("HighlightTextBrush", typeof(Brush), typeof(HighlightTextBlock));
public Brush HighlightTextBrush
{
get => (Brush) GetValue(HighlightTextBrushProperty);
set => SetValue(HighlightTextBrushProperty, value);
}
private static void OnSourceTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
((HighlightTextBlock) d).RefreshInlines();
private void RefreshInlines()
{
Inlines.Clear();
private static void OnQueriesTextChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
((HighlightTextBlock) d).RefreshInlines();
/// <summary>
/// Replace the original property Text for binding text.
/// </summary>
/// <remarks>
/// Don't use the <see cref="TextBlock.Text"/> property!
/// Because the <see cref="TextBlock.Text"/> has some unique behaviors
/// that is not needed at <see cref="HighlightTextBlock"/> at <see cref="TextBlock"/>,
/// which will cause some unexpected issue.
/// </remarks>
public string SourceText
if (string.IsNullOrEmpty(SourceText)) return;
if (string.IsNullOrEmpty(QueriesText))
{
get => (string) GetValue(SourceTextProperty);
set => SetValue(SourceTextProperty, value);
Inlines.Add(SourceText);
return;
}
/// <summary>
/// Gets or sets the text that need to be highlighted.
/// It can be an array of text separated by spaces.
/// </summary>
public string QueriesText
var sourceText = SourceText;
var queries = QueriesText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var intervals = from query in queries.Distinct()
from interval in GetQueryIntervals(sourceText, query)
select interval;
var mergedIntervals = MergeIntervals(intervals.ToList());
var fragments = SplitTextByOrderedDisjointIntervals(sourceText, mergedIntervals);
Inlines.AddRange(GenerateRunElement(fragments));
}
private IEnumerable GenerateRunElement(IEnumerable<Fragment> fragments)
{
return from item in fragments
select item.IsQuery
? GetHighlightRun(item.Text)
: new Run(item.Text);
}
private Run GetHighlightRun(string highlightText)
{
var run = new Run(highlightText);
run.SetBinding(TextElement.BackgroundProperty, new Binding(nameof(HighlightBrush)) { Source = this });
run.SetBinding(TextElement.ForegroundProperty, new Binding(nameof(HighlightTextBrush)) { Source = this });
return run;
}
private static IEnumerable<Fragment> SplitTextByOrderedDisjointIntervals(string sourceText, List<Range> mergedIntervals)
{
if (string.IsNullOrEmpty(sourceText)) yield break;
if (!mergedIntervals?.Any() ?? true)
{
get => (string) GetValue(QueriesTextProperty);
set => SetValue(QueriesTextProperty, value);
yield return new Fragment { Text = sourceText, IsQuery = false };
yield break;
}
/// <summary>
/// Gets or sets the <see cref="Brush"/> of the background of the highlight text.
/// </summary>
public Brush HighlightBrush
var range0 = mergedIntervals.First();
int start0 = range0.Start;
int end0 = range0.End;
if (start0 > 0) yield return new Fragment { Text = sourceText.Substring(0, start0), IsQuery = false };
yield return new Fragment { Text = sourceText.Substring(start0, end0 - start0), IsQuery = true };
int previousEnd = end0;
foreach (var range in mergedIntervals.Skip(1))
{
get => (Brush) GetValue(HighlightBrushProperty);
set => SetValue(HighlightBrushProperty, value);
int start = range.Start;
int end = range.End;
yield return new Fragment { Text = sourceText.Substring(previousEnd, start - previousEnd), IsQuery = false };
yield return new Fragment { Text = sourceText.Substring(start, end - start), IsQuery = true };
previousEnd = end;
}
public Brush HighlightTextBrush
{
get => (Brush) GetValue(HighlightTextBrushProperty);
set => SetValue(HighlightTextBrushProperty, value);
}
if (previousEnd < sourceText.Length)
yield return new Fragment { Text = sourceText.Substring(previousEnd), IsQuery = false };
}
private void RefreshInlines()
{
Inlines.Clear();
private static List<Range> MergeIntervals(List<Range> intervals)
{
if (!intervals?.Any() ?? true) return new List<Range>();
if (string.IsNullOrEmpty(SourceText)) return;
if (string.IsNullOrEmpty(QueriesText))
intervals.Sort((x, y) => x.Start != y.Start ? x.Start - y.Start : x.End - y.End);
var pointer = intervals[0];
int startPointer = pointer.Start;
int endPointer = pointer.End;
var result = new List<Range>();
foreach (var range in intervals.Skip(1))
{
int start = range.Start;
int end = range.End;
if (start <= endPointer)
{
Inlines.Add(SourceText);
return;
}
var sourceText = SourceText;
var queries = QueriesText.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
var intervals = from query in queries.Distinct()
from interval in GetQueryIntervals(sourceText, query)
select interval;
var mergedIntervals = MergeIntervals(intervals.ToList());
var fragments = SplitTextByOrderedDisjointIntervals(sourceText, mergedIntervals);
Inlines.AddRange(GenerateRunElement(fragments));
}
private IEnumerable GenerateRunElement(IEnumerable<Fragment> fragments)
{
return from item in fragments
select item.IsQuery
? GetHighlightRun(item.Text)
: new Run(item.Text);
}
private Run GetHighlightRun(string highlightText)
{
var run = new Run(highlightText);
run.SetBinding(TextElement.BackgroundProperty, new Binding(nameof(HighlightBrush)) { Source = this });
run.SetBinding(TextElement.ForegroundProperty, new Binding(nameof(HighlightTextBrush)) { Source = this });
return run;
}
private static IEnumerable<Fragment> SplitTextByOrderedDisjointIntervals(string sourceText, List<Range> mergedIntervals)
{
if (string.IsNullOrEmpty(sourceText)) yield break;
if (!mergedIntervals?.Any() ?? true)
{
yield return new Fragment { Text = sourceText, IsQuery = false };
yield break;
}
var range0 = mergedIntervals.First();
int start0 = range0.Start;
int end0 = range0.End;
if (start0 > 0) yield return new Fragment { Text = sourceText.Substring(0, start0), IsQuery = false };
yield return new Fragment { Text = sourceText.Substring(start0, end0 - start0), IsQuery = true };
int previousEnd = end0;
foreach (var range in mergedIntervals.Skip(1))
{
int start = range.Start;
int end = range.End;
yield return new Fragment { Text = sourceText.Substring(previousEnd, start - previousEnd), IsQuery = false };
yield return new Fragment { Text = sourceText.Substring(start, end - start), IsQuery = true };
previousEnd = end;
}
if (previousEnd < sourceText.Length)
yield return new Fragment { Text = sourceText.Substring(previousEnd), IsQuery = false };
}
private static List<Range> MergeIntervals(List<Range> intervals)
{
if (!intervals?.Any() ?? true) return new List<Range>();
intervals.Sort((x, y) => x.Start != y.Start ? x.Start - y.Start : x.End - y.End);
var pointer = intervals[0];
int startPointer = pointer.Start;
int endPointer = pointer.End;
var result = new List<Range>();
foreach (var range in intervals.Skip(1))
{
int start = range.Start;
int end = range.End;
if (start <= endPointer)
if (endPointer < end)
{
if (endPointer < end)
{
endPointer = end;
}
}
else
{
result.Add(new Range { Start = startPointer, End = endPointer });
startPointer = start;
endPointer = end;
}
}
result.Add(new Range { Start = startPointer, End = endPointer });
return result;
}
private static IEnumerable<Range> GetQueryIntervals(string sourceText, string query)
{
if (string.IsNullOrEmpty(sourceText) || string.IsNullOrEmpty(query)) yield break;
int nextStartIndex = 0;
while (nextStartIndex < sourceText.Length)
else
{
int index = sourceText.IndexOf(query, nextStartIndex, StringComparison.CurrentCultureIgnoreCase);
if (index == -1) yield break;
nextStartIndex = index + query.Length;
yield return new Range { Start = index, End = nextStartIndex };
result.Add(new Range { Start = startPointer, End = endPointer });
startPointer = start;
endPointer = end;
}
}
result.Add(new Range { Start = startPointer, End = endPointer });
return result;
}
private struct Fragment
private static IEnumerable<Range> GetQueryIntervals(string sourceText, string query)
{
if (string.IsNullOrEmpty(sourceText) || string.IsNullOrEmpty(query)) yield break;
int nextStartIndex = 0;
while (nextStartIndex < sourceText.Length)
{
public string Text { get; set; }
int index = sourceText.IndexOf(query, nextStartIndex, StringComparison.CurrentCultureIgnoreCase);
public bool IsQuery { get; set; }
}
if (index == -1) yield break;
private struct Range
{
public int Start { get; set; }
public int End { get; set; }
nextStartIndex = index + query.Length;
yield return new Range { Start = index, End = nextStartIndex };
}
}
}
private struct Fragment
{
public string Text { get; set; }
public bool IsQuery { get; set; }
}
private struct Range
{
public int Start { get; set; }
public int End { get; set; }
}
}
@@ -6,277 +6,276 @@ using System.Windows.Media;
using HandyControl.Data;
using HandyControl.Tools.Helper;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class OutlineText : FrameworkElement
{
public class OutlineText : FrameworkElement
private Pen _pen;
private FormattedText _formattedText;
private Geometry _textGeometry;
private PathGeometry _clipGeometry;
static OutlineText()
{
private Pen _pen;
SnapsToDevicePixelsProperty.OverrideMetadata(typeof(OutlineText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
UseLayoutRoundingProperty.OverrideMetadata(typeof(OutlineText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
}
private FormattedText _formattedText;
public static readonly DependencyProperty StrokePositionProperty = DependencyProperty.Register(
"StrokePosition", typeof(StrokePosition), typeof(OutlineText), new PropertyMetadata(default(StrokePosition)));
private Geometry _textGeometry;
public StrokePosition StrokePosition
{
get => (StrokePosition) GetValue(StrokePositionProperty);
set => SetValue(StrokePositionProperty, value);
}
private PathGeometry _clipGeometry;
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(OutlineText), new FrameworkPropertyMetadata(
string.Empty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender, OnFormattedTextInvalidated));
static OutlineText()
public string Text
{
get => (string) GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly DependencyProperty TextAlignmentProperty = DependencyProperty.Register(
"TextAlignment", typeof(TextAlignment), typeof(OutlineText),
new PropertyMetadata(default(TextAlignment), OnFormattedTextUpdated));
public TextAlignment TextAlignment
{
get => (TextAlignment) GetValue(TextAlignmentProperty);
set => SetValue(TextAlignmentProperty, value);
}
public static readonly DependencyProperty TextTrimmingProperty = DependencyProperty.Register(
"TextTrimming", typeof(TextTrimming), typeof(OutlineText),
new PropertyMetadata(default(TextTrimming), OnFormattedTextInvalidated));
public TextTrimming TextTrimming
{
get => (TextTrimming) GetValue(TextTrimmingProperty);
set => SetValue(TextTrimmingProperty, value);
}
public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register(
"TextWrapping", typeof(TextWrapping), typeof(OutlineText),
new PropertyMetadata(TextWrapping.NoWrap, OnFormattedTextInvalidated));
public TextWrapping TextWrapping
{
get => (TextWrapping) GetValue(TextWrappingProperty);
set => SetValue(TextWrappingProperty, value);
}
public static readonly DependencyProperty FillProperty = DependencyProperty.Register(
"Fill", typeof(Brush), typeof(OutlineText), new PropertyMetadata(Brushes.Black, OnFormattedTextUpdated));
public Brush Fill
{
get => (Brush) GetValue(FillProperty);
set => SetValue(FillProperty, value);
}
public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register(
"Stroke", typeof(Brush), typeof(OutlineText), new PropertyMetadata(Brushes.Black, OnFormattedTextUpdated));
public Brush Stroke
{
get => (Brush) GetValue(StrokeProperty);
set => SetValue(StrokeProperty, value);
}
public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register(
"StrokeThickness", typeof(double), typeof(OutlineText), new PropertyMetadata(ValueBoxes.Double0Box, OnFormattedTextUpdated));
public double StrokeThickness
{
get => (double) GetValue(StrokeThicknessProperty);
set => SetValue(StrokeThicknessProperty, value);
}
public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(
typeof(OutlineText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontFamily FontFamily
{
get => (FontFamily) GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(
typeof(OutlineText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
[TypeConverter(typeof(FontSizeConverter))]
public double FontSize
{
get => (double) GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner(
typeof(OutlineText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontStretch FontStretch
{
get => (FontStretch) GetValue(FontStretchProperty);
set => SetValue(FontStretchProperty, value);
}
public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner(
typeof(OutlineText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontStyle FontStyle
{
get => (FontStyle) GetValue(FontStyleProperty);
set => SetValue(FontStyleProperty, value);
}
public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner(
typeof(OutlineText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontWeight FontWeight
{
get => (FontWeight) GetValue(FontWeightProperty);
set => SetValue(FontWeightProperty, value);
}
protected override void OnRender(DrawingContext drawingContext)
{
if (StrokeThickness > 0)
{
SnapsToDevicePixelsProperty.OverrideMetadata(typeof(OutlineText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
UseLayoutRoundingProperty.OverrideMetadata(typeof(OutlineText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
}
EnsureGeometry();
public static readonly DependencyProperty StrokePositionProperty = DependencyProperty.Register(
"StrokePosition", typeof(StrokePosition), typeof(OutlineText), new PropertyMetadata(default(StrokePosition)));
public StrokePosition StrokePosition
{
get => (StrokePosition) GetValue(StrokePositionProperty);
set => SetValue(StrokePositionProperty, value);
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(OutlineText), new FrameworkPropertyMetadata(
string.Empty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender, OnFormattedTextInvalidated));
public string Text
{
get => (string) GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly DependencyProperty TextAlignmentProperty = DependencyProperty.Register(
"TextAlignment", typeof(TextAlignment), typeof(OutlineText),
new PropertyMetadata(default(TextAlignment), OnFormattedTextUpdated));
public TextAlignment TextAlignment
{
get => (TextAlignment) GetValue(TextAlignmentProperty);
set => SetValue(TextAlignmentProperty, value);
}
public static readonly DependencyProperty TextTrimmingProperty = DependencyProperty.Register(
"TextTrimming", typeof(TextTrimming), typeof(OutlineText),
new PropertyMetadata(default(TextTrimming), OnFormattedTextInvalidated));
public TextTrimming TextTrimming
{
get => (TextTrimming) GetValue(TextTrimmingProperty);
set => SetValue(TextTrimmingProperty, value);
}
public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register(
"TextWrapping", typeof(TextWrapping), typeof(OutlineText),
new PropertyMetadata(TextWrapping.NoWrap, OnFormattedTextInvalidated));
public TextWrapping TextWrapping
{
get => (TextWrapping) GetValue(TextWrappingProperty);
set => SetValue(TextWrappingProperty, value);
}
public static readonly DependencyProperty FillProperty = DependencyProperty.Register(
"Fill", typeof(Brush), typeof(OutlineText), new PropertyMetadata(Brushes.Black, OnFormattedTextUpdated));
public Brush Fill
{
get => (Brush) GetValue(FillProperty);
set => SetValue(FillProperty, value);
}
public static readonly DependencyProperty StrokeProperty = DependencyProperty.Register(
"Stroke", typeof(Brush), typeof(OutlineText), new PropertyMetadata(Brushes.Black, OnFormattedTextUpdated));
public Brush Stroke
{
get => (Brush) GetValue(StrokeProperty);
set => SetValue(StrokeProperty, value);
}
public static readonly DependencyProperty StrokeThicknessProperty = DependencyProperty.Register(
"StrokeThickness", typeof(double), typeof(OutlineText), new PropertyMetadata(ValueBoxes.Double0Box, OnFormattedTextUpdated));
public double StrokeThickness
{
get => (double) GetValue(StrokeThicknessProperty);
set => SetValue(StrokeThicknessProperty, value);
}
public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(
typeof(OutlineText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontFamily FontFamily
{
get => (FontFamily) GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(
typeof(OutlineText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
[TypeConverter(typeof(FontSizeConverter))]
public double FontSize
{
get => (double) GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner(
typeof(OutlineText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontStretch FontStretch
{
get => (FontStretch) GetValue(FontStretchProperty);
set => SetValue(FontStretchProperty, value);
}
public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner(
typeof(OutlineText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontStyle FontStyle
{
get => (FontStyle) GetValue(FontStyleProperty);
set => SetValue(FontStyleProperty, value);
}
public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner(
typeof(OutlineText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontWeight FontWeight
{
get => (FontWeight) GetValue(FontWeightProperty);
set => SetValue(FontWeightProperty, value);
}
protected override void OnRender(DrawingContext drawingContext)
{
if (StrokeThickness > 0)
{
EnsureGeometry();
drawingContext.DrawGeometry(Fill, null, _textGeometry);
if (StrokePosition == StrokePosition.Outside)
{
drawingContext.PushClip(_clipGeometry);
}
else if (StrokePosition == StrokePosition.Inside)
{
drawingContext.PushClip(_textGeometry);
}
drawingContext.DrawGeometry(null, _pen, _textGeometry);
if (StrokePosition == StrokePosition.Outside || StrokePosition == StrokePosition.Inside)
{
drawingContext.Pop();
}
}
else
{
UpdateFormattedText();
drawingContext.DrawText(_formattedText, new Point());
}
}
private void UpdatePen()
{
_pen = new Pen(Stroke, StrokeThickness);
if (StrokePosition == StrokePosition.Outside || StrokePosition == StrokePosition.Inside)
{
_pen.Thickness = StrokeThickness * 2;
}
}
private void EnsureFormattedText()
{
if (_formattedText != null || Text == null)
{
return;
}
_formattedText = TextHelper.CreateFormattedText(Text, FlowDirection,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize);
UpdateFormattedText();
}
private void EnsureGeometry()
{
if (_textGeometry != null)
{
return;
}
EnsureFormattedText();
_textGeometry = _formattedText.BuildGeometry(new Point(0, 0));
drawingContext.DrawGeometry(Fill, null, _textGeometry);
if (StrokePosition == StrokePosition.Outside)
{
var geometry = new RectangleGeometry(new Rect(0, 0, ActualWidth, ActualHeight));
_clipGeometry = Geometry.Combine(geometry, _textGeometry, GeometryCombineMode.Exclude, null);
drawingContext.PushClip(_clipGeometry);
}
}
private void UpdateFormattedText()
{
if (_formattedText == null)
else if (StrokePosition == StrokePosition.Inside)
{
return;
drawingContext.PushClip(_textGeometry);
}
_formattedText.MaxLineCount = TextWrapping == TextWrapping.NoWrap ? 1 : int.MaxValue;
_formattedText.TextAlignment = TextAlignment;
_formattedText.Trimming = TextTrimming;
drawingContext.DrawGeometry(null, _pen, _textGeometry);
_formattedText.SetFontSize(FontSize);
_formattedText.SetFontStyle(FontStyle);
_formattedText.SetFontWeight(FontWeight);
_formattedText.SetFontFamily(FontFamily);
_formattedText.SetFontStretch(FontStretch);
_formattedText.SetForegroundBrush(Fill);
if (StrokePosition == StrokePosition.Outside || StrokePosition == StrokePosition.Inside)
{
drawingContext.Pop();
}
}
private static void OnFormattedTextUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
else
{
var outlinedTextBlock = (OutlineText) d;
outlinedTextBlock.UpdateFormattedText();
outlinedTextBlock._textGeometry = null;
outlinedTextBlock.InvalidateMeasure();
outlinedTextBlock.InvalidateVisual();
}
private static void OnFormattedTextInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var outlinedTextBlock = (OutlineText) d;
outlinedTextBlock._formattedText = null;
outlinedTextBlock._textGeometry = null;
outlinedTextBlock.InvalidateMeasure();
outlinedTextBlock.InvalidateVisual();
}
protected override Size MeasureOverride(Size availableSize)
{
EnsureFormattedText();
// constrain the formatted text according to the available size
// the Math.Min call is important - without this constraint (which seems arbitrary, but is the maximum allowable text width), things blow up when availableSize is infinite in both directions
// the Math.Max call is to ensure we don't hit zero, which will cause MaxTextHeight to throw
_formattedText.MaxTextWidth = Math.Min(3579139, availableSize.Width);
_formattedText.MaxTextHeight = Math.Max(0.0001d, availableSize.Height);
UpdatePen();
// return the desired size
return new Size(_formattedText.Width, _formattedText.Height);
UpdateFormattedText();
drawingContext.DrawText(_formattedText, new Point());
}
}
}
private void UpdatePen()
{
_pen = new Pen(Stroke, StrokeThickness);
if (StrokePosition == StrokePosition.Outside || StrokePosition == StrokePosition.Inside)
{
_pen.Thickness = StrokeThickness * 2;
}
}
private void EnsureFormattedText()
{
if (_formattedText != null || Text == null)
{
return;
}
_formattedText = TextHelper.CreateFormattedText(Text, FlowDirection,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize);
UpdateFormattedText();
}
private void EnsureGeometry()
{
if (_textGeometry != null)
{
return;
}
EnsureFormattedText();
_textGeometry = _formattedText.BuildGeometry(new Point(0, 0));
if (StrokePosition == StrokePosition.Outside)
{
var geometry = new RectangleGeometry(new Rect(0, 0, ActualWidth, ActualHeight));
_clipGeometry = Geometry.Combine(geometry, _textGeometry, GeometryCombineMode.Exclude, null);
}
}
private void UpdateFormattedText()
{
if (_formattedText == null)
{
return;
}
_formattedText.MaxLineCount = TextWrapping == TextWrapping.NoWrap ? 1 : int.MaxValue;
_formattedText.TextAlignment = TextAlignment;
_formattedText.Trimming = TextTrimming;
_formattedText.SetFontSize(FontSize);
_formattedText.SetFontStyle(FontStyle);
_formattedText.SetFontWeight(FontWeight);
_formattedText.SetFontFamily(FontFamily);
_formattedText.SetFontStretch(FontStretch);
_formattedText.SetForegroundBrush(Fill);
}
private static void OnFormattedTextUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var outlinedTextBlock = (OutlineText) d;
outlinedTextBlock.UpdateFormattedText();
outlinedTextBlock._textGeometry = null;
outlinedTextBlock.InvalidateMeasure();
outlinedTextBlock.InvalidateVisual();
}
private static void OnFormattedTextInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var outlinedTextBlock = (OutlineText) d;
outlinedTextBlock._formattedText = null;
outlinedTextBlock._textGeometry = null;
outlinedTextBlock.InvalidateMeasure();
outlinedTextBlock.InvalidateVisual();
}
protected override Size MeasureOverride(Size availableSize)
{
EnsureFormattedText();
// constrain the formatted text according to the available size
// the Math.Min call is important - without this constraint (which seems arbitrary, but is the maximum allowable text width), things blow up when availableSize is infinite in both directions
// the Math.Max call is to ensure we don't hit zero, which will cause MaxTextHeight to throw
_formattedText.MaxTextWidth = Math.Min(3579139, availableSize.Width);
_formattedText.MaxTextHeight = Math.Max(0.0001d, availableSize.Height);
UpdatePen();
// return the desired size
return new Size(_formattedText.Width, _formattedText.Height);
}
}
@@ -6,180 +6,179 @@ using System.Windows.Media;
using HandyControl.Data;
using HandyControl.Tools.Helper;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class SimpleText : FrameworkElement
{
public class SimpleText : FrameworkElement
private FormattedText _formattedText;
static SimpleText()
{
private FormattedText _formattedText;
static SimpleText()
{
SnapsToDevicePixelsProperty.OverrideMetadata(typeof(SimpleText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
UseLayoutRoundingProperty.OverrideMetadata(typeof(SimpleText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(SimpleText), new FrameworkPropertyMetadata(
string.Empty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender, OnFormattedTextInvalidated));
public string Text
{
get => (string) GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly DependencyProperty TextAlignmentProperty = DependencyProperty.Register(
"TextAlignment", typeof(TextAlignment), typeof(SimpleText),
new PropertyMetadata(default(TextAlignment), OnFormattedTextUpdated));
public TextAlignment TextAlignment
{
get => (TextAlignment) GetValue(TextAlignmentProperty);
set => SetValue(TextAlignmentProperty, value);
}
public static readonly DependencyProperty TextTrimmingProperty = DependencyProperty.Register(
"TextTrimming", typeof(TextTrimming), typeof(SimpleText),
new PropertyMetadata(default(TextTrimming), OnFormattedTextInvalidated));
public TextTrimming TextTrimming
{
get => (TextTrimming) GetValue(TextTrimmingProperty);
set => SetValue(TextTrimmingProperty, value);
}
public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register(
"TextWrapping", typeof(TextWrapping), typeof(SimpleText),
new PropertyMetadata(TextWrapping.NoWrap, OnFormattedTextInvalidated));
public TextWrapping TextWrapping
{
get => (TextWrapping) GetValue(TextWrappingProperty);
set => SetValue(TextWrappingProperty, value);
}
public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register(
"Foreground", typeof(Brush), typeof(SimpleText), new PropertyMetadata(Brushes.Black, OnFormattedTextUpdated));
public Brush Foreground
{
get => (Brush) GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}
public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(
typeof(SimpleText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontFamily FontFamily
{
get => (FontFamily) GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(
typeof(SimpleText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
[TypeConverter(typeof(FontSizeConverter))]
public double FontSize
{
get => (double) GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner(
typeof(SimpleText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontStretch FontStretch
{
get => (FontStretch) GetValue(FontStretchProperty);
set => SetValue(FontStretchProperty, value);
}
public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner(
typeof(SimpleText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontStyle FontStyle
{
get => (FontStyle) GetValue(FontStyleProperty);
set => SetValue(FontStyleProperty, value);
}
public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner(
typeof(SimpleText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontWeight FontWeight
{
get => (FontWeight) GetValue(FontWeightProperty);
set => SetValue(FontWeightProperty, value);
}
protected override void OnRender(DrawingContext drawingContext) => drawingContext.DrawText(_formattedText, new Point());
private void EnsureFormattedText()
{
if (_formattedText != null || Text == null)
{
return;
}
_formattedText = TextHelper.CreateFormattedText(Text, FlowDirection,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize);
UpdateFormattedText();
}
private void UpdateFormattedText()
{
if (_formattedText == null)
{
return;
}
_formattedText.MaxLineCount = TextWrapping == TextWrapping.NoWrap ? 1 : int.MaxValue;
_formattedText.TextAlignment = TextAlignment;
_formattedText.Trimming = TextTrimming;
_formattedText.SetFontSize(FontSize);
_formattedText.SetFontStyle(FontStyle);
_formattedText.SetFontWeight(FontWeight);
_formattedText.SetFontFamily(FontFamily);
_formattedText.SetFontStretch(FontStretch);
_formattedText.SetForegroundBrush(Foreground);
}
private static void OnFormattedTextUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var outlinedTextBlock = (SimpleText) d;
outlinedTextBlock.UpdateFormattedText();
outlinedTextBlock.InvalidateMeasure();
outlinedTextBlock.InvalidateVisual();
}
private static void OnFormattedTextInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var outlinedTextBlock = (SimpleText) d;
outlinedTextBlock._formattedText = null;
outlinedTextBlock.InvalidateMeasure();
outlinedTextBlock.InvalidateVisual();
}
protected override Size MeasureOverride(Size availableSize)
{
EnsureFormattedText();
_formattedText.MaxTextWidth = Math.Min(3579139, availableSize.Width);
_formattedText.MaxTextHeight = Math.Max(0.0001d, availableSize.Height);
return new Size(_formattedText.Width, _formattedText.Height);
}
SnapsToDevicePixelsProperty.OverrideMetadata(typeof(SimpleText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
UseLayoutRoundingProperty.OverrideMetadata(typeof(SimpleText), new FrameworkPropertyMetadata(ValueBoxes.TrueBox));
}
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
"Text", typeof(string), typeof(SimpleText), new FrameworkPropertyMetadata(
string.Empty,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender, OnFormattedTextInvalidated));
public string Text
{
get => (string) GetValue(TextProperty);
set => SetValue(TextProperty, value);
}
public static readonly DependencyProperty TextAlignmentProperty = DependencyProperty.Register(
"TextAlignment", typeof(TextAlignment), typeof(SimpleText),
new PropertyMetadata(default(TextAlignment), OnFormattedTextUpdated));
public TextAlignment TextAlignment
{
get => (TextAlignment) GetValue(TextAlignmentProperty);
set => SetValue(TextAlignmentProperty, value);
}
public static readonly DependencyProperty TextTrimmingProperty = DependencyProperty.Register(
"TextTrimming", typeof(TextTrimming), typeof(SimpleText),
new PropertyMetadata(default(TextTrimming), OnFormattedTextInvalidated));
public TextTrimming TextTrimming
{
get => (TextTrimming) GetValue(TextTrimmingProperty);
set => SetValue(TextTrimmingProperty, value);
}
public static readonly DependencyProperty TextWrappingProperty = DependencyProperty.Register(
"TextWrapping", typeof(TextWrapping), typeof(SimpleText),
new PropertyMetadata(TextWrapping.NoWrap, OnFormattedTextInvalidated));
public TextWrapping TextWrapping
{
get => (TextWrapping) GetValue(TextWrappingProperty);
set => SetValue(TextWrappingProperty, value);
}
public static readonly DependencyProperty ForegroundProperty = DependencyProperty.Register(
"Foreground", typeof(Brush), typeof(SimpleText), new PropertyMetadata(Brushes.Black, OnFormattedTextUpdated));
public Brush Foreground
{
get => (Brush) GetValue(ForegroundProperty);
set => SetValue(ForegroundProperty, value);
}
public static readonly DependencyProperty FontFamilyProperty = TextElement.FontFamilyProperty.AddOwner(
typeof(SimpleText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontFamily FontFamily
{
get => (FontFamily) GetValue(FontFamilyProperty);
set => SetValue(FontFamilyProperty, value);
}
public static readonly DependencyProperty FontSizeProperty = TextElement.FontSizeProperty.AddOwner(
typeof(SimpleText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
[TypeConverter(typeof(FontSizeConverter))]
public double FontSize
{
get => (double) GetValue(FontSizeProperty);
set => SetValue(FontSizeProperty, value);
}
public static readonly DependencyProperty FontStretchProperty = TextElement.FontStretchProperty.AddOwner(
typeof(SimpleText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontStretch FontStretch
{
get => (FontStretch) GetValue(FontStretchProperty);
set => SetValue(FontStretchProperty, value);
}
public static readonly DependencyProperty FontStyleProperty = TextElement.FontStyleProperty.AddOwner(
typeof(SimpleText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontStyle FontStyle
{
get => (FontStyle) GetValue(FontStyleProperty);
set => SetValue(FontStyleProperty, value);
}
public static readonly DependencyProperty FontWeightProperty = TextElement.FontWeightProperty.AddOwner(
typeof(SimpleText),
new FrameworkPropertyMetadata(OnFormattedTextUpdated));
public FontWeight FontWeight
{
get => (FontWeight) GetValue(FontWeightProperty);
set => SetValue(FontWeightProperty, value);
}
protected override void OnRender(DrawingContext drawingContext) => drawingContext.DrawText(_formattedText, new Point());
private void EnsureFormattedText()
{
if (_formattedText != null || Text == null)
{
return;
}
_formattedText = TextHelper.CreateFormattedText(Text, FlowDirection,
new Typeface(FontFamily, FontStyle, FontWeight, FontStretch), FontSize);
UpdateFormattedText();
}
private void UpdateFormattedText()
{
if (_formattedText == null)
{
return;
}
_formattedText.MaxLineCount = TextWrapping == TextWrapping.NoWrap ? 1 : int.MaxValue;
_formattedText.TextAlignment = TextAlignment;
_formattedText.Trimming = TextTrimming;
_formattedText.SetFontSize(FontSize);
_formattedText.SetFontStyle(FontStyle);
_formattedText.SetFontWeight(FontWeight);
_formattedText.SetFontFamily(FontFamily);
_formattedText.SetFontStretch(FontStretch);
_formattedText.SetForegroundBrush(Foreground);
}
private static void OnFormattedTextUpdated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var outlinedTextBlock = (SimpleText) d;
outlinedTextBlock.UpdateFormattedText();
outlinedTextBlock.InvalidateMeasure();
outlinedTextBlock.InvalidateVisual();
}
private static void OnFormattedTextInvalidated(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var outlinedTextBlock = (SimpleText) d;
outlinedTextBlock._formattedText = null;
outlinedTextBlock.InvalidateMeasure();
outlinedTextBlock.InvalidateVisual();
}
protected override Size MeasureOverride(Size availableSize)
{
EnsureFormattedText();
_formattedText.MaxTextWidth = Math.Min(3579139, availableSize.Width);
_formattedText.MaxTextHeight = Math.Max(0.0001d, availableSize.Height);
return new Size(_formattedText.Width, _formattedText.Height);
}
}