diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs index 8e6eb9f92..9db4f6c83 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookColumns.cs @@ -9,6 +9,7 @@ using mbts.ui.converters; using mbts.ui.spark; using mbts.ui.utility; using mbts.ui.utility.text; +using mbts.ui.utilities; using mbts.utils; using System; using System.Collections.Generic; @@ -24,10 +25,8 @@ using System.Windows.Media; using static bts.shell.OrderBookWindowFactory; using Columns = mbts.spark.OrderBookGridView.ColumnKeys; using static mbts.ui.controls.DIYGrid; - using OrderType = bts.api.orderman.CustomOrderDefinition.CustomOneofCase; -using System.Diagnostics; -using System.Windows.Input; + namespace mbts.spark { // This interface is used to hide ColumnBase.SortDirection, and offer a SortDirection member that does NOT require a DependencyProperty lookup. @@ -819,7 +818,6 @@ namespace mbts.spark { row_vm_attach_command_binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(MiniOrderBookRowView), 1); attach_button.SetBinding(Button.CommandProperty, row_vm_attach_command_binding); - #endregion #region OrderParamsPanel @@ -841,37 +839,72 @@ namespace mbts.spark { order_params.SetBinding(OrderParamsPanel.AlgoVMProperty, new Binding(_algo_vm_binding_path)); #endregion - #region Send Attached Button - var send_attached_orders_button = new Button() { - Name = "mini_ob_send_attached_orders_button", - Content = "Attach to Orders" - }; + #region Dialog Skirt + var dialog_skirt_button_style = + Application.Current.TryFindResource(DialogSkirt.DialogSkirtButtonStyleKey) as Style ?? + Application.Current.TryFindResource("CommonButton") as Style ?? + Application.Current.TryFindResource(typeof(Button)) as Style; // go find the OBVM on the ContextMenu to get the command - var send_attach_button_binding = new Binding(_orderbook_vm_send_attached_orders_cmd_binding_path); - send_attach_button_binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Popup), 1); - send_attached_orders_button.SetBinding(Button.CommandProperty, send_attach_button_binding); - - // close the ContextMenu when the send attached button is clicked - send_attached_orders_button.Click += (s, e) => - { - popup.IsOpen = false; + // The button for which this will server as a Binding to needs to both perform the send_attached_orders + // command *AND* close the algo param popup. Previously this was accomplished by setting both the Command + // and Click event on a Button appropriately, but since the Button that will perform this + // ultimately lives inside of the DialogSkirt, we don't have direct access to it. Ergo, + // we make use of this addition trick from John that allows for both of these things to happen. + // We take our original ICommand (send_attached) and wrap it in another Command by way of a Converter on the binding, + // pure genious! + var send_attach_button_binding = new Binding(_orderbook_vm_send_attached_orders_cmd_binding_path) { + Converter = CommandWithActionWrapper.mk_wrap_converter(() => popup.IsOpen = false) }; + send_attach_button_binding.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(Popup), 1); - var stack_p = new StackPanel() { Margin = new Thickness(0, 0, 0, 5) }; - stack_p.Children.Add(order_params); - stack_p.Children.Add(send_attached_orders_button); - #endregion + // Our ultimate goal is to have the Ok button (DialogSkirtItem) run the send attached orders command. + // Since we're only dealing with binding paths in here and not actual objects, we don't have access + // to an actual Command, which is what the DialogSkirtItem expects. John came up with a sneaky + // way of setting the Button.CommandProperty via a Style backdoor, but it's also necessary to + // create a KeyedStyleSelector here I guess. + // Also, the DialogSkirt Ok button seems to magically close the popup, cool! + var send_attached_button_style = new Style(typeof(Button), dialog_skirt_button_style); + send_attached_button_style.Setters.Add(new Setter(Button.CommandProperty, send_attach_button_binding)); + var awesome_command_style_key = "SendAttachButtonStyleKey"; + var cancel_style_key = "CancelButtonStyleKey"; + + var dialog_skirt = new DialogSkirt { + RightItems = new[] { + new DialogSkirtItem("Ok", null, awesome_command_style_key) { is_default = true }, + new DialogSkirtItem("Cancel", new DelegateCommand(() => popup.IsOpen = false), cancel_style_key) { is_cancel = true } + }}; + + var kss = new KeyedStyleSelector(); + kss.Map.Add(new StyleSelectorKeyValue { + Key = awesome_command_style_key, + Value = send_attached_button_style + }); + kss.Map.Add( + new StyleSelectorKeyValue { + Key = cancel_style_key, + Value = new Style(typeof(Button), dialog_skirt_button_style) + }); + dialog_skirt.RightItemContainerStyleSelector = kss; + + Grid.SetRow(dialog_skirt, 1); + var grid = new Grid(); + grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); + grid.RowDefinitions.Add(new RowDefinition { Height = GridLength.Auto }); + grid.Children.Add(order_params); + grid.Children.Add(dialog_skirt); var border = new Border() { Background = BTSColors.common_background_brush, BorderThickness = new Thickness(1), Name = "attach_border", - Child = stack_p, + Child = grid, }; - + + #endregion + popup.Child = border; - popup.StaysOpen = false; + popup.StaysOpen = true; popup.PlacementTarget = attach_button; popup.Placement = PlacementMode.Bottom; // assign the Popup the DataContext of PlacementTarget, which will always be kept in sync with our parent button @@ -985,5 +1018,6 @@ namespace mbts.spark { public static Brush get_side_brush(OrderBookRowVM r) => r.side == Side.Buy ? BTSColors.MarketData.bid : BTSColors.MarketData.ask; public static BindingBase get_font_weight_binding() => new Binding(nameof(OrderBookRowVM.is_child)) { Converter = _font_weight_converter }; } + } } diff --git a/src/client/spark/view/Utility/CommandWithActionWrapper.cs b/src/client/spark/view/Utility/CommandWithActionWrapper.cs new file mode 100644 index 000000000..a8ef5e669 --- /dev/null +++ b/src/client/spark/view/Utility/CommandWithActionWrapper.cs @@ -0,0 +1,25 @@ +using mbts.ui.converters; +using System; +using System.Windows.Data; +using System.Windows.Input; + +namespace mbts.ui.utilities { + static class CommandWithActionWrapper { + public static IValueConverter mk_wrap_converter(Action action) => + ValueConverter.create(cmd => wrap(cmd, action)); + + public static ICommand wrap(ICommand command, Action action) { + var wrapped_cmd = new DelegateCommand(execute, command.CanExecute); + command.CanExecuteChanged += on_can_execute_changed; + return wrapped_cmd; + + void execute(object parameter) { + command.Execute(parameter); + action(); + } + + void on_can_execute_changed(object sender, EventArgs e) => + wrapped_cmd.RaiseCanExecuteChanged(); + } + } +}