Files
spark/src/client/spark/view/Controls/DIYGrid/CellRender/TextCell.cs
T

256 lines
11 KiB
C#

using mbts.ui.dpi;
using mbts.ui.utility.text;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using FontFamily = System.Windows.Media.FontFamily;
namespace mbts.ui.controls {
class TextCell : FrameworkElement {
// these fields each represent properties that are accessed frequently,
// they are stored locally to reduce repetative DependencyProperty lookups
Thickness _padding => _gcr.padding;
Brush _background, _foreground, _border_brush_backing;
double _glyph_width => _gcr.glyph_run.AdvanceWidths.Sum();
GlyphRun _glyph_run => _gcr.glyph_run;
bool _snaps_to_device_pixels;
Pen _border_stroke_pen_backing;
GlyphContentRect _gcr = new GlyphContentRect();
Brush _border_brush {
get => _border_brush_backing;
set {
_border_brush_backing = value;
_border_stroke_pen_backing = null;
}
}
Thickness _border_thickness {
get => _snaps_to_device_pixels ? _gcr.border_thickness.RoundToLayout() : _gcr.border_thickness;
set {
_gcr.border_thickness = new Thickness(value.Left);
_border_stroke_pen_backing = null;
}
}
Pen _border_stroke_pen =>
// border_thickness.Left is used as the uniform border thickness. This can be changed if need be.
_border_stroke_pen_backing == null && _border_brush != null && _border_thickness.Left > 0 ?
(_border_stroke_pen_backing = _border_stroke_pen_backing = new Pen(_border_brush, _border_thickness.Left)) :
_border_stroke_pen_backing;
static TextCell() {
WPFHelper.override_metadata<HorizontalAlignment, TextCell>(
HorizontalAlignmentProperty,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._gcr.horizontal_alignment = v, HorizontalAlignment.Left);
WPFHelper.override_metadata<double, TextCell>(
WidthProperty,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._gcr.width = v,
double.NaN);
WPFHelper.override_metadata<bool, TextCell>(
SnapsToDevicePixelsProperty,
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._snaps_to_device_pixels = v,
true);
}
public TextCell() {
_gcr.min_width = MinWidth;
_gcr.width = Width;
_gcr.max_width = MaxWidth;
_gcr.min_height = MinHeight;
_gcr.height = Height;
_gcr.max_height = MaxHeight;
_gcr.padding = Padding;
_gcr.border_thickness = BorderThickness;
_gcr.horizontal_alignment = HorizontalAlignment;
_gcr.horizontal_content_alignment = HorizontalContentAlignment;
_gcr.vertical_alignment = VerticalAlignment;
_gcr.vertical_content_alignment = VerticalContentAlignment;
_background = Background;
_foreground = Foreground;
_border_brush = BorderBrush;
_gcr.font_size = FontSize;
if (Text is string str && !string.IsNullOrEmpty(str))
_gcr.text_lines = new[] { str };
_gcr.glyph_context = GlyphContext.create(FontFamily, FontWeight);
_snaps_to_device_pixels = SnapsToDevicePixels;
}
#region BorderThickness
public static readonly DependencyProperty BorderThicknessProperty =
Control.BorderThicknessProperty.AddOwner(typeof(TextCell),
new FrameworkPropertyMetadata(
new Thickness(0),
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsArrange,
(d, e) => ((TextCell)d)._gcr.border_thickness = (Thickness)e.NewValue));
public Thickness BorderThickness {
get => (Thickness)GetValue(BorderThicknessProperty);
set => SetValue(BorderThicknessProperty, value);
}
#endregion
#region BorderBrush
public static readonly DependencyProperty BorderBrushProperty =
Control.BorderBrushProperty.AddOwner(typeof(TextCell),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.AffectsRender,
(d, e) => ((TextCell)d)._border_brush = e.NewValue as Brush));
public Brush BorderBrush {
get => (Brush)GetValue(BorderBrushProperty);
set => SetValue(BorderBrushProperty, value);
}
#endregion
#region HorizontalContentAlignment
public static readonly DependencyProperty HorizontalContentAlignmentProperty =
Control.HorizontalContentAlignmentProperty.AddOwner(typeof(TextCell),
new FrameworkPropertyMetadata(
HorizontalAlignment.Center,
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.AffectsArrange,
(d, e) => ((TextCell)d)._gcr.horizontal_content_alignment = (HorizontalAlignment)e.NewValue));
public HorizontalAlignment HorizontalContentAlignment {
get => (HorizontalAlignment)GetValue(HorizontalContentAlignmentProperty);
set => SetValue(HorizontalContentAlignmentProperty, value);
}
#endregion
#region VerticalContentAlignment
public static readonly DependencyProperty VerticalContentAlignmentProperty = Control.VerticalContentAlignmentProperty.AddOwner(typeof(TextCell),
new FrameworkPropertyMetadata(
VerticalAlignment.Center,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsRender,
(d, e) => ((TextCell)d)._gcr.vertical_content_alignment = (VerticalAlignment)e.NewValue));
public VerticalAlignment VerticalContentAlignment {
get => (VerticalAlignment)GetValue(VerticalContentAlignmentProperty);
set => SetValue(VerticalContentAlignmentProperty, value);
}
#endregion
#region Background
public static readonly DependencyProperty BackgroundProperty =
WPFHelper.create_dp<Brush, TextCell>(
nameof(Background),
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._background = v,
Brushes.Transparent);
public Brush Background { get => GetValue(BackgroundProperty) as Brush; set => SetValue(BackgroundProperty, value); }
#endregion
#region Foreground
public static readonly DependencyProperty ForegroundProperty =
WPFHelper.create_dp<Brush, TextCell>(
nameof(Foreground),
FrameworkPropertyMetadataOptions.AffectsRender,
(p, v) => p._foreground = v,
Brushes.Black);
public Brush Foreground { get => (Brush)GetValue(ForegroundProperty); set => SetValue(ForegroundProperty, value); }
#endregion
#region FontWeight
public static readonly DependencyProperty FontWeightProperty =
TextElement.FontWeightProperty.AddOwner(
typeof(TextCell),
new FrameworkPropertyMetadata(
FontWeights.Normal,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
WPFHelper.prop_changed_callback<FontWeight, TextCell>(
(p, v) => p._gcr.glyph_context = p._gcr.glyph_context.with(v))));
public FontWeight FontWeight { get => (FontWeight)GetValue(FontWeightProperty); set => SetValue(FontWeightProperty, value); }
#endregion
#region FontFamily
public static readonly DependencyProperty FontFamilyProperty =
TextElement.FontFamilyProperty.AddOwner(
typeof(TextCell),
new FrameworkPropertyMetadata(
TextUtils.StandardFontFamily,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
WPFHelper.prop_changed_callback<FontFamily, TextCell>(
(p, v) => p._gcr.glyph_context = p._gcr.glyph_context.with(v))));
public FontFamily FontFamily { get => GetValue(FontFamilyProperty) as FontFamily; set => SetValue(FontFamilyProperty, value); }
#endregion
#region FontSize
public static readonly DependencyProperty FontSizeProperty =
TextElement.FontSizeProperty.AddOwner(
typeof(TextCell),
new FrameworkPropertyMetadata(
FontSizes.Small,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.Inherits,
WPFHelper.prop_changed_callback<double, TextCell>(
(p, v) => p._gcr.font_size = v)));
public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); }
#endregion
#region Padding
public static readonly DependencyProperty PaddingProperty =
WPFHelper.create_dp<Thickness, TextCell>(
nameof(Padding),
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(p, v) => p._gcr.padding = v,
DIYGrid.DefaultCellPadding);
public Thickness Padding { get => (Thickness)GetValue(PaddingProperty); set => SetValue(PaddingProperty, value); }
#endregion
#region Text
public static readonly DependencyProperty TextProperty =
WPFHelper.create_dp<string, TextCell>(
nameof(Text),
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(p, v) => p._gcr.text_lines = string.IsNullOrEmpty(v) ? null : new[] { v }, string.Empty);
public string Text { get => GetValue(TextProperty) as string; set => SetValue(TextProperty, value); }
#endregion
protected override Size ArrangeOverride(Size finalSize) => finalSize;
protected override Size MeasureOverride(Size availableSize) {
_gcr.available_size = availableSize;
if (double.IsInfinity(_gcr.layout_size.Height) || double.IsInfinity(_gcr.layout_size.Width)) {
_gcr.remeasure();
}
return _gcr.layout_size;
}
protected override void OnRender(DrawingContext drawingContext) {
var rs = RenderSize;
var rect = new Rect(0, 0, rs.Width, rs.Height);
var bg = Background;
drawingContext.DrawRectangle(bg, _border_stroke_pen, rect, _snaps_to_device_pixels);
var gr = _glyph_run;
if (gr != null) {
drawingContext.PushClip(new RectangleGeometry(rect));
drawingContext.DrawGlyphRun(_foreground, gr);
drawingContext.Pop();
}
}
}
}