refactor DIYGrid.SelectionHighlight to be Color instead of Brush (also fixes drag rect adorner bug, where the color wouldn't respond to settings changes)

This commit is contained in:
John Colvin committed 2019-11-25 12:58:12 -06:00
1 parent 6692b52718
commit 09631447c7
18 files changed
+283 -104

No files matched your search

@@ -1,6 +1,7 @@
using bts.api;
using bts.proto;
using bts.settings;
using bts.utils;
using mbts.persist;
using mbts.spark;
using mbts.spark.filters;
@@ -311,5 +312,11 @@ namespace bts.shell {
return false;
}
}
[Export(typeof(IMarketViewGridSettingsMigrationRule))]
class EnsureSelectionHighlightTranslucency : IMarketViewGridSettingsMigrationRule {
public bool apply(GridSettings settings, out GridSettings new_settings) =>
GridSettingsMigrationRules.SelectionHighlightIsTranslucent.apply(settings, out new_settings);
}
}
}
@@ -72,5 +72,11 @@ namespace bts.shell {
public bool apply(GridSettings settings, out GridSettings new_settings) =>
settings.add_missing_columns(FillsSettings.DefaultGridSettings.columns, out new_settings);
}
[Export(typeof(IFillsGridSettingsMigrationRule))]
class EnsureSelectionHighlightTranslucency : IFillsGridSettingsMigrationRule {
public bool apply(GridSettings settings, out GridSettings new_settings) =>
GridSettingsMigrationRules.SelectionHighlightIsTranslucent.apply(settings, out new_settings);
}
}
}
@@ -9,7 +9,6 @@
xmlns:usp="clr-namespace:mbts.ui.spark"
xmlns:uspcv="clr-namespace:mbts.ui.spark.converters"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
InactiveSelectionOpacity="0.3"
>
<c:DIYGrid.Resources>
<ResourceDictionary>
@@ -49,8 +49,9 @@ namespace bts.shell.pnl {
PNLHeaderRowDisplay _header_row_display = PNLHeaderRowDisplay.Total;
PNLRowCollectionView _rows_view;
IReadOnlyList<(ColumnKeys key, ListSortDirection direction)> _sorting_hierarchy;
Brush _selection_highlight;
Color _grid_background_color = Colors.Transparent;
Color
_grid_background_color = Colors.Transparent,
_selection_highlight;
bool _is_paused;
// default appearances
ImmutableDictionary<ColumnKeys, PNLCellAppearance>
@@ -175,7 +176,7 @@ namespace bts.shell.pnl {
}
}
public Brush selection_highlight { get => _selection_highlight; set => Set(ref _selection_highlight, value); }
public Color selection_highlight { get => _selection_highlight; set => Set(ref _selection_highlight, value); }
public Color grid_background_color { get => _grid_background_color; set => Set(ref _grid_background_color, value); }
public IValueConverter group_background_converter => _group_background_converter;
@@ -291,7 +292,7 @@ namespace bts.shell.pnl {
};
public void load_from(PNLSettings settings) {
selection_highlight = settings.grid.selection_highlight.to_brush();
selection_highlight = settings.grid.selection_highlight;
grid_background_color = settings.grid.background;
calculation_type = settings.calculation_type;
if (settings.filters != null) {
@@ -24,7 +24,7 @@ namespace mbts.ui.controls.adorners {
nameof(Fill),
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._fill = v,
DIYGrid.DefaultSelectionHighlight);
FrozenBrushCache.get_brush(DIYGrid.DefaultSelectionHighlight));
public SolidColorBrush Fill {
get => GetValue(FillProperty) as SolidColorBrush;
@@ -38,7 +38,7 @@ namespace mbts.ui.controls.adorners {
nameof(Stroke),
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._stroke = v,
DIYGrid.DefaultSelectionStroke);
FrozenPenCache.get_pen(DIYGrid.DefaultSelectionHighlight));
public Pen Stroke {
get => GetValue(StrokeProperty) as Pen;
@@ -8,8 +8,8 @@ using System.Windows.Media;
namespace mbts.ui.controls.adorners {
public class DragRectAdorner : Adorner, IDisposable {
Brush _fill = DIYGrid.DefaultSelectionHighlight;
Pen _stroke = DIYGrid.DefaultSelectionStroke;
Brush _fill;
Pen _stroke;
readonly ScrollViewerTuple _scroll_viewer_tup;
readonly double _drift_multiplier;
Point? _drag_start;
@@ -20,7 +20,7 @@ namespace mbts.ui.controls.adorners {
nameof(Fill),
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._fill = v,
DIYGrid.DefaultSelectionHighlight);
FrozenBrushCache.get_brush(DIYGrid.DefaultSelectionHighlight));
public SolidColorBrush Fill {
get => GetValue(FillProperty) as SolidColorBrush;
@@ -32,7 +32,7 @@ namespace mbts.ui.controls.adorners {
nameof(Stroke),
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._stroke = v,
DIYGrid.DefaultSelectionStroke);
FrozenPenCache.get_pen(DIYGrid.DefaultSelectionHighlight));
public Pen Stroke {
get => GetValue(StrokeProperty) as Pen;
@@ -48,6 +48,8 @@ namespace mbts.ui.controls.adorners {
/// <param name="scroll_viewer">The element's ScrollViewer.</param>
/// <param name="drift_multiplier">The number of pixels per scroll unit.</param>
public DragRectAdorner(FrameworkElement adorned_element, ScrollViewer scroll_viewer = null, double drift_multiplier = 1) : base(adorned_element) {
_fill = Fill;
_stroke = Stroke;
_drift_multiplier = drift_multiplier;
AdornedElement.PreviewMouseDown += _on_preview_mouse_down;
AdornedElement.PreviewMouseMove += _on_preview_mouse_move;
@@ -1,18 +1,16 @@
using System;
using mbts.ui.extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using bts.utils;
using mbts.ui.extensions;
namespace mbts.ui.controls.adorners {
public class RowSelectionAdorner : Adorner, DIYGrid.ISelectionHighlightListener, IDisposable {
Brush _fill = DIYGrid.DefaultSelectionHighlight;
Pen _stroke = DIYGrid.DefaultSelectionStroke;
class RowSelectionAdorner : Adorner<ListBox>, IDisposable {
Brush _fill;
Pen _stroke;
DIYGridRowsPresenter _diy_rp;
ScrollViewer _diy_scroll;
@@ -25,20 +23,21 @@ namespace mbts.ui.controls.adorners {
nameof(Fill),
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._fill = v,
DIYGrid.DefaultSelectionHighlight);
FrozenBrushCache.get_brush(DIYGrid.DefaultSelectionHighlight));
public SolidColorBrush Fill {
get => GetValue(FillProperty) as SolidColorBrush;
set => SetValue(FillProperty, value);
}
#endregion
#region Stroke
public static readonly DependencyProperty StrokeProperty =
WPFHelper.create_dp<Pen, RowSelectionAdorner>(
nameof(Stroke),
FrameworkPropertyMetadataOptions.AffectsRender,
(o, v) => o._stroke = v,
DIYGrid.DefaultSelectionStroke);
FrozenPenCache.get_pen(DIYGrid.DefaultSelectionHighlight.with(255)));
public Pen Stroke {
get => GetValue(StrokeProperty) as Pen;
@@ -46,7 +45,9 @@ namespace mbts.ui.controls.adorners {
}
#endregion
internal RowSelectionAdorner(DIYGridRowsPresenter adorned_element, ScrollViewer scroll_viewer) : base(adorned_element) {
public RowSelectionAdorner(DIYGridRowsPresenter adorned_element, ScrollViewer scroll_viewer) : base(adorned_element) {
_fill = Fill;
_stroke = Stroke;
// we want our clicks to go through to the underlying grid
IsHitTestVisible = false;
@@ -60,7 +61,6 @@ namespace mbts.ui.controls.adorners {
_disposables.Add(_diy_scroll.SubscribeScrollChanged((s, e) => InvalidateVisual()));
}
protected override void OnRender(DrawingContext drawingContext) {
base.OnRender(drawingContext);
if (AdornedElement is ListBox lb && lb.SelectedItems != null && lb.SelectedItems.Count > 0) {
@@ -3,6 +3,7 @@ using bts.collections;
using bts.utils;
using mbts.spark.filters;
using mbts.ui.controls.adorners;
using mbts.ui.converters;
using mbts.ui.dpi;
using mbts.ui.extensions;
using mbts.ui.utility;
@@ -223,6 +224,13 @@ namespace mbts.ui.controls {
}
class DIYGridRowsPresenter : ListBox {
static readonly PropertyPath
_highlight_path = new PropertyPath(DIYGrid.SelectionHighlightProperty),
_active_path = new PropertyPath(IsSelectionActiveProperty);
static readonly IValueConverter
_active_opacity_converter = ValueConverter.create<bool, double>(is_active => is_active ? 1.0 : 0.5);
readonly LoggerContext _log = new LoggerContext(Log.Ui, $"{nameof(DIYGridRowsPresenter)} ", null);
double _row_height = DIYGrid.DefaultRowHeight;
ScrollContentPresenter _content_presenter_backing;
@@ -240,7 +248,10 @@ namespace mbts.ui.controls {
DefaultStyleKeyProperty.OverrideMetadata(typeof(DIYGridRowsPresenter), new FrameworkPropertyMetadata(typeof(DIYGridRowsPresenter)));
public DIYGridRowsPresenter() {
Loaded += (s, e) => {
Loaded += on_loaded;
Unloaded += on_unloaded;
//
void on_loaded(object sender, RoutedEventArgs e) {
if (_grid != null)
_grid.RowHeightChanged -= on_row_height_changed;
_grid = this.find_ancestor_of_type<DIYGrid>();
@@ -249,16 +260,23 @@ namespace mbts.ui.controls {
_support_group_items = _grid.SupportGroupItems;
_grid.SupportGroupItemsChanged += on_support_group_items_changed;
_scroll_viewer = this.descendants_of_type<ScrollViewer>().First();
if (SelectionMode != SelectionMode.Single) {
_drag_rect_adorner = new DragRectAdorner(this, _scroll_viewer, _row_height);
_drag_rect_adorner.SetResourceReference(DragRectAdorner.FillProperty, DIYGrid.ActiveSelectionHighlightKey);
_drag_rect_adorner.SetResourceReference(DragRectAdorner.StrokeProperty, DIYGrid.SelectionStrokeKey);
// selection highlighting
if (SelectionMode == SelectionMode.Single) {
_drag_rect_adorner = new DragRectAdorner(this, _scroll_viewer);
_drag_rect_adorner.bind(DragRectAdorner.FillProperty, _highlight_path, DIYGrid.SelectionHighlightBrushConverter, _grid);
_drag_rect_adorner.bind(DragRectAdorner.StrokeProperty, _highlight_path, DIYGrid.SelectionHighlightPenConverter, _grid);
_drag_rect_adorner.bind(OpacityProperty, _active_path, DIYGrid.SelectionHighlightOpacityConverter, _grid);
_drag_rect_adorner.attach();
}
_rsa = new RowSelectionAdorner(this, _scroll_viewer);
_rsa.bind(RowSelectionAdorner.FillProperty, _highlight_path, DIYGrid.SelectionHighlightBrushConverter, _grid);
_rsa.bind(RowSelectionAdorner.StrokeProperty, _highlight_path, DIYGrid.SelectionHighlightPenConverter, _grid);
_rsa.bind(OpacityProperty, _active_path, DIYGrid.SelectionHighlightOpacityConverter, _grid);
_rsa.attach();
};
Unloaded += (s, e) => {
}
void on_unloaded(object sender, RoutedEventArgs e) {
if (_drag_rect_adorner != null) {
_drag_rect_adorner.detach();
Disposable.dispose(ref _drag_rect_adorner);
@@ -267,7 +285,8 @@ namespace mbts.ui.controls {
_rsa.detach();
Disposable.dispose(ref _rsa);
}
};
}
void on_row_height_changed(double v) => _row_height = v;
void on_support_group_items_changed(bool v) => _support_group_items = v;
}
@@ -399,10 +418,6 @@ namespace mbts.ui.controls {
}
partial class DIYGrid : Control {
public const string
ActiveSelectionHighlightKey = nameof(ActiveSelectionHighlightKey),
InactiveSelectionHighlightKey = nameof(InactiveSelectionHighlightKey),
SelectionStrokeKey = nameof(SelectionStrokeKey);
public const double
DefaultRowHeight = 17,
DefaultColumnHeaderHeight = 30;
@@ -432,40 +447,22 @@ namespace mbts.ui.controls {
// TODO: SelectionFill/Stroke should not be hardcoded, for now this serves as the central location for DragRectAdorner,
// and FrameworkElementColumns that paint the selection brush themselves
// (e.g MarketView bid/ask columns, bc their background is not transparent, it covers the ListBoxItem selection-highlight).
public static readonly SolidColorBrush DefaultSelectionHighlight = new SolidColorBrush(Colors.MediumSlateBlue) { Opacity = 0.4 };
public static readonly Pen DefaultSelectionStroke = new Pen(Brushes.MediumSlateBlue, 1);
public static readonly Color DefaultSelectionHighlight = Colors.MediumSlateBlue.with(0x63);
readonly NotifyCollectionChangedEventHandler _sort_descriptions_collection_changed_cb;
double _inactive_selection_opacity = 0.2;
DataContextHooks _data_context_hooks;
public static readonly DependencyProperty SelectionHighlightProperty =
WPFHelper.create_dp<SolidColorBrush, DIYGrid>(
nameof(SelectionHighlight),
(o, v) => {
o.Resources[ActiveSelectionHighlightKey] = v;
o._reset_inactive_selection_brush();
o.Resources[SelectionStrokeKey] = new Pen(o.SelectionHighlight.Color.to_solid_brush().frozen(), 1).frozen();
foreach (var col in o.Columns.empty_if_null().OfType<ISelectionHighlightListener>())
col.on_selection_fill(v);
var ad_layer = System.Windows.Documents.AdornerLayer.GetAdornerLayer(o);
if (ad_layer != null) {
foreach (var ad in ad_layer.descendants_of_type<ISelectionHighlightListener>()) {
ad.on_selection_fill(v);
}
}
}, (o, v) => v as SolidColorBrush ?? DefaultSelectionHighlight,
DefaultSelectionHighlight);
WPFHelper.create_dp<Color, DIYGrid>(nameof(SelectionHighlight), DefaultSelectionHighlight);
public SolidColorBrush SelectionHighlight {
get => GetValue(SelectionHighlightProperty) as SolidColorBrush;
public Color SelectionHighlight {
get => (Color)GetValue(SelectionHighlightProperty);
set => SetValue(SelectionHighlightProperty, value);
}
static DIYGrid() {
DefaultStyleKeyProperty.OverrideMetadata(typeof(DIYGrid), new FrameworkPropertyMetadata(typeof(DIYGrid)));
UseLayoutRoundingProperty.OverrideMetadata(typeof(DIYGrid), new FrameworkPropertyMetadata(OnUseLayoutRoundingChanged));
DefaultSelectionHighlight.Freeze();
DefaultSelectionStroke.Freeze();
void OnUseLayoutRoundingChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
d.CoerceValue(RowHeightProperty);
@@ -536,8 +533,6 @@ namespace mbts.ui.controls {
}
}
}));
Resources[ActiveSelectionHighlightKey] = SelectionHighlight;
_reset_inactive_selection_brush();
_data_context_hooks = new DataContextHooks(this, DataContext);
DataContextChanged += OnDataContextChanged;
}
@@ -545,15 +540,14 @@ namespace mbts.ui.controls {
public event DIYGridSortingEventHandler Sorting;
public event Action<bool> SupportGroupItemsChanged;
public double InactiveSelectionOpacity {
get => _inactive_selection_opacity;
set {
if (value != _inactive_selection_opacity && value.is_within_range(0.1, 0.9)) {
_inactive_selection_opacity = value;
_reset_inactive_selection_brush();
}
}
}
public static IValueConverter SelectionHighlightPenConverter =>
ValueConverter.create<Color, Pen>(c => FrozenPenCache.get_pen(c.with(255)));
public static IValueConverter SelectionHighlightBrushConverter =>
ValueConverter.create<Color, Brush>(FrozenBrushCache.get_brush);
public static IValueConverter SelectionHighlightOpacityConverter =>
ValueConverter.create<bool, double>(is_active => is_active ? 1.0 : 0.5);
public bool SupportGroupItems {
get => _support_group_items;
@@ -615,18 +609,6 @@ namespace mbts.ui.controls {
void OnDataContextChanged(object sender, DependencyPropertyChangedEventArgs e) =>
Disposable.replace(ref _data_context_hooks, new DataContextHooks(this, e.NewValue));
void _reset_inactive_selection_brush() {
try {
if (TryFindResource(ActiveSelectionHighlightKey) is SolidColorBrush brush) {
var clone = brush.Clone();
if (_inactive_selection_opacity < clone.Opacity) {
clone.Opacity = _inactive_selection_opacity;
}
Resources[InactiveSelectionHighlightKey] = clone;
}
} catch { }
}
#region Column Handling
public void invalidate_column_layout(object sender = null, EventArgs args = null) {
var geometry = GetColumnGeometry().ToList();
@@ -1,6 +1,5 @@
using bts.proto;
using bts.utils;
using mbts.ui.controls;
using mbts.ui.converters;
using ProtoBuf;
using System;
@@ -24,7 +23,7 @@ namespace mbts.ui.settings {
public static GridSettings Default =>
new GridSettings(
selection_highlight: new ColorOpacityProto(FallbackValues.selection_highlight.Color, FallbackValues.selection_highlight.Opacity),
selection_highlight: FallbackValues.selection_highlight.rgb_color(),
background: _default_background_color, //new ColorOpacityProto(FallbackValues.background_color.Color, FallbackValues.background_color.Opacity));
enable_background_color: _default_enable_background_color);
@@ -45,11 +44,10 @@ namespace mbts.ui.settings {
public static double font_size => _default_font_size;
public static Color font_color => _default_font_color.rgb_color();
public static FontWeight font_weight => FontWeight.FromOpenTypeWeight(_default_font_weight);
public static SolidColorBrush selection_highlight => DIYGrid.DefaultSelectionHighlight;
public static Color selection_highlight => Colors.MediumSlateBlue.with(0x63);
public static uint background_color => _default_background;
}
public static IValueConverter selection_highlight_converter => ValueConverter.create<GridSettings, SolidColorBrush>(gs => gs.selection_highlight.to_brush().frozen());
public static IValueConverter font_family_converter => ValueConverter.create<GridSettings, FontFamily>(gs => new FontFamily(gs.font_family));
public static IValueConverter font_weight_converter => ValueConverter.create<GridSettings, FontWeight>(gs => gs.font_weight);
public static IValueConverter font_size_converter => ValueConverter.create<GridSettings, double>(gs => gs.font_size);
@@ -71,8 +69,29 @@ namespace mbts.ui.settings {
[DefaultValue(_default_font_color)]
readonly uint _font_color;
// selection highlight
ColorOpacityProto _obsolete_selection_highlight_backing;
uint _selection_highlight_backing;
[ProtoMember(5)]
readonly ColorOpacityProto _selection_highlight;
ColorOpacityProto _obsolete_selection_highlight {
get => _obsolete_selection_highlight_backing;
set {
_obsolete_selection_highlight_backing = value;
// only overwrite non-obsolete selection highlight if it is 0 (assume that means 'unassigned').
if (_selection_highlight_backing == 0) {
var color = value.argb_color;
// ensure reasonable opacity
color.A = MathUtils.within_range(color.A,
GridSettingsMigrationRules.SelectionHighlightIsTranslucent.min_alpha,
GridSettingsMigrationRules.SelectionHighlightIsTranslucent.max_alpha);
_selection_highlight_backing = color.rgb_color();
}
}
}
[ProtoMember(10)]
uint _selection_highlight {
get => _selection_highlight_backing;
set => _selection_highlight_backing = value;
}
// columns
[ProtoMember(6)]
List<GridColumnSettings> _columns;
@@ -101,7 +120,7 @@ namespace mbts.ui.settings {
double font_size = _default_font_size,
FontWeight font_weight = default,
uint font_color = _default_font_color,
ColorOpacityProto? selection_highlight = null,
uint selection_highlight = 0,
IEnumerable<GridColumnSettings> columns = null,
IEnumerable<SortingCriteria> sorting_criterion = null,
uint background = _default_background,
@@ -111,7 +130,7 @@ namespace mbts.ui.settings {
_font_size = font_size;
_font_color = font_color;
_font_weight = (ushort)font_weight.ToOpenTypeWeight();
_selection_highlight = selection_highlight ?? new ColorOpacityProto(0, 1);
_selection_highlight = selection_highlight;
_columns = new List<GridColumnSettings>(columns.empty_if_null());
_sorting_criterion = new List<SortingCriteria>(sorting_criterion.empty_if_null());
_background = background;
@@ -123,29 +142,27 @@ namespace mbts.ui.settings {
public uint font_color => _font_color;
public FontWeight font_weight => FontWeight.FromOpenTypeWeight(_font_weight);
public Color background => _background.argb_color();
public ColorOpacityProto selection_highlight => _selection_highlight;
public Color selection_highlight => _selection_highlight.argb_color();
public uint background_color => _background;
public bool enable_background_color => _enable_background_color;
public IReadOnlyList<GridColumnSettings> columns => _columns ?? (_columns = new List<GridColumnSettings>(0));
[Obsolete] // TODO: remove sorting_criterion from GridSettings entirely. This should be part of IShellWindow's StartupData, since it is an instance-specific setting, and prob not part of the shared defaults
public IReadOnlyList<SortingCriteria> sorting_criterion => _sorting_criterion ?? (_sorting_criterion = new List<SortingCriteria>(0));
public static GridSettings get_default() =>
new GridSettings(_default_font_family, _default_font_size, FontWeights.Normal, _default_font_color, new ColorOpacityProto(Colors.MediumSlateBlue, 0.4));
public GridSettings with(
IEnumerable<GridColumnSettings> columns = null,
IEnumerable<SortingCriteria> sorting_criterion = null,
string font_family = null,
double font_size = double.NaN,
uint? background = null,
bool? enable_background_color = null) =>
bool? enable_background_color = null,
uint? selection_highlight = null) =>
new GridSettings(
font_family ?? _font_family,
DoubleHelper.IsNaNOrInfinityOr(font_size, 0) ? _font_size : font_size,
font_weight,
_font_color,
_selection_highlight,
selection_highlight ?? _selection_highlight,
columns ?? _columns,
sorting_criterion ?? _sorting_criterion,
background ?? _background,
@@ -1,4 +1,6 @@
using bts.collections;
using bts.settings;
using bts.utils;
using mbts.ui.controls;
using System;
using System.Collections.Generic;
@@ -129,4 +131,26 @@ namespace mbts.ui.settings {
return false;
}
}
static class GridSettingsMigrationRules {
public class SelectionHighlightIsTranslucent : ISettingsMigrationRule<GridSettings> {
public const byte min_alpha = 30, max_alpha = 200;
public static bool apply(GridSettings settings, out GridSettings new_settings) {
if (settings != null) {
var sl = settings.selection_highlight;
var a = MathUtils.within_range(sl.A, (byte)30, (byte)200);
if (sl.A != a) {
sl.A = a;
new_settings = settings.with(selection_highlight: sl.rgb_color());
return true;
}
}
new_settings = null;
return false;
}
bool ISettingsMigrationRule<GridSettings>.apply(GridSettings settings, out GridSettings new_settings) =>
apply(settings, out new_settings);
}
}
}
@@ -30,7 +30,8 @@ namespace mbts.ui {
readonly DelegateProperty<FontFamily> _font_family;
readonly DelegateProperty<FontWeight> _font_weight;
readonly DelegateProperty<double> _font_size;
readonly DelegateProperty<SolidColorBrush> _font_color, _selection_highlight, _background_color;
readonly DelegateProperty<SolidColorBrush> _font_color, _background_color;
readonly DelegateProperty<Color> _selection_highlight;
readonly SetMap<IPredicate<TRowData>, AnimationDefinitionWrapper<TRow>> _animation_defs = new SetMap<IPredicate<TRowData>, AnimationDefinitionWrapper<TRow>>();
readonly Func<TPredicateData> _predicate_data_factory;
readonly Func<TPredicateData, IPredicate<TRowData>> _predicate_factory;
@@ -76,7 +77,7 @@ namespace mbts.ui {
_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));
_selection_highlight = backing(nameof(selection_highlight), () => grid_settings.selection_highlight, nameof(grid_settings));
_background_color = backing(nameof(background_color), () => grid_settings.background_color.argb_color().to_solid_brush().frozen(), nameof(grid_settings));
}
@@ -88,7 +89,7 @@ namespace mbts.ui {
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 Color selection_highlight => _selection_highlight.Value;
public SolidColorBrush background_color => _background_color.Value;
public GridSettingsVM<TRowData, TPredicateData, TSettingsData> create_vm(GridSettings settings) {
@@ -134,7 +134,7 @@ namespace mbts.spark {
foreach (IGrouping<TPredicateData, CellAnimation> g in animations)
animation_map.AddOrUpdate(g.Key, p => animation_tuple(c: g), (p, t) => t.cell_animations.AddRange(g));
}
return _create_settings_obj(new GridSettings(font_family.name(), font_size, font_weight, font_color.color.rgb_color(), selection_highlight.to_proto(), col_settings,
return _create_settings_obj(new GridSettings(font_family.name(), font_size, font_weight, font_color.color.rgb_color(), selection_highlight.color.rgb_color(), col_settings,
background: background_color.color.with((byte)(MathUtils.within_range(0, 1, background_color.opacity) * 255.0)).rgb_color()),
animation_map.Select(kv => PredicateAnimations.create_data<TRowData, TPredicateData>(kv.Key, kv.Value.cell_animations, kv.Value.row_animations, kv.Value.sounds)));
//
@@ -146,7 +146,7 @@ namespace mbts.spark {
_font_family.set(new FontFamily(sd.general.font_family));
_font_weight.set(sd.general.font_weight);
font_color.load_from(sd.general.font_color.rgb_solid_brush());
selection_highlight.load_from(sd.general.selection_highlight.to_brush());
selection_highlight.color = sd.general.selection_highlight;
_show_background_color.set(sd.general.enable_background_color);
if (sd.general.enable_background_color) {
// note that when the OB is moved to allow background colors
@@ -20,7 +20,8 @@ namespace mbts.ui {
readonly DelegateProperty<FontFamily> _font_family;
readonly DelegateProperty<FontWeight> _font_weight;
readonly DelegateProperty<double> _font_size;
readonly DelegateProperty<SolidColorBrush> _font_color, _selection_highlight, _background_color;
readonly DelegateProperty<SolidColorBrush> _font_color, _background_color;
readonly DelegateProperty<Color> _selection_highlight;
readonly IReadOnlyDictionary<string, ImmutableColumnSettingStore> _column_flags;
public SimpleGridSettingsManager(ObservableCollection<ColumnBase> columns)
@@ -34,7 +35,7 @@ namespace mbts.ui {
_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));
_selection_highlight = backing(nameof(selection_highlight), () => grid_settings.selection_highlight, nameof(grid_settings));
_background_color = backing(nameof(background_color), () => grid_settings.background_color.argb_color().to_solid_brush().frozen(), nameof(grid_settings));
_row_vms = row_vms;
}
@@ -44,8 +45,8 @@ namespace mbts.ui {
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 Color selection_highlight => _selection_highlight.Value;
public SimpleGridSettingsVM create_vm(GridSettings settings) {
var vm = new SimpleGridSettingsVM(_column_flags.ToDictionary(kv => kv.Key, kv => kv.Value.flags));
@@ -120,7 +120,7 @@ namespace mbts.spark {
font_size,
font_weight,
font_color.color.rgb_color(),
selection_highlight.to_proto(),
selection_highlight.color.rgb_color(),
columns.Select(c => c.create()));
if (show_background_color)
rv = rv.with(background: background_color.color.rgb_color());
@@ -134,7 +134,7 @@ namespace mbts.spark {
_font_family.set(new FontFamily(value.font_family));
_font_weight.set(value.font_weight);
font_color.load_from(value.font_color.rgb_solid_brush());
selection_highlight.color = value.selection_highlight.argb_color;
selection_highlight.color = value.selection_highlight;
_show_background_color.set(value.enable_background_color);
if (value.enable_background_color) {
background_color.color = value.background;
@@ -1,7 +1,6 @@
using bts;
using bts.collections;
using bts.utils;
using mbts.ui.utility;
using mbts.utils;
using System;
using System.Collections.Generic;
@@ -19,6 +18,20 @@ using System.Windows.Media;
namespace mbts.ui {
static class WPFExtensions {
public static void bind(this DependencyObject target, DependencyProperty property, IValueConverter converter = null, object source = null, string path = null, params object[] path_parameters) {
var pp = new PropertyPath(path ?? string.Join(".", Array.ConvertAll(path_parameters, i => $"({i})")));
for (int i = 0; i < path_parameters.Length; i++)
pp.PathParameters.Add(path_parameters[i]);
bind(target, property, pp, converter: converter, source: source);
}
public static void bind(this DependencyObject target, DependencyProperty property, PropertyPath path, IValueConverter converter = null, object source = null) =>
BindingOperations.SetBinding(target, property, new Binding {
Path = path,
Source = source,
Converter = converter
});
public static object get_template_child(this Control control, string name) {
if (control == null) throw new ArgumentNullException(nameof(control));
return control?.Template?.FindName(name, control);
@@ -0,0 +1,61 @@
using mbts.utils;
using System;
using System.Collections.Generic;
using System.Windows.Media;
namespace mbts.ui {
readonly struct BrushKey : IEquatable<BrushKey> {
readonly int _color;
// Opacity is converted to a ushort, so the 32 hash bits can be split in half: 16 for color, and 16 for opacity.
// This costs some precision wrt opacity; it is doubtful the human eye would be able to discern the difference in any case.
readonly ushort _opacity;
BrushKey(int color, ushort opacity) {
_color = color;
_opacity = opacity;
}
public static BrushKey get(Color color, double opacity = 1.0) {
ExceptionAssert.Argument.Between(opacity, 0.0, 1.0, nameof(opacity));
unchecked {
var opac = (ushort)(opacity * ushort.MaxValue);
// color has a perfect hash (it is 32 bits, so it damn well better).
return new BrushKey(color.GetHashCode(), opac);
}
}
public bool Equals(BrushKey other) =>
_color == other._color &&
_opacity == other._opacity;
public override bool Equals(object obj) =>
obj is BrushKey other && Equals(other);
public override int GetHashCode() =>
// ushort.MaxValue indicates opacity of '1.'
// The value we have is the actual opacity multiplied by 100,000;
// so 1 would actually be a very small opacity: 0.0001.
_opacity == ushort.MaxValue ?
// the last bit indicates if opacity is zero
_color | -1 :
((ushort)_color | (_opacity << 16) | -1) ^ -1;
}
static class FrozenBrushCache {
[ThreadStatic]
static Dictionary<BrushKey, SolidColorBrush> _brushes;
public static SolidColorBrush get_brush(Color color) =>
get_brush(color, 1.0);
public static SolidColorBrush get_brush(Color color, double opacity) {
if (_brushes == null)
_brushes = new Dictionary<BrushKey, SolidColorBrush>();
var key = BrushKey.get(color, opacity);
if (!_brushes.TryGetValue(key, out var brush)) {
_brushes[key] = brush = new SolidColorBrush(color) { Opacity = opacity }.frozen();
}
return brush;
}
}
}
@@ -0,0 +1,59 @@
using bts.utils;
using mbts.ui.dpi;
using mbts.utils;
using System;
using System.Collections.Generic;
using System.Windows.Media;
namespace mbts.ui {
readonly struct PenKey : IEquatable<PenKey> {
// This code asserts that thickness will not exceed 100
// this is an arbitrary max that can adjusted if needed
const ushort
_max_thickness = 100,
_thickness_mult = ushort.MaxValue / _max_thickness;
readonly BrushKey _brush_key;
readonly ushort _thickness;
PenKey(BrushKey brush_key, ushort thickness) {
_brush_key = brush_key;
_thickness = thickness;
}
public static PenKey get(BrushKey brush_key, double thickness) {
ExceptionAssert.Argument.Between(thickness, 0, _max_thickness, nameof(thickness));
return new PenKey(brush_key, (ushort)(thickness * _thickness_mult));
}
public bool Equals(PenKey other) =>
_brush_key.Equals(other._brush_key) &&
_thickness == other._thickness;
public override bool Equals(object obj) =>
obj is BrushKey other && Equals(other);
public override int GetHashCode() =>
EqualityComparer.auto_phase(
_brush_key.GetHashCode(),
_thickness.GetHashCode());
}
static class FrozenPenCache {
[ThreadStatic]
static Dictionary<PenKey, Pen> _pens;
public static Pen get_pen(Color color) =>
get_pen(color, HighDpiExtensions.pixel_unit, 1);
public static Pen get_pen(Color color, double thickness, double opacity) {
if (_pens == null)
_pens = new Dictionary<PenKey, Pen>();
var key = PenKey.get(BrushKey.get(color, opacity), thickness);
if (!_pens.TryGetValue(key, out var pen)) {
_pens[key] = pen = new Pen(FrozenBrushCache.get_brush(color, opacity), thickness).frozen();
}
return pen;
}
}
}
+6
View File
@@ -99,6 +99,12 @@ namespace bts.utils {
return value;
}
public static byte within_range(byte value, byte min, byte max) {
if (value < min) return min;
if (value > max) return max;
return value;
}
public static int RoundFractionalUnits(double value, double fractional_unit) =>
(int)Math.Round(value / fractional_unit);