Skip to content

Commit

Permalink
Merge pull request #47 from w-ahmad/nullabe_refTypes
Browse files Browse the repository at this point in the history
make dependency reference type properties as nullable
  • Loading branch information
w-ahmad authored Oct 10, 2024
2 parents 236dfc4 + dc15b8f commit 28b04c8
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 76 deletions.
4 changes: 2 additions & 2 deletions src/WinUI.TableView/TableView.Properties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,9 @@ public double RowMaxHeight
set => SetValue(RowMaxHeightProperty, value);
}

public new IList ItemsSource
public new IList? ItemsSource
{
get => (IList)GetValue(ItemsSourceProperty);
get => (IList?)GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}

Expand Down
35 changes: 17 additions & 18 deletions src/WinUI.TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -430,25 +430,24 @@ protected virtual void OnAutoGeneratingColumn(TableViewAutoGeneratingColumnEvent

private static TableViewBoundColumn GetTableViewColumnFromType(Type type)
{
switch (Type.GetTypeCode(type))
return Type.GetTypeCode(type) switch
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Single:
case TypeCode.Double:
case TypeCode.Decimal:
return new TableViewNumberColumn();
case TypeCode.Boolean:
return new TableViewCheckBoxColumn();
default:
return new TableViewTextColumn();
}
TypeCode.Byte
or TypeCode.SByte
or TypeCode.UInt16
or TypeCode.UInt32
or TypeCode.UInt64
or TypeCode.Int16
or TypeCode.Int32
or TypeCode.Int64
or TypeCode.Single
or TypeCode.Double
or TypeCode.Decimal
=> new TableViewNumberColumn(),
TypeCode.Boolean
=> new TableViewCheckBoxColumn(),
_ => new TableViewTextColumn(),
};
}

private void OnItemsSourceChanged(DependencyPropertyChangedEventArgs e)
Expand Down
58 changes: 30 additions & 28 deletions src/WinUI.TableView/TableViewCell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ protected override Size MeasureOverride(Size availableSize)

_contentPresenter ??= (ContentPresenter)GetTemplateChild("Content");

var contentWidth = Column.ActualWidth;
var contentWidth = Column?.ActualWidth ?? 0d;
contentWidth -= element.Margin.Left;
contentWidth -= element.Margin.Right;
contentWidth -= Padding.Left;
Expand Down Expand Up @@ -97,8 +97,8 @@ protected override void OnPointerEntered(PointerRoutedEventArgs e)
{
base.OnPointerEntered(e);

if ((TableView.SelectionMode is not ListViewSelectionMode.None
&& TableView.SelectionUnit is not TableViewSelectionUnit.Row)
if ((TableView?.SelectionMode is not ListViewSelectionMode.None
&& TableView?.SelectionUnit is not TableViewSelectionUnit.Row)
|| !TableView.IsReadOnly)
{
VisualStates.GoToState(this, false, VisualStates.StatePointerOver);
Expand All @@ -109,8 +109,8 @@ protected override void OnPointerExited(PointerRoutedEventArgs e)
{
base.OnPointerEntered(e);

if ((TableView.SelectionMode is not ListViewSelectionMode.None
&& TableView.SelectionUnit is not TableViewSelectionUnit.Row)
if ((TableView?.SelectionMode is not ListViewSelectionMode.None
&& TableView?.SelectionUnit is not TableViewSelectionUnit.Row)
|| !TableView.IsReadOnly)
{
VisualStates.GoToState(this, false, VisualStates.StateNormal);
Expand All @@ -121,7 +121,7 @@ protected override void OnTapped(TappedRoutedEventArgs e)
{
base.OnTapped(e);

if (TableView.SelectionUnit is not TableViewSelectionUnit.Row || !IsReadOnly)
if (TableView?.SelectionUnit is not TableViewSelectionUnit.Row || !IsReadOnly)
{
MakeSelection();
e.Handled = true;
Expand All @@ -132,7 +132,7 @@ protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
base.OnPointerPressed(e);

if (!KeyBoardHelper.IsShiftKeyDown())
if (!KeyBoardHelper.IsShiftKeyDown() && TableView is not null)
{
TableView.SelectionStartCellSlot = TableView.SelectionUnit is not TableViewSelectionUnit.Row || !IsReadOnly ? Slot : default; ;
TableView.SelectionStartRowIndex = Index;
Expand All @@ -144,7 +144,7 @@ protected override void OnPointerReleased(PointerRoutedEventArgs e)
{
base.OnPointerReleased(e);

if (!KeyBoardHelper.IsShiftKeyDown())
if (!KeyBoardHelper.IsShiftKeyDown() && TableView is not null)
{
var cell = FindCell(e.GetCurrentPoint(this).Position);
TableView.SelectionStartCellSlot = TableView.SelectionUnit is not TableViewSelectionUnit.Row || !IsReadOnly ? cell?.Slot : default;
Expand All @@ -167,14 +167,14 @@ protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e)
if (cell is not null && cell != this)
{
var ctrlKey = KeyBoardHelper.IsCtrlKeyDown();
TableView.MakeSelection(cell.Slot, true, ctrlKey);
TableView?.MakeSelection(cell.Slot, true, ctrlKey);
}
}
}

private TableViewCell? FindCell(Point position)
{
_scrollViewer ??= TableView.FindDescendant<ScrollViewer>();
_scrollViewer ??= TableView?.FindDescendant<ScrollViewer>();

if (_scrollViewer is { })
{
Expand All @@ -191,7 +191,7 @@ protected override void OnManipulationDelta(ManipulationDeltaRoutedEventArgs e)

protected override void OnDoubleTapped(DoubleTappedRoutedEventArgs e)
{
if (!IsReadOnly && !TableView.IsEditing && !Column.UseSingleElement)
if (!IsReadOnly && TableView is not null && !TableView.IsEditing && !Column?.UseSingleElement is true)
{
PrepareForEdit();

Expand All @@ -204,6 +204,11 @@ private void MakeSelection()
var shiftKey = KeyBoardHelper.IsShiftKeyDown();
var ctrlKey = KeyBoardHelper.IsCtrlKeyDown();

if (TableView is null || Column is null)
{
return;
}

if ((TableView.IsEditing || Column.UseSingleElement) && IsCurrent)
{
return;
Expand Down Expand Up @@ -239,12 +244,12 @@ internal async void PrepareForEdit()

internal void SetElement()
{
Content = Column.GenerateElement();
Content = Column?.GenerateElement();
}

private void SetEditingElement()
{
if (!Column.UseSingleElement)
if (Column?.UseSingleElement is false)
{
Content = Column.GenerateEditingElement();
}
Expand All @@ -257,7 +262,7 @@ private void SetEditingElement()

internal void RefreshElement()
{
Column.RefreshElement(this, Content);
Column?.RefreshElement(this, Content);
}

internal void ApplySelectionState()
Expand All @@ -284,10 +289,7 @@ internal void ApplyCurrentCellState()

internal void UpdateElementState()
{
if (Column is { })
{
Column.UpdateElementState(this);
}
Column?.UpdateElementState(this);
}

private static void OnColumnChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
Expand All @@ -305,30 +307,30 @@ private static void OnColumnChanged(DependencyObject d, DependencyPropertyChange
}
}

public bool IsReadOnly => TableView.IsReadOnly || Column is TableViewTemplateColumn { EditingTemplate: null } or { IsReadOnly: true };
public bool IsReadOnly => TableView?.IsReadOnly is true || Column is TableViewTemplateColumn { EditingTemplate: null } or { IsReadOnly: true };

internal TableViewCellSlot Slot => new(Row.Index, Index);
internal TableViewCellSlot Slot => new(Row?.Index ?? -1, Index);

internal int Index { get; set; }

public bool IsSelected => TableView.SelectedCells.Contains(Slot);
public bool IsCurrent => TableView.CurrentCellSlot == Slot;
public bool IsSelected => TableView?.SelectedCells.Contains(Slot) is true;
public bool IsCurrent => TableView?.CurrentCellSlot == Slot;

public TableViewColumn Column
public TableViewColumn? Column
{
get => (TableViewColumn)GetValue(ColumnProperty);
get => (TableViewColumn?)GetValue(ColumnProperty);
set => SetValue(ColumnProperty, value);
}

public TableViewRow Row
public TableViewRow? Row
{
get => (TableViewRow)GetValue(TableViewRowProperty);
get => (TableViewRow?)GetValue(TableViewRowProperty);
set => SetValue(TableViewRowProperty, value);
}

public TableView TableView
public TableView? TableView
{
get => (TableView)GetValue(TableViewProperty);
get => (TableView?)GetValue(TableViewProperty);
set => SetValue(TableViewProperty, value);
}

Expand Down
4 changes: 2 additions & 2 deletions src/WinUI.TableView/TableViewColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public bool IsReadOnly
set => SetValue(IsReadOnlyProperty, value);
}

public Style HeaderStyle
public Style? HeaderStyle
{
get => (Style)GetValue(HeaderStyleProperty);
get => (Style?)GetValue(HeaderStyleProperty);
set => SetValue(HeaderStyleProperty, value);
}

Expand Down
10 changes: 5 additions & 5 deletions src/WinUI.TableView/TableViewComboBoxColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,21 @@ public override FrameworkElement GenerateEditingElement()
return comboBox;
}

public object ItemsSource
public object? ItemsSource
{
get => GetValue(ItemsSourceProperty);
set => SetValue(ItemsSourceProperty, value);
}

public string DisplayMemberPath
public string? DisplayMemberPath
{
get => (string)GetValue(DisplayMemberPathProperty);
get => (string?)GetValue(DisplayMemberPathProperty);
set => SetValue(DisplayMemberPathProperty, value);
}

public string SelectedValuePath
public string? SelectedValuePath
{
get => (string)GetValue(SelectedValuePathProperty);
get => (string?)GetValue(SelectedValuePathProperty);
set => SetValue(SelectedValuePathProperty, value);
}

Expand Down
8 changes: 4 additions & 4 deletions src/WinUI.TableView/TableViewHeaderRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,11 @@ private void OnTableViewSelectionChanged()

private void SetSelectAllButtonState()
{
if (TableView.SelectionMode == ListViewSelectionMode.Multiple)
if (TableView?.SelectionMode == ListViewSelectionMode.Multiple)
{
VisualStates.GoToState(this, false, VisualStates.StateSelectAllCheckBox);
}
else if (TableView.ShowOptionsButton)
else if (TableView?.ShowOptionsButton is true)
{
VisualStates.GoToState(this, false, VisualStates.StateOptionsButton);
}
Expand Down Expand Up @@ -376,9 +376,9 @@ private static void OnTableViewChanged(DependencyObject d, DependencyPropertyCha
}
}

public TableView TableView
public TableView? TableView
{
get => (TableView)GetValue(TableViewProperty);
get => (TableView?)GetValue(TableViewProperty);
set => SetValue(TableViewProperty, value);
}

Expand Down
19 changes: 12 additions & 7 deletions src/WinUI.TableView/TableViewRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,15 @@ protected override void OnContentChanged(object oldContent, object newContent)

protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
if (!KeyBoardHelper.IsShiftKeyDown())
if (!KeyBoardHelper.IsShiftKeyDown() && TableView is not null)
{
TableView.SelectionStartRowIndex = Index;
}
}

protected override void OnPointerReleased(PointerRoutedEventArgs e)
{
if (!KeyBoardHelper.IsShiftKeyDown())
if (!KeyBoardHelper.IsShiftKeyDown() && TableView is not null)
{
TableView.SelectionStartCellSlot = null;
TableView.SelectionStartRowIndex = null;
Expand All @@ -58,6 +58,11 @@ protected override void OnTapped(TappedRoutedEventArgs e)
var shiftKey = KeyBoardHelper.IsShiftKeyDown();
var ctrlKey = KeyBoardHelper.IsCtrlKeyDown();

if (TableView is null)
{
return;
}

if (IsSelected && (ctrlKey || TableView.SelectionMode is ListViewSelectionMode.Multiple) && !shiftKey)
{
TableView.DeselectRange(new(Index, 1));
Expand Down Expand Up @@ -88,7 +93,7 @@ private void EnsureCells()

private async void OnSizeChanged(object sender, SizeChangedEventArgs e)
{
if (TableView.CurrentCellSlot?.Row == Index)
if (TableView?.CurrentCellSlot?.Row == Index)
{
_ = await TableView.ScrollCellIntoView(TableView.CurrentCellSlot.Value);
}
Expand Down Expand Up @@ -153,7 +158,7 @@ private void RemoveCells(IEnumerable<TableViewColumn> columns)

private void AddCells(IEnumerable<TableViewColumn> columns, int index = -1)
{
if (_cellPresenter is not null)
if (_cellPresenter is not null && TableView is not null)
{
foreach (var column in columns)
{
Expand Down Expand Up @@ -242,11 +247,11 @@ internal void ApplyCellsSelectionState()

internal IList<TableViewCell> Cells => _cellPresenter?.Cells ?? new List<TableViewCell>();

public int Index => TableView.IndexFromContainer(this);
public int Index => TableView?.IndexFromContainer(this) ?? -1;

public TableView TableView
public TableView? TableView
{
get => (TableView)GetValue(TableViewProperty);
get => (TableView?)GetValue(TableViewProperty);
set => SetValue(TableViewProperty, value);
}

Expand Down
16 changes: 8 additions & 8 deletions src/WinUI.TableView/TableViewTemplateColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ public override FrameworkElement GenerateEditingElement()
return GenerateElement();
}

public DataTemplate CellTemplate
public DataTemplate? CellTemplate
{
get => (DataTemplate)GetValue(CellTemplateProperty);
get => (DataTemplate?)GetValue(CellTemplateProperty);
set => SetValue(CellTemplateProperty, value);
}

public DataTemplateSelector CellTemplateSelector
public DataTemplateSelector? CellTemplateSelector
{
get => (DataTemplateSelector)GetValue(CellTemplateSelectorProperty);
get => (DataTemplateSelector?)GetValue(CellTemplateSelectorProperty);
set => SetValue(CellTemplateSelectorProperty, value);
}

public DataTemplate EditingTemplate
public DataTemplate? EditingTemplate
{
get => (DataTemplate)GetValue(EditingTemplateProperty);
get => (DataTemplate?)GetValue(EditingTemplateProperty);
set => SetValue(EditingTemplateProperty, value);
}

public DataTemplateSelector EditingTemplateSelector
public DataTemplateSelector? EditingTemplateSelector
{
get => (DataTemplateSelector)GetValue(EditingTemplateSelectorProperty);
get => (DataTemplateSelector?)GetValue(EditingTemplateSelectorProperty);
set => SetValue(EditingTemplateSelectorProperty, value);
}

Expand Down
Loading

0 comments on commit 28b04c8

Please sign in to comment.