From bd4d2ecc1692a7697fe030ccc5e7ed2ff0fdc8b9 Mon Sep 17 00:00:00 2001 From: chinboy Date: Sat, 18 Jan 2025 16:59:19 +0800 Subject: [PATCH] finish review code style --- .../Collections/DataGridColumnCollection.cs | 6 +- .../Colum/DataGridTextColumn.cs | 11 ++-- .../DataGrid.Columns.cs | 29 ++++----- src/AtomUI.Controls.DataGrid/DataGrid.Rows.cs | 23 ++++--- src/AtomUI.Controls.DataGrid/DataGrid.cs | 6 +- .../DataGridDisplayData.cs | 5 +- .../DataGridLengthConverter.cs | 46 ++++++------- src/AtomUI.Controls.DataGrid/EventArgs.cs | 4 +- .../Row/DataGridRow.cs | 40 ++++++------ .../Row/DataGridRowGroupHeader.cs | 35 +++++----- .../Row/DataGridRowHeader.cs | 21 +++--- .../Row/DataGridRowsPresenter.cs | 2 +- .../Utils/IndexToValueTable.cs | 65 ++++++------------- src/AtomUI.Controls.DataGrid/Utils/Range.cs | 5 +- .../Utils/TypeHelper.cs | 27 +++++--- .../Utils/ValidationUtil.cs | 12 ++-- 16 files changed, 158 insertions(+), 179 deletions(-) diff --git a/src/AtomUI.Controls.DataGrid/Collections/DataGridColumnCollection.cs b/src/AtomUI.Controls.DataGrid/Collections/DataGridColumnCollection.cs index 928ca1363..9c48a5797 100644 --- a/src/AtomUI.Controls.DataGrid/Collections/DataGridColumnCollection.cs +++ b/src/AtomUI.Controls.DataGrid/Collections/DataGridColumnCollection.cs @@ -1,3 +1,8 @@ +// (c) Copyright Microsoft Corporation. +// This source is subject to the Microsoft Public License (Ms-PL). +// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. +// All other rights reserved. + using System.Collections.ObjectModel; using System.Diagnostics; @@ -134,7 +139,6 @@ internal class DataGridColumnCollection : ObservableCollection // insert the column into our internal list ItemsInternal.Insert(columnIndexWithFiller, dataGridColumn); dataGridColumn.Index = columnIndexWithFiller; - dataGridColumn.OwningGrid = _owningGrid; dataGridColumn.RemoveEditingElement(); if (dataGridColumn.IsVisible) { diff --git a/src/AtomUI.Controls.DataGrid/Colum/DataGridTextColumn.cs b/src/AtomUI.Controls.DataGrid/Colum/DataGridTextColumn.cs index 8ebc4fb2c..1cfe08863 100644 --- a/src/AtomUI.Controls.DataGrid/Colum/DataGridTextColumn.cs +++ b/src/AtomUI.Controls.DataGrid/Colum/DataGridTextColumn.cs @@ -3,7 +3,6 @@ using AtomUI.Controls.Utils; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Documents; -using Avalonia.Data; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Media; @@ -128,11 +127,11 @@ public class DataGridTextColumn : DataGridBoundColumn { base.OnPropertyChanged(change); - if (change.Property == FontFamilyProperty - || change.Property == FontSizeProperty - || change.Property == FontStyleProperty - || change.Property == FontWeightProperty - || change.Property == ForegroundProperty) + if (change.Property == FontFamilyProperty || + change.Property == FontSizeProperty || + change.Property == FontStyleProperty || + change.Property == FontWeightProperty || + change.Property == ForegroundProperty) { NotifyPropertyChanged(change.Property.Name); } diff --git a/src/AtomUI.Controls.DataGrid/DataGrid.Columns.cs b/src/AtomUI.Controls.DataGrid/DataGrid.Columns.cs index c3a8925d1..a38f9eeea 100644 --- a/src/AtomUI.Controls.DataGrid/DataGrid.Columns.cs +++ b/src/AtomUI.Controls.DataGrid/DataGrid.Columns.cs @@ -1,3 +1,8 @@ +// (c) Copyright Microsoft Corporation. +// This source is subject to the Microsoft Public License (Ms-PL). +// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. +// All other rights reserved. + using System.ComponentModel.DataAnnotations; using System.Diagnostics; using System.Reflection; @@ -479,7 +484,7 @@ public partial class DataGrid CurrentColumn == targetColumn) { // Column of the current cell is made invisible. Trying to move the current cell to a neighbor column. May throw an exception. - DataGridColumn? dataGridColumn = ColumnsInternal.GetNextVisibleColumn(targetColumn); + var dataGridColumn = ColumnsInternal.GetNextVisibleColumn(targetColumn); if (dataGridColumn == null) { dataGridColumn = ColumnsInternal.GetPreviousVisibleNonFillerColumn(targetColumn); @@ -756,7 +761,7 @@ public partial class DataGrid _horizontalOffset -= GetEdgedColumnWidth(dataGridColumn); } - if (_hScrollBar != null && _hScrollBar.IsVisible) // + if (_hScrollBar != null && _hScrollBar.IsVisible) { _hScrollBar.Value = _horizontalOffset; } @@ -862,7 +867,7 @@ public partial class DataGrid if (scaleStarWeights) { double starRatio = (totalStarColumnsWidth + adjustment - remainingAdjustment) / totalStarColumnsWidth; - foreach (DataGridColumn starColumn in starColumns) + foreach (var starColumn in starColumns) { starColumn.SetWidthStarValue(Math.Min(double.MaxValue, starRatio * starColumn.Width.Value)); } @@ -1156,21 +1161,15 @@ public partial class DataGrid _negHorizontalOffset = 0; return -1; } - else + if (!MathUtils.AreClose(_negHorizontalOffset, _horizontalOffset)) { - if (!MathUtils.AreClose(_negHorizontalOffset, _horizontalOffset)) - { - _negHorizontalOffset = 0; - } - - return dataGridColumn.Index; + _negHorizontalOffset = 0; } - } - else - { - _negHorizontalOffset = GetEdgedColumnWidth(dataGridColumn) - (cx - _horizontalOffset); + return dataGridColumn.Index; } + _negHorizontalOffset = GetEdgedColumnWidth(dataGridColumn) - (cx - _horizontalOffset); + return dataGridColumn.Index; } private void CorrectColumnDisplayIndexesAfterDeletion(DataGridColumn deletedColumn) @@ -1739,7 +1738,7 @@ public partial class DataGrid private void AutoGenerateColumnsPrivate() { - if (!_measured || (_autoGeneratingColumnOperationCount > 0)) + if (!_measured || _autoGeneratingColumnOperationCount > 0) { // Reading the DataType when we generate columns could cause the CollectionView to // raise a Reset if its Enumeration changed. In that case, we don't want to generate again. diff --git a/src/AtomUI.Controls.DataGrid/DataGrid.Rows.cs b/src/AtomUI.Controls.DataGrid/DataGrid.Rows.cs index 203aa84f1..3e0c9d816 100644 --- a/src/AtomUI.Controls.DataGrid/DataGrid.Rows.cs +++ b/src/AtomUI.Controls.DataGrid/DataGrid.Rows.cs @@ -1,3 +1,8 @@ +// (c) Copyright Microsoft Corporation. +// This source is subject to the Microsoft Public License (Ms-PL). +// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. +// All other rights reserved. + using System.Collections.Specialized; using System.ComponentModel; using System.Diagnostics; @@ -2910,15 +2915,13 @@ public partial class DataGrid // If the new item is a root level element, it has no parent group, so create an empty RowGroupInfo return new DataGridRowGroupInfo(null, true, -1, -1, -1); } - else + + foreach (int slot in RowGroupHeadersTable.GetIndexes()) { - foreach (int slot in RowGroupHeadersTable.GetIndexes()) + DataGridRowGroupInfo? groupInfo = RowGroupHeadersTable.GetValueAt(slot); + if (groupInfo!.CollectionViewGroup!.Items == collection) { - DataGridRowGroupInfo? groupInfo = RowGroupHeadersTable.GetValueAt(slot); - if (groupInfo!.CollectionViewGroup!.Items == collection) - { - return groupInfo; - } + return groupInfo; } } @@ -3062,12 +3065,12 @@ public partial class DataGrid return indexCount - _showDetailsTable.GetIndexCount(lowerBound, upperBound, false) - RowGroupHeadersTable.GetIndexCount(lowerBound, upperBound); } - else if (RowDetailsVisibilityMode == DataGridRowDetailsVisibilityMode.Collapsed) + if (RowDetailsVisibilityMode == DataGridRowDetailsVisibilityMode.Collapsed) { // Total rows with details explicitly turned on return _showDetailsTable.GetIndexCount(lowerBound, upperBound, true); - } - else if (RowDetailsVisibilityMode == DataGridRowDetailsVisibilityMode.VisibleWhenSelected) + } + if (RowDetailsVisibilityMode == DataGridRowDetailsVisibilityMode.VisibleWhenSelected) { // Total number of remaining rows that are selected return _selectedItems.GetIndexCount(lowerBound, upperBound); diff --git a/src/AtomUI.Controls.DataGrid/DataGrid.cs b/src/AtomUI.Controls.DataGrid/DataGrid.cs index d201bd78c..7c4ab597f 100644 --- a/src/AtomUI.Controls.DataGrid/DataGrid.cs +++ b/src/AtomUI.Controls.DataGrid/DataGrid.cs @@ -508,8 +508,8 @@ public partial class DataGrid : TemplatedControl private static bool IsValidRowHeight(double value) { return double.IsNaN(value) || - (value >= DataGridRow.DATAGRIDROW_minimumHeight && - value <= DataGridRow.DATAGRIDROW_maximumHeight); + (value >= DataGridRow.MinimumHeight && + value <= DataGridRow.MaximumHeight); } /// @@ -843,7 +843,7 @@ public partial class DataGrid : TemplatedControl // Update the RowDetails templates if necessary if (_rowsPresenter != null) { - foreach (DataGridRow row in GetAllRows()) + foreach (var row in GetAllRows()) { if (GetRowDetailsVisibility(row.Index)) { diff --git a/src/AtomUI.Controls.DataGrid/DataGridDisplayData.cs b/src/AtomUI.Controls.DataGrid/DataGridDisplayData.cs index b975291af..d14841969 100644 --- a/src/AtomUI.Controls.DataGrid/DataGridDisplayData.cs +++ b/src/AtomUI.Controls.DataGrid/DataGridDisplayData.cs @@ -64,7 +64,7 @@ internal class DataGridDisplayData { return _recyclableGroupHeaders.Pop(); } - else if (_fullyRecycledGroupHeaders.Count > 0) + if (_fullyRecycledGroupHeaders.Count > 0) { // For fully recycled rows, we need to set the Visibility back to Visible var groupHeader = _fullyRecycledGroupHeaders.Pop(); @@ -227,7 +227,8 @@ internal class DataGridDisplayData { return _recyclableRows.Pop(); } - else if (_fullyRecycledRows.Count > 0) + + if (_fullyRecycledRows.Count > 0) { // For fully recycled rows, we need to set the Visibility back to Visible var row = _fullyRecycledRows.Pop(); diff --git a/src/AtomUI.Controls.DataGrid/DataGridLengthConverter.cs b/src/AtomUI.Controls.DataGrid/DataGridLengthConverter.cs index a4eb5084e..f515d9732 100644 --- a/src/AtomUI.Controls.DataGrid/DataGridLengthConverter.cs +++ b/src/AtomUI.Controls.DataGrid/DataGridLengthConverter.cs @@ -125,10 +125,7 @@ public class DataGridLengthConverter : TypeConverter // WPF returns Auto in this case as well return DataGridLength.Auto; } - else - { - return new DataGridLength(doubleValue); - } + return new DataGridLength(doubleValue); } /// @@ -174,33 +171,30 @@ public class DataGridLengthConverter : TypeConverter { throw DataGridError.DataGridLengthConverter.InvalidDataGridLength("value"); } - else + // Convert dataGridLength to a string + switch (dataGridLength.Value.UnitType) { - // Convert dataGridLength to a string - switch (dataGridLength.Value.UnitType) - { - // for Auto print out "Auto". value is always "1.0" - case DataGridLengthUnitType.Auto: - return "Auto"; + // for Auto print out "Auto". value is always "1.0" + case DataGridLengthUnitType.Auto: + return "Auto"; - case DataGridLengthUnitType.SizeToHeader: - return "SizeToHeader"; + case DataGridLengthUnitType.SizeToHeader: + return "SizeToHeader"; - case DataGridLengthUnitType.SizeToCells: - return "SizeToCells"; + case DataGridLengthUnitType.SizeToCells: + return "SizeToCells"; - // Star has one special case when value is "1.0". - // in this case drop value part and print only "Star" - case DataGridLengthUnitType.Star: - return ( - MathUtilities.AreClose(1.0, dataGridLength.Value.Value) - ? _starSuffix - : Convert.ToString(dataGridLength.Value.Value, culture ?? CultureInfo.CurrentCulture) + - DataGridLengthConverter._starSuffix); + // Star has one special case when value is "1.0". + // in this case drop value part and print only "Star" + case DataGridLengthUnitType.Star: + return ( + MathUtilities.AreClose(1.0, dataGridLength.Value.Value) + ? _starSuffix + : Convert.ToString(dataGridLength.Value.Value, culture ?? CultureInfo.CurrentCulture) + + DataGridLengthConverter._starSuffix); - default: - return (Convert.ToString(dataGridLength.Value.Value, culture ?? CultureInfo.CurrentCulture)); - } + default: + return (Convert.ToString(dataGridLength.Value.Value, culture ?? CultureInfo.CurrentCulture)); } } } \ No newline at end of file diff --git a/src/AtomUI.Controls.DataGrid/EventArgs.cs b/src/AtomUI.Controls.DataGrid/EventArgs.cs index b16439457..6ae4cbe12 100644 --- a/src/AtomUI.Controls.DataGrid/EventArgs.cs +++ b/src/AtomUI.Controls.DataGrid/EventArgs.cs @@ -331,8 +331,8 @@ public class DataGridRowEditEndedEventArgs : EventArgs /// The editing action that has been taken. public DataGridRowEditEndedEventArgs(DataGridRow row, DataGridEditAction editAction) { - this.Row = row; - this.EditAction = editAction; + Row = row; + EditAction = editAction; } /// diff --git a/src/AtomUI.Controls.DataGrid/Row/DataGridRow.cs b/src/AtomUI.Controls.DataGrid/Row/DataGridRow.cs index 1491349c4..d9b54233f 100644 --- a/src/AtomUI.Controls.DataGrid/Row/DataGridRow.cs +++ b/src/AtomUI.Controls.DataGrid/Row/DataGridRow.cs @@ -21,23 +21,23 @@ using DataGridFrozenGrid = AtomUI.Controls.Utils.DataGridFrozenGrid; namespace AtomUI.Controls; -[TemplatePart(DATAGRIDROW_elementBottomGridLine, typeof(Rectangle))] -[TemplatePart(DATAGRIDROW_elementCells, typeof(DataGridCellsPresenter))] -[TemplatePart(DATAGRIDROW_elementDetails, typeof(DataGridDetailsPresenter))] -[TemplatePart(DATAGRIDROW_elementRoot, typeof(Panel))] -[TemplatePart(DATAGRIDROW_elementRowHeader, typeof(DataGridRowHeader))] -[PseudoClasses(":selected", ":editing", ":invalid")] +[TemplatePart(ElementBottomGridLinePC, typeof(Rectangle))] +[TemplatePart(ElementCellsPC, typeof(DataGridCellsPresenter))] +[TemplatePart(ElementDetailsPC, typeof(DataGridDetailsPresenter))] +[TemplatePart(ElementRootPC, typeof(Panel))] +[TemplatePart(ElementRowHeaderPC, typeof(DataGridRowHeader))] +[PseudoClasses(StdPseudoClass.Selected, StdPseudoClass.Editing, StdPseudoClass.Invalid)] public class DataGridRow : TemplatedControl { - private const byte DATAGRIDROW_defaultMinHeight = 0; - internal const int DATAGRIDROW_maximumHeight = 65536; - internal const double DATAGRIDROW_minimumHeight = 0; + private const byte DefaultMinHeight = 0; + internal const int MaximumHeight = 65536; + internal const double MinimumHeight = 0; - private const string DATAGRIDROW_elementBottomGridLine = "PART_BottomGridLine"; - private const string DATAGRIDROW_elementCells = "PART_CellsPresenter"; - private const string DATAGRIDROW_elementDetails = "PART_DetailsPresenter"; - internal const string DATAGRIDROW_elementRoot = "PART_Root"; - internal const string DATAGRIDROW_elementRowHeader = "PART_RowHeader"; + private const string ElementBottomGridLinePC = "PART_BottomGridLine"; + private const string ElementCellsPC = "PART_CellsPresenter"; + private const string ElementDetailsPC = "PART_DetailsPresenter"; + internal const string ElementRootPC = "PART_Root"; + internal const string ElementRowHeaderPC = "PART_RowHeader"; private DataGridCellsPresenter? _cellsElement; private DataGridCell? _fillerCell; @@ -150,7 +150,7 @@ public class DataGridRow : TemplatedControl /// public DataGridRow() { - MinHeight = DATAGRIDROW_defaultMinHeight; + MinHeight = DefaultMinHeight; Index = -1; IsValid = true; @@ -527,7 +527,7 @@ public class DataGridRow : TemplatedControl /// protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { - RootElement = e.NameScope.Find(DATAGRIDROW_elementRoot); + RootElement = e.NameScope.Find(ElementRootPC); if (RootElement != null) { ApplyState(); @@ -541,7 +541,7 @@ public class DataGridRow : TemplatedControl updateVerticalScrollBar = true; } - _cellsElement = e.NameScope.Find(DATAGRIDROW_elementCells); + _cellsElement = e.NameScope.Find(ElementCellsPC); if (_cellsElement != null) { _cellsElement.OwningRow = this; @@ -556,7 +556,7 @@ public class DataGridRow : TemplatedControl } } - _detailsElement = e.NameScope.Find(DATAGRIDROW_elementDetails); + _detailsElement = e.NameScope.Find(ElementDetailsPC); if (_detailsElement != null && OwningGrid != null) { _detailsElement.OwningRow = this; @@ -569,10 +569,10 @@ public class DataGridRow : TemplatedControl } } - _bottomGridLine = e.NameScope.Find(DATAGRIDROW_elementBottomGridLine); + _bottomGridLine = e.NameScope.Find(ElementBottomGridLinePC); EnsureGridLines(); - _headerElement = e.NameScope.Find(DATAGRIDROW_elementRowHeader); + _headerElement = e.NameScope.Find(ElementRowHeaderPC); if (_headerElement != null) { _headerElement.Owner = this; diff --git a/src/AtomUI.Controls.DataGrid/Row/DataGridRowGroupHeader.cs b/src/AtomUI.Controls.DataGrid/Row/DataGridRowGroupHeader.cs index 5ec19430a..fd6993580 100644 --- a/src/AtomUI.Controls.DataGrid/Row/DataGridRowGroupHeader.cs +++ b/src/AtomUI.Controls.DataGrid/Row/DataGridRowGroupHeader.cs @@ -1,6 +1,7 @@ using System.Diagnostics; using System.Reactive.Linq; using AtomUI.Controls.Utils; +using AtomUI.Utils; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Metadata; @@ -12,19 +13,19 @@ using DataGridFrozenGrid = AtomUI.Controls.Utils.DataGridFrozenGrid; namespace AtomUI.Controls; -[TemplatePart(DATAGRIDROWGROUPHEADER_expanderButton, typeof(ToggleButton))] -[TemplatePart(DATAGRIDROWGROUPHEADER_indentSpacer, typeof(Control))] -[TemplatePart(DATAGRIDROWGROUPHEADER_itemCountElement, typeof(TextBlock))] -[TemplatePart(DATAGRIDROWGROUPHEADER_propertyNameElement, typeof(TextBlock))] -[TemplatePart(DataGridRow.DATAGRIDROW_elementRoot, typeof(Panel))] -[TemplatePart(DataGridRow.DATAGRIDROW_elementRowHeader, typeof(DataGridRowHeader))] -[PseudoClasses(":pressed", ":current", ":expanded")] +[TemplatePart(ExpanderButtonPC, typeof(ToggleButton))] +[TemplatePart(IndentSpacerPC, typeof(Control))] +[TemplatePart(ItemCountElementPC, typeof(TextBlock))] +[TemplatePart(PropertyNameElementPC, typeof(TextBlock))] +[TemplatePart(DataGridRow.ElementRootPC, typeof(Panel))] +[TemplatePart(DataGridRow.ElementRowHeaderPC, typeof(DataGridRowHeader))] +[PseudoClasses(StdPseudoClass.Pressed, StdPseudoClass.Current, StdPseudoClass.Expanded)] public class DataGridRowGroupHeader : TemplatedControl { - private const string DATAGRIDROWGROUPHEADER_expanderButton = "PART_ExpanderButton"; - private const string DATAGRIDROWGROUPHEADER_indentSpacer = "PART_IndentSpacer"; - private const string DATAGRIDROWGROUPHEADER_itemCountElement = "PART_ItemCountElement"; - private const string DATAGRIDROWGROUPHEADER_propertyNameElement = "PART_PropertyNameElement"; + private const string ExpanderButtonPC = "PART_ExpanderButton"; + private const string IndentSpacerPC = "PART_IndentSpacer"; + private const string ItemCountElementPC = "PART_ItemCountElement"; + private const string PropertyNameElementPC = "PART_PropertyNameElement"; private bool _areIsCheckedHandlersSuspended; private ToggleButton? _expanderButton; @@ -165,10 +166,10 @@ public class DataGridRowGroupHeader : TemplatedControl protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { - _rootElement = e.NameScope.Find(DataGridRow.DATAGRIDROW_elementRoot); + _rootElement = e.NameScope.Find(DataGridRow.ElementRootPC); _expanderButtonSubscription?.Dispose(); - _expanderButton = e.NameScope.Find(DATAGRIDROWGROUPHEADER_expanderButton); + _expanderButton = e.NameScope.Find(ExpanderButtonPC); if (_expanderButton != null) { EnsureExpanderButtonIsChecked(); @@ -178,21 +179,21 @@ public class DataGridRowGroupHeader : TemplatedControl .Subscribe(v => OnExpanderButtonIsCheckedChanged(v)); } - _headerElement = e.NameScope.Find(DataGridRow.DATAGRIDROW_elementRowHeader); + _headerElement = e.NameScope.Find(DataGridRow.ElementRowHeaderPC); if (_headerElement != null) { _headerElement.Owner = this; EnsureHeaderVisibility(); } - _indentSpacer = e.NameScope.Find(DATAGRIDROWGROUPHEADER_indentSpacer); + _indentSpacer = e.NameScope.Find(IndentSpacerPC); if (_indentSpacer != null) { _indentSpacer.Width = _totalIndent; } - _itemCountElement = e.NameScope.Find(DATAGRIDROWGROUPHEADER_itemCountElement); - _propertyNameElement = e.NameScope.Find(DATAGRIDROWGROUPHEADER_propertyNameElement); + _itemCountElement = e.NameScope.Find(ItemCountElementPC); + _propertyNameElement = e.NameScope.Find(PropertyNameElementPC); UpdateTitleElements(); } diff --git a/src/AtomUI.Controls.DataGrid/Row/DataGridRowHeader.cs b/src/AtomUI.Controls.DataGrid/Row/DataGridRowHeader.cs index 48c6c1ced..9958bedeb 100644 --- a/src/AtomUI.Controls.DataGrid/Row/DataGridRowHeader.cs +++ b/src/AtomUI.Controls.DataGrid/Row/DataGridRowHeader.cs @@ -1,4 +1,5 @@ using System.Diagnostics; +using AtomUI.Utils; using Avalonia; using Avalonia.Automation; using Avalonia.Controls; @@ -12,11 +13,11 @@ namespace AtomUI.Controls; /// /// Represents an individual row header. /// -[TemplatePart(DATAGRIDROWHEADER_elementRootName, typeof(Control))] -[PseudoClasses(":invalid", ":selected", ":editing", ":current")] +[TemplatePart(ElementRootNamePC, typeof(Control))] +[PseudoClasses(StdPseudoClass.Invalid, StdPseudoClass.Selected, StdPseudoClass.Editing, StdPseudoClass.Current)] public class DataGridRowHeader : ContentControl { - private const string DATAGRIDROWHEADER_elementRootName = "PART_Root"; + private const string ElementRootNamePC = "PART_Root"; private Control? _rootElement; public static readonly StyledProperty SeparatorBrushProperty = @@ -100,7 +101,7 @@ public class DataGridRowHeader : ContentControl /// protected override void OnApplyTemplate(TemplateAppliedEventArgs e) { - _rootElement = e.NameScope.Find(DATAGRIDROWHEADER_elementRootName); + _rootElement = e.NameScope.Find(ElementRootNamePC); if (_rootElement != null) { UpdatePseudoClasses(); @@ -142,20 +143,18 @@ public class DataGridRowHeader : ContentControl { if (OwningRow != null) { - PseudoClasses.Set(":invalid", !OwningRow.IsValid); - - PseudoClasses.Set(":selected", OwningRow.IsSelected); - - PseudoClasses.Set(":editing", OwningRow.IsEditing); + PseudoClasses.Set(StdPseudoClass.Invalid, !OwningRow.IsValid); + PseudoClasses.Set(StdPseudoClass.Selected, OwningRow.IsSelected); + PseudoClasses.Set(StdPseudoClass.Editing, OwningRow.IsEditing); if (OwningGrid != null) { - PseudoClasses.Set(":current", OwningRow.Slot == OwningGrid.CurrentSlot); + PseudoClasses.Set(StdPseudoClass.Current, OwningRow.Slot == OwningGrid.CurrentSlot); } } else if (OwningRowGroupHeader != null && OwningGrid != null) { - PseudoClasses.Set(":current", OwningRowGroupHeader.RowGroupInfo?.Slot == OwningGrid.CurrentSlot); + PseudoClasses.Set(StdPseudoClass.Current, OwningRowGroupHeader.RowGroupInfo?.Slot == OwningGrid.CurrentSlot); } } } diff --git a/src/AtomUI.Controls.DataGrid/Row/DataGridRowsPresenter.cs b/src/AtomUI.Controls.DataGrid/Row/DataGridRowsPresenter.cs index 2102986de..95deeaf61 100644 --- a/src/AtomUI.Controls.DataGrid/Row/DataGridRowsPresenter.cs +++ b/src/AtomUI.Controls.DataGrid/Row/DataGridRowsPresenter.cs @@ -74,7 +74,7 @@ public sealed class DataGridRowsPresenter : Panel, IChildIndexProvider OwningGrid.ColumnsInternal.VisibleEdgedColumnsWidth + OwningGrid.ColumnsInternal.FillerColumn.FillerWidth; double topEdge = -OwningGrid.NegVerticalOffset; - foreach (Control element in OwningGrid.DisplayData.GetScrollingElements()) + foreach (var element in OwningGrid.DisplayData.GetScrollingElements()) { if (element is DataGridRow row) { diff --git a/src/AtomUI.Controls.DataGrid/Utils/IndexToValueTable.cs b/src/AtomUI.Controls.DataGrid/Utils/IndexToValueTable.cs index 9d3bb5832..b4c2572d7 100644 --- a/src/AtomUI.Controls.DataGrid/Utils/IndexToValueTable.cs +++ b/src/AtomUI.Controls.DataGrid/Utils/IndexToValueTable.cs @@ -137,7 +137,7 @@ internal class IndexToValueTable : IEnumerable> public bool ContainsIndexAndValue(int index, T value) { var lowerRangeIndex = FindRangeIndex(index); - return ((IsCorrectRangeIndex(lowerRangeIndex, index)) && (_list[lowerRangeIndex].ContainsValue(value))); + return IsCorrectRangeIndex(lowerRangeIndex, index) && _list[lowerRangeIndex].ContainsValue(value); } /// @@ -168,10 +168,7 @@ internal class IndexToValueTable : IEnumerable> return _list[rangeIndex].UpperBound + 1; } - else - { - return targetIndex; - } + return targetIndex; } public int GetNextIndex(int index) @@ -182,11 +179,8 @@ internal class IndexToValueTable : IEnumerable> { return targetIndex; } - else - { - rangeIndex++; - return rangeIndex < _list.Count ? _list[rangeIndex].LowerBound : -1; - } + rangeIndex++; + return rangeIndex < _list.Count ? _list[rangeIndex].LowerBound : -1; } public int GetPreviousGap(int index) @@ -202,10 +196,7 @@ internal class IndexToValueTable : IEnumerable> return _list[rangeIndex].LowerBound - 1; } - else - { - return targetIndex; - } + return targetIndex; } public int GetPreviousIndex(int index) @@ -216,10 +207,7 @@ internal class IndexToValueTable : IEnumerable> { return targetIndex; } - else - { - return rangeIndex >= 0 && rangeIndex < _list.Count ? _list[rangeIndex].UpperBound : -1; - } + return rangeIndex >= 0 && rangeIndex < _list.Count ? _list[rangeIndex].UpperBound : -1; } /// @@ -390,10 +378,7 @@ internal class IndexToValueTable : IEnumerable> { return range.LowerBound + n - cumulatedEntries; } - else - { - cumulatedEntries += range.Count; - } + cumulatedEntries += range.Count; } return -1; @@ -423,11 +408,8 @@ internal class IndexToValueTable : IEnumerable> found = true; return _list[rangeIndex].Value; } - else - { - found = false; - return default; - } + found = false; + return default; } /// @@ -445,10 +427,7 @@ internal class IndexToValueTable : IEnumerable> cumulatedIndexes += index - range.LowerBound; break; } - else - { - cumulatedIndexes += range.Count; - } + cumulatedIndexes += range.Count; } return cumulatedIndexes; @@ -495,7 +474,7 @@ internal class IndexToValueTable : IEnumerable> Debug.Assert(count > 0); var lowerRangeIndex = FindRangeIndex(startIndex); InsertIndexesPrivate(startIndex, count, lowerRangeIndex); - if ((lowerRangeIndex >= 0) && (_list[lowerRangeIndex].LowerBound > startIndex)) + if (lowerRangeIndex >= 0 && _list[lowerRangeIndex].LowerBound > startIndex) { // Because of the insert, the original range no longer contains the startIndex lowerRangeIndex--; @@ -618,7 +597,7 @@ internal class IndexToValueTable : IEnumerable> lowerRangeIndex = 0; } - while ((lowerRangeIndex < _list.Count) && (_list[lowerRangeIndex].UpperBound < startIndex)) + while (lowerRangeIndex < _list.Count && _list[lowerRangeIndex].UpperBound < startIndex) { lowerRangeIndex++; } @@ -712,7 +691,7 @@ internal class IndexToValueTable : IEnumerable> // At this point the newRange has been inserted in the correct place, now we need to remove // any subsequent ranges that no longer make sense and possibly update the one at newRange.UpperBound var upperRangeIndex = lowerRangeIndex + 1; - while ((upperRangeIndex < _list.Count) && (_list[upperRangeIndex].UpperBound < endIndex)) + while (upperRangeIndex < _list.Count && _list[upperRangeIndex].UpperBound < endIndex) { _list.RemoveAt(upperRangeIndex); } @@ -771,17 +750,11 @@ internal class IndexToValueTable : IEnumerable> // we found it or the index isn't there and we're one range before return front; } - else - { - // not found and we're one range after - return front - 1; - } - } - else - { - // end is one index before front in this case so it's the range before - return end; + // not found and we're one range after + return front - 1; } + // end is one index before front in this case so it's the range before + return end; } private bool Merge(int lowerRangeIndex) @@ -808,7 +781,7 @@ internal class IndexToValueTable : IEnumerable> Debug.Assert(count > 0); // Same as AddRange after we fix the indicies affected by the insertion - var startRangeIndex = (lowerRangeIndex >= 0) ? lowerRangeIndex : 0; + var startRangeIndex = lowerRangeIndex >= 0 ? lowerRangeIndex : 0; for (var i = startRangeIndex; i < _list.Count; i++) { Range range = _list[i]; @@ -837,7 +810,7 @@ internal class IndexToValueTable : IEnumerable> private bool IsCorrectRangeIndex(int rangeIndex, int index) { - return (-1 != rangeIndex) && (_list[rangeIndex].ContainsIndex(index)); + return -1 != rangeIndex && _list[rangeIndex].ContainsIndex(index); } private bool RemoveRangeIfInvalid(Range range, int rangeIndex) diff --git a/src/AtomUI.Controls.DataGrid/Utils/Range.cs b/src/AtomUI.Controls.DataGrid/Utils/Range.cs index cdada7b11..3e3ac511b 100644 --- a/src/AtomUI.Controls.DataGrid/Utils/Range.cs +++ b/src/AtomUI.Controls.DataGrid/Utils/Range.cs @@ -33,10 +33,7 @@ internal class Range { return value == null; } - else - { - return Value.Equals(value); - } + return Value.Equals(value); } public Range Copy() diff --git a/src/AtomUI.Controls.DataGrid/Utils/TypeHelper.cs b/src/AtomUI.Controls.DataGrid/Utils/TypeHelper.cs index e3cf8bad0..8e6f88e01 100644 --- a/src/AtomUI.Controls.DataGrid/Utils/TypeHelper.cs +++ b/src/AtomUI.Controls.DataGrid/Utils/TypeHelper.cs @@ -151,7 +151,7 @@ internal static class TypeHelper var type = parentType; PropertyInfo? propertyInfo = null; var propertyNames = SplitPropertyPath(propertyPath); - for (int i = 0; i < propertyNames.Count; i++) + for (var i = 0; i < propertyNames.Count; i++) { // if we can't find the property or it is not of the correct type, // treat it as a null value @@ -265,15 +265,15 @@ internal static class TypeHelper if (elementType != null) { + var genericList = typeof(List<>).MakeGenericType(elementType); // If the object is of type IList, try to use its default indexer. - if (typeof(IList<>).MakeGenericType(elementType) is Type genericList - && genericList.IsAssignableFrom(type)) + if (genericList.IsAssignableFrom(type)) { indexer = FindIndexerInMembers(genericList.GetDefaultMembers(), stringIndex, out index); } - - if (typeof(IReadOnlyList<>).MakeGenericType(elementType) is Type genericReadOnlyList - && genericReadOnlyList.IsAssignableFrom(type)) + + var genericReadOnlyList = typeof(List<>).MakeGenericType(elementType); + if (genericReadOnlyList.IsAssignableFrom(type)) { indexer = FindIndexerInMembers(genericReadOnlyList.GetDefaultMembers(), stringIndex, out index); } @@ -419,11 +419,20 @@ internal static class TypeHelper { // We haven't located a type yet.. try a different approach. // Does the list have anything in it? - var enumerator = list.GetEnumerator(); - if (enumerator.MoveNext() && enumerator.Current != null) + try { - return enumerator.Current.GetType(); + if (enumerator.MoveNext() && enumerator.Current != null) + { + return enumerator.Current.GetType(); + } + } + finally + { + if (enumerator is IDisposable disposable) + { + disposable.Dispose(); + } } } diff --git a/src/AtomUI.Controls.DataGrid/Utils/ValidationUtil.cs b/src/AtomUI.Controls.DataGrid/Utils/ValidationUtil.cs index 0761424e9..93a01f9d2 100644 --- a/src/AtomUI.Controls.DataGrid/Utils/ValidationUtil.cs +++ b/src/AtomUI.Controls.DataGrid/Utils/ValidationUtil.cs @@ -41,8 +41,8 @@ internal static class ValidationUtil { if (oldValidationResult.ErrorMessage == target.ErrorMessage) { - bool movedOld = true; - bool movedTarget = true; + bool movedOld = true; + bool movedTarget = true; using IEnumerator oldEnumerator = oldValidationResult.MemberNames.GetEnumerator(); using IEnumerator targetEnumerator = target.MemberNames.GetEnumerator(); while (movedOld && movedTarget) @@ -160,9 +160,9 @@ internal static class ValidationUtil /// True if the process cannot be recovered from the exception. public static bool IsCriticalException(Exception exception) { - return (exception is OutOfMemoryException) || - (exception is StackOverflowException) || - (exception is AccessViolationException) || - (exception is ThreadAbortException); + return exception is OutOfMemoryException || + exception is StackOverflowException || + exception is AccessViolationException || + exception is ThreadAbortException; } } \ No newline at end of file