diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs index fda5d6cc6..a54d039a7 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs +++ b/src/client/spark/view/Controls/DIYGrid/DIYGrid.cs @@ -1,9 +1,9 @@ using bts; +using bts.utils; using mbts.spark.filters; using mbts.ui.controls.adorners; using mbts.ui.extensions; using mbts.ui.utility; -using bts.utils; using System; using System.Collections; using System.Collections.Generic; @@ -12,14 +12,13 @@ using System.Collections.Specialized; using System.ComponentModel; using System.Diagnostics; using System.Linq; +using System.Reactive.Linq; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; -using mbts.utils; -using System.Reactive.Linq; namespace mbts.ui.controls { interface IHandleGridSorting { @@ -562,6 +561,11 @@ namespace mbts.ui.controls { public static readonly DependencyProperty HeaderTotalSizeHackProperty = DependencyProperty.Register("HeaderTotalSizeHack", typeof(double), typeof(DIYGrid), new FrameworkPropertyMetadata(0.0, dp_invalidate_column_layout)); + public double HeaderTotalSizeHack { + get => (double)GetValue(HeaderTotalSizeHackProperty); + set => SetValue(HeaderTotalSizeHackProperty, value); + } + private static void dp_invalidate_column_layout(DependencyObject d, DependencyPropertyChangedEventArgs unused) { var grid = d as DIYGrid; if (grid != null) { @@ -594,6 +598,61 @@ namespace mbts.ui.controls { // Public Properties #region Public Properties + #region ColumnKey + public static readonly DependencyProperty ColumnKeyProperty = + DependencyProperty.RegisterAttached("ColumnKey", + typeof(object), + typeof(DIYGrid), + new PropertyMetadata((d, e) => { + if (!(d is UIElement child)) + return; + if (VisualTreeHelper.GetParent(child) is Grid panel) { + if (GetSyncWithColumnGeometrySource(d) is DIYGrid diy_grid || panel.try_find_ancestor_of_type(out diy_grid)) + ColumnGeometrySyncHelper.reset_panel_child(child, diy_grid, e.NewValue); + } + })); + + public static object GetColumnKey(DependencyObject d) => d.GetValue(ColumnKeyProperty); + public static void SetColumnKey(DependencyObject d, object value) => d.SetValue(ColumnKeyProperty, value); + #endregion + + #region SyncWithColumnGeometrySource + public static readonly DependencyProperty SyncWithColumnGeometrySourceProperty = + DependencyProperty.RegisterAttached( + "SyncWithColumnGeometrySource", + typeof(DIYGrid), + typeof(DIYGrid), + new PropertyMetadata(ColumnGeometrySyncHelper.reset_sync)); + + public static DIYGrid GetSyncWithColumnGeometrySource(DependencyObject d) => d.GetValue(SyncWithColumnGeometrySourceProperty) as DIYGrid; + public static void SetSyncWithColumnGeometrySource(DependencyObject d, DIYGrid value) => d.SetValue(SyncWithColumnGeometrySourceProperty, value); + #endregion + + #region SyncWithColumnGeometry + public static readonly DependencyProperty SyncWithColumnGeometryProperty = + DependencyProperty.RegisterAttached( + "SyncWithColumnGeometry", + typeof(bool), + typeof(DIYGrid), + new PropertyMetadata(false, ColumnGeometrySyncHelper.reset_sync)); + + public static bool GetSyncWithColumnGeometry(DependencyObject d) => (bool)d.GetValue(SyncWithColumnGeometryProperty); + public static void SetSyncWithColumnGeometry(DependencyObject d, bool value) => d.SetValue(SyncWithColumnGeometryProperty, value); + + static readonly DependencyPropertyKey SyncWithColumnGeometryHookPropertyKey = + DependencyProperty.RegisterAttachedReadOnly( + "SyncWithColumnGeometryHook", + typeof(IDisposable), + typeof(DIYGrid), + new PropertyMetadata(FrameworkElementLifeDependencyHelper.DisposeOld)); + + internal static IDisposable GetSyncWithColumnGeometryHook(DependencyObject d) => + d.GetValue(SyncWithColumnGeometryHookPropertyKey.DependencyProperty) as IDisposable; + + static void SetSyncWithColumnGeometryHook(DependencyObject d, IDisposable value) => + d.SetValue(SyncWithColumnGeometryHookPropertyKey, value); + #endregion + public static readonly DependencyProperty BackgroundOverlayProperty = DependencyProperty.RegisterAttached( "BackgroundOverlay", @@ -783,7 +842,7 @@ namespace mbts.ui.controls { /// Compute the geometry of all visible (or partially visible columns) columns /// This effectively replaces the entire Measure/Arrange pass of the grid rows (and enables column virtualization) /// Mouse events will be hit tested against this as well - internal IEnumerable GetColumnGeometry() { + internal IEnumerable GetColumnGeometry(bool include_obscured_cols = false) { if (_header_scroll_viewer != null && ActualWidth > 0) { double frozen_offset = 0; int vis_frozen_col_count = _get_visible_frozen_column_count(); @@ -805,7 +864,7 @@ namespace mbts.ui.controls { double left = Math.Max(offset, frozen_offset); double right = offset + col.ActualWidth; // If right < frozen_offset column is completely obscured - if (right > frozen_offset && left <= ActualWidth) { + if (include_obscured_cols || (right > frozen_offset && left <= ActualWidth)) { // fully visible yield return new ColumnGeometry() { column = col, @@ -883,6 +942,98 @@ namespace mbts.ui.controls { } #endregion + static class ColumnGeometrySyncHelper { + public static void reset_panel_child(UIElement child, DIYGrid diy_grid) => + reset_panel_child(child, diy_grid, GetColumnKey(child)); + + public static void reset_panel_child(UIElement child, DIYGrid diy_grid, object col_key) { + if (col_key == null) + return; + var key_str = col_key.ToString(); + if (diy_grid._visible_columns.TryGetFirst(c => string.Equals(c.id, key_str, StringComparison.CurrentCultureIgnoreCase), out var target_col, out int target_pos)) { + // set clip to null in case this child was hidden before (see 'else' block). + child.Clip = null; + Grid.SetColumn(child, target_pos); + } else { + // hide element by setting clip to 0x0 rect, rather than visibility to collapsed, + // to avoid incidentally breaking visibility bindings, triggers, animations, etc. + child.Clip = new RectangleGeometry(new Rect(0, 0, 0, 0)); + } + } + + public static void reset_sync(DependencyObject d, DependencyPropertyChangedEventArgs e) { + if (!GetSyncWithColumnGeometry(d)) + SetSyncWithColumnGeometryHook(d, null); + if (!(d is Grid g)) + return; + var hook = new DisposableContainer(); + if (g.IsLoaded) + hook.replace(impl()); + SetSyncWithColumnGeometryHook(d, Disposable.Create(g.SubscribeLoaded(on_loaded), hook)); + // + void on_loaded(object sender, RoutedEventArgs loaded_e) { + if (GetSyncWithColumnGeometry(d)) + hook.replace(impl()); + } + IDisposable impl() { + // Avoid resetting children on every collection change, as that could be very noisy and hurt perf. + // In the visible columns CollectionChanged handler, call reset_children inside a Dispatcher.InvokeAsync request + // to effectively wait for potential batch-changes to finish. + bool resetting_children = false; + if (!(GetSyncWithColumnGeometrySource(d) is DIYGrid diy_grid) && !g.try_find_ancestor_of_type(out diy_grid)) + return null; + g.SetBinding(Grid.WidthProperty, new Binding(nameof(HeaderTotalSizeHack)) { Source = diy_grid }); + reset_children(); + on_layout_changed(diy_grid.GetColumnGeometry(true).ToList()); + diy_grid.ColumnLayoutChanged += on_layout_changed; + diy_grid._visible_columns.CollectionChanged += on_vis_columns_changed; + return Disposable.Create(() => { + diy_grid.ColumnLayoutChanged -= on_layout_changed; + diy_grid._visible_columns.CollectionChanged -= on_vis_columns_changed; + }); + // + void on_vis_columns_changed(object sender, NotifyCollectionChangedEventArgs vce) { + if (!resetting_children) { + // avoid resetting children on every collection change, as that could be very noisy and hurt perf. + resetting_children = true; + diy_grid.Dispatcher.InvokeAsync(() => { + on_layout_changed(diy_grid.GetColumnGeometry().ToList()); + reset_children(); + resetting_children = false; + }); + } + } + void on_layout_changed(List layout) { + // TODO: figure out how to make the ColumnLayoutChanged event lazy, or somehow avoid making 2 lists of columns + // (the layout param does not include obscured cols, so we have to create a new list of ColumnGeoemtry to satisfy our needs). + layout = (from col in diy_grid._visible_columns + join pos in diy_grid.GetColumnGeometry(true) + on col.id equals pos.column.id + select pos).ToList(); + + int n_removed = g.ColumnDefinitions.Count - layout.Count; + if (n_removed > 0) + g.ColumnDefinitions.RemoveRange(g.ColumnDefinitions.Count - n_removed, n_removed); + int i = 0; + for (; i < g.ColumnDefinitions.Count; i++) { + var col_pos = layout[i]; + var col = g.ColumnDefinitions[i]; + if (col.Width.GridUnitType == GridUnitType.Pixel && col.Width.Value == col_pos.visible_width) + continue; + col.Width = new GridLength(col_pos.visible_width, GridUnitType.Pixel); + } + for (; i < layout.Count; i++) { + g.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(layout[i].visible_width, GridUnitType.Pixel) }); + } + } + void reset_children() { + foreach (UIElement child in g.Children) + reset_panel_child(child, diy_grid); + } + } + } + } + class DataContextHooks : IDisposable { List _hooks = new List(4);