From a3968bd35bc77dbbdc2c08d3aef549205cbeacee Mon Sep 17 00:00:00 2001 From: John Colvin Date: Fri, 8 Jul 2022 00:02:33 -0400 Subject: [PATCH] ui: Add theme picker ui and stock dark theme --- .../Model/Settings/IWindowSettingsManager.cs | 1 + .../spark/Model/Settings/UserSettings.cs | 14 ++++- .../spark/Model/Settings/UserSettingsHub.cs | 57 +++++++++++++++++-- .../spark/Model/Settings/UserSettingsTheme.cs | 36 ++++++++++++ .../Market/MarketView/MarketViewSettings.cs | 20 +++++++ .../Settings/TimeAndSalesSettings.cs | 6 ++ .../Modules/Settings/SettingsShellModule.cs | 24 +++++++- .../Modules/Settings/ThemePickerModel.cs | 38 +++++++++++++ .../spark/Modules/Settings/ThemePickerVM.cs | 52 +++++++++++++++++ .../Modules/Settings/ThemePickerView.xaml | 45 +++++++++++++++ .../Modules/Settings/ThemePickerView.xaml.cs | 9 +++ .../Spreader/SyntheticManagementSettings.cs | 6 ++ .../Trading/Fills/Settings/FillsSettings.cs | 7 +++ .../Modules/Trading/Ladder/LadderSettings.cs | 18 ++++++ .../ReconcileReportSettings.cs | 5 +- src/client/spark/ViewModel/ViewModelBase.cs | 42 +++++++++++--- .../core/FontsAndColors/BTSDarkColors.cs | 14 +++++ .../Controls/DIYGrid/Settings/GridSettings.cs | 5 +- .../Shell/ViewModels/ShellThreadFactory.cs | 6 +- src/client/spark_api/utils/Optional.cs | 3 + 20 files changed, 390 insertions(+), 18 deletions(-) create mode 100644 src/client/spark/Model/Settings/UserSettingsTheme.cs create mode 100644 src/client/spark/Modules/Settings/ThemePickerModel.cs create mode 100644 src/client/spark/Modules/Settings/ThemePickerVM.cs create mode 100644 src/client/spark/Modules/Settings/ThemePickerView.xaml create mode 100644 src/client/spark/Modules/Settings/ThemePickerView.xaml.cs create mode 100644 src/client/spark/core/FontsAndColors/BTSDarkColors.cs diff --git a/src/client/spark/Model/Settings/IWindowSettingsManager.cs b/src/client/spark/Model/Settings/IWindowSettingsManager.cs index 2a41cd247..c0bacd6d0 100644 --- a/src/client/spark/Model/Settings/IWindowSettingsManager.cs +++ b/src/client/spark/Model/Settings/IWindowSettingsManager.cs @@ -34,6 +34,7 @@ namespace mbts.persist { _instance_settings = instance_settings; _get = get; _set = set; + hub.request_clear_instance_settings += () => instance_settings = null; } public T instance_settings { diff --git a/src/client/spark/Model/Settings/UserSettings.cs b/src/client/spark/Model/Settings/UserSettings.cs index c7c6121d3..e65104f27 100644 --- a/src/client/spark/Model/Settings/UserSettings.cs +++ b/src/client/spark/Model/Settings/UserSettings.cs @@ -6,6 +6,7 @@ namespace mbts.spark { using bts.shell.orderbook; using bts.shell.pnl; using bts.shell.time_and_sales; + using bts.utils; using Google.Protobuf.WellKnownTypes; @@ -75,6 +76,11 @@ namespace mbts.spark { [ProtoMember(13)] GlobalAlertSettings? _alerts; + /// Actual settings may deviate from those of the theme , as + /// the user is free to change specific properties after selecting a theme. + [ProtoMember(15)] + string? _last_picked_theme; + public UserSettings() { } public LadderSettings ladder => @@ -116,6 +122,8 @@ namespace mbts.spark { public GlobalAlertSettings alerts => _alerts ??= new GlobalAlertSettings(); + public string? last_picked_theme => _last_picked_theme; + public UserSettings with( LadderSettings? ladder = null, LadderSettingsProfile[]? ladder_profiles = null, @@ -127,7 +135,8 @@ namespace mbts.spark { GridSettings? reconcile_report = null, PNLSettings? pnl = null, OrderBookGridSettings? order_book_grid = null, - GlobalAlertSettings? alerts = null) { + GlobalAlertSettings? alerts = null, + Optional last_picked_theme = default) { var us = new UserSettings() { _ladder_profiles = ladder_profiles ?? _ladder_profiles, _ladder = ladder ?? _ladder, @@ -139,7 +148,8 @@ namespace mbts.spark { _reconcile_report = reconcile_report ?? _reconcile_report, _pnl = pnl ?? _pnl, _order_book = order_book_grid ?? _order_book, - _alerts = alerts ?? _alerts + _alerts = alerts ?? _alerts, + _last_picked_theme = last_picked_theme.or(_last_picked_theme) }; us._last_ladder_profile_id = Math.Max(_last_ladder_profile_id, us.ladder_profiles.Select(p => p.id).MaxOrDefault()); return us; diff --git a/src/client/spark/Model/Settings/UserSettingsHub.cs b/src/client/spark/Model/Settings/UserSettingsHub.cs index 573343998..4629b8fa5 100644 --- a/src/client/spark/Model/Settings/UserSettingsHub.cs +++ b/src/client/spark/Model/Settings/UserSettingsHub.cs @@ -6,12 +6,15 @@ using bts.utils; using mbts.spark; using mbts.ui.settings; +using Spark.Settings; + using System; using System.Collections.Generic; using System.Composition; using System.Linq; using System.Reactive.Linq; using System.Reactive.Subjects; +using System.Threading; using System.Threading.Tasks; namespace mbts.persist { @@ -35,14 +38,18 @@ namespace mbts.persist { readonly object _default_settings_lock = new object(); readonly BehaviorSubject _default_settings_subject = new BehaviorSubject(UserSettings.factory_defaults); + // A 'settings theme' is activated by updating the default settings object, and clearing all + // 'instance' settings so existing windows adopt the new theme (i.e., the new defaults). readonly IEnumerable _fills_migration_rules; readonly IEnumerable _mview_migration_rules; readonly IEnumerable _pnl_migration_rules; readonly IEnumerable _synth_migration_rules; readonly IEnumerable _reconcile_report_migration_rules; readonly IUserPersist _persist; + readonly Dictionary _themes; UserSettings _default_settings; + IDisposable _theme_op = null; [ImportingConstructor] public UserSettingsHub( @@ -52,7 +59,8 @@ namespace mbts.persist { [ImportMany] IEnumerable pandl_migration_rules, [ImportMany] IEnumerable pnl_migration_rules, [ImportMany] IEnumerable synth_migration_rules, - [ImportMany] IEnumerable reconcile_report_migration_rules) { + [ImportMany] IEnumerable reconcile_report_migration_rules, + [ImportMany] IEnumerable themes) { // _persist = persist; _fills_migration_rules = fills_migration_rules ?? Enumerable.Empty(); @@ -60,6 +68,7 @@ namespace mbts.persist { _pnl_migration_rules = pnl_migration_rules ?? Enumerable.Empty(); _synth_migration_rules = synth_migration_rules ?? Enumerable.Empty(); _reconcile_report_migration_rules = reconcile_report_migration_rules.empty_if_null(); + _themes = themes.ToDictionaryCrushed(t => t.name); init(); async void init() { UserSettings s; @@ -107,9 +116,26 @@ namespace mbts.persist { } } + public event Action request_clear_instance_settings { + add { + lock (_default_settings_lock) { + _request_clear_instance_settings += value; + } + } + remove { + lock (_default_settings_lock) { + _request_clear_instance_settings -= value; + } + } + } + event Action _request_clear_instance_settings; + // indicates whether or not the settings have been loaded from the server (this happens once, on startup - see the constructor for details) public bool initialized { get; private set; } + public IEnumerable themes => + _themes.Values; + internal UserSettings current { get { lock (_default_settings_lock) @@ -152,18 +178,41 @@ namespace mbts.persist { _reconcile_report_migration_rules, _reconcile_report_propi); - public async Task save_default(UserSettings value) { - // notify observers before persisting so the changes are reflected immediately in the ui + public async Task save_default(UserSettings value, bool clear_instance_settings = false) { + // * Notify observers before persisting so the changes are reflected immediately in the ui. + // * If instance settings are to be cleared, don't clear them until the new defaults are + // ready. We want to avoid the case where windows w/ instance settings consume cpu + // loading the original defaults, just to load the new defaults immediately after that. lock (_default_settings_lock) { + Disposable.dispose(ref _theme_op); + if (clear_instance_settings) { + clear_instance_async().ignore(); + } _default_settings_subject.OnNext(_default_settings = value); } await _persist_async(); + + async Task clear_instance_async() { + var cts = new CancellationTokenSource(); + _theme_op = Disposable.Create(() => cts.Cancel()); + var next = await _default_settings_subject.Skip(1).FirstOrDefaultAsync(); + if (!cts.IsCancellationRequested) { + _request_clear_instance_settings?.Invoke(); + } + } } IWindowSettingsManager _create_settings_manager(T instance_settings, IEnumerable> migration_rules, IImmutableProperty propi) where T : class { if (instance_settings != null && migration_rules.merge().apply(instance_settings, out var new_settings)) instance_settings = new_settings; - return new WindowSettingsManager(this, instance_settings, propi.get, propi.set); + var wsm = new WindowSettingsManager(this, instance_settings, propi.get, propi.set); + lock (_default_settings_lock) { + request_clear_instance_settings += clear_instance_settings; + } + return wsm; + void clear_instance_settings() { + wsm.instance_settings = null; + } } async Task _persist_async() { diff --git a/src/client/spark/Model/Settings/UserSettingsTheme.cs b/src/client/spark/Model/Settings/UserSettingsTheme.cs new file mode 100644 index 000000000..5781c930e --- /dev/null +++ b/src/client/spark/Model/Settings/UserSettingsTheme.cs @@ -0,0 +1,36 @@ +#nullable enable +namespace Spark.Settings { + using mbts.spark; + + using System.Composition; + + class UserSettingsTheme { + public UserSettingsTheme() { + name = string.Empty; + value = new(); + } + + public UserSettingsTheme(string name, UserSettings value) { + this.name = name; + this.value = value; + } + + public string name { get; } + public UserSettings value { get; } + } + + class ExportedThemes { + [Export] + public UserSettingsTheme light_theme => + new("Light", UserSettings.factory_defaults); + + [Export] + public UserSettingsTheme dark_theme => + new("Dark", UserSettings.factory_defaults.with( + market_view: bts.shell.market_view.MarketViewSettings.dark_theme_grid_settings, + time_and_sales: bts.shell.time_and_sales.TimeAndSalesSettings.dark_theme_grid_settings, + fills: bts.shell.fills.FillsSettings.dark_theme_grid_settings, + synthetic_management: bts.shell.SyntheticManagementSettings.dark_theme_grid_settings, + ladder: LadderSettings.dark_theme)); + } +} diff --git a/src/client/spark/Modules/Market/MarketView/MarketViewSettings.cs b/src/client/spark/Modules/Market/MarketView/MarketViewSettings.cs index 827a33468..0e28d2ab5 100644 --- a/src/client/spark/Modules/Market/MarketView/MarketViewSettings.cs +++ b/src/client/spark/Modules/Market/MarketView/MarketViewSettings.cs @@ -38,6 +38,26 @@ namespace bts.shell.market_view { public static GridSettings DefaultGridSettings => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.default_sorting_criteria, enable_background_color: true); + public static GridSettings dark_theme_grid_settings => + DefaultGridSettings.with( + selection_highlight: BTSDarkColors.selection_highlight.argb_color(), + background: BTSDarkColors.background.argb_color(), + font_color: BTSDarkColors.foreground.argb_color(), + columns: DefaultColumns.all.Select(c => { + Color? fg = c.strong_id() switch { + Columns.Ask or Columns.AskDirectQty or Columns.AskMergedQty or Columns.AskImpliedQty or Columns.WorkingAsks => BTSDarkColors.ask_foreground, + Columns.Bid or Columns.BidDirectQty or Columns.BidMergedQty or Columns.BidImpliedQty or Columns.WorkingBids => BTSDarkColors.bid_foreground, + _ => null, + }; + Color? bg = c.strong_id() switch { + Columns.Ask or Columns.Bid => BTSDarkColors.dark_background, + _ => null + }; + return c.with( + background_color_override: new(bg), + font_color_override: new(fg)); + })); + public static fills.FillsSettings DefaultAuxiliaryFills { get { var grid = fills.FillsSettings.MiniGridSettings; diff --git a/src/client/spark/Modules/Market/TimeAndSales/Settings/TimeAndSalesSettings.cs b/src/client/spark/Modules/Market/TimeAndSales/Settings/TimeAndSalesSettings.cs index 7f229068d..20b133244 100644 --- a/src/client/spark/Modules/Market/TimeAndSales/Settings/TimeAndSalesSettings.cs +++ b/src/client/spark/Modules/Market/TimeAndSales/Settings/TimeAndSalesSettings.cs @@ -41,6 +41,12 @@ namespace bts.shell.time_and_sales { public static GridSettings DefaultGridSettings => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.default_sorting_criteria); + public static GridSettings dark_theme_grid_settings => + DefaultGridSettings.with( + selection_highlight: BTSDarkColors.selection_highlight.argb_color(), + background: BTSDarkColors.background.argb_color(), + font_color: BTSDarkColors.foreground.argb_color()); + [ProtoMember(1)] public SecurityCriteria? security_criteria { get; } diff --git a/src/client/spark/Modules/Settings/SettingsShellModule.cs b/src/client/spark/Modules/Settings/SettingsShellModule.cs index 0a737d859..616f0c885 100644 --- a/src/client/spark/Modules/Settings/SettingsShellModule.cs +++ b/src/client/spark/Modules/Settings/SettingsShellModule.cs @@ -8,12 +8,17 @@ using mbts.spark; using mbts.ui; using mbts.ui.shell; using mbts.ui.spark; +using mbts.ui.utility; + +using Spark.Settings; +using Spark.View; using System; using System.Collections.Generic; using System.Composition; using System.IO; using System.Threading.Tasks; +using System.Windows; using System.Windows.Media; namespace bts.ui { @@ -30,7 +35,9 @@ namespace bts.ui { ws_model = ws; var actions_list = new List(2) { SparkCommands.ShowGlobalAlerts, - SparkCommands.ShowWorkspaces + SparkCommands.ShowWorkspaces, + new SparkCommandCommand( + "Choose Theme", Icons.ViewSettings, new DialogCommand(mk_theme_picker_dialog, -60, -40)) }; if (Configuration.get().enabled) { actions_list.Add(SparkCommands.ShowSettings); @@ -46,6 +53,21 @@ namespace bts.ui { })); _bus = bus; + + Window mk_theme_picker_dialog() { + var model = new ThemePickerModel(settings_hub); + var vm = new ThemePickerVM(model); + var view = new ThemePickerView() { }; + var win = new Window() { + DataContext = vm, + Content = view, + Title = "Choose Theme", + SizeToContent = SizeToContent.WidthAndHeight, + Style = Application.Current.FindResource("BTSDialog") as Style + }; + vm.RequestClose += (s, e) => WindowUtilities.force_close(win); + return win; + } } public ImageSource icon => Icons.Admin; diff --git a/src/client/spark/Modules/Settings/ThemePickerModel.cs b/src/client/spark/Modules/Settings/ThemePickerModel.cs new file mode 100644 index 000000000..0b80cd125 --- /dev/null +++ b/src/client/spark/Modules/Settings/ThemePickerModel.cs @@ -0,0 +1,38 @@ +#nullable enable +namespace Spark.Settings { + using bts; + + using mbts.persist; + + using System; + using System.Collections.Generic; + using System.Linq; + using System.Reactive.Linq; + using System.Threading.Tasks; + + class ThemePickerModel { + readonly UserSettingsHub _settings_hub; + + public ThemePickerModel(UserSettingsHub settings_hub) => + _settings_hub = settings_hub; + + public IObservable selected_theme => + _settings_hub.subscribe_default_settings() + .Select(us => _settings_hub.themes.FirstOrDefault(t => t.name == us.last_picked_theme)) + .DistinctUntilChanged(); + + public IEnumerable themes => + _settings_hub.themes; + + public async Task set_theme(UserSettingsTheme theme) { + try { + Log.Gen.info($"Applying settings theme '{theme.name}'"); + await _settings_hub.save_default(theme.value, true); + return true; + } catch (Exception ex) { + Log.Gen.error($"Error applying theme '{theme.name}': {ex}"); + } + return false; + } + } +} diff --git a/src/client/spark/Modules/Settings/ThemePickerVM.cs b/src/client/spark/Modules/Settings/ThemePickerVM.cs new file mode 100644 index 000000000..bde9ddc87 --- /dev/null +++ b/src/client/spark/Modules/Settings/ThemePickerVM.cs @@ -0,0 +1,52 @@ +#nullable enable +namespace Spark.Settings { + using bts; + using bts.collections; + + using mbts; + using mbts.ui; + using mbts.ui.shell; + + using System; + using System.Collections.ObjectModel; + using System.Reactive.Linq; + using System.Threading.Tasks; + using System.Windows.Input; + + class ThemePickerVM : DisposableViewModelBase, IRequestViewClose { + readonly ThemePickerModel _model; + readonly Property _selected_theme; + + public ThemePickerVM(ThemePickerModel model) { + _model = model; + themes.AddRange(model.themes); + _selected_theme = backing(nameof(selected_theme), model.selected_theme.OnDispatcher()); + commit_cmd = observant_cmd( + commit, t => t is not null, _selected_theme.observe_current(true).OnDispatcher()); + cancel_cmd = new Command(close); + + async Task commit(UserSettingsTheme? theme) { + if (theme is null) { + Log.Ui.warn($"Unable to apply theme: {nameof(theme)} arg is null"); + return; + } + await _model.set_theme(theme); + close(); + } + + void close() => + RequestClose?.Invoke(this, EventArgs.Empty); + } + + public event EventHandler? RequestClose; + + public ObservableCollection themes { get; } = new(); + public UserSettingsTheme? selected_theme { + get => _selected_theme.Value; + set => _selected_theme.set(value); + } + + public ICommand commit_cmd { get; } + public ICommand cancel_cmd { get; } + } +} diff --git a/src/client/spark/Modules/Settings/ThemePickerView.xaml b/src/client/spark/Modules/Settings/ThemePickerView.xaml new file mode 100644 index 000000000..1ff4f9c39 --- /dev/null +++ b/src/client/spark/Modules/Settings/ThemePickerView.xaml @@ -0,0 +1,45 @@ + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/client/spark/Modules/Settings/ThemePickerView.xaml.cs b/src/client/spark/Modules/Settings/ThemePickerView.xaml.cs new file mode 100644 index 000000000..d5a09cfe1 --- /dev/null +++ b/src/client/spark/Modules/Settings/ThemePickerView.xaml.cs @@ -0,0 +1,9 @@ +#nullable enable +namespace Spark.View { + using System.Windows.Controls; + + partial class ThemePickerView : UserControl { + public ThemePickerView() => + InitializeComponent(); + } +} diff --git a/src/client/spark/Modules/Spreader/SyntheticManagementSettings.cs b/src/client/spark/Modules/Spreader/SyntheticManagementSettings.cs index 60758553a..9dab61633 100644 --- a/src/client/spark/Modules/Spreader/SyntheticManagementSettings.cs +++ b/src/client/spark/Modules/Spreader/SyntheticManagementSettings.cs @@ -57,6 +57,12 @@ namespace bts.shell { public static GridSettings default_grid_settings => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.default_sorting_criteria); + public static GridSettings dark_theme_grid_settings => + default_grid_settings.with( + selection_highlight: BTSDarkColors.selection_highlight.argb_color(), + background: BTSDarkColors.background.argb_color(), + font_color: BTSDarkColors.foreground.argb_color()); + public static string col_id(Columns col) => col.ToString(); diff --git a/src/client/spark/Modules/Trading/Fills/Settings/FillsSettings.cs b/src/client/spark/Modules/Trading/Fills/Settings/FillsSettings.cs index ea7e7aced..55c6e4bfe 100644 --- a/src/client/spark/Modules/Trading/Fills/Settings/FillsSettings.cs +++ b/src/client/spark/Modules/Trading/Fills/Settings/FillsSettings.cs @@ -39,6 +39,13 @@ namespace bts.shell.fills { } public static GridSettings DefaultGridSettings => GridSettings.Default.with(DefaultColumns.all, DefaultColumns.default_sorting_criteria); + + public static GridSettings dark_theme_grid_settings => + DefaultGridSettings.with( + selection_highlight: BTSDarkColors.selection_highlight.argb_color(), + background: BTSDarkColors.background.argb_color(), + font_color: BTSDarkColors.foreground.argb_color()); + public static GridSettings MiniGridSettings => GridSettings.Default.with(DefaultColumns.Mini.all, DefaultColumns.Mini.sorting_criteria); public IReadOnlyList sorting_criteria => _sorting_criteria ?? (_sorting_criteria = DefaultColumns.default_sorting_criteria.ToList()); public static FillsSettings Default() => diff --git a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs index 45c08c91d..fb85d978e 100644 --- a/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs +++ b/src/client/spark/Modules/Trading/Ladder/LadderSettings.cs @@ -646,6 +646,24 @@ namespace mbts.spark { public static LadderSettings Default => mimic_tt ? tt_default : historic_default; + public static LadderSettings dark_theme { + get { + var rv = Default; + rv.orders_column.background = BTSDarkColors.background.argb_color(); + rv.orders_column.foreground = BTSDarkColors.foreground.argb_color(); + + rv.bid_column.background = BTSDarkColors.dark_background.argb_color(); + rv.bid_column.foreground = BTSDarkColors.bid_foreground.argb_color(); + + rv.price_column.background = BTSDarkColors.background.argb_color(); + rv.price_column.foreground = BTSDarkColors.foreground.argb_color(); + + rv.ask_column.background = BTSDarkColors.dark_background.argb_color(); + rv.ask_column.foreground = BTSDarkColors.ask_foreground.argb_color(); + return rv; + } + } + public enum BadgeStyle { Normal, Alternate } // TODO: rename to PriceDisplayFormat (today is merge monday, since this isn't a functional change, it is not worth the potential merge headaches right now) diff --git a/src/client/spark/Modules/Trading/ReconcileReport/ReconcileReportSettings.cs b/src/client/spark/Modules/Trading/ReconcileReport/ReconcileReportSettings.cs index 194b6e55d..67ce84cf7 100644 --- a/src/client/spark/Modules/Trading/ReconcileReport/ReconcileReportSettings.cs +++ b/src/client/spark/Modules/Trading/ReconcileReport/ReconcileReportSettings.cs @@ -36,10 +36,13 @@ namespace Spark.Trading { rv.sorting_criteria.AddRange( new(nameof(ColKey.Security), true), new(nameof(ColKey.Account), true)); - rv.grid = GridSettings.Default.with(columns: DefaultColumns.all); + rv.grid = default_grid_settings; return rv; } + public static GridSettings default_grid_settings => + GridSettings.Default.with(columns: DefaultColumns.all); + public IList sorting_criteria => _sorting_criteria ??= new(1); diff --git a/src/client/spark/ViewModel/ViewModelBase.cs b/src/client/spark/ViewModel/ViewModelBase.cs index b00994158..41dfc12fb 100644 --- a/src/client/spark/ViewModel/ViewModelBase.cs +++ b/src/client/spark/ViewModel/ViewModelBase.cs @@ -4,6 +4,7 @@ using mbts.ui.utility; using mbts.utils; using Spark.Core.Reactive; +using Spark.ViewModel.Commands; using System; using System.Collections.Generic; @@ -633,8 +634,9 @@ namespace mbts.ui.shell { public abstract class DisposableViewModelBase : ViewModelBaseWithValidation, IPropertyChangedDemuxer, IDisposable, IWillLetYouKnowWhenIDispose { protected IDisposable _dispose = null; - List _observable_properties_backing; - List _observable_properties => _observable_properties_backing ?? (_observable_properties_backing = new List()); + + readonly List _internal_resources = new(0); + PropertyChangedDemuxer _property_changed_demuxer_backing; IPropertyChangedDemuxer _property_changed_demuxer => @@ -650,7 +652,7 @@ namespace mbts.ui.shell { protected Property backing(string name, IObservable source, T default_value = default(T)) { var prop = new ObservablePropertySink(this, name, default_value); - _observable_properties.Add(source.Subscribe(prop)); + _internal_resources.Add(source.Subscribe(prop)); return prop; } @@ -660,7 +662,7 @@ namespace mbts.ui.shell { /// protected Property backing(string name, IObservable source, Func select, T default_value = default) { var prop = new ObservablePropertySink(this, name, default_value); - _observable_properties.Add(source.OnDispatcher().Subscribe(d => prop.set(select(d)))); + _internal_resources.Add(source.OnDispatcher().Subscribe(d => prop.set(select(d)))); return prop; } @@ -724,9 +726,35 @@ namespace mbts.ui.shell { return prop; } + /// + /// Creates an that automatically invokes the CanExecuteChanged event + /// in response to notifications from + /// + protected ICommand observant_cmd( + Action execute, Func can_execute, IObservable source) { + var cmd = ObservantCommand.create(execute, can_execute, out var obs); + _internal_resources.Add(source.Subscribe(obs)); + return cmd; + } + + /// + /// Creates an that automatically invokes the CanExecuteChanged event + /// in response to notifications from + /// + protected ICommand observant_cmd( + Func execute, + Func can_execute, + IObservable source, + bool allow_concurrent_executions = false) { + var cmd = ObservantAsyncCommand.create( + execute, can_execute, out var obs, allow_concurrent_executions); + _internal_resources.Add(source.Subscribe(obs)); + return cmd; + } + protected void attach(SelectorProperty selector_property, IObservable> items, IObservable> desired_item) { - _observable_properties.Add(items.OnDispatcher().Subscribe(selector_property)); - _observable_properties.Add(desired_item.OnDispatcher().Subscribe(selector_property)); + _internal_resources.Add(items.OnDispatcher().Subscribe(selector_property)); + _internal_resources.Add(desired_item.OnDispatcher().Subscribe(selector_property)); } protected void set_dispose(IDisposable disposable) { @@ -761,7 +789,7 @@ namespace mbts.ui.shell { protected virtual void Dispose(bool disposing) { } public void Dispose() { Disposable.dispose(ref _dispose); - Disposable.dispose(_observable_properties_backing); + Disposable.dispose(_internal_resources); Disposable.dispose(ref _property_changed_demuxer_backing); Dispose(true); OnDisposed?.Invoke(true); diff --git a/src/client/spark/core/FontsAndColors/BTSDarkColors.cs b/src/client/spark/core/FontsAndColors/BTSDarkColors.cs new file mode 100644 index 000000000..a13c603c9 --- /dev/null +++ b/src/client/spark/core/FontsAndColors/BTSDarkColors.cs @@ -0,0 +1,14 @@ +#nullable enable +namespace mbts.ui { + using System.Windows.Media; + + static class BTSDarkColors { + public static Color light_background => Color.FromArgb(194, 40, 40, 40); + public static Color background => Color.FromArgb(194, 20, 20, 20); + public static Color dark_background => Color.FromArgb(210, 0, 0, 0); + public static Color foreground => Color.FromRgb(236, 246, 255); + public static Color bid_foreground => Color.FromRgb(118, 183, 250); + public static Color ask_foreground => Color.FromRgb(255, 102, 102); + public static Color selection_highlight => Color.FromArgb(96, 0, 0, 0); + } +} diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs index 893813d8c..b5567bc58 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettings.cs @@ -159,12 +159,13 @@ double font_size = double.NaN, uint? background = null, bool? enable_background_color = null, - uint? selection_highlight = null) => + uint? selection_highlight = null, + uint? font_color = null) => new GridSettings( font_family ?? _font_family, DoubleHelper.IsNaNOrInfinityOr(font_size, 0) ? _font_size : font_size, font_weight, - _font_color, + font_color ?? _font_color, selection_highlight ?? _selection_highlight, columns ?? _columns, sorting_criteria ?? _sorting_criteria, diff --git a/src/client/spark/view/Shell/ViewModels/ShellThreadFactory.cs b/src/client/spark/view/Shell/ViewModels/ShellThreadFactory.cs index 441dcbf4e..fdfb6e2f6 100644 --- a/src/client/spark/view/Shell/ViewModels/ShellThreadFactory.cs +++ b/src/client/spark/view/Shell/ViewModels/ShellThreadFactory.cs @@ -11,6 +11,7 @@ using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; +using System.Windows; using System.Windows.Interop; using System.Windows.Threading; @@ -89,7 +90,7 @@ namespace mbts.ui.shell { public Dispatcher get_dispatcher(ISharedWindow window) { if (window.thread_id == null) { window.thread_init(); - return Dispatcher.CurrentDispatcher; + return Application.Current.Dispatcher; } var uid = get(window.thread_id); return attach_window(uid, window); @@ -101,6 +102,9 @@ namespace mbts.ui.shell { } public Dispatcher get_dispatcher(string id) { + if (id is null) { + return Application.Current.Dispatcher; + } return get(id).dispatcher; } } diff --git a/src/client/spark_api/utils/Optional.cs b/src/client/spark_api/utils/Optional.cs index b98ae17b7..e37db8a5d 100644 --- a/src/client/spark_api/utils/Optional.cs +++ b/src/client/spark_api/utils/Optional.cs @@ -15,6 +15,9 @@ namespace bts.utils { public T value => has_value ? _value : throw new InvalidOperationException($"value uninitialized"); + public T or(T fallback) => + has_value ? _value : fallback; + public bool Equals(Optional other) => has_value == other.has_value && (!has_value || EqualityComparer.Default.Equals(value, other.value));