From d6499464cf9637f8aad0ba7fdca954792ed413bb Mon Sep 17 00:00:00 2001 From: Jake Norbut Date: Mon, 12 Aug 2019 13:01:34 -0500 Subject: [PATCH] fleshing out per Column bg color override --- .../data/Proto/NullableColorOpacityProto.cs | 45 +++++++++++++++++++ .../Controls/DIYGrid/Columns/ColumnBase.cs | 6 +-- .../DIYGrid/Settings/GridColumnSettings.cs | 18 +++++--- .../DIYGrid/Settings/GridColumnSettingsVM.cs | 26 +++++++++-- .../DIYGrid/Settings/GridSettingsView.xaml | 10 +++++ .../Settings/ImmutableColumnSettingsStore.cs | 16 +++++++ .../Settings/SimpleGridColumnSettingsVM.cs | 24 +++++++++- 7 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 src/client/spark/core/data/Proto/NullableColorOpacityProto.cs diff --git a/src/client/spark/core/data/Proto/NullableColorOpacityProto.cs b/src/client/spark/core/data/Proto/NullableColorOpacityProto.cs new file mode 100644 index 000000000..77344d6a3 --- /dev/null +++ b/src/client/spark/core/data/Proto/NullableColorOpacityProto.cs @@ -0,0 +1,45 @@ +using bts.utils; +using mbts.ui; +using mbts.utils; +using ProtoBuf; +using System; +using System.Windows.Media; + +namespace bts.proto { + [ProtoContract] + struct NullableColorOpacityProto : IEquatable { + [ProtoMember(1)] + readonly uint? _color; + [ProtoMember(2)] + readonly double? _opacity; + + public NullableColorOpacityProto(ColorOpacityProto? cop = default) { + _color = cop?.color.rgb_color(); + _opacity = cop?.opacity; + } + + public ColorOpacityProto? value { + get { + if (_color.HasValue && _opacity.HasValue) + return new ColorOpacityProto(_color.Value, _opacity.Value); + return null; + } + } + + public bool Equals(NullableColorOpacityProto other) => + EqualityComparer.nullable_equals(_color, other._color) && + EqualityComparer.nullable_equals(_opacity, other._opacity); + + public override bool Equals(object obj) => + obj is NullableColorProto ncp && Equals(ncp); + + public override int GetHashCode() { + uint c = _color.HasValue ? _color.Value : 0; + double o = _opacity.HasValue ? _opacity.Value : 0; + return EqualityComparer.aggregate_hash(c, o); + } + + public static bool operator ==(NullableColorOpacityProto a, NullableColorOpacityProto b) => a.Equals(b); + public static bool operator !=(NullableColorOpacityProto a, NullableColorOpacityProto b) => !a.Equals(b); + } +} \ No newline at end of file diff --git a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs index 6de340375..6bbaabe2c 100644 --- a/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs +++ b/src/client/spark/view/Controls/DIYGrid/Columns/ColumnBase.cs @@ -231,7 +231,7 @@ namespace mbts.ui.controls { } public GridColumnSettings create() => - new GridColumnSettings(id, _header_copy?.ToString(), is_visible, is_collapsed, font_weight_override, font_color_override, ActualWidth, horizontal_alignment, horizontal_offset); + new GridColumnSettings(id, _header_copy?.ToString(), is_visible, is_collapsed, font_weight_override, font_color_override, ActualWidth, horizontal_alignment, horizontal_offset, background_color_override); void _set_font_param(ref T field, T value) { field = value; @@ -242,7 +242,7 @@ namespace mbts.ui.controls { if (settings == null) return; using (ReflectionHelper.detect_property_changes(this, _raise_prop_changed, nameof(is_visible), nameof(is_collapsed), nameof(font_weight_override), nameof(font_weight), - nameof(font_color_override), nameof(font_color), nameof(font_brush), nameof(font_family), nameof(font_size), nameof(horizontal_alignment), nameof(horizontal_offset), nameof(background_color), nameof(background_brush), nameof(background_color_override))) { + nameof(font_color_override), nameof(font_color), nameof(font_brush), nameof(font_family), nameof(font_size), nameof(horizontal_alignment), nameof(horizontal_offset), nameof(background_color_override), nameof(background_color), nameof(background_brush))) { // id = settings.id; if (!string.IsNullOrEmpty(settings.display_name)) @@ -253,7 +253,7 @@ namespace mbts.ui.controls { font_color_override = settings.font_color_override; horizontal_alignment = settings.horizontal_alignment; horizontal_offset = settings.horizontal_offset; - //background_color_override = settings.background_color_override; + background_color_override = settings.background_color_override; } if (original_width != settings.width) { original_width = settings.width; diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs index cce3f9388..73650567a 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettings.cs @@ -60,6 +60,9 @@ namespace mbts.ui.settings { [DefaultValue(_default_horizontal_offset)] readonly double _horizontal_offset; + [ProtoMember(10)] + readonly NullableColorOpacityProto _background_color_override; + public GridColumnSettings() { _is_visible = _default_is_visible; _is_collapsed = _default_is_collapsed; @@ -69,7 +72,7 @@ namespace mbts.ui.settings { } public GridColumnSettings(string id, string display_name = null, bool is_visible = _default_is_visible, bool is_collapsed = _default_is_collapsed, FontWeight? font_weight_override = null, Color? font_color_override = null, - double width = _default_width, HorizontalAlignment horizontal_alignment = _default_horizontal_alignment, double horizontal_offset = _default_horizontal_offset) { + double width = _default_width, HorizontalAlignment horizontal_alignment = _default_horizontal_alignment, double horizontal_offset = _default_horizontal_offset, ColorOpacityProto? background_color_override = null) { _id = id; _display_name = display_name; _is_visible = is_visible; @@ -79,6 +82,7 @@ namespace mbts.ui.settings { _width = width; _horizontal_alignment = horizontal_alignment; _horizontal_offset = horizontal_offset; + _background_color_override = new NullableColorOpacityProto(background_color_override); } public string id => _id; @@ -90,10 +94,11 @@ namespace mbts.ui.settings { public HorizontalAlignment horizontal_alignment => _horizontal_alignment; public double horizontal_offset => _horizontal_offset; public string display_name => _display_name; + public ColorOpacityProto? background_color_override => _background_color_override.value; public GridColumnSettings with( string display_name = null, bool? is_visible = null, bool? is_collapsed = null, NullableFontWeightProto? font_weight_override = null, NullableColorProto? font_color_override = null, - double? width = null, HorizontalAlignment? horizontal_alignment = null, double? horizontal_offset = null) => + double? width = null, HorizontalAlignment? horizontal_alignment = null, double? horizontal_offset = null, NullableColorOpacityProto? background_color_override = null) => // new GridColumnSettings(_id, display_name ?? this.display_name, @@ -103,7 +108,8 @@ namespace mbts.ui.settings { (font_color_override ?? _font_color_override).value, width ?? this.width, horizontal_alignment ?? this.horizontal_alignment, - horizontal_offset ?? this.horizontal_offset); + horizontal_offset ?? this.horizontal_offset, + (background_color_override ?? _background_color_override).value); public bool Equals(GridColumnSettings other) => other != null && @@ -116,7 +122,8 @@ namespace mbts.ui.settings { width == other.width && horizontal_alignment == other.horizontal_alignment && horizontal_offset == other.horizontal_offset && - display_name == other.display_name; + display_name == other.display_name && + background_color_override.Equals(other.background_color_override); public override bool Equals(object obj) => Equals(obj as GridColumnSettings); @@ -132,7 +139,8 @@ namespace mbts.ui.settings { width.GetHashCode(), horizontal_alignment.GetHashCode(), horizontal_offset.GetHashCode(), - display_name.GetHashCode() + display_name.GetHashCode(), + background_color_override.GetHashCode() ); public override string ToString() { diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs index c8aac5300..521924964 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridColumnSettingsVM.cs @@ -26,7 +26,8 @@ namespace mbts.spark { None = 0, ImmutableFontColor = 1, ImmutableFontWeight = 1 << 1, - ImmutableHeader = 1 << 2 + ImmutableHeader = 1 << 2, + ImmutableBackgroundColor = 1 << 3 } partial class GridSettingsVM { @@ -50,7 +51,9 @@ namespace mbts.spark { _can_override_font_color = true, _is_font_color_overridden, _can_override_font_weight = true, - _is_font_weight_overridden; + _is_font_weight_overridden, + _can_override_background_color = true, + _is_background_color_overridden; FontWeight _font_weight_override; GridColumnSettings _settings; @@ -78,6 +81,10 @@ namespace mbts.spark { public bool can_override_font_weight { get => _can_override_font_weight; private set => Set(ref _can_override_font_weight, value); } public bool is_font_weight_overridden { get => _is_font_weight_overridden; set => Set(ref _is_font_weight_overridden, value); } public FontWeight font_weight_override { get => _font_weight_override; set => Set(ref _font_weight_override, value); } + // background color override + public bool can_override_background_color { get => _can_override_background_color; private set => Set(ref _can_override_background_color, value); } + public bool is_background_color_overridden { get => _is_background_color_overridden; set => Set(ref _is_background_color_overridden, value); } + public ColorPickerPopupVM background_color_override { get; } = new ColorPickerPopupVM(); // animations public ObservableEditableCollection predicate_animations { get; } @@ -89,7 +96,10 @@ namespace mbts.spark { is_font_weight_overridden ? new NullableFontWeightProto(font_weight_override) : default, is_font_color_overridden ? new NullableColorProto(font_color_override.color) : default, width, - horizontal_alignment); + horizontal_alignment, + background_color_override: is_background_color_overridden ? + new NullableColorOpacityProto(new ColorOpacityProto(background_color_override.color, background_color_override.opacity)) + : default); var animations_lookup = predicate_animations.ToFlatLookup(pa => pa.predicate.create_assigned(owner._predicate_data_factory), pa => pa.animations.Select(a => a.create())); return (settings, animations_lookup); } @@ -111,6 +121,13 @@ namespace mbts.spark { if (is_font_weight_overridden) { _font_weight_override = _settings.font_weight_override.Value; } + // background color + is_background_color_overridden = _settings.background_color_override.HasValue; + if (is_background_color_overridden) { + background_color_override.load_from( + new SolidColorBrush(_settings.background_color_override.Value.color) { + Opacity = _settings.background_color_override.Value.opacity }); + } predicate_animations.SetItems(value.animations.empty_if_null().Select(g => { var vm = new PredicateCellAnimationVM(id, owner._create_column_predicate_vm(id), owner._predicate_data_factory); vm.load_from((g.Key, g)); @@ -129,6 +146,9 @@ namespace mbts.spark { // font weight can_override_font_weight = (flags & GridColumnSettingsFeatureFlags.ImmutableFontWeight) == 0; is_font_weight_overridden &= can_override_font_weight; + // background color + can_override_background_color = (flags & GridColumnSettingsFeatureFlags.ImmutableBackgroundColor) == 0; + is_background_color_overridden &= can_override_background_color; } public override string ToString() => $"{display_name} ({id})"; diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml index a9dd99c68..e525965b0 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml +++ b/src/client/spark/view/Controls/DIYGrid/Settings/GridSettingsView.xaml @@ -642,6 +642,8 @@ + + @@ -672,6 +674,14 @@ Visibility="{Binding DataContext.current_column.is_font_color_overridden, Converter={StaticResource BooleanToVisibilityConverter}, RelativeSource={RelativeSource AncestorType=usp:GridSettingsView}}" /> + Override Background Color + + diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/ImmutableColumnSettingsStore.cs b/src/client/spark/view/Controls/DIYGrid/Settings/ImmutableColumnSettingsStore.cs index 4ab50812d..01503387f 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/ImmutableColumnSettingsStore.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/ImmutableColumnSettingsStore.cs @@ -21,6 +21,9 @@ namespace mbts.ui { if ((flags & GridColumnSettingsFeatureFlags.ImmutableFontWeight) != 0) { stores_per_flag.Add(new ImmutableFontWeightOverrideStore()); } + if ((flags & GridColumnSettingsFeatureFlags.ImmutableBackgroundColor) != 0) { + stores_per_flag.Add(new ImmutableBackgroundColorOverrideStore()); + } return new CompositeImmutableSettingsStore(stores_per_flag, flags); } @@ -66,6 +69,19 @@ namespace mbts.ui { _value = new NullableFontWeightProto(settings.font_weight_override); } + class ImmutableBackgroundColorOverrideStore : ImmutableColumnSettingStore { + NullableColorOpacityProto _value; + + public ImmutableBackgroundColorOverrideStore() + : base(GridColumnSettingsFeatureFlags.ImmutableBackgroundColor) { } + + public override GridColumnSettings apply(GridColumnSettings settings) => + settings.with(background_color_override: _value); + + public override void store(GridColumnSettings settings) => + _value = new NullableColorOpacityProto(settings.background_color_override); + } + class CompositeImmutableSettingsStore : ImmutableColumnSettingStore { readonly IReadOnlyList _stores; public CompositeImmutableSettingsStore(IReadOnlyList stores, GridColumnSettingsFeatureFlags flags) diff --git a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridColumnSettingsVM.cs b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridColumnSettingsVM.cs index 407f5b2d4..d4dc86c50 100644 --- a/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridColumnSettingsVM.cs +++ b/src/client/spark/view/Controls/DIYGrid/Settings/SimpleGridColumnSettingsVM.cs @@ -21,7 +21,9 @@ namespace mbts.spark { _can_override_font_color = true, _is_font_color_overridden, _can_override_font_weight = true, - _is_font_weight_overridden; + _is_font_weight_overridden, + _can_override_background_color = true, + _is_background_color_overridden; FontWeight _font_weight_override; GridColumnSettings _settings; @@ -43,6 +45,10 @@ namespace mbts.spark { public bool can_override_font_weight { get => _can_override_font_weight; private set => Set(ref _can_override_font_weight, value); } public bool is_font_weight_overridden { get => _is_font_weight_overridden; set => Set(ref _is_font_weight_overridden, value); } public FontWeight font_weight_override { get => _font_weight_override; set => Set(ref _font_weight_override, value); } + // background color override + public bool can_override_background_color { get => _can_override_background_color; private set => Set(ref _can_override_background_color, value); } + public bool is_background_color_overridden { get => _is_background_color_overridden; set => Set(ref _is_background_color_overridden, value); } + public ColorPickerPopupVM background_color_override { get; } = new ColorPickerPopupVM(); public GridColumnSettings create() => _settings.with( @@ -52,7 +58,10 @@ namespace mbts.spark { is_font_weight_overridden ? new NullableFontWeightProto(font_weight_override) : (NullableFontWeightProto?)null, is_font_color_overridden ? new NullableColorProto(font_color_override.color) : (NullableColorProto?)null, width, - horizontal_alignment); + horizontal_alignment, + background_color_override: is_background_color_overridden ? + new NullableColorOpacityProto(new ColorOpacityProto(background_color_override.color, background_color_override.opacity)) + : default); public void load_from(GridColumnSettings value) { _settings = value; @@ -71,6 +80,14 @@ namespace mbts.spark { if (is_font_weight_overridden) { _font_weight_override = _settings.font_weight_override.Value; } + // background color + is_background_color_overridden = _settings.background_color_override.HasValue; + if (is_background_color_overridden) { + background_color_override.load_from( + new SolidColorBrush(_settings.background_color_override.Value.color) { + Opacity = _settings.background_color_override.Value.opacity + }); + } } public void apply_flags(GridColumnSettingsFeatureFlags flags) { @@ -84,6 +101,9 @@ namespace mbts.spark { // font weight can_override_font_weight = (flags & GridColumnSettingsFeatureFlags.ImmutableFontWeight) == 0; is_font_weight_overridden &= can_override_font_weight; + // background color + can_override_background_color = (flags & GridColumnSettingsFeatureFlags.ImmutableBackgroundColor) == 0; + is_background_color_overridden &= can_override_background_color; } public override string ToString() => $"{display_name} ({id})";