redo HyperlinkCell (alignment/measurement doesn't work right outside of the HighPerformanceRow)

This commit is contained in:
John Colvin committed 2019-08-14 00:24:34 -05:00
1 parent 084e99325d
commit fcff996025
3 files changed
+347 -280

No files matched your search

@@ -4,344 +4,393 @@ using System;
using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using FontFamily = System.Windows.Media.FontFamily;
namespace mbts.ui.controls {
partial class DIYGrid {
public class HyperlinkCell : FrameworkElement, ICommandSource {
// these fields each represent properties that are accessed frequently,
// they are stored locally to reduce repetative DependencyProperty lookups
Thickness _padding;
HorizontalAlignment _horizontal_alignment;
Brush _background, _foreground;
double _glyph_width, _font_size;
string _text;
GlyphRun _glyph_run;
GlyphContext _glyph_context;
bool _is_mouse_over_text, _can_execute;
Pen _underline_pen;
class HyperlinkCell : FrameworkElement, ICommandSource {
// 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_backing, _border_brush_backing;
double _glyph_width => _gcr.glyph_run.AdvanceWidths.Sum();
GlyphRun _glyph_run => _gcr.glyph_run;
bool _is_mouse_over_text, _can_execute;
Pen _underline_pen_backing, _border_stroke_pen_backing;
GlyphContentRect _gcr = new GlyphContentRect();
static HyperlinkCell() {
WPFHelper.override_metadata<HorizontalAlignment, HyperlinkCell>(
HorizontalAlignmentProperty,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => {
o._horizontal_alignment = v;
o._glyph_run = null;
}, HorizontalAlignment.Left);
WPFHelper.override_metadata<double, HyperlinkCell>(
WidthProperty,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._glyph_run = null);
Brush _foreground {
get => _foreground_backing;
set {
_foreground_backing = value;
_underline_pen_backing = null;
}
public HyperlinkCell() {
_background = Background;
_foreground = Foreground;
_horizontal_alignment = HorizontalAlignment;
_padding = Padding;
_font_size = FontSize;
_text = Text;
_glyph_context = GlyphContext.create(FontFamily, FontWeight);
IsMouseDirectlyOverChanged += (s, e) => {
if (_is_mouse_over_text != IsMouseDirectlyOver)
_toggle_mouse_over_text();
};
}
Brush _border_brush {
get => _border_brush_backing;
set {
_border_brush_backing = value;
_border_stroke_pen_backing = null;
}
}
Thickness _border_thickness {
get => _gcr.border_thickness;
set {
_gcr.border_thickness = new Thickness(value.Left);
_border_stroke_pen_backing = null;
}
}
Pen _underline_pen => _underline_pen_backing ?? (_foreground == null ? null : (_underline_pen_backing = new Pen(_foreground, 1)));
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;
#region Background
public static readonly DependencyProperty BackgroundProperty =
WPFHelper.create_dp<Brush, HyperlinkCell>(
nameof(Background),
static HyperlinkCell() {
WPFHelper.override_metadata<HorizontalAlignment, HyperlinkCell>(
HorizontalAlignmentProperty,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._gcr.horizontal_alignment = v, HorizontalAlignment.Left);
WPFHelper.override_metadata<double, HyperlinkCell>(
WidthProperty,
FrameworkPropertyMetadataOptions.AffectsArrange |
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._gcr.width = v,
double.NaN);
}
public HyperlinkCell() {
_gcr.min_width = MinWidth;
_gcr.width = Width;
_gcr.max_width = MaxWidth;
_gcr.min_height = MinHeight;
_gcr.height = Height;
_gcr.max_height = Height;
_gcr.padding = _underline_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_backing = 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);
}
#region BorderThickness
public static readonly DependencyProperty BorderThicknessProperty =
Control.BorderThicknessProperty.AddOwner(typeof(HyperlinkCell),
new FrameworkPropertyMetadata(
new Thickness(0),
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsArrange,
(d, e) => ((HyperlinkCell)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(HyperlinkCell),
new FrameworkPropertyMetadata(
null,
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._background = v,
Brushes.Transparent);
(d, e) => ((HyperlinkCell)d)._border_brush = e.NewValue as Brush));
public Brush Background { get => GetValue(BackgroundProperty) as Brush; set => SetValue(BackgroundProperty, value); }
#endregion
public Brush BorderBrush {
get => (Brush)GetValue(BorderBrushProperty);
set => SetValue(BorderBrushProperty, value);
}
#endregion
#region Foreground
public static readonly DependencyProperty ForegroundProperty =
WPFHelper.create_dp<Brush, HyperlinkCell>(
nameof(Foreground),
FrameworkPropertyMetadataOptions.AffectsRender,
(p, v) => {
p._foreground = v;
p._underline_pen = new Pen(v, 1);
}, Brushes.Black);
#region HorizontalContentAlignment
public static readonly DependencyProperty HorizontalContentAlignmentProperty =
Control.HorizontalContentAlignmentProperty.AddOwner(typeof(HyperlinkCell),
new FrameworkPropertyMetadata(
HorizontalAlignment.Center,
FrameworkPropertyMetadataOptions.AffectsArrange,
(d, e) => ((HyperlinkCell)d)._gcr.horizontal_content_alignment = (HorizontalAlignment)e.NewValue));
public Brush Foreground { get => (Brush)GetValue(ForegroundProperty); set => SetValue(ForegroundProperty, value); }
#endregion
public HorizontalAlignment HorizontalContentAlignment {
get => (HorizontalAlignment)GetValue(HorizontalContentAlignmentProperty);
set => SetValue(HorizontalContentAlignmentProperty, value);
}
#endregion
#region FontWeight
public static readonly DependencyProperty FontWeightProperty =
TextElement.FontWeightProperty.AddOwner(
typeof(HyperlinkCell),
new FrameworkPropertyMetadata(
FontWeights.Normal,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
WPFHelper.prop_changed_callback<FontWeight, HyperlinkCell>(
(p, v) => {
p._glyph_context = p._glyph_context.with(v);
p._glyph_run = null;
})));
#region VerticalContentAlignment
public static readonly DependencyProperty VerticalContentAlignmentProperty = Control.VerticalContentAlignmentProperty.AddOwner(typeof(HyperlinkCell),
new FrameworkPropertyMetadata(
VerticalAlignment.Center,
FrameworkPropertyMetadataOptions.AffectsArrange,
(d, e) => ((HyperlinkCell)d)._gcr.vertical_content_alignment = (VerticalAlignment)e.NewValue));
public FontWeight FontWeight { get => (FontWeight)GetValue(FontWeightProperty); set => SetValue(FontWeightProperty, value); }
#endregion
public VerticalAlignment VerticalContentAlignment {
get => (VerticalAlignment)GetValue(VerticalContentAlignmentProperty);
set => SetValue(VerticalContentAlignmentProperty, value);
}
#endregion
#region FontFamily
public static readonly DependencyProperty FontFamilyProperty =
TextElement.FontFamilyProperty.AddOwner(
typeof(HyperlinkCell),
new FrameworkPropertyMetadata(
TextUtils.StandardFontFamily,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
WPFHelper.prop_changed_callback<FontFamily, HyperlinkCell>(
(p, v) => {
p._glyph_context = p._glyph_context.with(v);
p._glyph_run = null;
})));
#region Background
public static readonly DependencyProperty BackgroundProperty =
WPFHelper.create_dp<Brush, HyperlinkCell>(
nameof(Background),
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._background = v,
Brushes.Transparent);
public FontFamily FontFamily { get => GetValue(FontFamilyProperty) as FontFamily; set => SetValue(FontFamilyProperty, value); }
#endregion
public Brush Background { get => GetValue(BackgroundProperty) as Brush; set => SetValue(BackgroundProperty, value); }
#endregion
#region FontSize
public static readonly DependencyProperty FontSizeProperty =
TextElement.FontSizeProperty.AddOwner(
typeof(HyperlinkCell),
new FrameworkPropertyMetadata(
FontSizes.Small,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.Inherits,
WPFHelper.prop_changed_callback<double, HyperlinkCell>(
(p, v) => {
p._font_size = v;
p._glyph_run = null;
})));
#region Foreground
public static readonly DependencyProperty ForegroundProperty =
WPFHelper.create_dp<Brush, HyperlinkCell>(
nameof(Foreground),
FrameworkPropertyMetadataOptions.AffectsRender,
(p, v) => p._foreground = v,
Brushes.Black);
public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); }
#endregion
public Brush Foreground { get => (Brush)GetValue(ForegroundProperty); set => SetValue(ForegroundProperty, value); }
#endregion
#region Padding
public static readonly DependencyProperty PaddingProperty =
WPFHelper.create_dp<Thickness, HyperlinkCell>(
nameof(Padding),
#region FontWeight
public static readonly DependencyProperty FontWeightProperty =
TextElement.FontWeightProperty.AddOwner(
typeof(HyperlinkCell),
new FrameworkPropertyMetadata(
FontWeights.Normal,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(p, v) => {
p._padding = v;
p._glyph_run = null;
}, DefaultCellPadding);
WPFHelper.prop_changed_callback<FontWeight, HyperlinkCell>(
(p, v) => p._gcr.glyph_context = p._gcr.glyph_context.with(v))));
public Thickness Padding { get => (Thickness)GetValue(PaddingProperty); set => SetValue(PaddingProperty, value); }
#endregion
public FontWeight FontWeight { get => (FontWeight)GetValue(FontWeightProperty); set => SetValue(FontWeightProperty, value); }
#endregion
#region Text
public static readonly DependencyProperty TextProperty =
WPFHelper.create_dp<string, HyperlinkCell>(
nameof(Text),
#region FontFamily
public static readonly DependencyProperty FontFamilyProperty =
TextElement.FontFamilyProperty.AddOwner(
typeof(HyperlinkCell),
new FrameworkPropertyMetadata(
TextUtils.StandardFontFamily,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => {
o._text = v;
o._glyph_run = null;
}, string.Empty);
WPFHelper.prop_changed_callback<FontFamily, HyperlinkCell>(
(p, v) => p._gcr.glyph_context = p._gcr.glyph_context.with(v))));
public string Text { get => GetValue(TextProperty) as string; set => SetValue(TextProperty, value); }
#endregion
#region ICommandSource
public FontFamily FontFamily { get => GetValue(FontFamilyProperty) as FontFamily; set => SetValue(FontFamilyProperty, value); }
#endregion
#region Command
public static readonly DependencyProperty CommandProperty =
WPFHelper.create_dp<ICommand, HyperlinkCell>(
nameof(Command),
(s, old_val, new_val) => {
if (old_val != null)
CanExecuteChangedEventManager.RemoveHandler(old_val, s.OnCanExecuteChanged);
if (new_val != null)
CanExecuteChangedEventManager.AddHandler(new_val, s.OnCanExecuteChanged);
s.UpdateCanExecute();
});
#region FontSize
public static readonly DependencyProperty FontSizeProperty =
TextElement.FontSizeProperty.AddOwner(
typeof(HyperlinkCell),
new FrameworkPropertyMetadata(
FontSizes.Small,
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender |
FrameworkPropertyMetadataOptions.Inherits,
WPFHelper.prop_changed_callback<double, HyperlinkCell>(
(p, v) => p._gcr.font_size = v)));
[Bindable(true), Category("Action")]
[Localizability(LocalizationCategory.NeverLocalize)]
public ICommand Command { get => (ICommand)GetValue(CommandProperty); set => SetValue(CommandProperty, value); }
#endregion
public double FontSize { get => (double)GetValue(FontSizeProperty); set => SetValue(FontSizeProperty, value); }
#endregion
#region CommandParameter
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register(
nameof(CommandParameter),
typeof(object),
typeof(HyperlinkCell));
#region Padding
public static readonly DependencyProperty PaddingProperty =
WPFHelper.create_dp<Thickness, HyperlinkCell>(
nameof(Padding),
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(p, v) => p._gcr.padding = _underline_padding(v), DIYGrid.DefaultCellPadding);
[Bindable(true), Category("Action")]
[Localizability(LocalizationCategory.NeverLocalize)]
public object CommandParameter { get => GetValue(CommandParameterProperty); set => SetValue(CommandParameterProperty, value); }
#endregion
public Thickness Padding { get => (Thickness)GetValue(PaddingProperty); set => SetValue(PaddingProperty, value); }
#endregion
#region CommandTarget
public static readonly DependencyProperty CommandTargetProperty =
DependencyProperty.Register(
nameof(CommandTarget),
typeof(IInputElement),
typeof(HyperlinkCell));
#region Text
public static readonly DependencyProperty TextProperty =
WPFHelper.create_dp<string, HyperlinkCell>(
nameof(Text),
FrameworkPropertyMetadataOptions.AffectsMeasure |
FrameworkPropertyMetadataOptions.AffectsRender,
(p, v) => p._gcr.text_lines = string.IsNullOrEmpty(v) ? null : new[] { v }, string.Empty);
[Bindable(true), Category("Action")]
public IInputElement CommandTarget { get => (IInputElement)GetValue(CommandTargetProperty); set => SetValue(CommandTargetProperty, value); }
#endregion
public string Text { get => GetValue(TextProperty) as string; set => SetValue(TextProperty, value); }
#endregion
protected override bool IsEnabledCore => base.IsEnabledCore && CanExecute;
#region ICommandSource
bool CanExecute {
get => _can_execute;
set {
if (_can_execute != value) {
_can_execute = value;
CoerceValue(IsEnabledProperty);
}
#region Command
public static readonly DependencyProperty CommandProperty =
WPFHelper.create_dp<ICommand, HyperlinkCell>(
nameof(Command),
(s, old_val, new_val) => {
if (old_val != null)
CanExecuteChangedEventManager.RemoveHandler(old_val, s.OnCanExecuteChanged);
if (new_val != null)
CanExecuteChangedEventManager.AddHandler(new_val, s.OnCanExecuteChanged);
s.UpdateCanExecute();
});
[Bindable(true), Category("Action")]
[Localizability(LocalizationCategory.NeverLocalize)]
public ICommand Command { get => (ICommand)GetValue(CommandProperty); set => SetValue(CommandProperty, value); }
#endregion
#region CommandParameter
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.Register(
nameof(CommandParameter),
typeof(object),
typeof(HyperlinkCell));
[Bindable(true), Category("Action")]
[Localizability(LocalizationCategory.NeverLocalize)]
public object CommandParameter { get => GetValue(CommandParameterProperty); set => SetValue(CommandParameterProperty, value); }
#endregion
#region CommandTarget
public static readonly DependencyProperty CommandTargetProperty =
DependencyProperty.Register(
nameof(CommandTarget),
typeof(IInputElement),
typeof(HyperlinkCell));
[Bindable(true), Category("Action")]
public IInputElement CommandTarget { get => (IInputElement)GetValue(CommandTargetProperty); set => SetValue(CommandTargetProperty, value); }
#endregion
protected override bool IsEnabledCore => base.IsEnabledCore && CanExecute;
bool CanExecute {
get => _can_execute;
set {
if (_can_execute != value) {
_can_execute = value;
CoerceValue(IsEnabledProperty);
}
}
}
void OnCanExecuteChanged(object sender, EventArgs e) =>
UpdateCanExecute();
void OnCanExecuteChanged(object sender, EventArgs e) =>
UpdateCanExecute();
void UpdateCanExecute() =>
CanExecute = Command == null || CommandHelper.CanExecuteCommandSource(this);
void UpdateCanExecute() =>
CanExecute = Command == null || CommandHelper.CanExecuteCommandSource(this);
#endregion
#endregion
#region Paraphrased/Simplified from https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Documents/Hyperlink.cs
#region Paraphrased/Simplified from https://referencesource.microsoft.com/#PresentationFramework/src/Framework/System/Windows/Documents/Hyperlink.cs
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
base.OnMouseLeftButtonDown(e);
_check_mouse_over_text(e);
if (_is_mouse_over_text) {
// Hyperlink should take focus when left mouse button is clicked on it
// This is consistent with all ButtonBase controls and current Win32 behavior
Focus();
protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e) {
base.OnMouseLeftButtonDown(e);
_check_mouse_over_text(e);
if (_is_mouse_over_text) {
// Hyperlink should take focus when left mouse button is clicked on it
// This is consistent with all ButtonBase controls and current Win32 behavior
Focus();
// It is possible that the mouse state could have changed during all of
// the call-outs that have happened so far.
if (e.ButtonState == MouseButtonState.Pressed) {
// Capture the mouse, and make sure we got it.
CaptureMouse();
}
e.Handled = true;
// It is possible that the mouse state could have changed during all of
// the call-outs that have happened so far.
if (e.ButtonState == MouseButtonState.Pressed) {
// Capture the mouse, and make sure we got it.
CaptureMouse();
}
}
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) {
base.OnMouseLeftButtonUp(e);
_check_mouse_over_text(e);
if (IsMouseCaptured)
ReleaseMouseCapture();
if (_can_execute && _is_mouse_over_text)
CommandHelper.ExecuteCommandSource(this);
e.Handled = true;
}
}
#endregion
protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) {
base.OnMouseLeftButtonUp(e);
_check_mouse_over_text(e);
if (IsMouseCaptured)
ReleaseMouseCapture();
if (_can_execute && _is_mouse_over_text)
CommandHelper.ExecuteCommandSource(this);
e.Handled = true;
}
protected override void OnPreviewMouseMove(MouseEventArgs e) => _check_mouse_over_text(e);
protected override void OnMouseEnter(MouseEventArgs e) => _check_mouse_over_text(e);
protected override void OnMouseLeave(MouseEventArgs e) => _check_mouse_over_text(e);
#endregion
protected override Size ArrangeOverride(Size finalSize) {
if (_glyph_run == null)
_set_glyph(finalSize);
return finalSize;
protected override void OnIsMouseDirectlyOverChanged(DependencyPropertyChangedEventArgs e) {
if (_is_mouse_over_text != IsMouseDirectlyOver)
_toggle_mouse_over_text();
}
protected override void OnPreviewMouseMove(MouseEventArgs e) => _check_mouse_over_text(e);
protected override void OnMouseEnter(MouseEventArgs e) => _check_mouse_over_text(e);
protected override void OnMouseLeave(MouseEventArgs e) => _check_mouse_over_text(e);
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 Size MeasureOverride(Size availableSize) {
if (_glyph_run == null)
_set_glyph(availableSize);
return new Size(Math.Max(availableSize.Width, _padding.Left + _glyph_width + _padding.Right),
Math.Max(availableSize.Height, _padding.Top + _font_size + _padding.Bottom));
protected override void OnRender(DrawingContext drawingContext) {
var rs = RenderSize;
var rect = new Rect(0, 0, rs.Width, rs.Height);
var bg = Background;
if (bg != null) {
drawingContext.DrawRectangle(bg, _border_stroke_pen, rect);
}
void _set_glyph(Size size) {
_glyph_run = _glyph_context.get_glyph_run(_text, _font_size, size, _horizontal_alignment, VerticalAlignment.Center, _padding);
_glyph_width = _glyph_run == null ? 0 : _glyph_run.AdvanceWidths.Sum();
}
protected override void OnRender(DrawingContext drawingContext) {
var rs = RenderSize;
var rect = new Rect(0, 0, rs.Width, rs.Height);
var bg = Background;
if (bg != null)
drawingContext.DrawRectangle(bg, null, rect);
if (_glyph_run != null) {
drawingContext.PushClip(new RectangleGeometry(rect));
drawingContext.DrawGlyphRun(_foreground, _glyph_run);
if (_is_mouse_over_text) {
// draw underline
Point pt;
switch (_horizontal_alignment) {
case HorizontalAlignment.Left:
pt = new Point(Padding.Left, 0.5 * (ActualHeight + _font_size) + 1);
break;
case HorizontalAlignment.Right:
pt = new Point(ActualWidth - Padding.Right - _glyph_width, 0.5 * (ActualHeight + _font_size) + 1);
break;
case HorizontalAlignment.Center:
default:
pt = new Point(0.5 * (ActualWidth - _glyph_width), 0.5 * (ActualHeight + _font_size) + 1);
break;
}
drawingContext.DrawLine(_underline_pen, pt, new Point(pt.X + _glyph_width, pt.Y));
}
drawingContext.Pop();
}
if (bg != null) {
var bgo = GetBackgroundOverlay(this);
if (bgo != null)
drawingContext.DrawRectangle(bgo, null, rect);
if (_glyph_run != null) {
drawingContext.PushClip(new RectangleGeometry(rect));
drawingContext.DrawGlyphRun(_foreground, _glyph_run);
if (_is_mouse_over_text) {
// draw underline
double x = _gcr.glyph_run.BaselineOrigin.X;
double y = _gcr.glyph_run.BaselineOrigin.Y + 2;
drawingContext.DrawLine(_underline_pen, new Point(x, y), new Point(x + _gcr.glyph_run.AdvanceWidths.Sum(), y));
}
drawingContext.Pop();
}
if (bg != null) {
var bgo = DIYGrid.GetBackgroundOverlay(this);
if (bgo != null)
drawingContext.DrawRectangle(bgo, null, rect);
}
}
bool _point_horizontally_over_text(double x) {
switch (_horizontal_alignment) {
case HorizontalAlignment.Left:
return x >= _padding.Left && x <= _padding.Left + _glyph_width;
case HorizontalAlignment.Right:
return x >= ActualWidth - Padding.Right - _glyph_width && x <= ActualWidth - _padding.Right;
case HorizontalAlignment.Center:
default:
return x >= 0.5 * (ActualWidth - _glyph_width) && x <= 0.5 * (ActualWidth - _glyph_width) + _glyph_width;
}
}
static Thickness _underline_padding(Thickness padding) =>
new Thickness(padding.Left, padding.Top, padding.Right, padding.Bottom + 2);
void _check_mouse_over_text(MouseEventArgs e) {
var pt = e.GetPosition(this);
bool over_text =
_glyph_run != null &&
_point_horizontally_over_text(pt.X) &&
pt.Y >= _padding.Top &&
pt.Y <= _padding.Top + _font_size;
if (_is_mouse_over_text != over_text)
_toggle_mouse_over_text();
void _check_mouse_over_text(MouseEventArgs e) {
var pt = e.GetPosition(this);
var orig = _gcr.content_area;
var exp = orig.Expanded(1);
if (_is_mouse_over_text != exp.Contains(pt)) {
bts.Log.assert(_is_mouse_over_text != _gcr.content_area.Expanded(1).Contains(e.GetPosition(this)));
_toggle_mouse_over_text();
return;
}
if (_is_mouse_over_text != _gcr.content_area.Expanded(1).Contains(e.GetPosition(this)))
_toggle_mouse_over_text();
}
void _toggle_mouse_over_text() {
_is_mouse_over_text = !_is_mouse_over_text;
if (_is_mouse_over_text)
SetCurrentValue(CursorProperty, Cursors.Hand);
else
ClearValue(CursorProperty);
InvalidateVisual();
}
void _toggle_mouse_over_text() {
_is_mouse_over_text = !_is_mouse_over_text;
if (_is_mouse_over_text)
SetCurrentValue(CursorProperty, Cursors.Hand);
else
ClearValue(CursorProperty);
InvalidateVisual();
}
}
}
@@ -411,5 +411,11 @@ namespace mbts.ui {
}
}
}
public static Rect Expanded(this Rect rect, double uniform_dimension) {
// since Rect is a struct, we are mutating a copy of the caller's actual rect
rect.Inflate(new Size(uniform_dimension, uniform_dimension));
return rect;
}
}
}
@@ -1,6 +1,5 @@
using bts.utils;
using mbts.ui.utility.text;
using mbts.utils;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Media;
@@ -76,6 +75,11 @@ namespace mbts.ui {
public Size layout_size { get { _ensure_current(); return _layout_size; } }
public Rect content_area { get { _ensure_current(); return _content_area; } }
public void remeasure() {
_dirty = true;
_ensure_current();
}
void _ensure_current() {
if (_dirty) {
WPFHelper.measure_visual_content(
@@ -147,6 +151,14 @@ namespace mbts.ui {
}
}
public void remeasure() {
_arrange_dirty = true;
_ensure_current();
_vcr.remeasure();
_arrange_dirty = true;
_ensure_current();
}
void _ensure_current() {
if (_arrange_dirty) {
if (_text_lines.is_null_or_empty()) {