diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs b/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs index 5f205a0f5..c6a19a17b 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewView.xaml.cs @@ -155,6 +155,7 @@ namespace mbts.ui.spark { void _show_popup(object content, object header, ResourceDictionary resources) { var view = new HeaderedContentControl { + DataContext = content, Content = content, Header = header, HorizontalAlignment = HorizontalAlignment.Left, diff --git a/src/client/spark/view/Controls/DIYGrid/DIYGridPopupManager.cs b/src/client/spark/view/Controls/DIYGrid/DIYGridPopupManager.cs index 51e56963d..fcf563bd0 100644 --- a/src/client/spark/view/Controls/DIYGrid/DIYGridPopupManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/DIYGridPopupManager.cs @@ -1,118 +1,126 @@ -using bts.utils; -using mbts.ui.controls; -using mbts.ui.dpi; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using System.Windows; -using System.Windows.Controls; -using System.Windows.Controls.Primitives; -using System.Windows.Data; -using System.Windows.Input; -using System.Windows.Media; - -namespace mbts.ui.spark { - class DIYGridPopupManager { - readonly DIYGrid _grid; - readonly FrameworkElement _owner; - readonly ScrollContentPresenter _scroll_content; - readonly ICollection _children; - Popup _active_popup; - bool _is_activating_popup; - - public static DIYGridPopupManager create(FrameworkElement owner) => - create(owner, new VisualCollectionWrapper(new VisualCollection(owner))); - - public static DIYGridPopupManager create(FrameworkElement owner, ICollection visual_children) => - owner.try_find_descendent_of_type(out DIYGrid grid) && - grid.try_find_descendent_of_type(out DIYGridRowsPresenter rows_presenter) && - rows_presenter.try_find_descendent_of_type(out ScrollContentPresenter scroll_content) ? - new DIYGridPopupManager(owner, grid, scroll_content, visual_children) : - null; - - DIYGridPopupManager(FrameworkElement owner, DIYGrid grid, ScrollContentPresenter scroll_content, ICollection children) { - _owner = owner ?? throw new ArgumentNullException(nameof(owner)); - _grid = grid ?? throw new ArgumentNullException(nameof(grid)); - _scroll_content = scroll_content ?? throw new ArgumentNullException(nameof(scroll_content)); - _children = children; - } - - public Popup active_popup { - get => _active_popup; - set { - if (_active_popup != value) { - _active_popup = value; - if (_active_popup != null) { - _is_activating_popup = true; - _active_popup.IsOpen = false; - _active_popup.IsOpen = true; - _is_activating_popup = false; - } - } - } - } - - public async void show(FrameworkElement view, int delay_measure_ms = 100) { - var layout = _grid.GetColumnGeometry().ToList(); - if (layout.Count == 0) - return; - var x_origin = layout[0].unclipped_left; - var mouse_pos = Mouse.GetPosition(_scroll_content); - var col_index = CollectionHelper.binary_search(layout, mouse_pos.X + x_origin, cg => cg.left); - if (col_index < 0) - col_index = ~col_index - 1; - col_index = MathUtils.within_range(0, layout.Count - 1, col_index); - var col = layout[col_index]; - // determine top left - var top_left = _scroll_content.PointToScreenPixels( - col.unclipped_left - x_origin, - Math.Ceiling(mouse_pos.Y / _grid.RowHeight) * _grid.RowHeight); - if (delay_measure_ms > 0) - await Task.Delay(delay_measure_ms); - view.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); - // popup - var popup = new ZPopup { - PlacementTarget = _grid, - Placement = PlacementMode.Absolute, - Child = view, - IsOpen = true, - HorizontalOffset = top_left.X, - VerticalOffset = top_left.Y, - StaysOpen = false - }; - popup.SetBinding(WindowStyle.UnsafeWindowProperty, new Binding { - Source = _owner, - Path = new PropertyPath(WindowStyle.UnsafeWindowProperty) - }); - view.InputBindings.Add(new KeyBinding(ApplicationCommands.Close, Key.Escape, ModifierKeys.None)); - view.CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, (s, e) => popup.IsOpen = false)); - - view.PreviewMouseDown += on_view_preview_mouse_down; - var drag_hooks = PopupViewController.enable_drag_move(_owner, popup); - _active_popup = popup; - _children.Add(popup); - _owner.ReleaseMouseCapture(); - popup.Closed += on_closed; - FocusManager.SetIsFocusScope(view, true); - Keyboard.Focus(view); - view.Focus(); - // - void on_closed(object sender, EventArgs e) { - if (_is_activating_popup && popup == _active_popup) { - return; - } - view.PreviewMouseDown -= on_view_preview_mouse_down; - Disposable.dispose(ref drag_hooks); - popup.ReleaseMouseCapture(); - if (Mouse.Captured == _owner) - _owner.ReleaseMouseCapture(); - popup.Closed -= on_closed; - _children.Remove(popup); - } - - void on_view_preview_mouse_down(object sender, MouseButtonEventArgs e) => - active_popup = popup; - } - } -} +using bts.utils; +using mbts.ui.controls; +using mbts.ui.dpi; +using mbts.ui.extensions; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Controls.Primitives; +using System.Windows.Data; +using System.Windows.Input; +using System.Windows.Media; + +namespace mbts.ui.spark { + class DIYGridPopupManager { + readonly DIYGrid _grid; + readonly FrameworkElement _owner; + readonly ScrollContentPresenter _scroll_content; + readonly ICollection _children; + Popup _active_popup; + bool _is_activating_popup; + + public static DIYGridPopupManager create(FrameworkElement owner) => + create(owner, new VisualCollectionWrapper(new VisualCollection(owner))); + + public static DIYGridPopupManager create(FrameworkElement owner, ICollection visual_children) => + owner.try_find_descendent_of_type(out DIYGrid grid) && + grid.try_find_descendent_of_type(out DIYGridRowsPresenter rows_presenter) && + rows_presenter.try_find_descendent_of_type(out ScrollContentPresenter scroll_content) ? + new DIYGridPopupManager(owner, grid, scroll_content, visual_children) : + null; + + DIYGridPopupManager(FrameworkElement owner, DIYGrid grid, ScrollContentPresenter scroll_content, ICollection children) { + _owner = owner ?? throw new ArgumentNullException(nameof(owner)); + _grid = grid ?? throw new ArgumentNullException(nameof(grid)); + _scroll_content = scroll_content ?? throw new ArgumentNullException(nameof(scroll_content)); + _children = children; + } + + public Popup active_popup { + get => _active_popup; + set { + if (_active_popup != value) { + _active_popup = value; + if (_active_popup != null) { + _is_activating_popup = true; + _active_popup.IsOpen = false; + _active_popup.IsOpen = true; + _is_activating_popup = false; + } + } + } + } + + public async void show(FrameworkElement view, int delay_measure_ms = 100) { + var layout = _grid.GetColumnGeometry().ToList(); + if (layout.Count == 0) + return; + var x_origin = layout[0].unclipped_left; + var mouse_pos = Mouse.GetPosition(_scroll_content); + var col_index = CollectionHelper.binary_search(layout, mouse_pos.X + x_origin, cg => cg.left); + if (col_index < 0) + col_index = ~col_index - 1; + col_index = MathUtils.within_range(0, layout.Count - 1, col_index); + var col = layout[col_index]; + // determine top left + var top_left = _scroll_content.PointToScreenPixels( + col.unclipped_left - x_origin, + Math.Ceiling(mouse_pos.Y / _grid.RowHeight) * _grid.RowHeight); + if (delay_measure_ms > 0) + await Task.Delay(delay_measure_ms); + view.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity)); + // popup + var popup = new ZPopup { + PlacementTarget = _grid, + Placement = PlacementMode.Absolute, + Child = view, + IsOpen = true, + HorizontalOffset = top_left.X, + VerticalOffset = top_left.Y, + StaysOpen = false + }; + popup.SetBinding(WindowStyle.UnsafeWindowProperty, new Binding { + Source = _owner, + Path = new PropertyPath(WindowStyle.UnsafeWindowProperty) + }); + view.InputBindings.Add(new KeyBinding(ApplicationCommands.Close, Key.Escape, ModifierKeys.None)); + view.CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, (s, e) => popup.IsOpen = false)); + + view.PreviewMouseDown += on_view_preview_mouse_down; + view.ConnectDataContext(rvc => { + rvc.RequestClose += on_request_close; + return Disposable.Create(() => rvc.RequestClose -= on_request_close); + void on_request_close(object sender, EventArgs e) { + popup.IsOpen = false; + } + }); + var drag_hooks = PopupViewController.enable_drag_move(_owner, popup); + _active_popup = popup; + _children.Add(popup); + _owner.ReleaseMouseCapture(); + popup.Closed += on_closed; + FocusManager.SetIsFocusScope(view, true); + Keyboard.Focus(view); + view.Focus(); + // + void on_closed(object sender, EventArgs e) { + if (_is_activating_popup && popup == _active_popup) { + return; + } + view.PreviewMouseDown -= on_view_preview_mouse_down; + Disposable.dispose(ref drag_hooks); + popup.ReleaseMouseCapture(); + if (Mouse.Captured == _owner) + _owner.ReleaseMouseCapture(); + popup.Closed -= on_closed; + _children.Remove(popup); + } + + void on_view_preview_mouse_down(object sender, MouseButtonEventArgs e) => + active_popup = popup; + } + } +}