diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs index d29df539d..69728d278 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridSettingsManager.cs @@ -1,5 +1,6 @@ using bts.utils; using mbts.spark; +using mbts.ui.controls; using mbts.ui.settings; using mbts.ui.shell; using mbts.utils; @@ -121,4 +122,111 @@ namespace mbts.ui { } } } + + class SimpleGridSettingsManagerVM : ViewModelBase, IObserver where TColumnKey : struct { + readonly IReadOnlyList> _columns; + readonly Property _grid_settings; + readonly DelegateProperty _font_family; + readonly DelegateProperty _font_weight; + readonly DelegateProperty _font_size; + readonly DelegateProperty _font_color, _selection_highlight, _background_color; + readonly IReadOnlyDictionary _column_flags; + readonly Action> _sync_column_order; + readonly Action _invalidate_rows; + + public SimpleGridSettingsManagerVM( + IReadOnlyList> columns, + IReadOnlyDictionary column_flags, + Action> sync_column_order, + Action invalidate_rows) { + // + _columns = columns; + _sync_column_order = sync_column_order; + _invalidate_rows = invalidate_rows; + _column_flags = (column_flags ?? DictionaryHelper.empty()) + .ToDictionary(kv => kv.Key, kv => ImmutableColumnSettingStore.create(kv.Value)); + _grid_settings = backing(nameof(grid_settings), GridSettings.Default); + _font_family = backing(nameof(font_family), () => new FontFamily(grid_settings.font_family), nameof(grid_settings)); + _font_weight = backing(nameof(font_weight), () => grid_settings.font_weight, nameof(grid_settings)); + _font_size = backing(nameof(font_size), () => grid_settings.font_size, nameof(font_size)); + _font_color = backing(nameof(font_color), () => grid_settings.font_color.rgb_solid_brush().frozen(), nameof(grid_settings)); + _selection_highlight = backing(nameof(selection_highlight), () => grid_settings.selection_highlight.to_brush().frozen(), nameof(grid_settings)); + _background_color = backing(nameof(background_color), () => grid_settings.background_color.argb_color().to_solid_brush().frozen(), nameof(grid_settings)); + } + + public GridSettings grid_settings => _grid_settings.Value; + public FontFamily font_family => _font_family.Value; + public FontWeight font_weight => _font_weight.Value; + public double font_size => _font_size.Value; + public SolidColorBrush font_color => _font_color.Value; + public SolidColorBrush selection_highlight => _selection_highlight.Value; + public SolidColorBrush background_color => _background_color.Value; + + 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; + } + + public GridSettings create() { + // IMPORTANT: don't overwrite current sorting + _grid_settings.set(grid_settings.with(_columns.Select(c => { + var col_settings = c.create(); + if (_column_flags.TryGetValue(c.id.ToString(), out ImmutableColumnSettingStore store)) + col_settings = store.apply(col_settings); + return col_settings; + }))); + return grid_settings; + } + + /// + /// This will overwrite the columns' immutable-value store (unlike the IObserver<> 'OnNext' implementation) + /// + public void load_from(GridSettings grid) => _load_from_core(grid, true); + + void IObserver.OnCompleted() { } + void IObserver.OnError(Exception error) { } + void IObserver.OnNext(GridSettings grid) { + if (grid.columns != null) { + var cols_with_applied_flags = grid.columns.ToList(); + for (int i = 0; i < cols_with_applied_flags.Count; i++) { + if (_column_flags.TryGetValue(cols_with_applied_flags[i].id, out ImmutableColumnSettingStore store)) { + cols_with_applied_flags[i] = store.apply(cols_with_applied_flags[i]); + } + } + grid = grid.with(cols_with_applied_flags); + } + _load_from_core(grid, false); + } + + void _load_from_core(GridSettings grid, bool reset_immutable_flag_stores) { + grid = grid ?? GridSettings.Default; + _grid_settings.set(grid); + foreach (var col in _columns) { + col.set_fallback_font_params(new FontFamily(grid.font_family), grid.font_size, grid.font_color.rgb_color(), grid.font_weight, grid.background_color.argb_color()); + } + // ** in the unfortunate event that we botch backwards compatability, and the columns don't bear a 1:1 relationship with the column-settings + // don't add insult to injury by crashing ** + if (grid_settings.columns != null) { + if (reset_immutable_flag_stores) { + foreach (var kv in _column_flags) { + if (grid.columns.TryGetFirst(c => c.id == kv.Key, out GridColumnSettings cs)) + kv.Value.store(cs); + } + } + if (_columns != null) { + var col_settings_id_map = grid_settings.columns.ToDictionary(cs => cs.id); + var columns_id_map = _columns.ToDictionary(c => c.id.ToString()); + if (col_settings_id_map.Count == columns_id_map.Count && col_settings_id_map.Count == grid_settings.columns.Count) + _sync_column_order?.Invoke(grid_settings.columns); + // ** don't assume that columns:column_settings have a perfect 1:1 relationship (they should, but it is possible we accidentally break the settings as grids/uis change) ** + foreach (var col in _columns) { + if (col_settings_id_map.TryGetValue(col.id.ToString(), out GridColumnSettings cs)) + col.load_from(cs); + } + } + _invalidate_rows?.Invoke(); + } + } + } }