diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs b/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs index a9652edcf..35f22e89c 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewWindow.cs @@ -7,6 +7,7 @@ using bts.utils; using mbts.persist; using mbts.ui; using mbts.ui.settings; +using mbts.ui.shell; using mbts.ui.spark; using mbts.ui.utility; using mbts.utils; @@ -135,6 +136,7 @@ namespace mbts.spark { var show_synth_create_cmd_wrapper = new SparkCommandCommand(show_synth_create_actual_cmd.title, show_synth_create_actual_cmd.icon, show_synth_create_cmd, SparkCommands.KeyGestures.ShowSyntheticCreate); var update_securities_cmd = SparkCommands.change_securities_callback(_window_type_display_name); + // commands _commands = new List { SparkCommands.ShowLadders(SparkCommands.KeyGestures.ShowLadder).complete(_vm.selected_security_mds_observable(), smds => smds.Count > 0), SparkCommands.ShowOrderTicketFromSecurity(SparkCommands.KeyGestures.ShowOrderTicket).complete(_vm.current_security_md, md => md.HasValue, ConvertUtils.ValueOrDefault), @@ -151,12 +153,11 @@ namespace mbts.spark { s => _vm.grid_settings_manager.create_vm( // we need to get the most up to date GridSettings when launching b/c users may // have changed some settings on the grid directly, like column widths. - s.with(_vm.columns.Select( c => c.create())), - this.observe_title() + s.with(_vm.columns.Select(c => c.create())) ) ); - // TODO: bind to main window's Title - var dialog = new ShellSettingsDialog { Title = "Market View Grid Settings", DataContext = vm }; + vm.SubscribeDisposed(literal_title_source.OnDispatcher().Subscribe(t => vm.title = t)); + var dialog = new ShellSettingsDialog { DataContext = vm }; vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); return dialog; })) diff --git a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesWindow.cs b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesWindow.cs index 8ab059534..85938778a 100644 --- a/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesWindow.cs +++ b/src/client/spark/Modules/Market/TimeAndSales/TimeAndSalesWindow.cs @@ -7,6 +7,7 @@ using mbts.co2; using mbts.persist; using mbts.ui; using mbts.ui.settings; +using mbts.ui.shell; using mbts.ui.spark; using mbts.ui.utility; using mbts.utils; @@ -17,7 +18,6 @@ using System.Reactive.Linq; using System.Windows; using System.Windows.Media; using static bts.shell.TimeAndSalesWindowFactory; -using static mbts.ui.controls.DIYGrid; namespace mbts.spark { class TimeAndSalesWindow : SparkWindow, ISharedWindow { @@ -77,12 +77,11 @@ namespace mbts.spark { s => _vm.value.grid_settings_manager.create_vm( // we need to get the most up to date GridSettings when launching b/c users may // have changed some settings on the grid directly, like column widths. - s.with(_vm.value.columns.Select( c => c.create())), - this.observe_title() + s.with(_vm.value.columns.Select(c => c.create())) ) ); - // TODO: bind to main window's Title - var dialog = new ShellSettingsDialog { Title = "Time and Sales Grid Settings", DataContext = vm }; + vm.SubscribeDisposed(literal_title_source.OnDispatcher().Subscribe(t => vm.title = t)); + var dialog = new ShellSettingsDialog { DataContext = vm }; vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); return dialog; })) diff --git a/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs b/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs index 6cfa693d2..52458556b 100644 --- a/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs +++ b/src/client/spark/Modules/Spreader/SyntheticManagementWindow.cs @@ -82,11 +82,10 @@ namespace mbts.spark { s => _vm.value.grid_settings_manager.create_vm( // we need to get the most up to date GridSettings when launching b/c users may // have changed some settings on the grid directly, like column widths. - s.with(_vm.value.columns.Select( c => c.create())), - this.observe_title() + s.with(_vm.value.columns.Select(c => c.create())) ) ); - // TODO: bind to main window's Title + vm.SubscribeDisposed(literal_title_source.OnDispatcher().Subscribe(t => vm.title = t)); var dialog = new ShellSettingsDialog { Title = "Synthetic Management Grid Settings", DataContext = vm }; vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); return dialog; diff --git a/src/client/spark/Modules/Support/Browser/BrowserWindow.cs b/src/client/spark/Modules/Support/Browser/BrowserWindow.cs index 553598a37..d7f506fe7 100644 --- a/src/client/spark/Modules/Support/Browser/BrowserWindow.cs +++ b/src/client/spark/Modules/Support/Browser/BrowserWindow.cs @@ -1,14 +1,9 @@ -using bts.api; +using mbts; using mbts.config; -using mbts.spark; using mbts.ui; -using ProtoBuf; +using mbts.ui.shell; using System; -using System.Collections.Generic; -using System.ComponentModel.Composition; -using System.Threading.Tasks; using System.Windows.Controls; -using mbts; namespace bts.shell { public class BrowserWindow : IShellWindow { @@ -75,12 +70,15 @@ namespace bts.shell { public string window_type => _window_type; public IShellModule Owner {get; set; } + public ShellWindowUI GetContent() { return new ShellWindowUI(_browser); } public void GetState(System.IO.Stream save_here) { _isw.save(new BrowserWindowState() { uri = uri.ToString() }, save_here); } + public void on_shell_window_vm(ShellWindowVM shell_window_vm) { } + public void Dispose() { if (_browser != null) { _browser.Dispose(); @@ -102,5 +100,4 @@ namespace bts.shell { } #endregion } - -} +} \ No newline at end of file diff --git a/src/client/spark/Modules/Trading/Fills/FillsWindow.cs b/src/client/spark/Modules/Trading/Fills/FillsWindow.cs index 20c36e90d..090fd86b5 100644 --- a/src/client/spark/Modules/Trading/Fills/FillsWindow.cs +++ b/src/client/spark/Modules/Trading/Fills/FillsWindow.cs @@ -6,6 +6,7 @@ using bts.spark; using mbts.persist; using mbts.ui; using mbts.ui.settings; +using mbts.ui.shell; using mbts.ui.spark; using mbts.ui.utility; using mbts.utils; @@ -67,12 +68,11 @@ namespace mbts.spark { s => _vm.grid_settings_manager.create_vm( // we need to get the most up to date GridSettings when launching b/c users may // have changed some settings on the grid directly, like column widths. - s.with(_vm.columns.Select( c => c.create())), - this.observe_title() + s.with(_vm.columns.Select(c => c.create())) ) ); - // TODO: bind to main window's Title - var dialog = new ShellSettingsDialog { Title = $"{window_type_name} Grid Settings", DataContext = vm }; + vm.SubscribeDisposed(literal_title_source.OnDispatcher().Subscribe(t => vm.title = t)); + var dialog = new ShellSettingsDialog { DataContext = vm }; vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); return dialog; })) diff --git a/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs b/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs index 9fe18516f..44406c7b8 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderWindowFactory.cs @@ -1,12 +1,12 @@ using bts.api; using bts.md; -using bts.shell.orderbook; using mbts; using mbts.config; using mbts.persist; using mbts.spark; using mbts.spark.utilities; using mbts.ui; +using mbts.ui.shell; using mbts.ui.spark; using ProtoBuf; using System; @@ -82,6 +82,11 @@ namespace bts.shell { IShellWindow create_ladder_window(SecurityMD md, LadderSettings state_settings, uint settings_id = 0) { IDisposable active_settings_dialog = null; var thread_id = Interlocked.Increment(ref _ladder_count) % Environment.ProcessorCount; + // on_shell_window_vm should behave like an event (do not set to null) + Action on_shell_window_vm = _ => { }; + ShellWindowVM shell_window_vm = null; + IShellWindow shell_window = null; + var title_source = new LazyObservable(() => ShellWindowExtensions.literal_title_source(shell_window, shell_window_vm)); var view = new Lazy(() => { // resources var model = new LadderModel(md, _orders_model, _mbo); @@ -170,10 +175,8 @@ namespace bts.shell { settings_id); dialog_vm.applied += apply; // dialog - var dialog = new ShellSettingsDialog { - DataContext = dialog_vm, - Title = $"Ladder Settings - '{vm.security}'" - }; + var dialog = new ShellSettingsDialog { DataContext = dialog_vm }; + dialog_vm.SubscribeDisposed(title_source.OnDispatcher().Subscribe(t => dialog_vm.title = t)); dialog.Resources[ShellSettingsDialog.SecurityMapKey] = _securities; dialog_vm.RequestClose += on_request_close; dialog.Closed += on_closed; @@ -223,11 +226,15 @@ namespace bts.shell { } }); string thread = _ladder_conf.ladders_on_threads ? $"Ladder{thread_id:00}" : ""; - return ShellWindow.create( + return shell_window = ShellWindow.create( module, md.security.ToString(), _window_type, view, - (v,s) => save(_get_state(((LadderView)v).vm, settings_id), s), - icon: Icons.Ladder, - thread_id: thread); + (v, s) => save(_get_state(((LadderView)v).vm, settings_id), s), + icon: Icons.Ladder, + thread_id: thread, + vm => { + shell_window_vm = vm; + title_source.connect(); + }); } void show_windows(IReadOnlyCollection securities) { diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderOrderVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderOrderVM.cs index eb29df182..4640d8a58 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderOrderVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderOrderVM.cs @@ -293,7 +293,7 @@ namespace mbts.spark { price_filter.minimum.value = real_price; price_filter.maximum.value = real_price; - var obvm = new OrderBookVM(null, _owner.bus, null, _owner.model.order_gateway, null, true); + var obvm = new OrderBookVM(_owner.bus, null, _owner.model.order_gateway, null, true); var state = new bts.shell.orderbook.OrderBookStartupData( new bts.shell.orderbook.FilterStates { security_criteria = new SecurityCriteria(_owner.security), diff --git a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs index 1adbbf1a8..f6581e581 100644 --- a/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs +++ b/src/client/spark/Modules/Trading/Ladder/VM/LadderSettingsVM.cs @@ -161,7 +161,7 @@ namespace mbts.spark { } public MarketSizeDisplayVM size_display { get; } = new MarketSizeDisplayVM(); - public SimpleGridSettingsVM mini_ob { get; } = new SimpleGridSettingsVM(Observable.Return("Mini OrderBook"), OrderBookGridSettingsManager.hard_column_flags); + public SimpleGridSettingsVM mini_ob { get; } = new SimpleGridSettingsVM(OrderBookGridSettingsManager.hard_column_flags); // price format public LadderSettings.NetChangeTerms price_format { get => _price_format; set => Set(ref _price_format, value); } public IEnumerable price_format_options => EnumHelper.get_values(); diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs index 03a0aee1e..e26cf5afc 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookVM.cs @@ -70,7 +70,6 @@ namespace mbts.spark { Dictionary _on_updated_single_callback_gatekeeper = new Dictionary(); public OrderBookVM( - IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model, @@ -148,8 +147,7 @@ namespace mbts.spark { s => fills_vm.value.grid_settings_manager.create_vm( // we need to get the most up to date GridSettings when launching b/c users may // have changed some settings on the grid directly, like column widths. - s.with(fills_vm.columns.Select(c => c.create())), - Observable.Return(title) + s.with(fills_vm.columns.Select(c => c.create())) ), show_save_as_defaults: false); // TODO: bind to main window's Title @@ -157,7 +155,7 @@ namespace mbts.spark { vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); return dialog; })); - grid_settings_manager = new OrderBookGridSettingsManager(owner_title, bus, notification_service, model, rows, columns); + grid_settings_manager = new OrderBookGridSettingsManager(bus, notification_service, model, rows, columns); grid_settings_manager.algo_tree_display_mode_changed += _on_filter_changed; IDisposable attach_vm_hooks = null; begin_attaching += on_begin_attaching; diff --git a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs index fa89ed3de..bcbc64aca 100644 --- a/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs +++ b/src/client/spark/Modules/Trading/OrderBook/OrderBookWindow.cs @@ -7,8 +7,8 @@ using bts.spark; using bts.view; using mbts.persist; using mbts.ui; -using mbts.ui.converters; using mbts.ui.extensions; +using mbts.ui.shell; using mbts.ui.spark; using mbts.ui.utility; using mbts.utils; @@ -17,10 +17,8 @@ using System.Collections.Generic; using System.Linq; using System.Reactive.Linq; using System.Windows; -using System.Windows.Data; using System.Windows.Input; using System.Windows.Media; -using OrderBookGridSettingsVM = mbts.spark.GridSettingsVM; namespace mbts.spark { internal class OrderBookWindow : SparkWindow, ISharedWindow { @@ -65,7 +63,7 @@ namespace mbts.spark { } public void thread_init() { - _vm = new OrderBookVM(this.observe_title(), _bus, _notification_service, _model, _settings_manager); + _vm = new OrderBookVM(_bus, _notification_service, _model, _settings_manager); _vm.load_from(_state); _view = new OrderBookView(_vm); @@ -171,14 +169,11 @@ namespace mbts.spark { // we need to get the most up to date GridSettings when launching b/c users may // have changed some settings on the grid directly, like column widths. s.general = s.general.with(_vm.core_columns.Select(c => c.create())); - return _vm.grid_settings_manager.create_vm(s, this.observe_title()); + return _vm.grid_settings_manager.create_vm(s); } ); - // TODO: bind to main window's Title + gs_vm.SubscribeDisposed(literal_title_source.OnDispatcher().Subscribe(t => gs_vm.title = t)); var dialog = new ShellSettingsDialog { DataContext = gs_vm }; - dialog.SetBinding(Window.TitleProperty, new Binding($"{nameof(gs_vm.instance_vm)}.{nameof(OrderBookGridSettingsVM.owner_title)}") { - Converter = ValueConverter.create(str => (string.IsNullOrEmpty(str) ? _default_title : str) + " Settings") - }); gs_vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); return dialog; })); diff --git a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettingsManager.cs b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettingsManager.cs index 542a7fc3e..a1a96f240 100644 --- a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettingsManager.cs +++ b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookGridSettingsManager.cs @@ -34,14 +34,13 @@ namespace bts.shell.orderbook { } public OrderBookGridSettingsManager( - IObservable owner_title, IBus bus, INotificationService notification_service, OrdersModel model, IEnumerable rows, ObservableCollection columns) { core_manager = new GridSettingsManager( - owner_title, bus, model, rows, columns, + bus, model, rows, columns, () => new OrderRecordTuplePredicate(), () => { var pvm = PredicateVMWrapper.create( @@ -90,8 +89,8 @@ namespace bts.shell.orderbook { return gs; } - public OrderBookSettingsVM create_vm(OrderBookGridSettings gs, IObservable owner_title) => - new OrderBookSettingsVM(core_manager.create_vm(gs.general, owner_title)) { + public OrderBookSettingsVM create_vm(OrderBookGridSettings gs) => + new OrderBookSettingsVM(core_manager.create_vm(gs.general)) { algo_tree_display_mode = gs.algo_tree_display_mode }; diff --git a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookSettingsVM.cs b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookSettingsVM.cs index 6791e07c4..8b3dd2b44 100644 --- a/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookSettingsVM.cs +++ b/src/client/spark/Modules/Trading/OrderBook/Settings/OrderBookSettingsVM.cs @@ -17,7 +17,8 @@ namespace bts.shell.orderbook { this.core_settings_vm = core_settings_vm; public OrderBookSettingsVM( - IObservable owner_title, IBus bus, OrdersModel orders_model, + IBus bus, + OrdersModel orders_model, Func predicate_data_factory, Func> notification_predicate_factory, Func> create_predicate_vm, @@ -25,7 +26,7 @@ namespace bts.shell.orderbook { Func>, OrderBookGridSettings> create_settings_obj, IReadOnlyDictionary column_flags = null) { core_settings_vm = new GridSettingsVM( - owner_title, bus, orders_model, predicate_data_factory, notification_predicate_factory, create_predicate_vm, create_column_predicate_vm, create_settings_obj, column_flags); + bus, orders_model, predicate_data_factory, notification_predicate_factory, create_predicate_vm, create_column_predicate_vm, create_settings_obj, column_flags); } public GridSettingsVM core_settings_vm { get; } diff --git a/src/client/spark/Tests/ViewTests/TestShellWindow.cs b/src/client/spark/Tests/ViewTests/TestShellWindow.cs index 06e6eb1f8..0c69e3d2b 100644 --- a/src/client/spark/Tests/ViewTests/TestShellWindow.cs +++ b/src/client/spark/Tests/ViewTests/TestShellWindow.cs @@ -1,14 +1,9 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using mbts.ui.shell; +using System; using System.Windows.Threading; -namespace mbts.ui.tests -{ - class TestShellWindow : IShellWindow - { +namespace mbts.ui.tests { + class TestShellWindow : IShellWindow { #region IShellWindow boilerplate public string Title { get; set; } public event Action TitleChanged = () => { }; @@ -20,6 +15,7 @@ namespace mbts.ui.tests #endregion public Dispatcher Dispatcher { get; private set; } + public ShellWindowUI GetContent() { this.Dispatcher = Dispatcher.CurrentDispatcher; var content = new System.Windows.Forms.Button() { Text = "Click Me!", Width = 100, Height = 100 }; @@ -27,10 +23,12 @@ namespace mbts.ui.tests } public bool IsDisposed { get; set; } + + public void on_shell_window_vm(ShellWindowVM shell_window_vm) { } public void Dispose() { IsDisposed = true; } } class TestSlowWindow : TestShellWindow, ISlowWindow { public void thread_init() { } } -} +} \ No newline at end of file diff --git a/src/client/spark/core/ShellInterface/IShellWindow.cs b/src/client/spark/core/ShellInterface/IShellWindow.cs index f257dc731..a6b6e68f1 100644 --- a/src/client/spark/core/ShellInterface/IShellWindow.cs +++ b/src/client/spark/core/ShellInterface/IShellWindow.cs @@ -27,6 +27,8 @@ namespace mbts.ui { // module to ask to restore the window after app restart. IShellModule Owner { get; } + void on_shell_window_vm(ShellWindowVM shell_window_vm); + // Return the UI controls which will be displayed ShellWindowUI GetContent(); @@ -113,5 +115,28 @@ namespace mbts.ui { return src.Subscribe(o); }); } + + /// + /// Returns an IObservable sequence of . + /// + public static IObservable literal_title_source(IShellWindow shell_window, ShellWindowVM shell_window_vm) { + return Observable.Create(o => { + shell_window_vm.TitleOverrideChanged += on_title_override_changed; + shell_window.TitleChanged += notify; + notify(); + connect(); + return Disposable.Create(disconnect); + void connect() { + shell_window_vm.Model.TitleChanged += notify; + shell_window_vm.TitleOverrideChanged += on_title_override_changed; + } + void disconnect() { + shell_window_vm.Model.TitleChanged -= notify; + shell_window_vm.TitleOverrideChanged -= on_title_override_changed; + } + void on_title_override_changed(string value) => notify(); + void notify() => o.OnNext(shell_window_vm.Title); + }); + } } } \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs index d6bcbeb8c..11dc2d487 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsManager.cs @@ -44,7 +44,6 @@ namespace mbts.ui { IEnumerable> _predicate_animations_data; public GridSettingsManager( - IObservable owner_title, IBus bus, OrdersModel orders_model, IEnumerable rows, @@ -90,8 +89,8 @@ namespace mbts.ui { public SolidColorBrush font_color => _font_color.Value; public SolidColorBrush selection_highlight => _selection_highlight.Value; - public GridSettingsVM create_vm(GridSettings settings, IObservable title) { - var vm = new GridSettingsVM(title, _bus, _orders_model, _predicate_data_factory, _notification_predicate_factory, _create_predicate_vm, _create_column_predicate_vm, _create_settings_obj, _column_flags.ToDictionary(kv => kv.Key, kv => kv.Value.flags)); + public GridSettingsVM create_vm(GridSettings settings) { + var vm = new GridSettingsVM(_bus, _orders_model, _predicate_data_factory, _notification_predicate_factory, _create_predicate_vm, _create_column_predicate_vm, _create_settings_obj, _column_flags.ToDictionary(kv => kv.Key, kv => kv.Value.flags)); vm.load(_create_settings_obj(settings, _predicate_animations_data)); return vm; } diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs index df64fb6b2..175940d56 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsVM.cs @@ -54,14 +54,15 @@ namespace mbts.spark { readonly Property _current_column; readonly Property _font_family; readonly Property _font_weight; - readonly Property _font_display_name, _owner_title; + readonly Property _font_display_name; readonly Property _font_size; readonly Property _show_predicate_animations, _show_cell_predicate_animations; readonly OrdersModel _orders_model; readonly IReadOnlyDictionary _column_flags; public GridSettingsVM( - IObservable owner_title, IBus bus, OrdersModel orders_model, + IBus bus, + OrdersModel orders_model, Func predicate_data_factory, Func> notification_predicate_factory, Func> create_predicate_vm, @@ -69,7 +70,6 @@ namespace mbts.spark { Func>, TSettingsData> create_settings_obj, IReadOnlyDictionary column_flags = null) { // - _owner_title = backing(nameof(owner_title), owner_title); this.bus = bus; _orders_model = orders_model; _predicate_data_factory = predicate_data_factory; @@ -92,7 +92,6 @@ namespace mbts.spark { public event EventHandler RequestClose; public event Action committed; - public string owner_title => _owner_title.Value; public IBus bus { get; } public ObservableCollection columns { get; } = new ObservableCollection(); public IColumnVM current_column { get => _current_column.Value; set => _current_column.set(value); } diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs index 3866d5236..76823094e 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs @@ -45,8 +45,8 @@ namespace mbts.ui { public SolidColorBrush font_color => _font_color.Value; public SolidColorBrush selection_highlight => _selection_highlight.Value; - public SimpleGridSettingsVM create_vm(GridSettings settings, IObservable title) { - var vm = new SimpleGridSettingsVM(title, _column_flags.ToDictionary(kv => kv.Key, kv => kv.Value.flags)); + public SimpleGridSettingsVM create_vm(GridSettings settings) { + var vm = new SimpleGridSettingsVM(_column_flags.ToDictionary(kv => kv.Key, kv => kv.Value.flags)); vm.load_from(settings); return vm; } diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsVM.cs index 68397a1f0..c27cce279 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsVM.cs @@ -34,15 +34,14 @@ namespace mbts.spark { readonly Property _current_column; readonly Property _font_family; readonly Property _font_weight; - readonly Property _font_display_name, _owner_title; + readonly Property _font_display_name; readonly Property _font_size; readonly Property _show_predicate_animations, _show_cell_predicate_animations; readonly DeferrableActionWrapper _update; readonly IReadOnlyDictionary _column_flags; - public SimpleGridSettingsVM(IObservable owner_title, IReadOnlyDictionary column_flags = null) { + public SimpleGridSettingsVM(IReadOnlyDictionary column_flags = null) { _column_flags = column_flags ?? new Dictionary(); - _owner_title = backing(nameof(owner_title), owner_title); _font_family = backing(nameof(font_family), TextUtils.StandardFontFamily); _font_size = backing(nameof(font_size), FontSizes.Small); _font_weight = backing(nameof(font_weight), FontWeights.Normal); @@ -73,7 +72,6 @@ namespace mbts.spark { public event EventHandler RequestClose; public event Action committed, result_changed; - public string owner_title => _owner_title.Value; public ObservableCollection columns { get; } = new ObservableCollection(); public ISimpleColumnVM current_column { get => _current_column.Value; set => _current_column.set(value); } public string font_display_name => _font_display_name.Value; diff --git a/src/client/spark/view/Controls/HierarchicalLaunchMenu.xaml.cs b/src/client/spark/view/Controls/HierarchicalLaunchMenu.xaml.cs index 7766023b6..2a26f4aa8 100644 --- a/src/client/spark/view/Controls/HierarchicalLaunchMenu.xaml.cs +++ b/src/client/spark/view/Controls/HierarchicalLaunchMenu.xaml.cs @@ -1,23 +1,14 @@ -using System; +using mbts.ui.shell; +using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; -using System.Text; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; -using mbts.ui; -using mbts.ui.shell; -namespace mbts.ui.controls -{ +namespace mbts.ui.controls { public partial class HierarchicalLaunchMenu : UserControl, IShellWindow { public HierarchicalLaunchMenu(object context) { @@ -36,27 +27,7 @@ namespace mbts.ui.controls public ShellWindowUI GetContent() { return new ShellWindowUI(this); } - - //bool _tree_was_clicked = false; - //private async void _tree_clicked(object sender, RoutedEventArgs e) - //{ - // e.Handled = true; - - // if (_tree_was_clicked) return; - // _tree_was_clicked = true; - // DependencyObject source = e.OriginalSource as DependencyObject; - - // while (source != null && !(source is TreeViewItem)) - // source = VisualTreeHelper.GetParent(source); - - // TreeViewItem tvi = source as TreeViewItem; - // if (tvi != null) - // tvi.IsExpanded = !tvi.IsExpanded; - - // await Task.Delay(1000); - // _tree_was_clicked = false; - - //} + public void on_shell_window_vm(ShellWindowVM shell_window_vm) { } } public class HierarchicalViewModel : ViewModelBase diff --git a/src/client/spark/view/Controls/MenuLaunchMenu.xaml.cs b/src/client/spark/view/Controls/MenuLaunchMenu.xaml.cs index 947ca39ae..4e1d77dd2 100644 --- a/src/client/spark/view/Controls/MenuLaunchMenu.xaml.cs +++ b/src/client/spark/view/Controls/MenuLaunchMenu.xaml.cs @@ -1,26 +1,12 @@ -using System; -using System.Collections.Generic; +using mbts.ui.shell; +using System; using System.Collections.ObjectModel; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows; using System.Windows.Controls; -using System.Windows.Data; -using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Navigation; -using System.Windows.Shapes; -using mbts.ui; -using mbts.ui.shell; - -namespace mbts.ui.controls -{ - public partial class MenuLaunchMenu : UserControl, IShellWindow - { +namespace mbts.ui.controls { + public partial class MenuLaunchMenu : UserControl, IShellWindow { public ObservableCollection MenuItems { get; set; } public MenuLaunchMenu(object context) { @@ -39,11 +25,10 @@ namespace mbts.ui.controls public ShellWindowUI GetContent() { return new ShellWindowUI(this); } - + public void on_shell_window_vm(ShellWindowVM shell_window_vm) { } } - public class MenuLaunchMenuViewModel : ViewModelBase - { + public class MenuLaunchMenuViewModel : ViewModelBase { public string Header { get; set; } public ObservableCollection MenuItems { get; set; } @@ -51,7 +36,5 @@ namespace mbts.ui.controls public ICommand Command { get; set; } public object Args { get; set; } - } - -} +} \ No newline at end of file diff --git a/src/client/spark/view/Extensions/ShellWindow.cs b/src/client/spark/view/Extensions/ShellWindow.cs index 2a86f0cc5..d698fe648 100644 --- a/src/client/spark/view/Extensions/ShellWindow.cs +++ b/src/client/spark/view/Extensions/ShellWindow.cs @@ -1,4 +1,5 @@ -using System; +using mbts.ui.shell; +using System; using System.IO; using System.Windows; using System.Windows.Media; @@ -38,30 +39,36 @@ namespace mbts.ui { public Lazy Content { get; set; } + public Action on_shell_window_vm { get; set; } + + void IShellWindow.on_shell_window_vm(ShellWindowVM shell_window_vm) => + on_shell_window_vm?.Invoke(shell_window_vm); } + class LazyAnonShellWindow : AnonShellWindow, ISharedWindow { public string thread_id { get; set; } public void thread_init() { } } - public static IShellWindow create(IShellModule owner, string title, string window_type, Lazy content, Action get_state = null, ImageSource icon = null, string thread_id=null) { + public static IShellWindow create(IShellModule owner, string title, string window_type, Lazy content, Action get_state = null, ImageSource icon = null, string thread_id=null, Action on_shell_window_vm = null) { if (string.IsNullOrEmpty(thread_id)) { - return create(owner, title, window_type, content, get_state, icon); + return create(owner, title, window_type, content, get_state, icon, on_shell_window_vm); } else { - var res = create(owner, title, window_type, content, get_state, icon); + var res = create(owner, title, window_type, content, get_state, icon, on_shell_window_vm); res.thread_id = thread_id; return res; } } - static T create(IShellModule owner, string title, string window_type, Lazy content, Action get_state=null, ImageSource icon=null) where T : AnonShellWindow, new() { + static T create(IShellModule owner, string title, string window_type, Lazy content, Action get_state=null, ImageSource icon=null, Action on_shell_window_vm = null) where T : AnonShellWindow, new() { var result = new T() { Title = title, window_type = window_type, Icon = icon ?? (owner != null ? owner.icon : Icons.Window), Owner = owner, get_state = get_state, + on_shell_window_vm = on_shell_window_vm, Content = content, Disposable = Disposable.Create(() => { if (content.IsValueCreated) { diff --git a/src/client/spark/view/Shell/Error/ErrorShellWindow.cs b/src/client/spark/view/Shell/Error/ErrorShellWindow.cs index 25567c479..e1e3fdb50 100644 --- a/src/client/spark/view/Shell/Error/ErrorShellWindow.cs +++ b/src/client/spark/view/Shell/Error/ErrorShellWindow.cs @@ -29,6 +29,8 @@ namespace mbts.ui.shell { } + public void on_shell_window_vm(ShellWindowVM shell_window_vm) { } + public void Dispose() { } diff --git a/src/client/spark/view/Shell/Settings/ShellSettingsDialog.xaml b/src/client/spark/view/Shell/Settings/ShellSettingsDialog.xaml index bb72d6eda..ed2f75df3 100644 --- a/src/client/spark/view/Shell/Settings/ShellSettingsDialog.xaml +++ b/src/client/spark/view/Shell/Settings/ShellSettingsDialog.xaml @@ -12,6 +12,7 @@ xmlns:sys="clr-namespace:System;assembly=mscorlib" u:WindowStyle.AllowResize="True" u:WindowStyle.SizeToContent="WidthAndHeight" + Title="{Binding title, StringFormat='{}{0} Settings'}" Style="{StaticResource BTSWindow}" UseLayoutRounding="True" > diff --git a/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs b/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs index ffcaa502d..398d994a4 100644 --- a/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs +++ b/src/client/spark/view/Shell/Settings/ShellSettingsDialogVM.cs @@ -23,11 +23,12 @@ namespace mbts.spark { T get_result(); } - class ShellSettingsDialogVM : ViewModelBase, IRequestViewClose, IDisposable where T : class, IEquatable where TVM : IWindowSettingsVM { + class ShellSettingsDialogVM : ViewModelBase, IRequestViewClose, IDisposable, IWillLetYouKnowWhenIDispose where T : class, IEquatable where TVM : IWindowSettingsVM { readonly Property _use_default; readonly DialogSkirtItem _ok_item, _apply_item, _cancel_item, _apply_to_defaults_item; readonly DelegateProperty> _left_skirt_items; readonly List _resources = new List(2); // initialize with 2 capacity bc the only potential disposables are the instance/default vms + string _title; public ShellSettingsDialogVM(IWindowSettingsManager settings_manager, Func get_factory_settings, string settings_file_ext, Func vm_factory, Func merge_with_current = null, bool show_save_as_defaults = true) { if (merge_with_current == null) @@ -94,7 +95,9 @@ namespace mbts.spark { } public event EventHandler RequestClose; + public event Action OnDisposed; + public string title { get => _title; set => Set(ref _title, value); } public TVM instance_vm { get; } public TVM default_vm { get; } public IReadOnlyCollection action_menu_items { get; } @@ -111,8 +114,10 @@ namespace mbts.spark { set => _use_default.Value = value; } - public void Dispose() => + public void Dispose() { Disposable.dispose(_resources); + OnDisposed?.Invoke(true); + } } class ProfiledShellSettingsDialogVM : DisposableViewModelBase, IRequestViewClose where T : class, IEquatable where TVM : class, IValueFactory, IObservable where TProfile : ISettingsProfile { @@ -120,6 +125,7 @@ namespace mbts.spark { readonly Property _selected_profile; readonly Func _vm_factory; readonly ProfileVM _local_profile; + string _title; bool _is_copy_menu_open; DialogSkirtItem _apply_item, _ok_item, _cancel_item; MenuItemVM _import_item, _export_item, _load_factory_item; @@ -271,6 +277,7 @@ namespace mbts.spark { public event Action> applied; public event EventHandler RequestClose; + public string title { get => _title; set => Set(ref _title, value); } public ObservableCollection profiles { get; } = new ObservableCollection(); public ProfileVM selected_profile { get => _selected_profile.Value; diff --git a/src/client/spark/view/Shell/ViewModels/ShellWindowVM.cs b/src/client/spark/view/Shell/ViewModels/ShellWindowVM.cs index 235e48268..094e933ff 100644 --- a/src/client/spark/view/Shell/ViewModels/ShellWindowVM.cs +++ b/src/client/spark/view/Shell/ViewModels/ShellWindowVM.cs @@ -1,31 +1,20 @@ -using System; -using mbts.ui; -using System.Windows; -using System.Windows.Media; -using System.Windows.Media.Imaging; -using System.Windows.Input; -using System.IO; -using System.Windows.Threading; -using System.Threading; -using System.Threading.Tasks; -using System.Windows.Controls; -using mbts.ui.settings; -using System.Windows.Interop; -using System.Runtime.InteropServices; -using System.Collections.Generic; using bts; +using System; +using System.Runtime.InteropServices; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Threading; -namespace mbts.ui.shell -{ - public class ShellWindowVM : ViewModelBase, IDisposable - { +namespace mbts.ui.shell { + public class ShellWindowVM : ViewModelBase, IDisposable { IShellWindow _model; UIElement _ui; IWindowManager _wm; Dispatcher _dispatcher; HwndContentHost _hwnd_host; - public ShellWindowVM(IShellWindow model, IWindowManager wm, IShellThreadFactory threads, string host = "") { Log.assert(model != null); _model = model; @@ -44,6 +33,7 @@ namespace mbts.ui.shell } _edit_mode = backing(nameof(EditMode), false); + model.on_shell_window_vm(this); } void title_changed() { @@ -56,6 +46,8 @@ namespace mbts.ui.shell RaisePropertyChanged("Icon"); } + public event Action TitleOverrideChanged; + public string host_id { get; set; } public Dispatcher Dispatcher { get { return _dispatcher; } } @@ -73,6 +65,7 @@ namespace mbts.ui.shell } RaisePropertyChanged(nameof(TitleOverride), nameof(Title), nameof(HasCustomTitle)); _edit_mode.set(false); + TitleOverrideChanged?.Invoke(_title_override); } } public bool HasCustomTitle => !string.IsNullOrWhiteSpace(TitleOverride); @@ -218,7 +211,6 @@ namespace mbts.ui.shell public void Dispose() { _model.TitleChanged -= title_changed; _model.IconChanged -= icon_changed; - if (IsShared) { // Shared threads continue running _dispatcher.Invoke(() => Disposable.dispose(ref _model)); @@ -237,5 +229,4 @@ namespace mbts.ui.shell } } - } \ No newline at end of file diff --git a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs index 4efa927b1..760e365b9 100644 --- a/src/client/spark/view/Spark/PAndL/PAndLWindow.cs +++ b/src/client/spark/view/Spark/PAndL/PAndLWindow.cs @@ -9,6 +9,7 @@ using mbts.ui.utility; using System; using System.Collections.Generic; using System.Linq; +using System.Reactive.Linq; using System.Windows; using System.Windows.Media; @@ -64,12 +65,12 @@ namespace mbts.spark { s => _vm.grid_settings_manager.create_vm( // we need to get the most up to date GridSettings when launching b/c users may // have changed some settings on the grid directly, like column widths. - s.with(_vm.columns.Select( c => c.create())), - this.observe_title() + s.with(_vm.columns.Select( c => c.create())) ) ); + vm.SubscribeDisposed(literal_title_source.OnDispatcher().Subscribe(t => vm.title = t)); // TODO: bind to main window's Title - var dialog = new ShellSettingsDialog { Title = "PnL Grid Settings", DataContext = vm }; + var dialog = new ShellSettingsDialog { DataContext = vm }; vm.RequestClose += (s, e) => WindowUtilities.force_close(dialog); return dialog; })) diff --git a/src/client/spark/view/Spark/SparkWindow.cs b/src/client/spark/view/Spark/SparkWindow.cs index 6fbfe6c80..9deec1e17 100644 --- a/src/client/spark/view/Spark/SparkWindow.cs +++ b/src/client/spark/view/Spark/SparkWindow.cs @@ -3,10 +3,12 @@ using bts.shell; using bts.view; using mbts; using mbts.ui; +using mbts.ui.shell; using System; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Reactive.Linq; using System.Threading.Tasks; using System.Windows; using System.Windows.Media; @@ -24,11 +26,12 @@ namespace bts.spark { ABSOLUTE_MIN_HEIGHT = 230; protected static readonly TimeSpan CONTENT_MEASURE_WAIT_PERIOD = TimeSpan.FromSeconds(0.3); - protected readonly IBus _bus; readonly ISaveWindow _save; string _title; ImageSource _icon; + ShellWindowVM _shell_window_vm; + IObservable _literal_title_source; public SparkWindow(string title, IBus bus, ISaveWindow isw, ImageSource icon = null) { _bus = bus; @@ -39,6 +42,7 @@ namespace bts.spark { public event Action TitleChanged; public event Action IconChanged; + event Action _shell_window_arrived; public string Title { get => _title; @@ -64,6 +68,20 @@ namespace bts.spark { public virtual double MinWidth => 400; public virtual double MinHeight => Math.Max(ABSOLUTE_MIN_HEIGHT, TITLE_PANEL_HEIGHT + COLUMN_HEADER_HEIGHT + GRID_FOOTER_HEIGHT + Commands.Count() * SPARK_MENU_ITEM_HEIGHT); + protected IObservable literal_title_source { + get { + if (_literal_title_source == null) { + if (_shell_window_vm != null) + _literal_title_source = ShellWindowExtensions.literal_title_source(this, _shell_window_vm); + else { + var lazy_wrapper = new LazyObservable(() => ShellWindowExtensions.literal_title_source(this, _shell_window_vm)); + _shell_window_arrived += lazy_wrapper.connect; + } + } + return _literal_title_source; + } + } + public static SparkWindow create(string window_type, string title, IBus bus, ISaveWindow save_window, ImageSource icon, string thread_id, Func<(UIElement content, IEnumerable commands, Func get_save_state, IDisposable dispose)> thread_init, double min_width = double.NaN, double min_height = double.NaN) => new AnonymousShared(window_type, title, bus, save_window, icon, thread_id, thread_init, min_width, min_height); @@ -88,6 +106,11 @@ namespace bts.spark { ); } + public void on_shell_window_vm(ShellWindowVM shell_window_vm) { + _shell_window_vm = shell_window_vm; + _shell_window_arrived?.Invoke(); + } + public abstract void Dispose(); protected virtual void OnParentViewCreated(SparkWindowView parent_view) { } diff --git a/src/client/spark_api/utils/LazyObservable.cs b/src/client/spark_api/utils/LazyObservable.cs new file mode 100644 index 000000000..93d77c41f --- /dev/null +++ b/src/client/spark_api/utils/LazyObservable.cs @@ -0,0 +1,50 @@ +using mbts; +using System; +using System.Reactive.Subjects; + +namespace bts.api { + /// Similar to an , except that the late-connection cannot be disposed (i.e., does not return an IDisposable). + class LazyObservable : IObservable { + readonly Func> _factory; + readonly object _lock = new object(); + IObservable _source; + + public LazyObservable(Func> factory) => + _factory = factory; + + event Action _connected; + + public void connect() { + // try once, without locking + if (_source != null) + return; + Action temp; + lock (_lock) { + // check if its non-null again (it may have been assigned on another thread since the first null check) + if (_source != null) + return; + _source = _factory(); + temp = _connected; + } + temp?.Invoke(); + } + + public IDisposable Subscribe(IObserver observer) { + // try once, without locking (source will only be assigned once, so no risk that it will transition from non-null to null) + if (_source != null) + return _source.Subscribe(observer); + LazySubscription sub; + lock (_lock) { + // check if its non-null again (it may have been assigned on another thread since the first null check) + if (_source != null) + return _source.Subscribe(observer); + sub = new LazySubscription(() => _source.Subscribe(observer)); + _connected += sub.connect; + } + return Disposable.Create(() => { + _connected -= sub.connect; + sub?.Dispose(); + }); + } + } +} \ No newline at end of file diff --git a/src/client/spark_api/utils/LazySubscription.cs b/src/client/spark_api/utils/LazySubscription.cs index ed6b6b4af..3c6a827a4 100644 --- a/src/client/spark_api/utils/LazySubscription.cs +++ b/src/client/spark_api/utils/LazySubscription.cs @@ -1,3 +1,4 @@ +using mbts; using System; namespace bts.api {