implement AlgoTemplate 'Favorites' in Ladder/OrderPropertiesEditorVM

This commit is contained in:
John Colvin committed 2018-11-27 23:58:58 -06:00
1 parent 6fdd6585ba
commit 2f3595365c
11 files changed
+375 -86

No files matched your search

+4 -5
View File
@@ -10,6 +10,7 @@ namespace mbts.ui {
public class Icons {
public const string
AddBoxesGeometryKey = nameof(AddBoxesGeometryKey),
CheckMarkGeometryKey = nameof(CheckMarkGeometryKey),
DiskGeometryKey = nameof(DiskGeometryKey),
DraggableGeometryKey = nameof(DraggableGeometryKey),
GearGeometryKey = nameof(GearGeometryKey),
@@ -17,8 +18,7 @@ namespace mbts.ui {
PaymentGeometryKey = nameof(PaymentGeometryKey),
PencilGeometryKey = nameof(PencilGeometryKey),
PlayGeometryKey = nameof(PlayGeometryKey),
StarHollowGeometryKey = nameof(StarHollowGeometryKey),
StarSolidGeometryKey = nameof(StarSolidGeometryKey),
StarGeometryKey = nameof(StarGeometryKey),
ToolsGeometryKey = nameof(ToolsGeometryKey),
UndoCircularGeometryKey = nameof(UndoCircularGeometryKey);
@@ -113,6 +113,7 @@ namespace mbts.ui {
public static ImageSource CloneSynthetic { get { return load_resource_and_freeze("CloneSynthetic"); } }
public static ImageSource ModifyOrder { get { return load_resource_and_freeze("ModifyOrder"); } }
public static ImageSource CheckMark => create_icon(CheckMarkGeometryKey);
public static ImageSource Clock { get { return load_resource_and_freeze("Clock"); } }
public static ImageSource Defrag { get { return load_resource_and_freeze("Defrag"); } }
public static ImageSource Disk => create_icon(DiskGeometryKey);
@@ -128,9 +129,7 @@ namespace mbts.ui {
public static ImageSource Payment => create_icon(PaymentGeometryKey);
public static ImageSource Pencil => create_icon(PencilGeometryKey);
public static ImageSource Spawn { get { return load_resource_and_freeze("Spawn"); } }
public static ImageSource StarHollow => create_icon(StarHollowGeometryKey);
public static ImageSource StarSolid => create_icon(StarSolidGeometryKey);
public static ImageSource StarSolidGold => create_icon(StarSolidGeometryKey, Brushes.Gold);
public static ImageSource Star => create_icon(StarGeometryKey);
public static ImageSource Target { get { return load_resource_and_freeze("Target"); } }
public static ImageSource Tetris { get { return load_resource_and_freeze("Tetris"); } }
public static ImageSource Tools => create_icon(ToolsGeometryKey);
+5 -10
View File
@@ -15,6 +15,9 @@
M105.5,393h374.8c5,0,9.1-4.1,9.1-9.1V9.1c0-5-4.1-9.1-9.1-9.1H105.5c-5,0-9.1,4.1-9.1,9.1v374.8 C96.4,388.9,100.5,393,105.5,393z M114.5,18.2h356.7v356.7H114.5V18.2z
</sys:String>
<sys:String x:Key="{x:Static mbts:Icons.CheckMarkGeometryKey}">
F1M9.97498,1.22334L4.6983,9.09834 4.52164,9.09834 0,5.19331 1.27664,3.52165 4.255,6.08833 8.33331,1.52588E-05 9.97498,1.22334z
</sys:String>
<sys:String x:Key="{x:Static mbts:Icons.DiskGeometryKey}">
M7.9999733,21.999969L7.9999733,29.999969 10.999944,29.999969 10.999944,23.999994 12.999944,23.999994 12.999944,29.999969 19.999973,29.999969 19.999973,21.999969z M7.9999437,2.9999943L7.9999437,14.999994 23.999945,14.999994 23.999945,2.9999943z M2.5,2C2.224,2,1.9999998,2.2250004,2,2.5L2,27.434 5.9999733,29.833797 5.9999733,19.999969 21.999973,19.999969 21.999973,30 29.5,30C29.775,30,30,29.775,30,29.5L30,2.5C30,2.2250004,29.775,2,29.5,2L25.999945,2 25.999945,16.999994 5.9999442,16.999994 5.9999442,2z M2.5,0L29.5,0C30.879,0,32,1.1210003,32,2.5L32,29.5C32,30.879,30.879,32,29.5,32L5.7219996,32 0,28.566 0,2.5C0,1.1210003,1.1209998,0,2.5,0z
</sys:String>
@@ -50,16 +53,8 @@
M0,0L25.194,16 0,32z
</sys:String>
<Geometry x:Key="StarGeometry">
M1540.22,2082.07L1546.95,2102.78 1568.73,2102.78 1551.11,2115.58 1557.84,2136.29 1540.22,2123.49 1522.6,2136.29 1529.33,2115.58 1511.71,2102.78 1533.49,2102.78 1540.22,2082.07
</Geometry>
<sys:String x:Key="{x:Static mbts:Icons.StarHollowGeometryKey}">
M15.719971,4.029998L13.156982,12.397001 3.9190063,12.312009 11.044983,17.740995 8.5620117,26.471996 15.749023,21.253993 22.960999,26.428004 20.138,17.617002 27.684998,12.669996 18.971008,12.587003z M15.586975,0L19.875977,11.288007 32,11.404004 21.685974,18.165006 25.411987,29.795999 15.754028,22.865001 6.2719727,29.751992 9.5440063,18.240995 0,10.969007 12.19397,11.081998z
</sys:String>
<sys:String x:Key="{x:Static mbts:Icons.StarSolidGeometryKey}">
M16,0L19.8,12.200003 32,12.200003 22.1,19.700004 25.9,31.900007 16,24.400005 6.0999994,31.900007 9.9000001,19.700004 0,12.200003 12.2,12.200003z
<sys:String x:Key="{x:Static mbts:Icons.StarGeometryKey}">
M1540.22,2082.07L1546.95,2102.78 1568.73,2102.78 1551.11,2115.58 1557.84,2136.29 1540.22,2123.49 1522.6,2136.29 1529.33,2115.58 1511.71,2102.78 1533.49,2102.78 1540.22,2082.07z
</sys:String>
<sys:String x:Key="{x:Static mbts:Icons.ToolsGeometryKey}">
@@ -67,7 +67,7 @@ namespace bts.shell {
var vm = new LadderVM(_bus, model, settings_manager);
return new LadderView(vm);
});
string thread = Configuration.get<bts.config.toggles.LaddersOnThreads>().enabled ? $"Ladder{id:00}" : "";
string thread = !Configuration.get<bts.config.toggles.LaddersOnThreads>().enabled ? $"Ladder{id:00}" : "";
return ShellWindow.create(
module, md.security.ToString(), _window_type, view,
(v,s) => save(_get_state(((LadderView)v).vm), s),
+54 -21
View File
@@ -1,56 +1,91 @@
using bts.api.orderman;
using bts.spark;
using bts.utils;
using mbts.ui.shell;
using System;
using System.ComponentModel;
using System.Windows.Input;
namespace mbts.spark {
interface IAlgoTemplateItem : IEditableObject {
string name { get; set; }
string abbreviation { get; set; }
CustomOrderDefinitionImmutable definition { get; }
OrderMetadata meta { get; }
bool is_edited { get; }
bool is_default { get; }
bool is_favorite { get; set; }
}
class AlgoTemplateItemVM : ViewModelBase, IAlgoTemplateItem, IEquatable<AlgoTemplateItemVM> {
readonly Property<string> _name;
readonly Property<bool> _is_default, _is_favorite;
readonly DelegateProperty<bool> _is_edited;
readonly Property<CustomOrderDefinitionImmutable> _definition;
readonly DelegateProperty<bool> _is_edited, _is_favorite, _is_default;
readonly OrderPropertiesEditorVM _owner;
CustomOrderDefinitionImmutable _definition;
string _name, _abbreviation;
Snapshot? _committed_state;
public static AlgoTemplateItemVM create_existing(OrderPropertiesEditorVM owner, AlgoTemplateImmutable template, bool is_default, bool is_favorite) =>
new AlgoTemplateItemVM(owner, template.Name, template.Id, template.Definition, is_default, is_favorite);
public static AlgoTemplateItemVM create_existing(OrderPropertiesEditorVM owner, AlgoTemplateImmutable template) =>
new AlgoTemplateItemVM(owner, template.Name, template.Id, template.Definition);
public static AlgoTemplateItemVM create_new(OrderPropertiesEditorVM owner, string name, bool is_default, bool is_favorite) =>
public static AlgoTemplateItemVM create_new(OrderPropertiesEditorVM owner, string name) =>
new AlgoTemplateItemVM(
owner, name, 0,
owner.definition ?? new CustomOrderDefinitionImmutable(
new CustomOrderDefinition { Limit = new LimitOrderDefinition() }),
is_default, is_favorite);
new CustomOrderDefinition { Limit = new LimitOrderDefinition() }));
AlgoTemplateItemVM(OrderPropertiesEditorVM owner, string name, uint id, CustomOrderDefinitionImmutable definition, bool is_default, bool is_favorite) {
AlgoTemplateItemVM(OrderPropertiesEditorVM owner, string name, uint id, CustomOrderDefinitionImmutable definition) {
_owner = owner;
_name = backing(nameof(name), name);
_name = name;
this.id = id;
_definition = backing(nameof(definition), definition);
_is_default = backing(nameof(this.is_default), is_default);
_is_favorite = backing(nameof(this.is_favorite), is_favorite);
_definition = definition;
_is_default = backing(nameof(is_default), () => _owner.get_is_default(this.id));
_is_favorite = backing(nameof(is_favorite), () => _owner.favorite_algo_templates.Contains(this));
_is_edited = backing(nameof(is_edited), () => {
if (_owner.selected_algo_template != this) return false;
var odef = _owner.definition;
return odef.HasValue && !odef.Value.Equals(definition);
return odef.HasValue && !odef.Value.Equals(this.definition);
});
}
public CustomOrderDefinitionImmutable definition { get => _definition.Value; set => _definition.set(value); }
public CustomOrderDefinitionImmutable definition { get => _definition; set => Set(ref _definition, value); }
public string group_name => definition.CustomCase.ToString();
public string name { get => _name.Value; set => _name.set(value); }
public bool is_default { get => _is_default.Value; set => _is_default.set(value); }
public bool is_favorite { get => _is_favorite.Value; set => _is_favorite.set(value); }
public string name {
get => _name;
set {
if (Set(ref _name, value))
RaisePropertyChanged(nameof(abbreviation));
}
}
public string abbreviation { get => _abbreviation ?? name; set => Set(ref _abbreviation, value); }
public bool is_default => _is_default.Value;
public bool is_favorite {
get => _is_favorite.Value;
set {
bool curr_is_fav = _owner.favorite_algo_templates.Contains(this);
if (value != curr_is_fav) {
if (value) {
_owner.favorite_algo_templates.Add(this);
} else _owner.favorite_algo_templates.Remove(this);
}
_is_favorite.set(value);
}
}
public bool is_edited => _is_edited.Value;
public uint id { get; set; }
public OrderMetadata meta => _owner.get_metadata(definition.CustomCase);
public ICommand reset_as_default_cmd { get; }
public void refresh_is_edited() => _is_edited.refresh();
public void refresh_is_default() => _is_default.refresh();
public void refresh_is_favorite() => _is_favorite.refresh();
public void load_from(AlgoTemplateImmutable value) {
name = value.Name;
definition = value.Definition;
id = value.Id;
if (_committed_state.HasValue)
_committed_state = new Snapshot(this);
refresh_is_edited();
}
public bool Equals(AlgoTemplateItemVM other) => other != null && id == other.id;
public override bool Equals(object obj) => Equals(obj as AlgoTemplateItemVM);
@@ -66,8 +101,6 @@ namespace mbts.spark {
void IEditableObject.CancelEdit() {
if (!_committed_state.HasValue) return;
name = _committed_state.Value.name;
is_default = _committed_state.Value.is_default;
is_favorite = _committed_state.Value.is_favorite;
definition = _committed_state.Value.definition;
_committed_state = null;
}
@@ -1,9 +1,13 @@
using bts;
using bts.api;
using bts.api.orderman;
using bts.spark;
using bts.utils;
using mbts.collections;
using mbts.spark.utilities;
using mbts.ui;
using mbts.ui.shell;
using mbts.utils;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
@@ -12,6 +16,7 @@ using System.ComponentModel;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using System.Windows.Data;
using System.Windows.Input;
namespace mbts.spark {
@@ -97,17 +102,31 @@ namespace mbts.spark {
class OrderPropertiesEditorVM : DisposableViewModelBase {
readonly OrdersModel _model;
readonly Property<SecurityImmutable> _security;
readonly Property<AlgoTemplateItemVM> _selected_algo_template;
readonly Property<AlgoTemplateItemVM> _selected_algo_template, _selected_favorite_algo_template;
readonly SelectorProperty<AccountTuple, AccountTuple?> _accounts;
readonly SelectorProperty<TimeInForce> _time_in_force;
readonly SelectorProperty<IOrderTypeVM> _order_type;
IReadOnlyDictionary<uint, AlgoTemplateImmutable> _cached_templates = new Dictionary<uint, AlgoTemplateImmutable>();
readonly DeferrableActionWrapper _definition_refresh_model;
readonly ObservableKeyedCollection<AlgoTemplateItemVM, uint>
_favorite_templates = new ObservableKeyedCollection<AlgoTemplateItemVM, uint>(tvm => tvm.id),
_algo_templates = new ObservableKeyedCollection<AlgoTemplateItemVM, uint>(tvm => tvm.id);
List<uint> _favorite_template_ids = new List<uint>();
HashSet<uint> _default_template_ids = new HashSet<uint>();
IDisposable _order_type_param_errors_sub;
public OrderPropertiesEditorVM(OrdersModel model, IObservable<SecurityMD> security_md_source, IObservable<InlineOrderProperties> order_properties) {
_model = model;
var security_source = security_md_source.Select(smd => smd.security);
_security = backing(nameof(security), security_source.OnDispatcher(), SecurityDataHelper.Defaults.SecurityImmutable);
_security = backing(nameof(security), SecurityDataHelper.Defaults.SecurityImmutable);
var resources = new List<IDisposable> {
security_source.OnDispatcher().Subscribe(s => {
using (_definition_refresh_model.defer_refresh()) {
_security.set(s);
foreach(var tvm in algo_templates)
tvm.definition = OrderDataHelper.with_security(tvm.definition, s);
}
})
};
// guarentee that "this.security" will always be set before observers are notified
// this is important bc some observers will reference "this.security" in their callbacks
// - namely the [SelectorProperty]order_type's "get_default" function,
@@ -144,12 +163,26 @@ namespace mbts.spark {
tif => time_in_forces.Contains(tif));
_selected_algo_template = backing<AlgoTemplateItemVM>(nameof(selected_algo_template));
_selected_algo_template.Changed += on_selected_algo_changed;
_selected_favorite_algo_template = backing<AlgoTemplateItemVM>(nameof(selected_favorite_algo_template));
// definition refresh model
_definition_refresh_model = new DeferrableActionWrapper(() => {
selected_algo_template?.refresh_is_edited();
_selected_favorite_algo_template.set(favorite_algo_templates.Contains(selected_algo_template) ? selected_algo_template : null);
});
// algo templates
_algo_templates.CollectionChanged += on_algo_templates_CollectionChanged;
visible_algo_templates = CollectionViewSource.GetDefaultView(_algo_templates);
visible_algo_templates.Filter = obj => selected_order_type != null && selected_order_type.order_type == ((IAlgoTemplateItem)obj).definition.CustomCase;
// favorite algo templates
visible_favorite_algo_templates = CollectionViewSource.GetDefaultView(_favorite_templates);
visible_favorite_algo_templates.Filter = obj => selected_order_type != null && selected_order_type.order_type == ((IAlgoTemplateItem)obj).definition.CustomCase;
// save template
save_template_cmd = new AsyncCommand<AlgoTemplateItemVM>(save_template);
async Task save_template(AlgoTemplateItemVM tvm) {
var result = await model.template_service.add(tvm.to_template());
if (result.success)
tvm.id = result.value.id;
selected_algo_template = tvm;
}
// revert template changes
revert_template_changes_cmd = new DelegateCommand(() => {
@@ -158,16 +191,43 @@ namespace mbts.spark {
});
// remove template
remove_template_cmd = new AsyncCommand<AlgoTemplateItemVM>(tvm => model.template_service.delete(tvm.id));
_dispose = Disposable.Create(
model.template_service.subscribe().OnDispatcher().Subscribe(d => {
_cached_templates = d;
sync_algo_templates();
}), Disposable.Create(() => _order_type.Changed -= on_selected_order_type_changed));
// local methods
resources.Add(model.template_service.subscribe().OnDispatcher().Subscribe(d => {
var old_selected_template = selected_algo_template;
var remove_keys = _algo_templates.Keys.Except(d.Keys).ToList();
foreach (var k in remove_keys) _algo_templates.Remove(k);
foreach (var kv in d) {
var mutable = kv.Value.CloneToMutable();
var def = mutable.Definition;
OrderDataHelper.assign_security(ref def, security);
mutable.Definition = def;
_algo_templates.AddOrUpdate(kv.Key, k => AlgoTemplateItemVM.create_existing(this, mutable), (k, curr) => curr.load_from(mutable));
}
if (old_selected_template != null) {
foreach (var tvm in algo_templates) {
if (tvm.id == old_selected_template.id) {
selected_algo_template = tvm;
break;
}
}
}
}));
resources.Add(Disposable.Create(() => {
_order_type.Changed -= on_selected_order_type_changed;
_algo_templates.CollectionChanged -= on_algo_templates_CollectionChanged;
}));
_dispose = Disposable.Create(resources.ToArray());
// add to favorites
add_to_favorites_cmd = new DelegateCommand<AlgoTemplateItemVM>(tvm => _favorite_templates.Add(tvm));
// toggle favorite
toggle_favorite_cmd = new DelegateCommand<AlgoTemplateItemVM>(tvm => {
_toggle_favorite(tvm.id);
tvm.refresh_is_favorite();
}, tvm => tvm != null);
// local members
IOrderTypeVM create_order_type_vm(CustomOrderDefinitionImmutable cd, string id, string display_name) {
IOrderTypeVM vm = null;
if(cd.CustomCase == CustomOrderDefinition.CustomOneofCase.Dynamic)
vm = OrderTypeVMFactory.create(cd.CustomCase, id, display_name, model, security_md_source, cd.Dynamic.DynamicOrderType);
if (cd.CustomCase == CustomOrderDefinition.CustomOneofCase.Dynamic)
vm = OrderTypeVMFactory.create(cd.CustomCase, id, display_name, model, security_md_source, cd.Dynamic.DynamicOrderType);
else
vm = OrderTypeVMFactory.create(cd.CustomCase, id, display_name, model, security_md_source);
vm.load_from(cd);
@@ -188,9 +248,8 @@ namespace mbts.spark {
return order_types.FirstOrDefault();
}
void on_selected_order_type_changed(IOrderTypeVM old_val, IOrderTypeVM new_val, SelectionReason reason) {
sync_algo_templates();
if (old_val != null)
old_val.definition_changed -= on_definition_changed;
old_val.definition_changed -= _definition_refresh_model.execute;
if (new_val == null) {
assert(false, nameof(selected_order_type));
} else {
@@ -205,33 +264,26 @@ namespace mbts.spark {
new_val.load_from(selected_algo_template.definition);
if (new_val.has_editable_properties && reason == SelectionReason.UserSelectedNewValue)
selected_order_type.editable_properties_open = true;
new_val.definition_changed += on_definition_changed;
}
}
void on_definition_changed() {
selected_algo_template?.refresh_is_edited();
}
void sync_algo_templates() {
uint? prev_selected_algo = selected_algo_template?.id;
algo_templates.Clear();
if (_order_type.selected_item != null) {
algo_templates.AddRange(_cached_templates.Values
.Where(t => t.Definition.CustomCase == selected_order_type.order_type)
.OrderBy(t => t.Name)
.Select(t => AlgoTemplateItemVM.create_existing(this, t, false, false)));
if (prev_selected_algo.HasValue) {
foreach (var t in algo_templates) {
if (t.id == prev_selected_algo.Value) {
selected_algo_template = t;
break;
}
}
}
new_val.definition_changed += _definition_refresh_model.execute;
}
visible_algo_templates.Refresh();
visible_favorite_algo_templates.Refresh();
}
void on_selected_algo_changed(AlgoTemplateItemVM oldval, AlgoTemplateItemVM newval) {
if (newval != null)
selected_order_type.load_from(newval.definition);
if (newval != null) {
using (_definition_refresh_model.defer_refresh())
selected_order_type.load_from(newval.definition);
}
if (oldval != null)
oldval.refresh_is_default();
}
void on_algo_templates_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
switch (e.Action) {
case NotifyCollectionChangedAction.Remove:
foreach (AlgoTemplateItemVM tvm in e.OldItems)
_favorite_templates.Remove(tvm);
break;
}
}
}
@@ -243,12 +295,33 @@ namespace mbts.spark {
public ReadOnlyObservableCollection<IOrderTypeVM> order_types => _order_type.items;
public IOrderTypeVM selected_order_type { get => _order_type.selected_item; set => _order_type.selected_item = value; }
public CustomOrderDefinitionImmutable? definition => selected_order_type?.definition;
public ObservableCollection<AlgoTemplateItemVM> algo_templates { get; } = new ObservableCollection<AlgoTemplateItemVM>();
public ObservableCollection<AlgoTemplateItemVM> algo_templates => _algo_templates;
public ObservableCollection<AlgoTemplateItemVM> favorite_algo_templates => _favorite_templates;
public AlgoTemplateItemVM selected_algo_template { get => _selected_algo_template.Value; set => _selected_algo_template.Value = value; }
public AlgoTemplateItemVM selected_favorite_algo_template {
get => _selected_favorite_algo_template.Value;
set {
var curr = selected_favorite_algo_template;
if (value != curr) {
_selected_favorite_algo_template.set(value);
curr?.refresh_is_default();
value?.refresh_is_default();
if (value != null)
selected_algo_template = value;
}
}
}
public ICollectionView visible_favorite_algo_templates { get; }
public ICollectionView visible_algo_templates { get; }
public Func<IAlgoTemplateItem> algo_template_factory => _algo_template_factory;
public ICommand save_template_cmd { get; }
public ICommand revert_template_changes_cmd { get; }
public ICommand remove_template_cmd { get; }
public ICommand add_to_favorites_cmd { get; }
public ICommand toggle_favorite_cmd { get; }
public bool get_is_default(uint template_id) => _default_template_ids.Contains(template_id);
public bool get_is_favorite(uint template_id) => _favorite_template_ids.Contains(template_id);
public IObservable<AccountTuple?> observe_account(bool start_immediately = false) =>
_accounts.observe_current(start_immediately);
@@ -269,12 +342,39 @@ namespace mbts.spark {
return order;
}
public OrderMetadata get_metadata(CustomOrderDefinition.CustomOneofCase order_type) =>
_model.factory.get_metadata(order_type);
public void on_template_settings(IEnumerable<uint> favorites, IEnumerable<uint> defaults) {
var curr_selected_favorite = selected_favorite_algo_template;
_favorite_template_ids = new List<uint>(favorites.empty_if_null());
_default_template_ids = new HashSet<uint>(defaults.empty_if_null());
_favorite_templates.Clear();
foreach (var tvm in _algo_templates.Values) {
if (_favorite_template_ids.Contains(tvm.id))
_favorite_templates.Add(tvm);
tvm.refresh_is_favorite();
}
if (curr_selected_favorite != null && _favorite_templates.Contains(curr_selected_favorite))
selected_favorite_algo_template = curr_selected_favorite;
}
protected override void Dispose(bool disposing) {
base.Dispose(disposing);
Disposable.dispose(ref _order_type_param_errors_sub);
_order_type.items.OfType<IDisposable>().DisposeAll();
}
IAlgoTemplateItem _algo_template_factory() => AlgoTemplateItemVM.create_new(this, selected_algo_template?.name ?? string.Empty, false, false);
IAlgoTemplateItem _algo_template_factory() => AlgoTemplateItemVM.create_new(this, selected_algo_template?.name ?? string.Empty);
void _toggle_favorite(uint template_id) {
if (_favorite_template_ids.Remove(template_id)) {
_favorite_templates.Remove(template_id);
} else {
_favorite_template_ids.Add(template_id);
if (_algo_templates.TryGetValue(template_id, out AlgoTemplateItemVM tvm))
_favorite_templates.Add(tvm);
}
}
}
}
@@ -29,7 +29,7 @@
<Setter Property="VerticalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Stretch"/>
<Setter Property="ItemsSource" Value="{Binding algo_templates}"/>
<Setter Property="ItemsSource" Value="{Binding visible_algo_templates}"/>
<Setter Property="SelectedItem" Value="{Binding selected_algo_template, Mode=TwoWay}"/>
<Setter Property="ItemContainerStyle" Value="{StaticResource {x:Type ComboBoxItem}}"/>
<Setter Property="Template">
@@ -121,6 +121,18 @@
<TextBlock Text="{Binding SelectedItem.name, RelativeSource={RelativeSource TemplatedParent}, StringFormat='{}Undo changes to \&apos;{0}\&apos;'}"/>
</MenuItem.ToolTip>
</MenuItem>
<MenuItem
x:Name="PART_IsFavorite_MenuItem"
u:Behavior.AutoRaiseCanExecuteChanged="True"
Command="{TemplateBinding ToggleFavoriteCommand}"
CommandParameter="{Binding SelectedItem, RelativeSource={RelativeSource TemplatedParent}}"
Style="{StaticResource AlgoMenuItem}"
ToolTip="Add to Favorites"
>
<MenuItem.Header>
<Image x:Name="_favorite_image" Source="{u:Icon Star, Brush=Transparent, Pen={u:Pen Black, Thickness=3}}" Stretch="Uniform"/>
</MenuItem.Header>
</MenuItem>
</Menu>
</Grid>
</Border>
@@ -134,6 +146,10 @@
<Setter TargetName="PART_Save_MenuItem" Property="IsEnabled" Value="True"/>
<Setter TargetName="PART_Revert_MenuItem" Property="IsEnabled" Value="True"/>
</Trigger>
<DataTrigger Binding="{Binding SelectedItem.is_favorite, RelativeSource={RelativeSource Self}}" Value="{u:True}">
<Setter TargetName="_favorite_image" Property="Source" Value="{u:Icon Star, Brush=Gold, Pen={u:Pen Black, Thickness=3}}"/>
<Setter TargetName="PART_IsFavorite_MenuItem" Property="ToolTip" Value="Remove from Favorites"/>
</DataTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ItemsSource.Count, RelativeSource={RelativeSource Self}}" Value="0"/>
@@ -1,4 +1,5 @@
using mbts.spark;
using mbts.ui.converters;
using mbts.ui.extensions;
using System;
using System.Collections.Generic;
@@ -7,6 +8,7 @@ using System.ComponentModel;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
namespace mbts.ui.spark.algo {
@@ -20,6 +22,7 @@ namespace mbts.ui.spark.algo {
BeginSaveAsCommand = new RoutedUICommand("Save Template As", "Save As", typeof(AlgoTemplateToolBar)),
BeginRevertCommand = new RoutedUICommand("Revert to Template", "Revert", typeof(AlgoTemplateToolBar)),
BeginRemoveCommand = new RoutedUICommand("Delete Template", "Delete", typeof(AlgoTemplateToolBar));
//ToggleFavoriteCommand = new RoutedUICommand("Toggle Favorite", "Favorite", typeof(AlgoTemplateToolBar));
public static readonly ExecutedRoutedEventHandler
BeginAddNewExecuted = WPFHelper.create_cmd_executed<object, AlgoTemplateToolBar>((tb, _) => tb._add_new()),
@@ -27,6 +30,10 @@ namespace mbts.ui.spark.algo {
BeginSaveAsExecuted = WPFHelper.create_cmd_executed<object, AlgoTemplateToolBar>((tb, _) => tb._save_as()),
BeginRevertExecuted = WPFHelper.create_cmd_executed<object, AlgoTemplateToolBar>((tb, _) => tb._revert()),
BeginRemoveExecuted = WPFHelper.create_cmd_executed<object, AlgoTemplateToolBar>((tb, t) => tb._remove(t));
//ToggleFavoriteExecuted = WPFHelper.create_cmd_executed<IAlgoTemplateItem, AlgoTemplateToolBar>((tb, template) => template.is_favorite = !template.is_favorite);
public static readonly CanExecuteRoutedEventHandler
ToggleFavoriteCanExecute = (s, e) => e.CanExecute = e.Parameter is IAlgoTemplateItem;
ComboBox _templates_combo;
IDisposable _templates_combo_hooks;
@@ -93,6 +100,12 @@ namespace mbts.ui.spark.algo {
public ICommand RemoveCommand { get => GetValue(RemoveCommandProperty) as ICommand; set => SetValue(RemoveCommandProperty, value); }
#endregion
#region ToggleFavorite
public static readonly DependencyProperty ToggleFavoriteCommandProperty =
WPFHelper.create_dp<ICommand, AlgoTemplateToolBar>(nameof(ToggleFavoriteCommand));
public ICommand ToggleFavoriteCommand { get => GetValue(ToggleFavoriteCommandProperty) as ICommand; set => SetValue(ToggleFavoriteCommandProperty, value); }
#endregion
#region IsEdited
/// <summary>
/// This property indicates whether or not the order definition held by the <see cref="SelectedItem"/> is 'dirty' - different from the one that would be created by the OrderPropertiesEditor at this time.
@@ -121,6 +134,12 @@ namespace mbts.ui.spark.algo {
public bool IsEditing { get => (bool)GetValue(IsEditingProperty); set => SetValue(IsEditingProperty, value); }
#endregion
#region IsAuxiliaryMenuExpanded
public static readonly DependencyProperty IsAuxiliaryMenuExpandedProperty =
WPFHelper.create_dp<bool, AlgoTemplateToolBar>(nameof(IsAuxiliaryMenuExpanded));
public bool IsAuxiliaryMenuExpanded { get => (bool)GetValue(IsAuxiliaryMenuExpandedProperty); set => SetValue(IsAuxiliaryMenuExpandedProperty, value); }
#endregion
public override void OnApplyTemplate() {
Disposable.dispose(ref _templates_combo_hooks);
_templates_combo = this.assert_template_child<ComboBox>("PART_Templates_ComboBox");
@@ -142,6 +161,7 @@ namespace mbts.ui.spark.algo {
}
}),
_templates_combo.SubscribeDropDownClosed((s, e) => _commit()));
//this.assert_template_child<MenuItem>("PART_AuxiliaryExpander_MenuItem").Command = new DelegateCommand(() => SetCurrentValue(IsAuxiliaryMenuExpandedProperty, !IsAuxiliaryMenuExpanded));
}
void _on_templates_combo_IsKeyboardFocusWithinChanged(object sender, DependencyPropertyChangedEventArgs e) {
@@ -101,6 +101,7 @@
RemoveCommand="{Binding remove_template_cmd}"
RevertCommand="{Binding revert_template_changes_cmd}"
SaveCommand="{Binding save_template_cmd}"
ToggleFavoriteCommand="{Binding toggle_favorite_cmd}"
SelectedItem="{Binding selected_algo_template, Mode=TwoWay}"
ItemContainerStyle="{StaticResource AlgoTemplateItemContainerStyle}"
>
@@ -178,7 +178,7 @@ namespace mbts.spark {
// From the settings dialog, default settings will only be persisted when they are NOT EQUAL to the current default settings.
// Settings are liable to change in the near future, so an internal IEqualityComparer simply won't do as it risks becoming out of date.
[ProtoContract]
class LadderSettings : IEquatable<LadderSettings> {
class LadderSettings : IDefaultableSettings, IEquatable<LadderSettings> {
public static LadderSettings Default {
get {
LadderColumnSettings
@@ -277,6 +277,16 @@ namespace mbts.spark {
public VolumeColumnSettings volume_column { get; set; }
[ProtoMember(26)]
public MarketSizeDisplay size_display { get; set; }
[ProtoMember(27)]
public bool use_defaults { get; set; }
[ProtoMember(28)]
List<uint> _template_favorites;
public List<uint> template_favorites => _template_favorites ?? (_template_favorites = new List<uint>());
[ProtoMember(29)]
List<uint> _template_defaults;
public List<uint> template_defaults => _template_defaults ?? (_template_defaults = new List<uint>());
public bool Equals(LadderSettings other) =>
other != null &&
@@ -302,7 +312,10 @@ namespace mbts.spark {
session_high_line.safe_equals(other.session_high_line) &&
session_range_fill.safe_equals(other.session_range_fill) &&
volume_column.safe_equals(other.volume_column) &&
size_display.safe_equals(other.size_display);
size_display.safe_equals(other.size_display) &&
use_defaults == use_defaults &&
template_favorites.SequenceEqual(other.template_favorites) &&
template_defaults.SequenceEqual(other.template_defaults);
public override bool Equals(object obj) => Equals(obj as LadderSettings);
@@ -330,7 +343,10 @@ namespace mbts.spark {
EqualityComparer.safe_get_hash_code(session_high_line),
EqualityComparer.safe_get_hash_code(session_range_fill),
EqualityComparer.safe_get_hash_code(volume_column),
EqualityComparer.safe_get_hash_code(size_display));
EqualityComparer.safe_get_hash_code(size_display),
use_defaults.GetHashCode(),
EqualityComparer.safe_aggregate_hash(template_favorites),
EqualityComparer.safe_aggregate_hash(template_defaults));
}
static class LadderSettingsExtensions {
+23 -6
View File
@@ -89,6 +89,7 @@ namespace mbts.spark {
order_properties_vm.observe_account(true).Select(a => a?.name ?? string.Empty).Subscribe(account_positions),
account_positions,
size_calculator = new CalculatorLogic(0, _settings_manager.subscribe_current().Select(s => s.calculator_buttons)),
settings.OnDispatcher().Subscribe(s => order_properties_vm.on_template_settings(s.template_favorites, s.template_defaults)),
bus.subscribe(SparkTags.LogActivity).OnDispatcher().Subscribe(status_update),
model.order_gateway.working_orders.OnDispatcher().Subscribe(working => {
// Here we manage N orders to 1 price slot in the ladder
@@ -234,18 +235,34 @@ namespace mbts.spark {
public Brush ask_foreground => _ask_foreground.Value;
public DialogCommand settings_command { get; }
internal LadderModel model => _model;
internal IObservable<LadderSettings> settings => _settings_manager.subscribe_current();
internal IObservable<LadderSettings> settings => _settings_manager.subscribe_current(t => {
var rv = SparkUtils.clone_proto(t.defaults);
rv.use_defaults = true;
if (t.instance != null) {
rv.template_favorites.SetItems(t.instance.template_favorites);
rv.template_defaults.SetItems(t.instance.template_defaults);
}
return rv;
});
public LadderSettings get_save_state() {
// TODO: save an actual OrderDefinition to preserve order params.
if (_settings_manager.instance_settings == null)
return null;
var settings_clone = SparkUtils.clone_proto(_settings_manager.instance_settings);
settings_clone.default_order_type = order_properties_vm.selected_order_type?.order_type ?? settings_clone.default_order_type;
return settings_clone;
LadderSettings settings;
if (_settings_manager.instance_settings == null) {
settings = SparkUtils.clone_proto(_settings_manager.default_settings);
settings.use_defaults = true;
} else {
settings = SparkUtils.clone_proto(_settings_manager.instance_settings);
}
settings.default_order_type = order_properties_vm.selected_order_type?.order_type ?? settings.default_order_type;
settings.template_favorites.SetItems(order_properties_vm.favorite_algo_templates.Select(t => t.id));
settings.template_defaults.SetItems(order_properties_vm.favorite_algo_templates.Where(t => t.is_default).Select(t => t.id));
return settings;
}
public void key_down(KeyEventArgs e) {
// the ctrl modifier requirement helps avoid keyboard-input collisions w/ algo-templates popup
if (!KeyboardExtensions.IsCtrlDown()) return;
var new_order_type = order_properties_vm.order_types.FirstOrDefault(o => o.key_match(e));
if (new_order_type != null) {
order_properties_vm.selected_order_type = new_order_type;
@@ -404,6 +404,98 @@
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<ListBox
Background="Transparent"
BorderThickness="0"
ItemsSource="{Binding order_properties_vm.visible_favorite_algo_templates}"
Focusable="False"
SelectedItem="{Binding order_properties_vm.selected_favorite_algo_template}"
>
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Columns="2" Focusable="False"/>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="Background" Value="{Binding meta, Converter={StaticResource OrderTypeBrushConverter}}"/>
<Setter Property="BorderThickness" Value="1"/>
<Setter Property="BorderBrush" Value="Black"/>
<Setter Property="Foreground" Value="Black"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="Width" Value="46"/>
<Setter Property="Padding" Value="3"/>
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Opacity" Value="0.8"/>
<Setter Property="Margin" Value="2"/>
<Setter Property="ToolTip" Value="{Binding name}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBoxItem">
<Grid Opacity="{TemplateBinding Opacity}">
<Rectangle
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Fill="{TemplateBinding Background}"
Stroke="{TemplateBinding BorderBrush}"
StrokeThickness="{Binding BorderThickness.Left, RelativeSource={RelativeSource TemplatedParent}}"
RadiusX="1"
RadiusY="1"
/>
<Grid
HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
Margin="{TemplateBinding Padding}"
>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock
HorizontalAlignment="Left"
VerticalAlignment="Center"
Text="{Binding abbreviation}"
Foreground="{TemplateBinding Foreground}"
/>
<Path
x:Name="_checkmark"
Grid.Column="1"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Margin="2,0,0,0"
Fill="{TemplateBinding BorderBrush}"
Data="{Binding Source={StaticResource {x:Static mbts:Icons.CheckMarkGeometryKey}}}"
Visibility="Collapsed"
/>
</Grid>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="TextElement.FontWeight" Value="Bold"/>
<Setter Property="Opacity" Value="1"/>
<Setter Property="BorderThickness" Value="2"/>
</Trigger>
<Trigger Property="IsSelected" Value="True">
<Setter Property="TextElement.FontWeight" Value="Bold"/>
<Setter Property="Opacity" Value="1"/>
<Setter TargetName="_checkmark" Property="Visibility" Value="Visible"/>
</Trigger>
<DataTrigger Binding="{Binding is_edited}" Value="True">
<Setter Property="BorderBrush" Value="{mbts:BTSColor UI.input_dirty_border}"/>
</DataTrigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding abbreviation}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Margin="2" HorizontalAlignment="Stretch" Command="{Binding size_calculator.clear}">Clear</Button>
<!-- reset to defaults after order -->
<CheckBox Margin="2" IsChecked="{Binding auto_clear}">Auto Clear</CheckBox>