DIYGridCellHoverAdorner
This commit is contained in:
1 parent
cd1fab327e
commit
97df614bc0
7 files changed
+627
-33
No files matched your search
@@ -0,0 +1,59 @@
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace mbts.ui.controls.adorners {
|
||||
class AdornerHost : Adorner {
|
||||
UIElement _child;
|
||||
|
||||
public AdornerHost(UIElement adorned_element)
|
||||
: base(adorned_element) { }
|
||||
|
||||
public static readonly DependencyProperty ChildProperty =
|
||||
WPFHelper.create_dp<UIElement, AdornerHost>(
|
||||
nameof(Child), (o, v) => {
|
||||
if (o._child != null)
|
||||
o.RemoveVisualChild(o._child);
|
||||
o._child = v;
|
||||
if (v != null)
|
||||
o.AddVisualChild(v);
|
||||
});
|
||||
|
||||
public UIElement Child {
|
||||
get => _child;
|
||||
set => SetValue(ChildProperty, value);
|
||||
}
|
||||
|
||||
protected override int VisualChildrenCount => _child != null ? 1 : 0;
|
||||
|
||||
protected override Visual GetVisualChild(int index) =>
|
||||
index == 0 && _child != null ? _child : throw new ArgumentOutOfRangeException();
|
||||
|
||||
protected override Size ArrangeOverride(Size finalSize) {
|
||||
if (_child == null) {
|
||||
return base.ArrangeOverride(finalSize);
|
||||
}
|
||||
_child.Arrange(new Rect(finalSize));
|
||||
return _child.DesiredSize;
|
||||
}
|
||||
|
||||
protected override Size MeasureOverride(Size constraint) {
|
||||
if (_child == null) {
|
||||
return base.MeasureOverride(constraint);
|
||||
}
|
||||
_child.Measure(constraint);
|
||||
return _child.DesiredSize;
|
||||
}
|
||||
}
|
||||
|
||||
class AdornerHost<TChild> : AdornerHost where TChild : UIElement {
|
||||
public AdornerHost(UIElement adorned_element)
|
||||
: base(adorned_element) { }
|
||||
|
||||
public new TChild Child {
|
||||
get => base.Child as TChild;
|
||||
set => base.Child = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
using mbts.ui.dpi;
|
||||
using mbts.ui.extensions;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace mbts.ui.controls.adorners {
|
||||
class DIYGridCellHoverAdorner : Adorner<ListBox>, IDisposable {
|
||||
Brush _row_fill, _cell_fill;
|
||||
Pen _row_stroke, _cell_stroke;
|
||||
|
||||
DIYGridRowsPresenter _rows_presenter;
|
||||
ScrollViewer _scroll_viewer;
|
||||
ScrollContentPresenter _scroll_content_backing;
|
||||
DIYGrid _grid;
|
||||
IDisposable _dispose;
|
||||
DIYGrid.CellGeometry? _hover_cell;
|
||||
AdornerHost _menu_host = null;
|
||||
ActionMenu _menu => _menu_host.Child as ActionMenu;
|
||||
|
||||
#region RowFill
|
||||
public static readonly DependencyProperty RowFillProperty =
|
||||
WPFHelper.create_dp<Brush, DIYGridCellHoverAdorner>(
|
||||
nameof(RowFill),
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
(o, v) => o._row_fill = v,
|
||||
FrozenBrushCache.get_brush(DIYGrid.DefaultSelectionHighlight));
|
||||
|
||||
public SolidColorBrush RowFill {
|
||||
get => GetValue(RowFillProperty) as SolidColorBrush;
|
||||
set => SetValue(RowFillProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region RowStroke
|
||||
public static readonly DependencyProperty RowStrokeProperty =
|
||||
WPFHelper.create_dp<Pen, DIYGridCellHoverAdorner>(
|
||||
nameof(RowStroke),
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
(o, v) => o._row_stroke = v);
|
||||
|
||||
public Pen RowStroke {
|
||||
get => GetValue(RowStrokeProperty) as Pen;
|
||||
set => SetValue(RowStrokeProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region CellFill
|
||||
public static readonly DependencyProperty CellFillProperty =
|
||||
WPFHelper.create_dp<Brush, DIYGridCellHoverAdorner>(
|
||||
nameof(CellFill),
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
(o, v) => o._cell_fill = v);
|
||||
|
||||
public SolidColorBrush CellFill {
|
||||
get => GetValue(CellFillProperty) as SolidColorBrush;
|
||||
set => SetValue(CellFillProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
#region CellStroke
|
||||
public static readonly DependencyProperty CellStrokeProperty =
|
||||
WPFHelper.create_dp<Pen, DIYGridCellHoverAdorner>(
|
||||
nameof(CellStroke),
|
||||
FrameworkPropertyMetadataOptions.AffectsRender,
|
||||
(o, v) => o._cell_stroke = v);
|
||||
|
||||
public Pen CellStroke {
|
||||
get => GetValue(CellStrokeProperty) as Pen;
|
||||
set => SetValue(CellStrokeProperty, value);
|
||||
}
|
||||
#endregion
|
||||
|
||||
public DIYGridCellHoverAdorner(DIYGridRowsPresenter adorned_element, ScrollViewer scroll_viewer)
|
||||
: base(adorned_element) {
|
||||
_row_fill = RowFill;
|
||||
_row_stroke = RowStroke;
|
||||
_cell_fill = CellFill;
|
||||
_cell_stroke = CellStroke;
|
||||
_scroll_viewer = scroll_viewer;
|
||||
IsHitTestVisible = false;
|
||||
_rows_presenter = adorned_element;
|
||||
|
||||
_grid = _rows_presenter.find_ancestor_of_type<DIYGrid>();
|
||||
_dispose = Disposable.Create(
|
||||
_grid.SubscribeMouseHoverCell().Subscribe(cell => {
|
||||
_hover_cell = cell;
|
||||
if (_hover_cell.HasValue) {
|
||||
var (row, col) = _hover_cell.Value;
|
||||
var row_tl = row.TranslatePoint(default, this);
|
||||
}
|
||||
InvalidateVisual();
|
||||
}));
|
||||
}
|
||||
|
||||
ScrollContentPresenter _scroll_content =>
|
||||
_scroll_content_backing ?? (_scroll_content_backing = _scroll_viewer.descendants_of_type<ScrollContentPresenter>().FirstOrDefault());
|
||||
|
||||
protected override void OnRender(DrawingContext drawingContext) {
|
||||
base.OnRender(drawingContext);
|
||||
if (_hover_cell.HasValue) {
|
||||
(var row, var cell) = _hover_cell.Value;
|
||||
if (row != null) {
|
||||
var row_tl = new Lazy<Point>(() => row.TranslatePoint(default, this));
|
||||
if (_row_fill != null || _row_stroke != null) {
|
||||
var row_rect = new Rect(row_tl.Value, row.RenderSize);
|
||||
if (_scroll_content != null) {
|
||||
var content_bounds = this.GetChildBounds(_scroll_content);
|
||||
row_rect.Intersect(content_bounds);
|
||||
}
|
||||
drawingContext.DrawRectangle(_row_fill, _row_stroke, row_rect, true);
|
||||
}
|
||||
if (_cell_fill != null || _cell_stroke != null) {
|
||||
var cell_rect = cell.to_visible_rect(row.ActualHeight, row_tl.Value.Y);
|
||||
drawingContext.DrawRectangle(_cell_fill, _cell_stroke, cell_rect, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() =>
|
||||
Disposable.dispose(ref _dispose);
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,37 @@
|
||||
using System;
|
||||
using bts.utils;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
|
||||
namespace mbts.ui.controls {
|
||||
// Column types are nested inside the DIYGrid
|
||||
partial class DIYGrid {
|
||||
public readonly struct ColumnGeometry {
|
||||
public readonly struct CellGeometry {
|
||||
public CellGeometry(ColumnGeometry column, ListBoxItem row) {
|
||||
this.column = column;
|
||||
this.row = row;
|
||||
}
|
||||
|
||||
public ColumnGeometry column { get; }
|
||||
public ListBoxItem row { get; }
|
||||
|
||||
public void Deconstruct(out ListBoxItem row, out ColumnGeometry column) {
|
||||
row = this.row;
|
||||
column = this.column;
|
||||
}
|
||||
|
||||
public Rect to_rect(double y) =>
|
||||
column.to_visible_rect(row.ActualHeight, y);
|
||||
|
||||
public static implicit operator (ListBoxItem, ColumnGeometry) (CellGeometry cell) =>
|
||||
(cell.row, cell.column);
|
||||
}
|
||||
|
||||
public readonly struct ColumnGeometry : IComparable<ColumnGeometry> {
|
||||
public static readonly IEqualityComparer<ColumnGeometry> PositionEqualityComparer =
|
||||
EqualityComparer.Create<ColumnGeometry, (double, double)>(c => (c.left, c.right));
|
||||
|
||||
public readonly ColumnBase column;
|
||||
public readonly DIYGridSuperHeader super_column;
|
||||
public readonly double
|
||||
@@ -32,9 +59,19 @@ namespace mbts.ui.controls {
|
||||
public Rect to_rect(double row_height) =>
|
||||
new Rect(left, 0, width, row_height);
|
||||
|
||||
public Rect to_visible_rect(double row_height, double y) =>
|
||||
new Rect(unclipped_left, y, visible_width, row_height);
|
||||
|
||||
public override string ToString() {
|
||||
return "{3} {0:0.0},{1:0.0} w{2:0.0}".fmt(left, right, width, column.Header);
|
||||
}
|
||||
|
||||
public int CompareTo(ColumnGeometry other) {
|
||||
int comp = left.CompareTo(other.left);
|
||||
if (comp != 0)
|
||||
return comp;
|
||||
return right.CompareTo(other.right);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -24,6 +24,7 @@ using System.Windows.Controls.Primitives;
|
||||
using System.Windows.Data;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace mbts.ui.controls {
|
||||
interface IHandleGridSorting {
|
||||
@@ -235,14 +236,18 @@ namespace mbts.ui.controls {
|
||||
double _row_height = DIYGrid.DefaultRowHeight;
|
||||
ScrollContentPresenter _content_presenter_backing;
|
||||
DIYGrid _grid;
|
||||
DragRectAdorner _drag_rect_adorner;
|
||||
AdornerHost<Rectangle> _drag_rect_adorner;
|
||||
RowSelectionAdorner _rsa;
|
||||
DIYGridCellHoverAdorner _cell_hover_adorner;
|
||||
ScrollViewer _scroll_viewer;
|
||||
Point? _drag_start;
|
||||
double _drag_start_vertical_drift;
|
||||
Point? _drag_start, _drag_stop;
|
||||
double
|
||||
_drag_start_horizontal_drift,
|
||||
_drag_start_vertical_drift;
|
||||
bool _support_group_items;
|
||||
int _drag_start_index;
|
||||
object _drag_start_item;
|
||||
IDisposable _drag_hook;
|
||||
|
||||
static DIYGridRowsPresenter() =>
|
||||
DefaultStyleKeyProperty.OverrideMetadata(typeof(DIYGridRowsPresenter), new FrameworkPropertyMetadata(typeof(DIYGridRowsPresenter)));
|
||||
@@ -260,31 +265,59 @@ namespace mbts.ui.controls {
|
||||
_support_group_items = _grid.SupportGroupItems;
|
||||
_grid.SupportGroupItemsChanged += on_support_group_items_changed;
|
||||
_scroll_viewer = this.descendants_of_type<ScrollViewer>().First();
|
||||
|
||||
// selection highlighting
|
||||
if (SelectionMode == SelectionMode.Single) {
|
||||
_drag_rect_adorner = new DragRectAdorner(this, _scroll_viewer);
|
||||
_drag_rect_adorner.bind(DragRectAdorner.FillProperty, _highlight_path, DIYGrid.SelectionHighlightBrushConverter, _grid);
|
||||
_drag_rect_adorner.bind(DragRectAdorner.StrokeProperty, _highlight_path, DIYGrid.SelectionHighlightPenConverter, _grid);
|
||||
_drag_rect_adorner.bind(OpacityProperty, _active_path, DIYGrid.SelectionHighlightOpacityConverter, _grid);
|
||||
_drag_rect_adorner.attach();
|
||||
}
|
||||
_rsa = new RowSelectionAdorner(this, _scroll_viewer);
|
||||
_rsa.bind(RowSelectionAdorner.FillProperty, _highlight_path, DIYGrid.SelectionHighlightBrushConverter, _grid);
|
||||
_rsa.bind(RowSelectionAdorner.StrokeProperty, _highlight_path, DIYGrid.SelectionHighlightPenConverter, _grid);
|
||||
_rsa.bind(OpacityProperty, _active_path, DIYGrid.SelectionHighlightOpacityConverter, _grid);
|
||||
_rsa.attach();
|
||||
// cell hover adorner
|
||||
_cell_hover_adorner = new DIYGridCellHoverAdorner(this, _scroll_viewer);
|
||||
_cell_hover_adorner.SetBinding(VisibilityProperty, new Binding {
|
||||
Path = new PropertyPath(IsMouseOverProperty),
|
||||
Converter = new converters.BooleanToVisibilityConverter(),
|
||||
Source = this
|
||||
});
|
||||
// drag
|
||||
var explicit_drag_src = Observable.Create<bool>(o => {
|
||||
_begin_explicit_drag += o.OnNext;
|
||||
return Disposable.Create(() => _begin_explicit_drag -= o.OnNext);
|
||||
});
|
||||
_drag_hook = Disposable.Create(
|
||||
DragDropHelper.subscribe_drag_coordinates(this, init_element: _content_presenter, use_preview_events: true, explicit_drag: explicit_drag_src)
|
||||
.Subscribe(pt => {
|
||||
if (_drag_start.HasValue) {
|
||||
if (pt.HasValue) {
|
||||
_drag_stop = pt.Value;
|
||||
_refresh_drag_rect();
|
||||
} else {
|
||||
if (_drag_stop.HasValue) {
|
||||
_end_drag(_drag_stop.Value);
|
||||
}
|
||||
_drag_start = null;
|
||||
_drag_stop = null;
|
||||
}
|
||||
} else if (pt.HasValue) {
|
||||
_begin_drag_selection(pt.Value);
|
||||
}
|
||||
}),
|
||||
_scroll_viewer.SubscribeScrollChanged((s, _) => _refresh_drag_rect()));
|
||||
}
|
||||
|
||||
void on_unloaded(object sender, RoutedEventArgs e) {
|
||||
Disposable.dispose(ref _drag_hook);
|
||||
if (_drag_rect_adorner != null) {
|
||||
_drag_rect_adorner.detach();
|
||||
Disposable.dispose(ref _drag_rect_adorner);
|
||||
_drag_rect_adorner = null;
|
||||
}
|
||||
if (_rsa != null) {
|
||||
_rsa.detach();
|
||||
Disposable.dispose(ref _rsa);
|
||||
}
|
||||
if (_cell_hover_adorner != null) {
|
||||
_cell_hover_adorner.detach();
|
||||
Disposable.dispose(ref _cell_hover_adorner);
|
||||
}
|
||||
}
|
||||
|
||||
void on_row_height_changed(double v) => _row_height = v;
|
||||
@@ -293,6 +326,8 @@ namespace mbts.ui.controls {
|
||||
|
||||
public event GroupItemSelectionPivotEventHandler GroupItemSelectionPivot;
|
||||
|
||||
event Action<bool> _begin_explicit_drag;
|
||||
|
||||
ScrollContentPresenter _content_presenter => _content_presenter_backing ??
|
||||
(_content_presenter_backing = this.descendants_of_type<ScrollContentPresenter>().FirstOrDefault());
|
||||
|
||||
@@ -304,8 +339,28 @@ namespace mbts.ui.controls {
|
||||
base.UnselectAll();
|
||||
}
|
||||
|
||||
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e) {
|
||||
var pt = e.GetPosition(this);
|
||||
public void begin_explicit_drag() {
|
||||
if (_begin_explicit_drag != null) {
|
||||
Mouse.Capture(this, CaptureMode.SubTree);
|
||||
if (!_drag_start.HasValue) {
|
||||
if (_begin_explicit_drag == null) {
|
||||
// capturing the mouse caused listener(s) to unhook from the event
|
||||
// release capture since the drag selection has been aborted.
|
||||
ReleaseMouseCapture();
|
||||
} else {
|
||||
var pt = Mouse.GetPosition(this);
|
||||
if (this.ChildAtPoint(pt, out ListBoxItem row)) {
|
||||
row.Focus();
|
||||
Keyboard.Focus(row);
|
||||
}
|
||||
_begin_explicit_drag(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void _begin_drag_selection(Point pt) {
|
||||
_drag_rect_adorner?.detach();
|
||||
if (InputHitTest(pt) is UIElement el) {
|
||||
var sb = new StringBuilderEx("beginning drag selection on ");
|
||||
OneOf.MatchFirst<DependencyObject, ListBoxItem, GroupItem>(el.VisualAncestors().Prepend(el)).Match(init_common, on_group);
|
||||
@@ -319,22 +374,42 @@ namespace mbts.ui.controls {
|
||||
}
|
||||
}
|
||||
void init_common(ListBoxItem item_container) {
|
||||
// drag state
|
||||
_drag_start = pt;
|
||||
_drag_stop = pt;
|
||||
_drag_start_item = ItemContainerGenerator.ItemFromContainer(item_container);
|
||||
_drag_start_index = ItemContainerGenerator.IndexFromContainer(item_container);
|
||||
_drag_start_horizontal_drift = _scroll_viewer.HorizontalOffset;
|
||||
_drag_start_vertical_drift = _scroll_viewer.VerticalOffset;
|
||||
if (!IsKeyboardFocusWithin)
|
||||
Keyboard.Focus(item_container);
|
||||
// drag rect
|
||||
_drag_rect_adorner = new AdornerHost<Rectangle>(_content_presenter ?? (UIElement)this) {
|
||||
Child = new Rectangle {
|
||||
HorizontalAlignment = HorizontalAlignment.Left,
|
||||
VerticalAlignment = VerticalAlignment.Top,
|
||||
StrokeThickness = HighDpiExtensions.pixel_unit
|
||||
}
|
||||
};
|
||||
_drag_rect_adorner.Child.bind(Shape.FillProperty, _highlight_path, DIYGrid.SelectionHighlightBrushConverter, _grid);
|
||||
_drag_rect_adorner.Child.bind(Shape.StrokeProperty, _highlight_path, DIYGrid.SelectionHighlightOpaqueBrushConverter, _grid);
|
||||
_refresh_drag_rect();
|
||||
_drag_rect_adorner.attach();
|
||||
// log
|
||||
sb.Add($"'{_drag_start_item}' (selection mode {SelectionMode}).");
|
||||
_log.info(sb.ToString());
|
||||
}
|
||||
}
|
||||
base.OnPreviewMouseLeftButtonDown(e);
|
||||
}
|
||||
|
||||
protected override void OnPreviewMouseUp(MouseButtonEventArgs e) {
|
||||
void _end_drag(Point drag_stop) {
|
||||
if (_drag_rect_adorner != null) {
|
||||
_drag_rect_adorner.detach();
|
||||
_drag_rect_adorner = null;
|
||||
}
|
||||
if (_drag_start.HasValue) {
|
||||
_log.info("ending drag selection on mouse up.");
|
||||
if (SelectionMode != SelectionMode.Single && _content_presenter != null) {
|
||||
var drag_stop = e.GetPosition(this);
|
||||
drag_stop.X = MathUtils.within_range(0, _content_presenter.RenderSize.Width - 1, drag_stop.X);
|
||||
drag_stop.Y = MathUtils.within_range(0, _content_presenter.RenderSize.Height - 1, drag_stop.Y);
|
||||
double vertical_diff = drag_stop.Y + (_row_height * (_scroll_viewer.VerticalOffset - _drag_start_vertical_drift)) - _drag_start.Value.Y;
|
||||
@@ -396,7 +471,22 @@ namespace mbts.ui.controls {
|
||||
_drag_start = null;
|
||||
_drag_start_item = null;
|
||||
}
|
||||
base.OnPreviewMouseUp(e);
|
||||
}
|
||||
|
||||
void _refresh_drag_rect() {
|
||||
if (_drag_rect_adorner == null || !_drag_start.HasValue || !_drag_stop.HasValue)
|
||||
return;
|
||||
var hdiff = _scroll_viewer.HorizontalOffset - _drag_start_horizontal_drift;
|
||||
var vdiff = _scroll_viewer.VerticalOffset - _drag_start_vertical_drift;
|
||||
var rect = new Rect(
|
||||
new Point(_drag_start.Value.X - hdiff, _drag_start.Value.Y - vdiff * _grid.RowHeight),
|
||||
_drag_stop.Value);
|
||||
if (rect.IsEmpty)
|
||||
return;
|
||||
rect.Intersect(this.GetChildBounds(_drag_rect_adorner.AdornedElement));
|
||||
_drag_rect_adorner.Child.Margin = new Thickness(rect.Left, rect.Top, 0, 0);
|
||||
_drag_rect_adorner.Child.Width = rect.Width;
|
||||
_drag_rect_adorner.Child.Height = rect.Height;
|
||||
}
|
||||
|
||||
void _select_items_between(object inclusive_begin, object inclusive_end) {
|
||||
@@ -546,9 +636,21 @@ namespace mbts.ui.controls {
|
||||
public static IValueConverter SelectionHighlightBrushConverter =>
|
||||
ValueConverter.create<Color, Brush>(FrozenBrushCache.get_brush);
|
||||
|
||||
public static IValueConverter SelectionHighlightOpaqueBrushConverter =>
|
||||
ValueConverter.create<Color, Brush>(c => FrozenBrushCache.get_brush(c.with(255)));
|
||||
|
||||
public static IValueConverter SelectionHighlightOpacityConverter =>
|
||||
ValueConverter.create<bool, double>(is_active => is_active ? 1.0 : 0.5);
|
||||
|
||||
public static IValueConverter RowHoverHighlightBrushConverter =>
|
||||
ValueConverter.create<Color, Brush>(c => FrozenBrushCache.get_brush(c, 0.35));
|
||||
|
||||
public static IValueConverter CellHoverHighlightPenConverter =>
|
||||
ValueConverter.create<Color, Pen>(c => FrozenPenCache.get_pen(c.with(255)));
|
||||
|
||||
public static IValueConverter CellHoverHighlightBrushConverter =>
|
||||
ValueConverter.create<Color, Brush>(c => FrozenBrushCache.get_brush(c, 0.5));
|
||||
|
||||
public bool SupportGroupItems {
|
||||
get => _support_group_items;
|
||||
set {
|
||||
@@ -1428,19 +1530,201 @@ namespace mbts.ui.controls {
|
||||
|
||||
public static IObservable<T> SubscribeMouseHoverRow<T>(this DIYGrid grid, IEqualityComparer<T> comparer) where T : class =>
|
||||
Observable.Create<T>(o => {
|
||||
T curr_row = null;
|
||||
T last_row = null;
|
||||
return grid.SubscribeMouseHoverRowContainer().Subscribe(row_view => {
|
||||
if (grid.RowContainerGenerator.ItemFromContainer(row_view) is T row && !comparer.Equals(row, last_row))
|
||||
o.OnNext(last_row = row);
|
||||
});
|
||||
});
|
||||
|
||||
public static IObservable<ListBoxItem> SubscribeMouseHoverRowContainer(this DIYGrid grid) =>
|
||||
Observable.Create<ListBoxItem>(o => {
|
||||
bool busy = false, dirty = false;
|
||||
ListBoxItem curr_row = null;
|
||||
return Disposable.Create(
|
||||
grid.SubscribeMouseEnter(on_mouse_enter),
|
||||
grid.SubscribeMouseLeave(on_mouse_leave),
|
||||
grid.SubscribePreviewMouseMove(on_mouse_move));
|
||||
void on_mouse_enter(object sender, MouseEventArgs e) => refresh();
|
||||
void on_mouse_move(object sender, MouseEventArgs e) => refresh();
|
||||
void on_mouse_leave(object sender, MouseEventArgs e) => refresh();
|
||||
grid.SubscribeMouseEnter(on_mouse),
|
||||
grid.SubscribeMouseLeave(on_mouse),
|
||||
grid.SubscribeMouseLeftButtonDown(on_mouse),
|
||||
grid.SubscribeMouseLeftButtonUp(on_mouse),
|
||||
grid.SubscribeMouseMove(on_mouse));
|
||||
void on_mouse(object sender, MouseEventArgs e) => refresh();
|
||||
void refresh() {
|
||||
T row = grid.ChildAtPoint<ListBoxItem>(Mouse.GetPosition(grid)) is var rview && rview != null ? grid.RowContainerGenerator.ItemFromContainer(rview) as T : null;
|
||||
if (!comparer.Equals(curr_row, row))
|
||||
o.OnNext(curr_row = row);
|
||||
if (busy) {
|
||||
dirty = true;
|
||||
} else {
|
||||
dirty = false;
|
||||
busy = true;
|
||||
do {
|
||||
grid.Dispatcher.InvokeAsync(() => {
|
||||
var row = grid.ChildAtPoint<ListBoxItem>(Mouse.GetPosition(grid));
|
||||
if (row != curr_row)
|
||||
o.OnNext(curr_row = row);
|
||||
dirty = false;
|
||||
});
|
||||
} while (dirty);
|
||||
busy = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
public static IObservable<DIYGrid.CellGeometry?> SubscribeMouseHoverCell(this DIYGrid grid) {
|
||||
return Observable.Create<DIYGrid.CellGeometry?>(o => {
|
||||
IDisposable row_mouse_hooks = null;
|
||||
DIYGrid.CellGeometry? last_value = null;
|
||||
var layout = grid.GetColumnGeometry().ToList();
|
||||
var layout_x_pts = layout.WrapSelector(cg => cg.left, null);
|
||||
ListBoxItem hover_row = null;
|
||||
grid.ColumnLayoutChanged += on_layout;
|
||||
var hover_sub = grid.SubscribeMouseHoverRowContainer().Subscribe(on_row_view);
|
||||
return Disposable.Create(dispose);
|
||||
//
|
||||
void notify(MouseEventArgs e = null) {
|
||||
if (hover_row == null || layout.Count == 0)
|
||||
on_cell(null);
|
||||
else {
|
||||
var pt = e?.GetPosition(hover_row) ?? Mouse.GetPosition(hover_row);
|
||||
int index = CollectionHelper.binary_search(layout_x_pts, pt.X);
|
||||
if (index < 0)
|
||||
index = ~index - 1;
|
||||
if (index >= 0 && index < layout.Count) {
|
||||
on_cell(new DIYGrid.CellGeometry(layout[index], hover_row));
|
||||
} else {
|
||||
on_cell(null);
|
||||
}
|
||||
}
|
||||
}
|
||||
void on_cell(DIYGrid.CellGeometry? cell) {
|
||||
if (!cell.Equals(last_value))
|
||||
o.OnNext(last_value = cell);
|
||||
}
|
||||
void on_row_view(ListBoxItem row_view) {
|
||||
row_mouse_hooks?.Dispose();
|
||||
hover_row = row_view;
|
||||
row_mouse_hooks = row_view?.SubscribeMouseMove(on_mouse_change);
|
||||
notify();
|
||||
}
|
||||
void on_layout(List<DIYGrid.ColumnGeometry> obj) {
|
||||
layout = grid.GetColumnGeometry().ToList();
|
||||
layout_x_pts = layout.WrapSelector(cg => cg.left, null);
|
||||
notify();
|
||||
}
|
||||
void on_mouse_change(object sender, MouseEventArgs e) =>
|
||||
notify(e);
|
||||
void dispose() {
|
||||
Disposable.dispose(ref hover_sub);
|
||||
grid.ColumnLayoutChanged -= on_layout;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static IObservable<(IEnumerable<DIYGrid.ColumnGeometry> columns, int row_start, int row_count)?> SubscribeCellDragRect(this DIYGrid grid) {
|
||||
return Observable.Create<(IEnumerable<DIYGrid.ColumnGeometry> columns, int row_start, int row_count)?>(o => {
|
||||
return grid.ConnectOnLoaded(init);
|
||||
IDisposable init() {
|
||||
if (!( grid.try_find_descendent_of_type(out DIYGridRowsPresenter presenter) && presenter.try_find_descendent_of_type(out ScrollViewer scroll) && scroll.try_find_descendent_of_type(out ScrollContentPresenter scroll_content)))
|
||||
return null;
|
||||
// init state
|
||||
var value_container = new DistinctSubject<(IEnumerable<DIYGrid.ColumnGeometry> columns, int row_start, int row_count)?>();
|
||||
double y_start_drift = 0;
|
||||
var layout = new List<DIYGrid.ColumnGeometry>();
|
||||
var layout_x_pts = layout.WrapSelector(cg => cg.left, null);
|
||||
object _drag_start_item = null;
|
||||
int _drag_start_index = -1;
|
||||
Point? drag_start = null, drag_curr = null;
|
||||
//
|
||||
scroll.ScrollChanged += on_scroll;
|
||||
grid.ColumnLayoutChanged += on_layout;
|
||||
var drag_hook = DragDropHelper.subscribe_drag_coordinates(presenter, grid, use_preview_events: false, mouse_button: MouseButton.Right).Subscribe(pt => {
|
||||
if (drag_start.HasValue) {
|
||||
if (!pt.HasValue) {
|
||||
drag_start = null;
|
||||
drag_curr = null;
|
||||
} else {
|
||||
drag_curr = pt;
|
||||
}
|
||||
} else {
|
||||
if (pt.HasValue) {
|
||||
var item_container = presenter.ChildAtPoint<ListBoxItem>(pt.Value);
|
||||
if (item_container == null) {
|
||||
pt = null;
|
||||
} else {
|
||||
_drag_start_item = grid.RowContainerGenerator.ItemFromContainer(item_container);
|
||||
_drag_start_index = grid.RowContainerGenerator.IndexFromContainer(item_container);
|
||||
y_start_drift = scroll.VerticalOffset;
|
||||
}
|
||||
}
|
||||
drag_start = pt;
|
||||
drag_curr = pt;
|
||||
}
|
||||
refresh();
|
||||
});
|
||||
grid.PreviewMouseUp += (s, e) => e.Handled = e.ChangedButton == MouseButton.Right;
|
||||
var sub = value_container.Subscribe(o);
|
||||
return Disposable.Create(dispose);
|
||||
void on_layout(List<DIYGrid.ColumnGeometry> new_layout) {
|
||||
layout = new_layout;
|
||||
layout_x_pts = layout.WrapSelector(cg => cg.left, null);
|
||||
}
|
||||
void on_scroll(object sender, ScrollChangedEventArgs e) {
|
||||
if (drag_start.HasValue)
|
||||
refresh();
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
if (!drag_start.HasValue) {
|
||||
value_container.OnNext(null);
|
||||
return;
|
||||
}
|
||||
var (xstart, ystart) = drag_start.Value.deconstruct();
|
||||
var (xstop, ystop) = drag_curr.Value.deconstruct();
|
||||
xstart = MathUtils.within_range(0, presenter.ActualWidth, xstart);
|
||||
var row_height = grid.RowHeight;
|
||||
xstop = MathUtils.within_range(0, scroll_content.RenderSize.Width - 1, xstop);
|
||||
ystop = MathUtils.within_range(0, scroll_content.RenderSize.Height - 1, ystop);
|
||||
double vertical_diff = ystop + (row_height * (scroll.VerticalOffset - y_start_drift)) - ystart;
|
||||
int start_index, n_items;
|
||||
if (vertical_diff > 0) {
|
||||
double first_item_partial_height = row_height - (ystart % row_height);
|
||||
n_items = (int)Math.Ceiling((vertical_diff - first_item_partial_height) / row_height) + 1;
|
||||
start_index = _drag_start_index;
|
||||
int overflow = start_index + n_items - presenter.Items.Count;
|
||||
if (overflow > 0)
|
||||
n_items -= overflow;
|
||||
} else {
|
||||
double first_item_partial_height = ystart % row_height;
|
||||
n_items = -(int)Math.Floor((vertical_diff + first_item_partial_height) / row_height) + 1;
|
||||
start_index = _drag_start_index - n_items + 1;
|
||||
if (start_index < 0) {
|
||||
n_items += start_index;
|
||||
start_index = 0;
|
||||
}
|
||||
}
|
||||
if (n_items > 0) {
|
||||
var colstart = find_col(xstart);
|
||||
var colstop = find_col(xstop);
|
||||
(colstart, colstop) = MathUtils.get_outliers(colstart, colstop);
|
||||
(ystart, ystop) = MathUtils.get_outliers(ystart, ystop);
|
||||
var cols = layout.WrapSubRange(colstart, colstop - colstart + 1);
|
||||
value_container.OnNext((cols, start_index, n_items));
|
||||
}
|
||||
}
|
||||
|
||||
int find_col(double x) {
|
||||
if (layout.Count == 0)
|
||||
return -1;
|
||||
int i = CollectionHelper.binary_search(layout, x, l => l.left);
|
||||
if (i < 0) {
|
||||
i = ~i - 1;
|
||||
}
|
||||
return MathUtils.within_range(0, layout.Count - 1, i);
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
Disposable.dispose(ref sub);
|
||||
grid.ColumnLayoutChanged -= on_layout;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -73,13 +73,15 @@ namespace mbts.ui {
|
||||
FrameworkElement element,
|
||||
FrameworkElement init_element = null,
|
||||
bool use_preview_events = false,
|
||||
MouseButton mouse_button = MouseButton.Left) {
|
||||
MouseButton mouse_button = MouseButton.Left,
|
||||
IObservable<bool> explicit_drag = null) {
|
||||
if (element == null)
|
||||
throw new ArgumentNullException(nameof(element));
|
||||
if (init_element == null)
|
||||
init_element = element;
|
||||
return Observable.Create<Point?>(o => {
|
||||
Point? mouse_down = null, drag_start = null;
|
||||
bool is_explict_drag = false;
|
||||
if (use_preview_events) {
|
||||
init_element.PreviewMouseDown += on_mouse_down;
|
||||
element.PreviewMouseMove += on_mouse_move;
|
||||
@@ -89,6 +91,12 @@ namespace mbts.ui {
|
||||
element.MouseMove += on_mouse_move;
|
||||
element.MouseUp += on_mouse_up;
|
||||
}
|
||||
IDisposable explicit_drag_hook = explicit_drag?.Subscribe(begin_drag => {
|
||||
if (begin_drag && drag_start == null && mouse_down == null) {
|
||||
mouse_down = Mouse.GetPosition(element);
|
||||
is_explict_drag = true;
|
||||
}
|
||||
});
|
||||
return Disposable.Create(() => {
|
||||
if (use_preview_events) {
|
||||
init_element.PreviewMouseDown -= on_mouse_down;
|
||||
@@ -99,10 +107,12 @@ namespace mbts.ui {
|
||||
element.MouseMove -= on_mouse_move;
|
||||
element.MouseUp -= on_mouse_up;
|
||||
}
|
||||
Disposable.dispose(ref explicit_drag_hook);
|
||||
});
|
||||
// callbacks
|
||||
void on_mouse_up(object sender, MouseButtonEventArgs e) {
|
||||
mouse_down = null;
|
||||
is_explict_drag = false;
|
||||
if (drag_start.HasValue)
|
||||
o.OnNext(drag_start = null);
|
||||
}
|
||||
@@ -116,13 +126,16 @@ namespace mbts.ui {
|
||||
e.Handled = true;
|
||||
drag_start = curr_pt;
|
||||
mouse_down = null;
|
||||
is_explict_drag = false;
|
||||
o.OnNext(curr_pt);
|
||||
}
|
||||
}
|
||||
}
|
||||
void on_mouse_down(object sender, MouseButtonEventArgs e) {
|
||||
drag_start = null;
|
||||
mouse_down = e.GetPosition(element);
|
||||
if (!is_explict_drag) {
|
||||
drag_start = null;
|
||||
mouse_down = e.GetPosition(element);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -32,6 +32,9 @@ namespace mbts.ui {
|
||||
Converter = converter
|
||||
});
|
||||
|
||||
public static (double x, double y) deconstruct(this Point point) =>
|
||||
(point.X, point.Y);
|
||||
|
||||
public static object get_template_child(this Control control, string name) {
|
||||
if (control == null) throw new ArgumentNullException(nameof(control));
|
||||
return control?.Template?.FindName(name, control);
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
using mbts;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reactive.Subjects;
|
||||
|
||||
namespace bts.utils {
|
||||
public class DistinctSubject<T> : ISubject<T> {
|
||||
readonly object _lock;
|
||||
readonly IEqualityComparer<T> _comparer;
|
||||
T _value;
|
||||
|
||||
public DistinctSubject()
|
||||
: this(false) { }
|
||||
|
||||
public DistinctSubject(bool is_synchronized)
|
||||
: this(is_synchronized, EqualityComparer<T>.Default) { }
|
||||
|
||||
public DistinctSubject(bool is_synchronized, IEqualityComparer<T> comparer) {
|
||||
if (is_synchronized)
|
||||
_lock = new object();
|
||||
_comparer = comparer;
|
||||
}
|
||||
|
||||
public event Action<T> ValueChanged;
|
||||
|
||||
public bool IsSynchronized => _lock != null;
|
||||
|
||||
public T Value {
|
||||
get {
|
||||
if (IsSynchronized) {
|
||||
lock (_lock)
|
||||
return _value;
|
||||
}
|
||||
return _value;
|
||||
}
|
||||
set {
|
||||
if (IsSynchronized) {
|
||||
lock (_lock) {
|
||||
if (!_comparer.Equals(_value, value)) {
|
||||
_value = value;
|
||||
ValueChanged?.Invoke(value);
|
||||
}
|
||||
}
|
||||
} else if (!_comparer.Equals(_value, value)) {
|
||||
_value = value;
|
||||
ValueChanged?.Invoke(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnCompleted() { }
|
||||
|
||||
public void OnError(Exception error) { }
|
||||
|
||||
public void OnNext(T value) =>
|
||||
Value = value;
|
||||
|
||||
public IDisposable Subscribe(IObserver<T> observer) {
|
||||
if (IsSynchronized) {
|
||||
lock (_lock)
|
||||
ValueChanged += observer.OnNext;
|
||||
return Disposable.Create(() => {
|
||||
lock (_lock)
|
||||
ValueChanged -= observer.OnNext;
|
||||
});
|
||||
} else {
|
||||
ValueChanged += observer.OnNext;
|
||||
return Disposable.Create(() => ValueChanged -= observer.OnNext);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user