From 2bd5731430555f9b4ddd9fecc8dcec54ed7df2f0 Mon Sep 17 00:00:00 2001 From: John Colvin Date: Tue, 29 Oct 2019 13:46:38 -0500 Subject: [PATCH] DIYGrid CellRender.draw_text with layout rounding --- .../Controls/DIYGrid/CellRender/CellRender.cs | 106 +++++------------- .../DIYGrid/Columns/ColumnGeometry.cs | 41 ++++--- .../spark/view/Controls/DIYGrid/DIYGrid.cs | 36 +++--- .../Controls/DIYGrid/DIYGridResources.xaml | 1 + .../DIYGrid/Row/HighPerformanceRow.cs | 3 +- .../view/Controls/DIYGrid/Row/RowUtils.cs | 6 +- .../view/Extensions/HighDpiExtensions.cs | 45 ++++++++ src/client/spark/view/Utility/GlyphContext.cs | 14 ++- 8 files changed, 135 insertions(+), 117 deletions(-) diff --git a/src/client/spark/view/Controls/DIYGrid/CellRender/CellRender.cs b/src/client/spark/view/Controls/DIYGrid/CellRender/CellRender.cs index d71dc69a2..d47306689 100644 --- a/src/client/spark/view/Controls/DIYGrid/CellRender/CellRender.cs +++ b/src/client/spark/view/Controls/DIYGrid/CellRender/CellRender.cs @@ -1,4 +1,5 @@ -using mbts.ui.converters; +using mbts.ui.dpi; +using mbts.ui.converters; using mbts.ui.shell; using mbts.ui.utility.text; using System; @@ -10,6 +11,7 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Media; +using bts; namespace mbts.ui.controls { partial class DIYGrid { @@ -83,79 +85,39 @@ namespace mbts.ui.controls { } } - public static Rect? get_clip(Size content_size, double row_height, double left, double unclipped_left, double width) => - content_size == null ? get_clip(0, 0, row_height, left, unclipped_left, width) : - get_clip(content_size.Width, content_size.Height, row_height, left, unclipped_left, width); - - public static Rect? get_clip(double content_width, double content_height, double row_height, double left, double unclipped_left, double width) { - if (content_width > width || unclipped_left < left) - return new Rect(left, 0, width - left + unclipped_left, row_height); - return null; - } - - public static IDisposable draw_with_clip(DrawingContext dc, double raw_content_width, double raw_content_height, Thickness padding, double row_height, ColumnGeometry position) => - draw_with_clip(dc, - raw_content_width + padding.Left + padding.Right, - raw_content_height + padding.Top + padding.Bottom, - row_height, position.left, position.unclipped_left, position.width); - - public static IDisposable draw_with_clip(DrawingContext dc, double content_width, double content_height, double row_height, double left, double unclipped_left, double width) { - var clip = get_clip(content_width, content_height, row_height, left, unclipped_left, width); - if (clip.HasValue) { - dc.PushClip(new RectangleGeometry(clip.Value)); - return Disposable.Create(dc.Pop); - } else return Disposable.Create(() => { }); - } - public static void draw_text( - DrawingContext dc, GlyphContext glyph_context, double font_size, string text, ColumnGeometry position, double row_height, - Brush foreground, Brush background, HorizontalAlignment x_align, VerticalAlignment y_align, Thickness padding) { + DrawingContext dc, + GlyphContext glyph_context, + double font_size, + string text, + ColumnGeometry position, + double row_height, + Brush foreground, + Brush background, + HorizontalAlignment x_align, + VerticalAlignment y_align, + Thickness padding, + bool round_to_layout = true) { // - if (!string.IsNullOrEmpty(text)) { - var gr = glyph_context.get_glyph_run(text, font_size, position.unclipped_left, 0, position.right, row_height, x_align, y_align, padding); - draw_text(dc, gr, gr.AdvanceWidths.Sum(), font_size, position, padding, row_height, foreground, background); - } + var gr = glyph_context.get_glyph_run(text, font_size, position.unclipped_left, 0, position.right, row_height, x_align, y_align, padding, round_to_layout); + var gwidth = gr == null ? 0 : gr.ComputeAlignmentBox().Width; + draw_text(dc, gr, gwidth, font_size, position, padding, row_height, foreground, background); } public static void draw_text(DrawingContext dc, GlyphRun glyph_run, double glyph_width, double font_size, ColumnGeometry position, Thickness padding, double row_height, Brush foreground, Brush background) { - using (draw_with_clip(dc, glyph_width, font_size, padding, row_height, position)) { - if (background != null) - dc.DrawRectangle(background, null, new Rect(position.left, 0, position.width, row_height)); - dc.DrawGlyphRun(foreground, glyph_run); + if (glyph_run == null && background == null) + return; + var rect = position.to_rect(row_height); + if (background != null) { + var guidelines = rect.AntiAliasGuidelines(0); + dc.PushGuidelineSet(guidelines); + dc.DrawRectangle(background, null, rect); + dc.Pop(); } - } - - public static void draw_text( - DrawingContext dc, GlyphContext glyph_context, double font_size, string text, ColumnGeometry position, double row_height, - Brush foreground, Brush background, HorizontalAlignment x_align, VerticalAlignment y_align, Thickness padding, IEnumerable trigger_actions) { - // - using (position.clip_draw(dc, row_height)) { - double adjusted_left = position.unclipped_left; - double available_width = position.width; - if (background != null) - dc.DrawRectangle(background, null, new Rect(position.left, 0, position.width, row_height)); - // store any trigger-glyphs in a list, to be drawn after iterating through all triggers (must draw background first, so glyphs will not be covered up). - List<(Brush brush, GlyphRun glyph)> glyphs = new List<(Brush brush, GlyphRun glyph)>(1); - if (trigger_actions != null) { - foreach (CellAnimation ta in trigger_actions) { - switch (ta.animation_type_case) { - case CellAnimation.AnimationTypeOneofCase.BackgroundAnimation: - dc.DrawRectangle(ta.background_animation.brush, ta.background_animation.pen, new Rect(position.left, 0, position.width, row_height)); - break; - case CellAnimation.AnimationTypeOneofCase.SymbolAnimation: - var (symbol_text, is_sideways) = _get_symbol_animation_args(ta.symbol_animation); - GlyphRun symbol_glyph = glyph_context.get_glyph_run(symbol_text, font_size, position.width, row_height, HorizontalAlignment.Left, adjusted_left, y_align, 0, is_sideways: is_sideways); - glyphs.Add((ta.symbol_animation.brush, symbol_glyph)); - adjusted_left += symbol_glyph.AdvanceWidths.Sum(); - break; - } - } - } - foreach (var (brush, glyph) in glyphs) { - dc.DrawGlyphRun(brush, glyph); - } - GlyphRun main_content = glyph_context.get_glyph_run(text, font_size, adjusted_left, 0, position.right, row_height, x_align, y_align, padding); - draw_text(dc, main_content, main_content.AdvanceWidths.Sum(), font_size, position, padding, row_height, foreground, background); + if (glyph_run != null) { + dc.PushClip(new RectangleGeometry(rect)); + dc.DrawGlyphRun(foreground, glyph_run); + dc.Pop(); } } @@ -165,14 +127,6 @@ namespace mbts.ui.controls { public static void change_position(FrameworkElement cell_view, double row_height, ColumnGeometry? position) { if (position.HasValue) { WPFHelper.set_if_changed(cell_view, VisibilityProperty, Visibility.Visible); - var clip = get_clip(cell_view.DesiredSize, row_height, 0, position.Value.unclipped_left - position.Value.left, position.Value.width); - var el_clip = cell_view.Clip as RectangleGeometry; - if (clip.HasValue) { - if (el_clip == null || !el_clip.Rect.Equals(clip.Value)) - cell_view.Clip = new RectangleGeometry(clip.Value); - } else if (cell_view.Clip != null) { - cell_view.Clip = null; - } } else { WPFHelper.set_if_changed(cell_view, VisibilityProperty, Visibility.Collapsed); } diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnGeometry.cs b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnGeometry.cs index 414c86666..26ae5c87b 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnGeometry.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnGeometry.cs @@ -1,30 +1,39 @@ using System; using System.Windows; -using System.Windows.Media; namespace mbts.ui.controls { // Column types are nested inside the DIYGrid partial class DIYGrid { - public struct ColumnGeometry { - public ColumnBase column; - public DIYGridSuperHeader super_column; - public double unclipped_left; /* If the column was unclipped */ - public double left; - public double width { get { return column.ActualWidth; } } - public double right { get { return unclipped_left + width; } } + public readonly struct ColumnGeometry { + public readonly ColumnBase column; + public readonly DIYGridSuperHeader super_column; + public readonly double + left, + // If the column was unclipped + unclipped_left, + right; + + public ColumnGeometry(ColumnBase column, DIYGridSuperHeader super_column, double left, double right) + : this(column, super_column, left, left, right) { } + + public ColumnGeometry(ColumnBase column, DIYGridSuperHeader super_column, double left, double unclipped_left, double right) { + this.column = column; + this.super_column = super_column; + this.left = left; + this.unclipped_left = left; + this.right = right; + } + + public double width => right - left; public bool is_partially_obscured { get { return unclipped_left < left; } } public double visible_width => Math.Max(0, width - left + unclipped_left); - public override string ToString() { - return "{3} {0:0.0},{1:0.0} w{2:0.0}".fmt(left, right, width, column.Header); - } public string group { get { return super_column == null ? "" : super_column.FullHeader; } } - public RectangleGeometry get_clip(double row_height) => - new RectangleGeometry(new Rect(left, 0, width - left + unclipped_left, row_height)); + public Rect to_rect(double row_height) => + new Rect(left, 0, width, row_height); - public IDisposable clip_draw(DrawingContext dc, double row_height) { - dc.PushClip(get_clip(row_height)); - return Disposable.Create(dc.Pop); + public override string ToString() { + return "{3} {0:0.0},{1:0.0} w{2:0.0}".fmt(left, right, width, column.Header); } } } diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs index 1fa622608..f7271c248 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs +++ b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs @@ -2,6 +2,7 @@ using bts.utils; using mbts.spark.filters; using mbts.ui.controls.adorners; +using mbts.ui.dpi; using mbts.ui.extensions; using mbts.ui.settings; using mbts.ui.utility; @@ -442,8 +443,12 @@ namespace mbts.ui.controls { static DIYGrid() { DefaultStyleKeyProperty.OverrideMetadata(typeof(DIYGrid), new FrameworkPropertyMetadata(typeof(DIYGrid))); + UseLayoutRoundingProperty.OverrideMetadata(typeof(DIYGrid), new FrameworkPropertyMetadata(OnUseLayoutRoundingChanged)); DefaultSelectionHighlight.Freeze(); DefaultSelectionStroke.Freeze(); + + void OnUseLayoutRoundingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) => + d.CoerceValue(RowHeightProperty); } public DIYGrid() { @@ -1069,7 +1074,7 @@ namespace mbts.ui.controls { if (v < min_row_height) return min_row_height; } - return v; + return o.UseLayoutRounding ? v.RoundLayoutValue() : v; }, DefaultRowHeight); public event Action RowHeightChanged; @@ -1146,33 +1151,30 @@ namespace mbts.ui.controls { double frozen_offset = 0; int vis_frozen_col_count = _get_visible_frozen_column_count(); foreach (var col in _visible_columns.Take(vis_frozen_col_count)) { - yield return new ColumnGeometry() { - column = col, - left = frozen_offset, - unclipped_left = frozen_offset, - }; + double right = frozen_offset + col.ActualWidth; + right = right.RoundLayoutValue(); + yield return new ColumnGeometry(col, null, frozen_offset, frozen_offset, right); frozen_offset += col.ActualWidth; } - // frozen_offset will be off by 2 pixels because of the GridViewHeaderRowPresenter PaddingHeader (see c_PaddingHeaderMinWidth in referencesource) - // We could use the _header_frozen.ActualWidth but due to the async nature of layout it get updated slightly too late - frozen_offset += 2; + if (_frozen_column_count > 0) { + // frozen_offset will be off by 2 pixels because of the GridViewHeaderRowPresenter PaddingHeader (see c_PaddingHeaderMinWidth in referencesource) + // We could use the _header_frozen.ActualWidth but due to the async nature of layout it get updated slightly too late + frozen_offset += 2; + } // Adjust by scroll amount - double offset = frozen_offset - (_scroll_horizontal.Value); + double offset = frozen_offset - _scroll_horizontal.Value; + offset = offset.RoundLayoutValue(); int count = 0; foreach (var col in _visible_columns.Skip(vis_frozen_col_count)) { double left = Math.Max(offset, frozen_offset); double right = offset + col.ActualWidth; + right = right.RoundLayoutValue(); // If right < frozen_offset column is completely obscured if (include_obscured_cols || (right > frozen_offset && left <= ActualWidth)) { // fully visible - yield return new ColumnGeometry() { - column = col, - super_column = _superheader_column_index.ElementAtOrDefault(count), - unclipped_left = offset, - left = left - }; + yield return new ColumnGeometry(col, _superheader_column_index.ElementAtOrDefault(count), left, offset, right); } - offset += col.ActualWidth; + offset = right; ++count; } } diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml b/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml index 3aa15b9b4..00481517b 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml +++ b/src/client/spark/view/Controls/DIYGrid/DIYGridResources.xaml @@ -55,6 +55,7 @@ +