diff --git a/src/client/spark/AW2/ViewModel/Grid/VolSurfaceGridVM.cs b/src/client/spark/AW2/ViewModel/Grid/VolSurfaceGridVM.cs index a01456898..c17bb53d7 100644 --- a/src/client/spark/AW2/ViewModel/Grid/VolSurfaceGridVM.cs +++ b/src/client/spark/AW2/ViewModel/Grid/VolSurfaceGridVM.cs @@ -29,6 +29,7 @@ namespace mbts.spark.VolSurface { class VolSurfaceGridVM : DisposableViewModelBase { readonly VolSurfaceVM _parent; + readonly UserTuple _user; readonly ObservableCollection _expirations = new(); readonly DeferrableAction _invalidate_rows; readonly ColumnsModel _columns_model; @@ -41,10 +42,12 @@ namespace mbts.spark.VolSurface { VolSurfaceVM vm, VolSurfaceDisplayModel model, ISet hidden_columns, - ISet hidden_expirations) { + ISet hidden_expirations, + UserTuple user) { Log.notnull(hidden_columns); Log.notnull(hidden_expirations); _parent = vm; + _user = user; this.model = model; _columns_model = new(create_exp_column_view()); _hidden_columns = hidden_columns; @@ -83,7 +86,7 @@ namespace mbts.spark.VolSurface { _columns_model); VolSurfaceRowVM row_factory(VolSurfaceDisplayGridExpiration exp) => - new(this, exp); + new(this, exp, _user); IDisposable connect_row(VolSurfaceRowVM row) { row.request_calculate += request_recalc; diff --git a/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContextMenuVM.cs b/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContextMenuVM.cs index 17624cdac..0ce90958f 100644 --- a/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContextMenuVM.cs +++ b/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowContextMenuVM.cs @@ -1,5 +1,6 @@ #nullable enable namespace mbts.spark { + using bts; using bts.collections; using mbts.spark.VolSurface; @@ -9,6 +10,7 @@ namespace mbts.spark { using mbts.ui.utility; using Spark.Core.Disposables; + using Spark.Core.Reactive; using System; using System.Collections.Generic; @@ -22,6 +24,10 @@ namespace mbts.spark { readonly VolSurfaceGridVM _parent; readonly Property _column; readonly Property _super_header; + readonly UserTuple _user; + readonly Property _is_open; + readonly DelegateProperty _vs_manager; + readonly List _resources = new(); ICommand? _revert_cmd; ICommand? _shift_selection_cmd; @@ -34,21 +40,29 @@ namespace mbts.spark { ICommand? _copy_all_to_clipboard_cmd; ICommand? _smart_copy_rolls_rates_cmd; ICommand? _import_csv_cmd; + ObservantCommand? _acquire_ownership_cmd; - public VolSurfaceRowContextMenuVM(VolSurfaceGridVM parent, VolSurfaceRowVM row) { + public VolSurfaceRowContextMenuVM(VolSurfaceGridVM parent, VolSurfaceRowVM row, UserTuple user) { _parent = parent; _row = row; + _user = user; _column = backing(nameof(column)); _super_header = backing(nameof(super_header)); + _vs_manager = backing( + nameof(vs_manager), + () => _parent.model.get_active_settings()?.core_settings?.vs_manager ?? string.Empty); + _is_open = backing(nameof(is_open), false); - _dispose = Disposable.Create( + _resources.AddRange( _column.invalidate_others_on_change(nameof(column_name)), _super_header.invalidate_others_on_change(nameof(group_name)), + _is_open.Subscribe(_ => _vs_manager.refresh()), _parent.visible_rows.ObserveSelf() .BufferLatest(TimeSpan.FromSeconds(1)) .OnDispatcher() .Subscribe(_ => sync_symbols())); + _dispose = Disposable.Create(() => _resources.DisposeAll(true)); void sync_symbols() { var visible_exps = _parent.visible_rows.Map(r => r.expiration.symbol); symbols.sync_with_models( @@ -164,6 +178,34 @@ namespace mbts.spark { _parent.model.csv_importing_enabled, null); + public ICommand acquire_ownership_cmd { + get { + if (_acquire_ownership_cmd is null) { + _acquire_ownership_cmd = new( + () => { + var settings = _parent.model.get_active_settings(); + if (settings is null) { + Log.Ui.error("Unable to acquire volsurface ownership: settings are null."); + return; + } + settings.core_settings.vs_manager = _user.name; + _parent.model.set_candidate_settings(settings); + }); + _resources.Add(_vs_manager.observe_current(true) + .Subscribe(SelectorObserver.builder(_acquire_ownership_cmd).wrap( + curr => curr != _user.name))); + } + return _acquire_ownership_cmd; + } + } + + public bool is_open { + get => _is_open.Value; + set => _is_open.set(value); + } + + public string vs_manager => _vs_manager.Value; + VolSurfaceDisplayGridColumn _assert_model() => column?.model ?? throw new InvalidOperationException(); diff --git a/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowVM.cs b/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowVM.cs index fb8972b59..0e9a13e38 100644 --- a/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowVM.cs +++ b/src/client/spark/AW2/ViewModel/Grid/VolSurfaceRowVM.cs @@ -1,5 +1,7 @@ #nullable enable namespace mbts.spark.VolSurface { + using bts; + using mbts.ui; using mbts.ui.controls; using mbts.ui.shell; @@ -16,14 +18,16 @@ namespace mbts.spark.VolSurface { readonly VolSurfaceGridVM _parent; readonly VolSurfaceDisplayGridExpiration _exp; readonly Dictionary _cells = new(); + readonly UserTuple _user; readonly DeferrableAction _request_calculate; VolSurfaceRowContextMenuVM? _context_menu; bool _is_selected; - public VolSurfaceRowVM(VolSurfaceGridVM parent, VolSurfaceDisplayGridExpiration exp) { + public VolSurfaceRowVM(VolSurfaceGridVM parent, VolSurfaceDisplayGridExpiration exp, UserTuple user) { _parent = parent; _exp = exp; + _user = user; _request_calculate = new(() => request_calculate?.Invoke()); _dispose = Disposable.Create(() => { _cells.Values.DisposeAll(); @@ -49,7 +53,7 @@ namespace mbts.spark.VolSurface { } public VolSurfaceRowContextMenuVM context_menu => - _context_menu ??= new(_parent, this); + _context_menu ??= new(_parent, this, _user); public VolSurfaceCellVM get_cell(VolSurfaceDisplayGridColumn column) => _cells.GetOrAdd(column.unique_col_name, _ => _create_cell(column)).vm; diff --git a/src/client/spark/AW2/ViewModel/VolSurfaceVM.cs b/src/client/spark/AW2/ViewModel/VolSurfaceVM.cs index 8fb859364..1ef389cb2 100644 --- a/src/client/spark/AW2/ViewModel/VolSurfaceVM.cs +++ b/src/client/spark/AW2/ViewModel/VolSurfaceVM.cs @@ -1,5 +1,6 @@ #nullable enable namespace mbts.spark { + using bts; using bts.api; using bts.collections; @@ -27,6 +28,7 @@ namespace mbts.spark { readonly IBus _bus; readonly string _request_id = Guid.NewGuid().ToString(); readonly VolSurfaceDisplayModel _model; + readonly UserTuple _user; readonly IRaiseCanExecuteChangedCommand _revert_cmd; readonly IRaiseCanExecuteChangedCommand _commit_cmd; readonly IDialogCommand _main_settings_cmd; @@ -38,10 +40,16 @@ namespace mbts.spark { bool _ignore_implied_vol_settings; public VolSurfaceVM( - VolSurfaceDisplayModel model, int id, IBus bus, VolSurfaceWindowSettings startup_data) { + VolSurfaceDisplayModel model, + int id, + IBus bus, + VolSurfaceWindowSettings startup_data, + UserTuple user + ) { _model = model; _vs_id = id; _bus = bus; + _user = user; macro_actions = new VolSurfaceMacroActionsVM(_model); globals = new VolSurfaceGlobalParameterVM(_model); @@ -49,7 +57,7 @@ namespace mbts.spark { _grid_settings = startup_data.macro_grid_settings ?? new VolSurfaceMacroGridSettings(); var hidden_exps_set = startup_data.hidden_exps.empty_if_null().ToHashSet(); grid = new VolSurfaceGridVM( - this, _model, _grid_settings.hidden_cols(), hidden_exps_set); + this, _model, _grid_settings.hidden_cols(), hidden_exps_set, _user); grid.on_selection_changed += _on_grid_selection_changed; _model.publish_grid_settings(_grid_settings); diff --git a/src/client/spark/Analytics/Core/Settings/VolSurfaceCoreSettings.cs b/src/client/spark/Analytics/Core/Settings/VolSurfaceCoreSettings.cs index 060bd4c25..4de7271d6 100644 --- a/src/client/spark/Analytics/Core/Settings/VolSurfaceCoreSettings.cs +++ b/src/client/spark/Analytics/Core/Settings/VolSurfaceCoreSettings.cs @@ -86,6 +86,6 @@ namespace mbts.analytics { set => _time_settings = value; } [ProtoBuf.ProtoMember(240)] - string vs_manager = String.Empty; + public string vs_manager { get; set; } = string.Empty; } } diff --git a/src/client/spark/Modules/Analytics/AnalyticsShellModule.cs b/src/client/spark/Modules/Analytics/AnalyticsShellModule.cs index 507799cc9..9923b6dd3 100644 --- a/src/client/spark/Modules/Analytics/AnalyticsShellModule.cs +++ b/src/client/spark/Modules/Analytics/AnalyticsShellModule.cs @@ -56,7 +56,7 @@ namespace mbts.spark { r => new ReportImmutable?(r), r => r?.Id ?? throw new ArgumentNullException(nameof(r))); _window_factories = new() { - new VolSurfaceWindowFactory(this, bus), + new VolSurfaceWindowFactory(this, bus, user.Value), new AnalyticsExpirationParametersWindowFactory(this, bus), new SlideReportWindowFactory(this, bus, reports_demuxer.subscribe), new ComboReportWindowFactory(this, bus, reports_demuxer.subscribe), diff --git a/src/client/spark/Modules/Analytics/VolSurface/View/VolSurfaceView.xaml b/src/client/spark/Modules/Analytics/VolSurface/View/VolSurfaceView.xaml index 95b1c86e0..55be0463a 100644 --- a/src/client/spark/Modules/Analytics/VolSurface/View/VolSurfaceView.xaml +++ b/src/client/spark/Modules/Analytics/VolSurface/View/VolSurfaceView.xaml @@ -262,7 +262,7 @@ IsDragAdornerEnabled="False" ContextMenuOpening="_on_context_menu_opening"> - + @@ -413,6 +413,11 @@ + + + + + diff --git a/src/client/spark/Modules/Analytics/VolSurface/View/VolSurfaceView.xaml.cs b/src/client/spark/Modules/Analytics/VolSurface/View/VolSurfaceView.xaml.cs index f6499d8f4..a7a701f9a 100644 --- a/src/client/spark/Modules/Analytics/VolSurface/View/VolSurfaceView.xaml.cs +++ b/src/client/spark/Modules/Analytics/VolSurface/View/VolSurfaceView.xaml.cs @@ -122,7 +122,7 @@ } var mouse_pos = Mouse.GetPosition(this); if (!this.ChildAtPoint(mouse_pos, out var row)) { - cm.IsOpen = false; + cm.SetCurrentValue(ContextMenu.IsOpenProperty, false); return; } var cgs = g.GetColumnGeometry().ToList(); diff --git a/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindow.cs b/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindow.cs index 89c23cc35..495eacd53 100644 --- a/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindow.cs +++ b/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindow.cs @@ -1,4 +1,5 @@ namespace mbts.spark { + using bts; using bts.api; using bts.spark; @@ -14,14 +15,20 @@ public const string WINDOW_TYPE = "VolSurface"; readonly VolSurfaceWindowSettings _startup_data; + readonly UserTuple _user; VolSurfaceVM _vm; VolSurfaceView _view; public VolSurfaceWindow( - VolSurfaceWindowFactory factory, IBus bus, string vs_name, VolSurfaceWindowSettings startup_data) : - base(vs_name, bus, factory, Icons.vol_surface) { + VolSurfaceWindowFactory factory, + IBus bus, + string vs_name, + VolSurfaceWindowSettings startup_data, + UserTuple user + ) : base(vs_name, bus, factory, Icons.vol_surface) { _startup_data = startup_data ?? throw new ArgumentNullException(nameof(startup_data)); + _user = user; this.vs_name = vs_name; _dispose = Disposable.Create(() => Disposable.dispose(ref _vm)); } @@ -37,7 +44,7 @@ public override UIElement Content => _view; public void thread_init() { - _vm = new VolSurfaceVM(new VolSurfaceDisplayModel(vs_id, _bus, true), vs_id, _bus, _startup_data); + _vm = new VolSurfaceVM(new (vs_id, _bus, true), vs_id, _bus, _startup_data, _user); _view = new VolSurfaceView { DataContext = _vm }; diff --git a/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindowFactory.cs b/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindowFactory.cs index 2d3e7ae2a..ff6225c85 100644 --- a/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindowFactory.cs +++ b/src/client/spark/Modules/Analytics/VolSurface/VolSurfaceWindowFactory.cs @@ -1,4 +1,5 @@ namespace mbts.spark { + using bts; using bts.api; using bts.shell; @@ -10,10 +11,12 @@ namespace mbts.spark { class VolSurfaceWindowFactory : ShellWindowFactory { readonly IBus _bus; + readonly UserTuple _user; - public VolSurfaceWindowFactory(IShellModule module, IBus bus) + public VolSurfaceWindowFactory(IShellModule module, IBus bus, UserTuple user) : base(module, "VolSurface") { _bus = bus; + _user = user; _dispose = bus.subscribe(SparkTags.ShowVolSurface).OnDispatcher().Subscribe( async state => _bus.post(SparkTags.ShowWindow, await _create(state))); } @@ -22,7 +25,7 @@ namespace mbts.spark { _create(state); async Task _create(VolSurfaceWindowSettings state) => - new VolSurfaceWindow(this, _bus, await _lookup_vs_name(state.id), state); + new VolSurfaceWindow(this, _bus, await _lookup_vs_name(state.id), state, _user); async Task _lookup_vs_name(int vs_id) { var (vol_surfaces, success) = await _bus.subscribe(SparkTags.VolSurfaceListTag).FirstAsyncTask(TimeSpan.FromSeconds(2)); diff --git a/src/client/spark_core/Reactive/ObserverUtils.cs b/src/client/spark_core/Reactive/ObserverUtils.cs index f7203fb71..df4cadb25 100644 --- a/src/client/spark_core/Reactive/ObserverUtils.cs +++ b/src/client/spark_core/Reactive/ObserverUtils.cs @@ -23,7 +23,7 @@ namespace Spark.Core.Reactive { throw new ArgumentNullException(nameof(selector)); } - return new AnonymousObserver(outer_val => observer.OnNext(selector(outer_val)), observer.OnError, observer.OnCompleted); + return new SelectorObserver(observer, selector); } } } diff --git a/src/client/spark_core/Reactive/SelectorObserver.cs b/src/client/spark_core/Reactive/SelectorObserver.cs new file mode 100644 index 000000000..1f8f15371 --- /dev/null +++ b/src/client/spark_core/Reactive/SelectorObserver.cs @@ -0,0 +1,29 @@ +#nullable enable + +namespace Spark.Core.Reactive { + using System; + + public static class SelectorObserver { + public static SelectorObserverBuilder builder(IObserver observer) => + new(observer); + } + + public class SelectorObserver : IObserver { + readonly IObserver _observer; + readonly Func _selector; + + public SelectorObserver(IObserver observer, Func selector) { + _observer = observer; + _selector = selector; + } + + public void OnCompleted() => + _observer.OnCompleted(); + + public void OnError(Exception error) => + _observer.OnError(error); + + public void OnNext(T value) => + _observer.OnNext(_selector(value)); + } +} diff --git a/src/client/spark_core/Reactive/SelectorObserverBuilder.cs b/src/client/spark_core/Reactive/SelectorObserverBuilder.cs new file mode 100644 index 000000000..9c0465968 --- /dev/null +++ b/src/client/spark_core/Reactive/SelectorObserverBuilder.cs @@ -0,0 +1,15 @@ +#nullable enable + +namespace Spark.Core.Reactive { + using System; + + public class SelectorObserverBuilder { + readonly IObserver _observer; + + public SelectorObserverBuilder(IObserver observer) => + _observer = observer; + + public SelectorObserver wrap(Func selector) => + new(_observer, selector); + } +}