From b5133a58f8040470ad6a5349be2250d30ed72b22 Mon Sep 17 00:00:00 2001 From: rabbitism Date: Thu, 20 Jun 2024 01:49:25 +0800 Subject: [PATCH] fix: fix a header issue. add handler to check date selection. --- demo/Ursa.Demo/Pages/DatePickerDemo.axaml | 2 +- demo/Ursa.Demo/Pages/DatePickerDemo.axaml.cs | 10 ++ .../Controls/DateTimePicker/CalendarView.cs | 100 ++++++++++-------- 3 files changed, 64 insertions(+), 48 deletions(-) diff --git a/demo/Ursa.Demo/Pages/DatePickerDemo.axaml b/demo/Ursa.Demo/Pages/DatePickerDemo.axaml index e1e8e8f..254a1ea 100644 --- a/demo/Ursa.Demo/Pages/DatePickerDemo.axaml +++ b/demo/Ursa.Demo/Pages/DatePickerDemo.axaml @@ -6,6 +6,6 @@ mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450" x:Class="Ursa.Demo.Pages.DatePickerDemo"> - + diff --git a/demo/Ursa.Demo/Pages/DatePickerDemo.axaml.cs b/demo/Ursa.Demo/Pages/DatePickerDemo.axaml.cs index 2c6057c..7517a50 100644 --- a/demo/Ursa.Demo/Pages/DatePickerDemo.axaml.cs +++ b/demo/Ursa.Demo/Pages/DatePickerDemo.axaml.cs @@ -12,4 +12,14 @@ public partial class DatePickerDemo : UserControl { InitializeComponent(); } + + private void CalendarView_OnOnDateSelected(object? sender, CalendarDayButtonEventArgs e) + { + Debug.WriteLine("Pressed: "+ e.Date.ToLongDateString()); + } + + private void CalendarView_OnOnDatePreviewed(object? sender, CalendarDayButtonEventArgs e) + { + Debug.WriteLine("Hovered: "+e.Date.ToLongDateString()); + } } \ No newline at end of file diff --git a/src/Ursa/Controls/DateTimePicker/CalendarView.cs b/src/Ursa/Controls/DateTimePicker/CalendarView.cs index 1569dee..5138a3a 100644 --- a/src/Ursa/Controls/DateTimePicker/CalendarView.cs +++ b/src/Ursa/Controls/DateTimePicker/CalendarView.cs @@ -1,5 +1,4 @@ -using System.Diagnostics; -using System.Globalization; +using System.Globalization; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.Metadata; @@ -38,7 +37,7 @@ public class CalendarView : TemplatedControl internal static readonly DirectProperty ModeProperty = AvaloniaProperty.RegisterDirect( - nameof(Mode), o => o.Mode, (o, v) => o.Mode = v, unsetValue: CalendarViewMode.Month); + nameof(Mode), o => o.Mode, (o, v) => o.Mode = v); public static readonly StyledProperty IsTodayHighlightedProperty = DatePickerBase.IsTodayHighlightedProperty.AddOwner(); @@ -47,20 +46,19 @@ public class CalendarView : TemplatedControl DatePickerBase.FirstDayOfWeekProperty.AddOwner(); private readonly Calendar _calendar = new GregorianCalendar(); - - private Button? _headerButton; - private Button? _monthButton; - private Button? _yearButton; - private Grid? _monthGrid; - private Grid? _yearGrid; - - private CalendarViewMode _mode; + private Button? _fastNextButton; private Button? _fastPreviousButton; - private Button? _previousButton; + + private Button? _headerButton; + + private CalendarViewMode _mode; + private Button? _monthButton; + private Grid? _monthGrid; private Button? _nextButton; - private Button? _fastNextButton; - + private Button? _previousButton; + private Button? _yearButton; + private Grid? _yearGrid; static CalendarView() @@ -131,7 +129,7 @@ public class CalendarView : TemplatedControl Button.ClickEvent.AddHandler(OnPrevious, _previousButton); Button.ClickEvent.AddHandler(OnNext, _nextButton); Button.ClickEvent.AddHandler(OnFastNext, _fastNextButton); - + ContextCalendar = new CalendarContext(DateTime.Today.Year, DateTime.Today.Month, DateTime.Today.Day); PseudoClasses.Set(PC_Month, Mode == CalendarViewMode.Month); InitializeGridButtons(); @@ -147,6 +145,7 @@ public class CalendarView : TemplatedControl ContextCalendar.Year += 1; UpdateDayButtons(); } + UpdateHeaderButtons(); } @@ -160,6 +159,7 @@ public class CalendarView : TemplatedControl ContextCalendar.Month = 1; ContextCalendar.Year += 1; } + UpdateDayButtons(); } else if (Mode == CalendarViewMode.Year) @@ -179,6 +179,7 @@ public class CalendarView : TemplatedControl ContextCalendar.EndYear += 100; UpdateYearButtons(); } + UpdateHeaderButtons(); } @@ -192,6 +193,7 @@ public class CalendarView : TemplatedControl ContextCalendar.Month = 12; ContextCalendar.Year -= 1; } + UpdateDayButtons(); } else if (Mode == CalendarViewMode.Year) @@ -211,6 +213,7 @@ public class CalendarView : TemplatedControl ContextCalendar.EndYear -= 100; UpdateYearButtons(); } + UpdateHeaderButtons(); } @@ -221,6 +224,7 @@ public class CalendarView : TemplatedControl ContextCalendar.Year -= 1; UpdateDayButtons(); } + UpdateHeaderButtons(); } @@ -241,9 +245,10 @@ public class CalendarView : TemplatedControl ContextCalendar.EndYear = range.end; UpdateHeaderButtons(); UpdateYearButtons(); - return; + return; } - if(Mode == CalendarViewMode.Decade) + + if (Mode == CalendarViewMode.Decade) { Mode = CalendarViewMode.Century; var range = DateTimeHelper.GetCenturyViewRangeByYear(ContextCalendar.StartYear!.Value); @@ -253,13 +258,14 @@ public class CalendarView : TemplatedControl UpdateYearButtons(); return; } + if (Mode == CalendarViewMode.Century) return; } /// - /// Generate Buttons and labels for MonthView. - /// Generate Buttons for YearView. - /// This method should be called only once. + /// Generate Buttons and labels for MonthView. + /// Generate Buttons for YearView. + /// This method should be called only once. /// private void InitializeGridButtons() { @@ -317,13 +323,11 @@ public class CalendarView : TemplatedControl var cell = children[i] as CalendarDayButton; if (cell is null) continue; cell.DataContext = day; - if (IsTodayHighlighted) - { - cell.IsToday = day == DateTime.Today; - } + if (IsTodayHighlighted) cell.IsToday = day == DateTime.Today; cell.Content = day.Day.ToString(info); dateToSet = dateToSet.AddDays(1); } + FadeOutDayButtons(); } @@ -335,24 +339,24 @@ public class CalendarView : TemplatedControl if (mode == CalendarViewMode.Century && contextDate.StartYear.HasValue) { var range = DateTimeHelper.GetCenturyViewRangeByYear(contextDate.StartYear.Value); - int start = range.start-10; - for (int i = 0; i < 12; i++) + var start = range.start - 10; + for (var i = 0; i < 12; i++) { var child = _yearGrid.Children[i] as CalendarYearButton; child?.SetContext(CalendarViewMode.Century, - new CalendarContext() { StartYear = start, EndYear = start + 10 }); - start+= 10; + new CalendarContext { StartYear = start, EndYear = start + 10 }); + start += 10; } } - else if(mode == CalendarViewMode.Decade && contextDate.StartYear.HasValue) + else if (mode == CalendarViewMode.Decade && contextDate.StartYear.HasValue) { var range = DateTimeHelper.GetDecadeViewRangeByYear(contextDate.StartYear.Value); - int year = range.start - 1; - for (int i = 0; i < 12; i++) + var year = range.start - 1; + for (var i = 0; i < 12; i++) { var child = _yearGrid.Children[i] as CalendarYearButton; child?.SetContext(CalendarViewMode.Decade, - new CalendarContext() { Year = year}); + new CalendarContext { Year = year }); year++; } } @@ -361,7 +365,7 @@ public class CalendarView : TemplatedControl for (var i = 0; i < 12; i++) { var child = _yearGrid.Children[i] as CalendarYearButton; - child?.SetContext(CalendarViewMode.Year, new CalendarContext() { Month = i }); + child?.SetContext(CalendarViewMode.Year, new CalendarContext { Month = i }); } } } @@ -371,13 +375,8 @@ public class CalendarView : TemplatedControl if (_monthGrid is null) return; var children = _monthGrid.Children; for (var i = 7; i < children.Count; i++) - { if (children[i] is CalendarDayButton { DataContext: DateTime d } button) - { button.IsNotCurrentMonth = d.Month != ContextCalendar.Month; - } - } - } private void UpdateMonthViewHeader(DayOfWeek day) @@ -417,6 +416,7 @@ public class CalendarView : TemplatedControl ContextCalendar.Month = e.Date.Month; UpdateDayButtons(); } + OnDateSelected?.Invoke(sender, e); } @@ -469,7 +469,7 @@ public class CalendarView : TemplatedControl ContextCalendar.StartYear = e.Context.StartYear; ContextCalendar.EndYear = e.Context.EndYear; } - else if(Mode == CalendarViewMode.Decade) + else if (Mode == CalendarViewMode.Decade) { Mode = CalendarViewMode.Year; ContextCalendar.Year = e.Context.Year; @@ -489,6 +489,7 @@ public class CalendarView : TemplatedControl { throw new NotImplementedException(); } + UpdateHeaderButtons(); UpdateYearButtons(); } @@ -498,30 +499,35 @@ public class CalendarView : TemplatedControl if (Mode == CalendarViewMode.Century) { IsVisibleProperty.SetValue(true, _headerButton, _yearGrid); - IsVisibleProperty.SetValue(false, _yearButton, _monthButton, _monthGrid, _fastPreviousButton, _fastNextButton); - _headerButton?.SetValue(ContentControl.ContentProperty, ContextCalendar.StartYear + "-" + ContextCalendar.EndYear); + IsVisibleProperty.SetValue(false, _yearButton, _monthButton, _monthGrid, _fastPreviousButton, + _fastNextButton); + _headerButton?.SetValue(ContentControl.ContentProperty, + ContextCalendar.StartYear + "-" + ContextCalendar.EndYear); } else if (Mode == CalendarViewMode.Decade) { IsVisibleProperty.SetValue(true, _headerButton, _yearGrid); - IsVisibleProperty.SetValue(false, _yearButton, _monthButton, _monthGrid, _fastPreviousButton, _fastNextButton); - _headerButton?.SetValue(ContentControl.ContentProperty, ContextCalendar.StartYear + "-" + ContextCalendar.EndYear); + IsVisibleProperty.SetValue(false, _yearButton, _monthButton, _monthGrid, _fastPreviousButton, + _fastNextButton); + _headerButton?.SetValue(ContentControl.ContentProperty, + ContextCalendar.StartYear + "-" + ContextCalendar.EndYear); } else if (Mode == CalendarViewMode.Year) { IsVisibleProperty.SetValue(true, _headerButton, _yearGrid); - IsVisibleProperty.SetValue(false, _yearButton, _monthButton, _monthGrid, _fastPreviousButton, _fastNextButton); + IsVisibleProperty.SetValue(false, _yearButton, _monthButton, _monthGrid, _fastPreviousButton, + _fastNextButton); _headerButton?.SetValue(ContentControl.ContentProperty, ContextCalendar.Year); } else if (Mode == CalendarViewMode.Month) { IsVisibleProperty.SetValue(false, _headerButton, _yearGrid); - IsVisibleProperty.SetValue(true, _yearButton, _monthButton, _monthGrid, _fastPreviousButton, _fastNextButton); + IsVisibleProperty.SetValue(true, _yearButton, _monthButton, _monthGrid, _fastPreviousButton, + _fastNextButton); // _headerButton?.SetValue(ContentControl.ContentProperty, ContextCalendar.Year); _yearButton?.SetValue(ContentControl.ContentProperty, ContextCalendar.Year); _monthButton?.SetValue(ContentControl.ContentProperty, - DateTimeHelper.GetCurrentDateTimeFormatInfo().AbbreviatedMonthNames[ContextCalendar.Month ?? 0]); + DateTimeHelper.GetCurrentDateTimeFormatInfo().AbbreviatedMonthNames[ContextCalendar.Month-1 ?? 0]); } - } } \ No newline at end of file