diff --git a/src/Avalonia.Controls/Calendar/Calendar.cs b/src/Avalonia.Controls/Calendar/Calendar.cs index 2dbb5f02f9..bb838a4f3f 100644 --- a/src/Avalonia.Controls/Calendar/Calendar.cs +++ b/src/Avalonia.Controls/Calendar/Calendar.cs @@ -224,7 +224,7 @@ namespace Avalonia.Controls /// /// [TemplatePart(PART_ElementMonth, typeof(CalendarItem))] - [TemplatePart(PART_ElementRoot, typeof(Panel))] + [TemplatePart(PART_ElementRoot, typeof(Panel))] public class Calendar : TemplatedControl { internal const int RowsPerMonth = 7; @@ -338,14 +338,11 @@ namespace Avalonia.Controls /// The DependencyPropertyChangedEventArgs. private void OnIsTodayHighlightedChanged(AvaloniaPropertyChangedEventArgs e) { - if (DisplayDate != null) - { - int i = DateTimeHelper.CompareYearMonth(DisplayDateInternal, DateTime.Today); + int i = DateTimeHelper.CompareYearMonth(DisplayDateInternal, DateTime.Today); - if (i > -2 && i < 2) - { - UpdateMonths(); - } + if (i > -2 && i < 2) + { + UpdateMonths(); } } @@ -655,7 +652,7 @@ namespace Avalonia.Controls SelectedDatesChanged?.Invoke(this, e); } } - + internal Collection RemovedItems { get; set; } internal DateTime? LastSelectedDateInternal { get; set; } internal DateTime? LastSelectedDate @@ -914,7 +911,7 @@ namespace Avalonia.Controls o => o.DisplayDateEnd, (o, v) => o.DisplayDateEnd = v, defaultBindingMode: BindingMode.TwoWay); - + /// /// Gets or sets the last date to be displayed. /// @@ -1242,7 +1239,7 @@ namespace Avalonia.Controls { b.IsSelected = false; } - } + } } } } @@ -1278,7 +1275,7 @@ namespace Avalonia.Controls internal void OnPreviousClick() { - if (DisplayMode == CalendarMode.Month && DisplayDate != null) + if (DisplayMode == CalendarMode.Month) { DateTime? d = DateTimeHelper.AddMonths(DateTimeHelper.DiscardDayTime(DisplayDate), -1); if (d.HasValue) @@ -1326,7 +1323,7 @@ namespace Avalonia.Controls } internal void OnNextClick() { - if (DisplayMode == CalendarMode.Month && DisplayDate != null) + if (DisplayMode == CalendarMode.Month) { DateTime? d = DateTimeHelper.AddMonths(DateTimeHelper.DiscardDayTime(DisplayDate), 1); if (d.HasValue) @@ -1645,7 +1642,7 @@ namespace Avalonia.Controls { if (DisplayMode == CalendarMode.Month) { - if (LastSelectedDate.HasValue && DisplayDateInternal != null) + if (LastSelectedDate.HasValue) { // If a blackout day is inactive, when clicked on it, the // previous inactive day which is not a blackout day can get @@ -1897,24 +1894,21 @@ namespace Avalonia.Controls { case CalendarMode.Month: { - if (DisplayDate != null) - { - DateTime? selectedDate = new DateTime(DisplayDateInternal.Year, DisplayDateInternal.Month, 1); + DateTime? selectedDate = new DateTime(DisplayDateInternal.Year, DisplayDateInternal.Month, 1); - if (DateTimeHelper.CompareYearMonth(DateTime.MaxValue, selectedDate.Value) > 0) - { - // since DisplayDate is not equal to - // DateTime.MaxValue we are sure selectedDate is\ - // not null - selectedDate = DateTimeHelper.AddMonths(selectedDate.Value, 1)!.Value; - selectedDate = DateTimeHelper.AddDays(selectedDate.Value, -1)!.Value; - } - else - { - selectedDate = DateTime.MaxValue; - } - ProcessSelection(shift, selectedDate, null); + if (DateTimeHelper.CompareYearMonth(DateTime.MaxValue, selectedDate.Value) > 0) + { + // since DisplayDate is not equal to + // DateTime.MaxValue we are sure selectedDate is\ + // not null + selectedDate = DateTimeHelper.AddMonths(selectedDate.Value, 1)!.Value; + selectedDate = DateTimeHelper.AddDays(selectedDate.Value, -1)!.Value; } + else + { + selectedDate = DateTime.MaxValue; + } + ProcessSelection(shift, selectedDate, null); break; } case CalendarMode.Year: @@ -2026,7 +2020,6 @@ namespace Avalonia.Controls focusDate = DisplayDate; LastSelectedDate = DisplayDate; } - Debug.Assert(focusDate != null, "focusDate should not be null!"); FocusButton = FindDayButtonFromDay(focusDate); if (FocusButton != null) @@ -2091,17 +2084,17 @@ namespace Avalonia.Controls static Calendar() { - IsEnabledProperty.Changed.AddClassHandler((x,e) => x.OnIsEnabledChanged(e)); - FirstDayOfWeekProperty.Changed.AddClassHandler((x,e) => x.OnFirstDayOfWeekChanged(e)); - IsTodayHighlightedProperty.Changed.AddClassHandler((x,e) => x.OnIsTodayHighlightedChanged(e)); - DisplayModeProperty.Changed.AddClassHandler((x,e) => x.OnDisplayModePropertyChanged(e)); - SelectionModeProperty.Changed.AddClassHandler((x,e) => x.OnSelectionModeChanged(e)); - SelectedDateProperty.Changed.AddClassHandler((x,e) => x.OnSelectedDateChanged(e)); - DisplayDateProperty.Changed.AddClassHandler((x,e) => x.OnDisplayDateChanged(e)); - DisplayDateStartProperty.Changed.AddClassHandler((x,e) => x.OnDisplayDateStartChanged(e)); - DisplayDateEndProperty.Changed.AddClassHandler((x,e) => x.OnDisplayDateEndChanged(e)); - KeyDownEvent.AddClassHandler((x,e) => x.Calendar_KeyDown(e)); - KeyUpEvent.AddClassHandler((x,e) => x.Calendar_KeyUp(e)); + IsEnabledProperty.Changed.AddClassHandler((x, e) => x.OnIsEnabledChanged(e)); + FirstDayOfWeekProperty.Changed.AddClassHandler((x, e) => x.OnFirstDayOfWeekChanged(e)); + IsTodayHighlightedProperty.Changed.AddClassHandler((x, e) => x.OnIsTodayHighlightedChanged(e)); + DisplayModeProperty.Changed.AddClassHandler((x, e) => x.OnDisplayModePropertyChanged(e)); + SelectionModeProperty.Changed.AddClassHandler((x, e) => x.OnSelectionModeChanged(e)); + SelectedDateProperty.Changed.AddClassHandler((x, e) => x.OnSelectedDateChanged(e)); + DisplayDateProperty.Changed.AddClassHandler((x, e) => x.OnDisplayDateChanged(e)); + DisplayDateStartProperty.Changed.AddClassHandler((x, e) => x.OnDisplayDateStartChanged(e)); + DisplayDateEndProperty.Changed.AddClassHandler((x, e) => x.OnDisplayDateEndChanged(e)); + KeyDownEvent.AddClassHandler((x, e) => x.Calendar_KeyDown(e)); + KeyUpEvent.AddClassHandler((x, e) => x.Calendar_KeyUp(e)); } /// diff --git a/src/Avalonia.Controls/Calendar/CalendarItem.cs b/src/Avalonia.Controls/Calendar/CalendarItem.cs index 4c958c83b7..75e9f52621 100644 --- a/src/Avalonia.Controls/Calendar/CalendarItem.cs +++ b/src/Avalonia.Controls/Calendar/CalendarItem.cs @@ -4,7 +4,6 @@ // All other rights reserved. using System; -using System.Collections.Generic; using System.Diagnostics; using System.Globalization; using Avalonia.Collections.Pooled; @@ -353,7 +352,6 @@ namespace Avalonia.Controls.Primitives { if (Owner != null) { - Debug.Assert(Owner.DisplayDate != null, "The Owner Calendar's DisplayDate should not be null!"); _currentMonth = Owner.DisplayDateInternal; } else @@ -361,17 +359,14 @@ namespace Avalonia.Controls.Primitives _currentMonth = DateTime.Today; } - if (_currentMonth != null) - { - SetMonthModeHeaderButton(); - SetMonthModePreviousButton(_currentMonth); - SetMonthModeNextButton(_currentMonth); + SetMonthModeHeaderButton(); + SetMonthModePreviousButton(_currentMonth); + SetMonthModeNextButton(_currentMonth); - if (MonthView != null) - { - SetDayTitles(); - SetCalendarDayButtons(_currentMonth); - } + if (MonthView != null) + { + SetDayTitles(); + SetCalendarDayButtons(_currentMonth); } } private void SetMonthModeHeaderButton() @@ -592,7 +587,6 @@ namespace Avalonia.Controls.Primitives { if (Owner != null) { - Debug.Assert(Owner.SelectedMonth != null, "The Owner Calendar's SelectedMonth should not be null!"); _currentMonth = (DateTime)Owner.SelectedMonth; } else @@ -600,16 +594,13 @@ namespace Avalonia.Controls.Primitives _currentMonth = DateTime.Today; } - if (_currentMonth != null) - { - SetYearModeHeaderButton(); - SetYearModePreviousButton(); - SetYearModeNextButton(); + SetYearModeHeaderButton(); + SetYearModePreviousButton(); + SetYearModeNextButton(); - if (YearView != null) - { - SetMonthButtonsForYearMode(); - } + if (YearView != null) + { + SetMonthButtonsForYearMode(); } } private void SetYearModeHeaderButton() @@ -660,7 +651,6 @@ namespace Avalonia.Controls.Primitives childButton.IsCalendarButtonFocused = false; } - Debug.Assert(Owner.DisplayDateInternal != null, "The Owner Calendar's DisplayDateInternal should not be null!"); childButton.IsSelected = (DateTimeHelper.CompareYearMonth(day, Owner.DisplayDateInternal) == 0); if (DateTimeHelper.CompareYearMonth(day, Owner.DisplayDateRangeStart) < 0 || DateTimeHelper.CompareYearMonth(day, Owner.DisplayDateRangeEnd) > 0) @@ -685,7 +675,6 @@ namespace Avalonia.Controls.Primitives if (Owner != null) { - Debug.Assert(Owner.SelectedYear != null, "The owning Calendar's selected year should not be null!"); selectedYear = Owner.SelectedYear; _currentMonth = (DateTime)Owner.SelectedMonth; } @@ -695,19 +684,16 @@ namespace Avalonia.Controls.Primitives selectedYear = DateTime.Today; } - if (_currentMonth != null) - { - int decade = DateTimeHelper.DecadeOfDate(selectedYear); - int decadeEnd = DateTimeHelper.EndOfDecade(selectedYear); + int decade = DateTimeHelper.DecadeOfDate(selectedYear); + int decadeEnd = DateTimeHelper.EndOfDecade(selectedYear); - SetDecadeModeHeaderButton(decade, decadeEnd); - SetDecadeModePreviousButton(decade); - SetDecadeModeNextButton(decadeEnd); + SetDecadeModeHeaderButton(decade, decadeEnd); + SetDecadeModePreviousButton(decade); + SetDecadeModeNextButton(decadeEnd); - if (YearView != null) - { - SetYearButtons(decade, decadeEnd); - } + if (YearView != null) + { + SetYearButtons(decade, decadeEnd); } } internal void UpdateYearViewSelection(CalendarButton calendarButton) @@ -822,22 +808,15 @@ namespace Avalonia.Controls.Primitives { if (Owner.DisplayMode == CalendarMode.Month) { - if (Owner.DisplayDate != null) - { - d = Owner.DisplayDateInternal; - Owner.SelectedMonth = new DateTime(d.Year, d.Month, 1); - } + d = Owner.DisplayDateInternal; + Owner.SelectedMonth = new DateTime(d.Year, d.Month, 1); Owner.DisplayMode = CalendarMode.Year; } else { Debug.Assert(Owner.DisplayMode == CalendarMode.Year, "The Owner Calendar's DisplayMode should be Year!"); - - if (Owner.SelectedMonth != null) - { - d = Owner.SelectedMonth; - Owner.SelectedYear = new DateTime(d.Year, d.Month, 1); - } + d = Owner.SelectedMonth; + Owner.SelectedYear = new DateTime(d.Year, d.Month, 1); Owner.DisplayMode = CalendarMode.Decade; } } diff --git a/src/Avalonia.Controls/Calendar/SelectedDatesCollection.cs b/src/Avalonia.Controls/Calendar/SelectedDatesCollection.cs index c7c35718e0..f4bc2528ba 100644 --- a/src/Avalonia.Controls/Calendar/SelectedDatesCollection.cs +++ b/src/Avalonia.Controls/Calendar/SelectedDatesCollection.cs @@ -297,7 +297,7 @@ namespace Avalonia.Controls.Primitives } else { - if (item != null && DateTime.Compare(this[index], item) != 0 && Calendar.IsValidDateSelection(_owner, item)) + if (DateTime.Compare(this[index], item) != 0 && Calendar.IsValidDateSelection(_owner, item)) { removedItems.Add(this[index]); base.SetItem(index, item);