From decdae424d65af7cefd30c52129d6e7ae8b10c04 Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Wed, 14 Aug 2019 09:22:02 -0500 Subject: [PATCH] new Adorner to convey row selection --- .../Controls/Adorners/RowSelectionAdorner.cs | 93 +++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 src/client/spark/view/Controls/Adorners/RowSelectionAdorner.cs diff --git a/src/client/spark/view/Controls/Adorners/RowSelectionAdorner.cs b/src/client/spark/view/Controls/Adorners/RowSelectionAdorner.cs new file mode 100644 index 000000000..aa9daf6df --- /dev/null +++ b/src/client/spark/view/Controls/Adorners/RowSelectionAdorner.cs @@ -0,0 +1,93 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Documents; +using System.Windows.Input; +using System.Windows.Media; +using bts.utils; +using mbts.ui.extensions; + +namespace mbts.ui.controls.adorners { + public class RowSelectionAdorner : Adorner, DIYGrid.ISelectionHighlightListener { + Brush _fill = DIYGrid.DefaultSelectionHighlight; + Pen _stroke = DIYGrid.DefaultSelectionStroke; + + DIYGridRowsPresenter _diy_rp; + ScrollViewer _diy_scroll; + ScrollContentPresenter _diy_scp; + + #region Fill + public static readonly DependencyProperty FillProperty = + WPFHelper.create_dp( + nameof(Fill), + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._fill = v, + DIYGrid.DefaultSelectionHighlight); + + public SolidColorBrush Fill { + get => GetValue(FillProperty) as SolidColorBrush; + set => SetValue(FillProperty, value); + } + #endregion + #region Stroke + public static readonly DependencyProperty StrokeProperty = + WPFHelper.create_dp( + nameof(Stroke), + FrameworkPropertyMetadataOptions.AffectsRender, + (o, v) => o._stroke = v, + DIYGrid.DefaultSelectionStroke); + + public Pen Stroke { + get => GetValue(StrokeProperty) as Pen; + set => SetValue(StrokeProperty, value); + } + #endregion + + public RowSelectionAdorner(FrameworkElement adorned_element, ScrollViewer scroll_viewer) : base(adorned_element) { + // we want our clicks to go through to the underlying grid + IsHitTestVisible = false; + + _diy_rp = (DIYGridRowsPresenter)adorned_element; + _diy_scroll = scroll_viewer; + _diy_scp = _diy_rp.descendants_of_type().empty_if_null().FirstOrDefault(); + + // so that we clear out any previously selected highlighted rows + _diy_rp.SubscribePreviewMouseDown((s, e) => InvalidateVisual()); + // the main event + _diy_rp.SubscribePreviewMouseUp((s, e) => InvalidateVisual()); + // so that we update the highlight as the grid scrolls + _diy_scroll.SubscribeScrollChanged((s, e) => InvalidateVisual()); + } + + + protected override void OnRender(DrawingContext drawingContext) { + base.OnRender(drawingContext); + if (AdornedElement is ListBox lb && lb.SelectedItems != null && lb.SelectedItems.Count > 0) { + foreach(var lbi in lb.SelectedItems) { + if (_diy_rp.ItemContainerGenerator.ContainerFromItem(lbi) is FrameworkElement row && row.IsVisible) { + var top_left = lb.GetChildTopLeft(row); + var top_is_within_bounds = top_left.Y >= 0; + + // Note that the top of the row can still technically be visible but we wont show + // the adorner rectangle b/c the bottom of the row will be obscured by the scrollbar + var bottom_is_within_bounds = lb.GetChildBounds(row).BottomRight.Y <= _diy_scroll.ActualHeight; + if (top_is_within_bounds && bottom_is_within_bounds) { + drawingContext.DrawRectangle(_fill, + _stroke, + new Rect(top_left, new Point(top_left.X + row.ActualWidth, top_left.Y + row.ActualHeight))); + } + } + } + } + } + + public void on_selection_fill(Brush selection_fill) { + var selection_brush = (SolidColorBrush)selection_fill; + Fill = selection_brush; + Stroke = new Pen(selection_brush.Color.to_solid_brush(), 1); + InvalidateVisual(); + } + } +} \ No newline at end of file