Skip to content

Commit

Permalink
Add NotNull annotations, fix R# warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-englert committed Sep 14, 2017
1 parent 8b20ba7 commit 86ba63c
Show file tree
Hide file tree
Showing 22 changed files with 230 additions and 185 deletions.
6 changes: 5 additions & 1 deletion DataGridExtensions.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/CodeInspection/CodeAnnotations/NamespacesWithAnnotations/=DataGridExtensionsSample_002EAnnotations/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
<s:Boolean x:Key="/Default/CodeInspection/CodeAnnotations/NamespacesWithAnnotations/=DataGridExtensionsSample_002EAnnotations/@EntryIndexedValue">True</s:Boolean>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBePrivate_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=MemberCanBePrivate_002ELocal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeInspection/Highlighting/InspectionSeverities/=UnusedMember_002EGlobal/@EntryIndexedValue">DO_NOT_SHOW</s:String>
<s:String x:Key="/Default/CodeStyle/Naming/JavaScriptNaming/UserRules/=TS_005FMIXED_005FENUM/@EntryIndexedValue">&lt;Policy Inspect="True" Prefix="" Suffix="" Style="AaBb" /&gt;</s:String></wpf:ResourceDictionary>
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void AssociatedObject_Click([NotNull] object sender, [NotNull] RoutedEve
var textBox = TextBox;

if (textBox != null)
textBox.Text = null;
textBox.Text = string.Empty;
}
}
}
18 changes: 5 additions & 13 deletions DataGridExtensions/Behaviors/DisableTargetWhileEditingBehavior.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public class DisableTargetWhileEditingBehavior : Behavior<DataGrid>
/// <summary>
/// Gets or sets the target element that will be disabled during editing.
/// </summary>
[CanBeNull]
public UIElement Target
{
get => (UIElement)GetValue(TargetProperty);
Expand All @@ -24,15 +25,11 @@ public UIElement Target
/// <summary>
/// Identifies the Target dependency property
/// </summary>
[NotNull]
public static readonly DependencyProperty TargetProperty =
DependencyProperty.Register("Target", typeof (UIElement), typeof (DisableTargetWhileEditingBehavior));
DependencyProperty.Register("Target", typeof(UIElement), typeof(DisableTargetWhileEditingBehavior));

/// <summary>
/// Called after the behavior is attached to an AssociatedObject.
/// </summary>
/// <remarks>
/// Override this to hook up functionality to the AssociatedObject.
/// </remarks>
/// <inheritdoc />
protected override void OnAttached()
{
base.OnAttached();
Expand All @@ -44,12 +41,7 @@ protected override void OnAttached()
dataGrid.CellEditEnding += DataGrid_CellEditEnding;
}

/// <summary>
/// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred.
/// </summary>
/// <remarks>
/// Override this to unhook functionality from the AssociatedObject.
/// </remarks>
/// <inheritdoc />
protected override void OnDetaching()
{
base.OnDetaching();
Expand Down
100 changes: 56 additions & 44 deletions DataGridExtensions/Behaviors/ExtendedStarSizeBehavior.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System.Diagnostics;

namespace DataGridExtensions.Behaviors
namespace DataGridExtensions.Behaviors
{
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
Expand All @@ -21,30 +20,39 @@ namespace DataGridExtensions.Behaviors

using JetBrains.Annotations;

/// <inheritdoc />
/// <summary>
/// Extended start size column behavior. Allows columns to get larger than the client area, but not smaller.
/// The Resizing behavior can be modified using Ctrl or Shift keys: Ctrl resizes all columns to the right proportionally, Shift fits all columns to the right into the client area.
/// A tool tip can be attached to the column headers resizing grippers to help the user with these features.
/// </summary>
public class ExtendedStarSizeBehavior : Behavior<DataGrid>
{
// ReSharper disable AssignNullToNotNullAttribute
[NotNull]
private static readonly DependencyPropertyDescriptor ViewportWidthPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(ScrollViewer.ViewportWidthProperty, typeof(ScrollViewer));
[NotNull]
private static readonly DependencyPropertyDescriptor NonFrozenColumnsViewportHorizontalOffsetPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(DataGrid.NonFrozenColumnsViewportHorizontalOffsetProperty, typeof(DataGrid));
// ReSharper restore AssignNullToNotNullAttribute

[NotNull]
private readonly DispatcherThrottle _updateColumnGripperToolTipVisibilityThrottle;

private int _changingGridSizeCounter;
[CanBeNull]
private ScrollViewer _scrollViewer;

private int _changingGridSizeCounter;
private bool _columnsAreFitWithinViewPort;

/// <summary>
/// The resrouce key for the default column header gripper tool tip style.
/// The resource key for the default column header gripper tool tip style.
/// </summary>
[NotNull]
public static readonly ResourceKey ColumnHeaderGripperToolTipStyleKey = new ComponentResourceKey(typeof(ExtendedStarSizeBehavior), "ColumnHeaderGripperToolTipStyle");

/// <inheritdoc />
/// <summary>
/// Initializes a new instance of the <see cref="ExtendedStarSizeBehavior"/> class.
/// Initializes a new instance of the <see cref="T:DataGridExtensions.Behaviors.ExtendedStarSizeBehavior" /> class.
/// </summary>
public ExtendedStarSizeBehavior()
{
Expand All @@ -54,6 +62,7 @@ public ExtendedStarSizeBehavior()
/// <summary>
/// Gets or sets the style of the tool tip for the grippers in the column headers.
/// </summary>
[CanBeNull]
public Style ColumnHeaderGripperToolTipStyle
{
get => (Style)GetValue(ColumnHeaderGripperToolTipStyleProperty);
Expand All @@ -62,12 +71,14 @@ public Style ColumnHeaderGripperToolTipStyle
/// <summary>
/// Identifies the ColumnHeaderGripperToolTipStyle dependency property
/// </summary>
[NotNull]
public static readonly DependencyProperty ColumnHeaderGripperToolTipStyleProperty =
DependencyProperty.Register("ColumnHeaderGripperToolTipStyle", typeof(Style), typeof(ExtendedStarSizeBehavior));

/// <summary>
/// Gets or sets the resource locator.
/// </summary>
[CanBeNull]
public IResourceLocator ResourceLocator
{
get => (IResourceLocator)GetValue(ResourceLocatorProperty);
Expand All @@ -76,15 +87,11 @@ public IResourceLocator ResourceLocator
/// <summary>
/// Identifies the <see cref="ResourceLocator"/> dependency property
/// </summary>
[NotNull]
public static readonly DependencyProperty ResourceLocatorProperty =
DependencyProperty.Register("ResourceLocator", typeof(IResourceLocator), typeof(ExtendedStarSizeBehavior));

/// <summary>
/// Called after the behavior is attached to an AssociatedObject.
/// </summary>
/// <remarks>
/// Override this to hook up functionality to the AssociatedObject.
/// </remarks>
/// <inheritdoc />
protected override void OnAttached()
{
base.OnAttached();
Expand Down Expand Up @@ -168,8 +175,6 @@ private void Columns_CollectionChanged([NotNull] object sender, [NotNull] Notify
private void DataGrid_NonFrozenColumnsViewportHorizontalOffsetChanged([NotNull] object sender, [NotNull] EventArgs e)
{
var dataGrid = (DataGrid)sender;
if (dataGrid == null)
return;

if (_changingGridSizeCounter > 0)
return;
Expand All @@ -184,10 +189,8 @@ private void ScrollViewer_ViewportWidthChanged([NotNull] object sender, [NotNull
_changingGridSizeCounter += 1;

var dataGrid = AssociatedObject;
if (dataGrid == null)
return;

dataGrid.BeginInvoke(() =>
dataGrid?.BeginInvoke(() =>
{
UpdateColumnWidths(dataGrid, null, UpdateMode.Default);
_changingGridSizeCounter -= 1;
Expand All @@ -201,8 +204,6 @@ private void DataGrid_ColumnActualWidthChanged([NotNull] object sender, [NotNull
return;

var dataGrid = (DataGrid)sender;
if (dataGrid == null)
return;

if (_changingGridSizeCounter > 0)
return;
Expand All @@ -223,20 +224,22 @@ private void DataGrid_ColumnVisibilityChanged([NotNull] object sender, [NotNull]
_updateColumnGripperToolTipVisibilityThrottle.Tick();
}

private void UpdateColumnWidths([NotNull] DataGrid dataGrid, DataGridColumn modifiedColum, UpdateMode updateMode)
private void UpdateColumnWidths([NotNull] DataGrid dataGrid, [CanBeNull] DataGridColumn modifiedColum, UpdateMode updateMode)
{
Contract.Requires(dataGrid != null);

// ReSharper disable PossibleNullReferenceException
var dataGridColumns = dataGrid.Columns
.OrderBy(c => c.DisplayIndex)
.Skip(dataGrid.FrozenColumnCount)
.Where(c => (c.Visibility == Visibility.Visible))
.ToArray();
// ReSharper restore PossibleNullReferenceException

_columnsAreFitWithinViewPort = !ApplyStarSize(dataGridColumns, modifiedColum) && DistributeAvailableSize(dataGrid, dataGridColumns, modifiedColum, updateMode);
}

private static bool ApplyStarSize(IEnumerable<DataGridColumn> dataGridColumns, DataGridColumn modifiedColum)
private static bool ApplyStarSize([NotNull, ItemNotNull] IEnumerable<DataGridColumn> dataGridColumns, [CanBeNull] DataGridColumn modifiedColum)
{
if ((modifiedColum == null) || (!Keyboard.IsKeyDown(Key.LeftCtrl) && !Keyboard.IsKeyDown(Key.RightCtrl)))
return false;
Expand All @@ -249,6 +252,7 @@ private static bool ApplyStarSize(IEnumerable<DataGridColumn> dataGridColumns, D

foreach (var column in dataGridColumns.SkipWhile(c => !Equals(c, modifiedColum)).Skip(1))
{
// ReSharper disable once AssignNullToNotNullAttribute
starSize = GetStarSize(column);
if (starSize > double.Epsilon)
{
Expand All @@ -259,7 +263,7 @@ private static bool ApplyStarSize(IEnumerable<DataGridColumn> dataGridColumns, D
return true;
}

private bool DistributeAvailableSize([NotNull] DataGrid dataGrid, [NotNull] DataGridColumn[] dataGridColumns, DataGridColumn modifiedColum, UpdateMode updateMode)
private bool DistributeAvailableSize([NotNull] DataGrid dataGrid, [NotNull, ItemNotNull] DataGridColumn[] dataGridColumns, [CanBeNull] DataGridColumn modifiedColum, UpdateMode updateMode)
{
Contract.Requires(dataGrid != null);
Contract.Requires(dataGridColumns != null);
Expand All @@ -268,20 +272,23 @@ private bool DistributeAvailableSize([NotNull] DataGrid dataGrid, [NotNull] Data
if (scrollViewer == null)
return false;

var startColumnIndex = (modifiedColum != null) ? modifiedColum.DisplayIndex : 0;
var startColumnIndex = modifiedColum?.DisplayIndex ?? 0;

Func<DataGridColumn, bool> isFixedColumn = c => (GetStarSize(c) <= double.Epsilon) || (c.DisplayIndex <= startColumnIndex);
Func<DataGridColumn, bool> isVariableColumn = c => !isFixedColumn(c);
// ReSharper disable AssignNullToNotNullAttribute
bool IsFixedColumn(DataGridColumn c) => (GetStarSize(c) <= double.Epsilon) || (c.DisplayIndex <= startColumnIndex);
bool IsVariableColumn(DataGridColumn c) => !IsFixedColumn(c);
// ReSharper restore AssignNullToNotNullAttribute

var fixedColumnsWidth = dataGridColumns
.Where(isFixedColumn)
.Where(IsFixedColumn)
// ReSharper disable once PossibleNullReferenceException
.Select(c => c.ActualWidth)
.Sum();

var getEffectiveColumnSize = (updateMode == UpdateMode.ResetStarSize) ? (Func<DataGridColumn, double>)GetStarSize : GetActualWidth;

var variableColumnWidths = dataGridColumns
.Where(isVariableColumn)
.Where(IsVariableColumn)
.Select(getEffectiveColumnSize)
.Sum();

Expand All @@ -297,7 +304,7 @@ private bool DistributeAvailableSize([NotNull] DataGrid dataGrid, [NotNull] Data

var factor = spaceAvailable / variableColumnWidths;

foreach (var column in dataGridColumns.Where(isVariableColumn))
foreach (var column in dataGridColumns.Where(IsVariableColumn))
{
column.Width = getEffectiveColumnSize(column) * factor;
}
Expand All @@ -311,6 +318,7 @@ private static void HijackStarSizeColumnsInfo([NotNull] DataGrid dataGrid)

foreach (var column in dataGrid.Columns)
{
// ReSharper disable once PossibleNullReferenceException
var width = column.Width;
if (!width.IsStar)
continue;
Expand All @@ -329,21 +337,26 @@ private void UpdateColumnGripperToolTipVisibility()
DataGridColumn leftColumn = null;
var isLeftColumnStarSized = false;

// ReSharper disable once PossibleNullReferenceException
foreach (var column in dataGrid.Columns.OrderBy(c => c.DisplayIndex))
{
var leftColumnRightGripperToolTip = (ToolTip)leftColumn?.GetValue(RightGripperToolTipProperty);
var thisColumnLeftGripperToolTip = (ToolTip)column?.GetValue(LeftGripperToolTipProperty);
var thisColumnLeftGripperToolTip = (ToolTip)column.GetValue(LeftGripperToolTipProperty);

var visibility = isLeftColumnStarSized ? Visibility.Visible : Visibility.Collapsed;

leftColumnRightGripperToolTip?.Do(t => t.Visibility = visibility);
thisColumnLeftGripperToolTip?.Do(t => t.Visibility = visibility);
if (leftColumnRightGripperToolTip != null)
leftColumnRightGripperToolTip.Visibility = visibility;

if (thisColumnLeftGripperToolTip != null)
thisColumnLeftGripperToolTip.Visibility = visibility;

leftColumn = column;
isLeftColumnStarSized = GetStarSize(column) > double.Epsilon;
}

(leftColumn?.GetValue(RightGripperToolTipProperty) as ToolTip)?.Do(t => t.Visibility = Visibility.Collapsed);
if (leftColumn?.GetValue(RightGripperToolTipProperty) is ToolTip toolTip)
toolTip.Visibility = Visibility.Collapsed;
}

private void InjectColumnHeaderStyle([NotNull] DataGrid dataGrid)
Expand All @@ -366,7 +379,7 @@ private static double GetActualWidth([NotNull] DataGridColumn column)
{
Contract.Requires(column != null);

return (double)column.ActualWidth;
return column.ActualWidth;
}

private static double GetStarSize([NotNull] DataGridColumn column)
Expand All @@ -376,45 +389,44 @@ private static double GetStarSize([NotNull] DataGridColumn column)
return column.GetValue<double>(StarSizeProperty);
}

[NotNull]
private static readonly DependencyProperty StarSizeProperty =
DependencyProperty.RegisterAttached("StarSize", typeof(double), typeof(ExtendedStarSizeBehavior), new FrameworkPropertyMetadata(0.0));

[NotNull]
private static readonly DependencyProperty LeftGripperToolTipProperty =
DependencyProperty.RegisterAttached("LeftGripperToolTip", typeof(ToolTip), typeof(ExtendedStarSizeBehavior));

[NotNull]
private static readonly DependencyProperty RightGripperToolTipProperty =
DependencyProperty.RegisterAttached("RightGripperToolTip", typeof(ToolTip), typeof(ExtendedStarSizeBehavior));

[NotNull]
private static readonly DependencyProperty ColumnHeaderGripperExtenderProperty =
DependencyProperty.RegisterAttached("ColumnHeaderGripperExtender", typeof(ExtendedStarSizeBehavior), typeof(ExtendedStarSizeBehavior), new FrameworkPropertyMetadata(null, ColumnHeaderGripperExtender_Changed));

private static void ColumnHeaderGripperExtender_Changed(DependencyObject d, [NotNull] DependencyPropertyChangedEventArgs e)
private static void ColumnHeaderGripperExtender_Changed([NotNull] DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var columnHeader = d as DataGridColumnHeader;
if (columnHeader == null)
if (!(d is DataGridColumnHeader columnHeader))
return;

var self = e.NewValue as ExtendedStarSizeBehavior;
if (self == null)
if (!(e.NewValue is ExtendedStarSizeBehavior self))
return;

var dataGrid = self.AssociatedObject;
if (dataGrid == null)
return;

dataGrid.BeginInvoke(DispatcherPriority.Background, () =>
dataGrid?.BeginInvoke(DispatcherPriority.Background, () =>
{
self.ApplyGripperToolTip(columnHeader, @"PART_LeftHeaderGripper", LeftGripperToolTipProperty);
self.ApplyGripperToolTip(columnHeader, @"PART_RightHeaderGripper", RightGripperToolTipProperty);
});
}

private void ApplyGripperToolTip([NotNull] DataGridColumnHeader columnHeader, string gripperName, DependencyProperty toolTipProperty)
private void ApplyGripperToolTip([NotNull] DataGridColumnHeader columnHeader, [NotNull] string gripperName, [NotNull] DependencyProperty toolTipProperty)
{
Contract.Requires(columnHeader != null);

var gripper = columnHeader.Template?.FindName(gripperName, columnHeader) as Thumb;
if (gripper == null)
if (!(columnHeader.Template?.FindName(gripperName, columnHeader) is Thumb gripper))
return;

var dataGrid = AssociatedObject;
Expand Down
6 changes: 5 additions & 1 deletion DataGridExtensions/ColumnStyles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ private static void DefaultColumnStyles_Changed([NotNull] DependencyObject d, De
if (styles == null)
return;

dataGrid.Columns.ForEach(col => ApplyStyle(styles, col));
foreach (var col in dataGrid.Columns)
{
ApplyStyle(styles, col);
}

dataGrid.Columns.CollectionChanged += (_, args) => Columns_CollectionChanged(styles, args);
}

Expand Down
1 change: 0 additions & 1 deletion DataGridExtensions/DataGridExtensions.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@
<Compile Include="RegexContentFilter.cs" />
<Compile Include="SimpleContentFilter.cs" />
<Compile Include="Tools.cs" />
<Compile Include="Framework\Maybe.cs" />
</ItemGroup>
<ItemGroup>
<Page Include="Themes\Generic.xaml">
Expand Down
Loading

0 comments on commit 86ba63c

Please sign in to comment.