SPARK-394 Change Ladder right click functionality
This commit is contained in:
1 parent
5654e79535
commit
d884114fc2
8 files changed
+156
-63
No files matched your search
@@ -1,4 +1,6 @@
|
||||
using System;
|
||||
using mbts.spark;
|
||||
using mbts.ui.extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -115,31 +117,5 @@ namespace mbts.ui.controls {
|
||||
dc.DrawText(_bottom_text, new Point(border_thickness, ActualHeight - _bottom_text.Height));
|
||||
}
|
||||
}
|
||||
|
||||
#region Drag/Drop
|
||||
Point _drag_start = new Point(0,0);
|
||||
Vector _min_drag_delta = new Vector(SystemParameters.MinimumHorizontalDragDistance, SystemParameters.MinimumVerticalDragDistance);
|
||||
protected override void OnMouseMove(MouseEventArgs e) {
|
||||
if (e.LeftButton == MouseButtonState.Pressed) {
|
||||
var loc = e.GetPosition(this);
|
||||
var delta = loc - _drag_start;
|
||||
if (delta.Length > _min_drag_delta.Length) {
|
||||
Logger.info(bts.log.Subsystem.Ui, "Begin Drag");
|
||||
DragDrop.DoDragDrop(this, DataContext, DragDropEffects.Move|DragDropEffects.All);
|
||||
}
|
||||
}
|
||||
base.OnMouseMove(e);
|
||||
}
|
||||
protected override void OnMouseDown(MouseButtonEventArgs e) {
|
||||
base.OnMouseDown(e);
|
||||
CaptureMouse();
|
||||
Logger.info(bts.log.Subsystem.Ui, "Mouse Down");
|
||||
}
|
||||
protected override void OnMouseUp(MouseButtonEventArgs e) {
|
||||
base.OnMouseUp(e);
|
||||
ReleaseMouseCapture();
|
||||
Logger.info(bts.log.Subsystem.Ui, "Mouse Up");
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -116,7 +116,7 @@ namespace mbts.ui.controls
|
||||
CoerceValue(DragDropManagerProperty);
|
||||
var effects = DragDropManager.Drag(container.Content);
|
||||
if (effects != DragDropEffects.None) {
|
||||
using (new DropIndicatorAdorner<DraggableContentControl>(this))
|
||||
using (new ItemsControlDropIndicatorAdorner<DraggableContentControl>(this))
|
||||
using (new DragShadowAdorner(this, container.get_drag_shadow()))
|
||||
DragDrop.DoDragDrop(this, new DataObject(DataFormats.StringFormat, container.Content), effects);
|
||||
}
|
||||
|
||||
@@ -116,7 +116,7 @@ namespace mbts.ui.controls
|
||||
CoerceValue(DragDropManagerProperty);
|
||||
var effects = DragDropManager.Drag(container.Content);
|
||||
if (effects != DragDropEffects.None) {
|
||||
using (new DropIndicatorAdorner<DraggableListBoxItem>(this))
|
||||
using (new ItemsControlDropIndicatorAdorner<DraggableListBoxItem>(this))
|
||||
using (new DragShadowAdorner(this, container.get_drag_shadow()))
|
||||
DragDrop.DoDragDrop(this, new DataObject(DataFormats.StringFormat, container.Content), effects);
|
||||
}
|
||||
|
||||
@@ -1,16 +1,14 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Documents;
|
||||
using System.Windows.Media;
|
||||
using System.Windows.Shapes;
|
||||
|
||||
namespace mbts.ui.controls
|
||||
{
|
||||
internal class DragShadowAdorner : Adorner, IDisposable
|
||||
class DragShadowAdorner : Adorner, IDisposable
|
||||
{
|
||||
public DragShadowAdorner(ItemsControl adorned_element, UIElement drag_shadow)
|
||||
public DragShadowAdorner(UIElement adorned_element, UIElement drag_shadow)
|
||||
: base(adorned_element) {
|
||||
IsHitTestVisible = false;
|
||||
_drag_shadow = drag_shadow;
|
||||
|
||||
@@ -9,18 +9,17 @@ using System.Windows.Media;
|
||||
namespace mbts.ui.controls
|
||||
{
|
||||
/// <summary>
|
||||
/// This class is used during DragDrop operations to display an InsertionArrow on the item that is poised to accept the drop.
|
||||
/// The ItemContainer must implement IHaveDropIndicatorZone.
|
||||
/// This class is used during DragDrop operations to display a visual drop indicator relative to the drop target.
|
||||
/// </summary>
|
||||
internal class DropIndicatorAdorner<TItemContainer> : Adorner, IDisposable where TItemContainer : UIElement, IHaveDropIndicatorZone
|
||||
{
|
||||
public DropIndicatorAdorner(ItemsControl adorned_element)
|
||||
: base(adorned_element) {
|
||||
abstract class DropIndicatorAdornerBase<T, TItemContainer> : Adorner, IDisposable
|
||||
where T : UIElement
|
||||
where TItemContainer : UIElement {
|
||||
protected DropIndicatorAdornerBase(T adorned_element) : base(adorned_element) {
|
||||
IsHitTestVisible = false;
|
||||
_hooks.Add(adorned_element.SubscribeDragEnter((s, e) => {
|
||||
if(e.OriginalSource is DependencyObject) {
|
||||
var container = adorned_element.ContainerFromElement((DependencyObject)e.OriginalSource) as TItemContainer;
|
||||
if(container != null && container != _drop_target) {
|
||||
if (e.OriginalSource is DependencyObject) {
|
||||
var container = get_drop_target((DependencyObject)e.OriginalSource);
|
||||
if (container != null && container != _drop_target) {
|
||||
_drop_target = container;
|
||||
InvalidateVisual();
|
||||
}
|
||||
@@ -29,23 +28,32 @@ namespace mbts.ui.controls
|
||||
AdornerLayer.GetAdornerLayer(adorned_element).Add(this);
|
||||
}
|
||||
|
||||
private IList<IDisposable> _hooks = new List<IDisposable>();
|
||||
private TItemContainer _drop_target;
|
||||
private ItemsControl _items_control { get { return (ItemsControl)AdornedElement; } }
|
||||
IList<IDisposable> _hooks = new List<IDisposable>();
|
||||
TItemContainer _drop_target;
|
||||
|
||||
public new T AdornedElement => (T)base.AdornedElement;
|
||||
|
||||
protected virtual TItemContainer get_drop_target(DependencyObject original_source) => AdornedElement.FindAncestor<TItemContainer>();
|
||||
protected abstract Rect get_drop_inidicator_zone(TItemContainer drop_target);
|
||||
protected override void OnRender(DrawingContext drawingContext) {
|
||||
if (_drop_target != null) {
|
||||
var diz = _drop_target.get_drop_indicator_zone();
|
||||
var diz = get_drop_inidicator_zone(_drop_target);
|
||||
if (diz != null) {
|
||||
var relative_tl = _drop_target.TranslatePoint(diz.TopLeft, _items_control);
|
||||
var relative_tl = _drop_target.TranslatePoint(diz.TopLeft, AdornedElement);
|
||||
drawingContext.DrawImage(Icons.InsertionArrow, new Rect(relative_tl.X, relative_tl.Y, diz.Width, diz.Height));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose() {
|
||||
Disposable.dispose(_hooks);
|
||||
AdornerLayer.GetAdornerLayer(AdornedElement).Remove(this);
|
||||
}
|
||||
}
|
||||
|
||||
class ItemsControlDropIndicatorAdorner<TItemContainer> : DropIndicatorAdornerBase<ItemsControl, TItemContainer>
|
||||
where TItemContainer : UIElement, IHaveDropIndicatorZone {
|
||||
public ItemsControlDropIndicatorAdorner(ItemsControl adorned_element) : base(adorned_element) { }
|
||||
protected override TItemContainer get_drop_target(DependencyObject original_source) => AdornedElement.ContainerFromElement(original_source) as TItemContainer;
|
||||
protected override Rect get_drop_inidicator_zone(TItemContainer drop_target) => drop_target.get_drop_indicator_zone();
|
||||
}
|
||||
}
|
||||
@@ -35,6 +35,11 @@ namespace mbts.ui.extensions
|
||||
return Disposable.Create(() => target.MouseLeave -= callback);
|
||||
}
|
||||
|
||||
public static IDisposable SubscribeMouseUp(this UIElement target, MouseButtonEventHandler callback) {
|
||||
target.MouseUp += callback;
|
||||
return Disposable.Create(() => target.MouseUp -= callback);
|
||||
}
|
||||
|
||||
public static IDisposable SubscribeMouseLeftButtonUp(this UIElement target, MouseButtonEventHandler callback) {
|
||||
target.MouseLeftButtonUp += callback;
|
||||
return Disposable.Create(() => target.MouseLeftButtonUp -= callback);
|
||||
|
||||
@@ -1,9 +1,12 @@
|
||||
using mbts.ui;
|
||||
using mbts.ui.controls;
|
||||
using mbts.ui.extensions;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using System.Windows.Input;
|
||||
using System.Windows.Media;
|
||||
using MyPanel = System.Windows.Controls.WrapPanel;
|
||||
using security_key = System.UInt32;
|
||||
@@ -13,6 +16,7 @@ namespace mbts.spark
|
||||
{
|
||||
class LadderOrdersHost : Canvas {
|
||||
LadderGridHost _parent;
|
||||
DragDropAgent _drag_drop_agent;
|
||||
Dictionary<double, MyPanel> _stacks = new Dictionary<double, MyPanel>();
|
||||
List<OrderVM> _orders = new List<OrderVM>();
|
||||
double[] _price_list;
|
||||
@@ -22,7 +26,13 @@ namespace mbts.spark
|
||||
public LadderOrdersHost(LadderGridHost parent) {
|
||||
_parent = parent;
|
||||
AllowDrop = true;
|
||||
Loaded += _loaded;
|
||||
Unloaded += _unloaded;
|
||||
}
|
||||
|
||||
void _loaded(object sender, RoutedEventArgs e) => _drag_drop_agent = new DragDropAgent(this);
|
||||
void _unloaded(object sender, RoutedEventArgs e) => Disposable.dispose(ref _drag_drop_agent);
|
||||
|
||||
security_key security_key => _parent.security.Id;
|
||||
|
||||
public void update_settings(double[] prices, double row_height, LadderColumnSettings settings) {
|
||||
@@ -59,7 +69,7 @@ namespace mbts.spark
|
||||
}
|
||||
}
|
||||
|
||||
void _add_order(OrderVM order, double price) {
|
||||
private void _add_order(OrderVM order, double price) {
|
||||
// add order to list for reset
|
||||
_orders.Add(order);
|
||||
// get the stackpanel it belongs in
|
||||
@@ -75,6 +85,33 @@ namespace mbts.spark
|
||||
b.DataContext = order;
|
||||
// Hook up the style from xaml
|
||||
b.Style = this.TryFindResource(typeof(Badge)) as Style;
|
||||
b.MouseUp += (s, e) => {
|
||||
// open context menu on mouse wheel click, cancel order on left click
|
||||
if (e.ChangedButton == MouseButton.Middle || (e.LeftButton == MouseButtonState.Pressed && e.RightButton == MouseButtonState.Pressed)) {
|
||||
b.ContextMenu.DataContext = b.DataContext;
|
||||
b.ContextMenu.IsOpen = true;
|
||||
// close context menu after command executed
|
||||
var curr_order = b.DataContext as OrderVM;
|
||||
if (curr_order != null) {
|
||||
Action command_executed = null;
|
||||
command_executed = () => {
|
||||
try {
|
||||
b.ContextMenu.IsOpen = false;
|
||||
curr_order.command_executed -= command_executed;
|
||||
} catch (Exception ex) {
|
||||
Logger.warn(bts.log.Subsystem.Ui, $"Error in {typeof(LadderGridHost)}'s {typeof(Badge)} {nameof(OrderVM.command_executed)} local handler: {ex}");
|
||||
}
|
||||
};
|
||||
curr_order.command_executed += command_executed;
|
||||
}
|
||||
} else if (e.ChangedButton == MouseButton.Left) {
|
||||
var curr_order = b.DataContext as OrderVM;
|
||||
if (curr_order != null && curr_order.cancel_command.CanExecute(null))
|
||||
curr_order.cancel_command.Execute(null);
|
||||
}
|
||||
};
|
||||
// block right click from opening context menu
|
||||
b.ContextMenuOpening += (s, e) => e.Handled = true;
|
||||
return b;
|
||||
}
|
||||
|
||||
@@ -104,8 +141,7 @@ namespace mbts.spark
|
||||
|
||||
#region Drag/Drop
|
||||
|
||||
protected override async void OnDrop(DragEventArgs e) {
|
||||
base.OnDrop(e);
|
||||
private async void drop(DragEventArgs e) {
|
||||
if (e.Data.GetDataPresent(typeof(OrderVM))) {
|
||||
OrderVM vm = e.Data.GetData(typeof(OrderVM)) as OrderVM;
|
||||
if (vm != null && vm.security_key == security_key) {
|
||||
@@ -124,14 +160,79 @@ namespace mbts.spark
|
||||
}
|
||||
}
|
||||
}
|
||||
protected override void OnDragEnter(DragEventArgs e) {
|
||||
e.Effects = DragDropEffects.Move;
|
||||
base.OnDragEnter(e);
|
||||
}
|
||||
protected override void OnGiveFeedback(GiveFeedbackEventArgs e) {
|
||||
base.OnGiveFeedback(e);
|
||||
}
|
||||
|
||||
private class DragDropAgent : IDisposable {
|
||||
public DragDropAgent(LadderOrdersHost host) {
|
||||
_host = host;
|
||||
_host.MouseDown += _host_MouseDown;
|
||||
_host.DragEnter += _host_DragEnter;
|
||||
_host.PreviewDrop += _host_PreviewDrop;
|
||||
}
|
||||
|
||||
readonly LadderOrdersHost _host;
|
||||
IDisposable _drag_operation;
|
||||
|
||||
void _log(string msg) => Logger.info(bts.log.Subsystem.Ui, $"{typeof(LadderGridHost)} {msg}");
|
||||
|
||||
void _host_MouseDown(object sender, MouseButtonEventArgs e) {
|
||||
switch (e.ChangedButton) {
|
||||
case MouseButton.Left: goto begin_drag;
|
||||
case MouseButton.Right: goto begin_drag;
|
||||
case MouseButton.Middle:
|
||||
_log("Mouse Down (M)");
|
||||
// TODO: toggle cancel/shatter dialog open/close
|
||||
break;
|
||||
begin_drag:
|
||||
_log("Mouse Down (L/R)");
|
||||
var pt = e.GetPosition(_host);
|
||||
var order_view = _host.ChildAtPoint<Badge>(pt);
|
||||
if (order_view != null) _begin_drag(order_view, pt);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void _host_DragEnter(object sender, DragEventArgs e) {
|
||||
Disposable.dispose(ref _drag_operation);
|
||||
e.Effects = DragDropEffects.Move;
|
||||
}
|
||||
|
||||
void _host_PreviewDrop(object sender, DragEventArgs e) => _host.drop(e);
|
||||
|
||||
void _begin_drag(Badge order_view, Point drag_start) {
|
||||
Disposable.dispose(ref _drag_operation);
|
||||
MouseEventHandler mouse_move = (s, e) => {
|
||||
if (DragDropHelper.is_sufficient_delta(drag_start, e.GetPosition(_host))) {
|
||||
Disposable.dispose(ref _drag_operation);
|
||||
var vm = order_view.DataContext as OrderVM;
|
||||
if (vm != null) {
|
||||
using (new DropIndicatorAdorner(_host))
|
||||
using (new DragShadowAdorner(_host, _drag_shadow(vm)))
|
||||
DragDrop.DoDragDrop(_host, vm, DragDropEffects.Move);
|
||||
}
|
||||
}
|
||||
};
|
||||
MouseButtonEventHandler mouse_up = (s, e) => {
|
||||
if (e.LeftButton == MouseButtonState.Released && e.RightButton == MouseButtonState.Released) {
|
||||
Disposable.dispose(ref _drag_operation);
|
||||
_log("Mouse Up (L/R)");
|
||||
}
|
||||
};
|
||||
_drag_operation = Disposable.Create(_host.SubscribeMouseMove(mouse_move), _host.SubscribeMouseUp(mouse_up));
|
||||
}
|
||||
|
||||
UIElement _drag_shadow(OrderVM vm) => new TextBlock { Text = vm.summary, FontWeight = FontWeights.Bold };
|
||||
|
||||
public void Dispose() {
|
||||
Disposable.dispose(ref _drag_operation);
|
||||
_host.MouseDown -= _host_MouseDown;
|
||||
_host.DragEnter -= _host_DragEnter;
|
||||
}
|
||||
|
||||
private class DropIndicatorAdorner : DropIndicatorAdornerBase<LadderOrdersHost, Badge> {
|
||||
public DropIndicatorAdorner(LadderOrdersHost adorned_element) : base(adorned_element) { }
|
||||
protected override Rect get_drop_inidicator_zone(Badge drop_target) => AdornedElement.GetChildBounds(drop_target);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -74,9 +74,14 @@ namespace mbts.spark
|
||||
public string description => _order.full_description(_parent.security);
|
||||
public bool has_parent => _order.has_parent();
|
||||
|
||||
public ICommand shatter_command => new DelegateCommand(_ => model.order_gateway.crack_order(_order.Key).ignore(), _ => !has_parent);
|
||||
public ICommand cancel_command => new DelegateCommand(_ => model.order_gateway.cancel_order(_order.Key).ignore(), _ => !has_parent);
|
||||
public ICommand modify_command => new DelegateCommand(() => { /*TODO*/ }, () => !has_parent);
|
||||
public ICommand shatter_command => new DelegateCommand(_ => _execute_command(model.order_gateway.crack_order(_order.Key).ignore) , _ => !has_parent);
|
||||
public ICommand cancel_command => new DelegateCommand(_ => _execute_command(model.order_gateway.cancel_order(_order.Key).ignore), _ => !has_parent);
|
||||
public ICommand modify_command => new DelegateCommand(() => _execute_command(() => { /*TODO*/ }), () => !has_parent);
|
||||
public event Action command_executed;
|
||||
void _execute_command(Action action) {
|
||||
action();
|
||||
command_executed?.Invoke();
|
||||
}
|
||||
|
||||
public Brush foreground => Brushes.Black;
|
||||
public Brush background { get; private set; }
|
||||
|
||||
Reference in new issue
Block a user