feat: wip.
This commit is contained in:
@@ -31,54 +31,31 @@ public class CalendarDisplayControl: TemplatedControl
|
|||||||
|
|
||||||
private CalendarMonthView? _monthView;
|
private CalendarMonthView? _monthView;
|
||||||
private CalendarYearView? _yearView;
|
private CalendarYearView? _yearView;
|
||||||
|
// Year button only shows the year in month mode.
|
||||||
private Button? _yearButton;
|
private Button? _yearButton;
|
||||||
|
// Month button only shows the month in month mode.
|
||||||
private Button? _monthButton;
|
private Button? _monthButton;
|
||||||
|
// Header button shows year in year mode, and year range in higher mode.
|
||||||
private Button? _headerButton;
|
private Button? _headerButton;
|
||||||
|
|
||||||
public event EventHandler<CalendarDayButtonEventArgs>? OnDateSelected;
|
public event EventHandler<CalendarDayButtonEventArgs>? OnDateSelected;
|
||||||
public event EventHandler<CalendarDayButtonEventArgs>? OnDatePreviewed;
|
public event EventHandler<CalendarDayButtonEventArgs>? OnDatePreviewed;
|
||||||
|
|
||||||
|
|
||||||
public static readonly StyledProperty<DateTime> SelectedDateProperty = AvaloniaProperty.Register<CalendarDisplayControl, DateTime>(nameof(SelectedDate), DateTime.Now);
|
|
||||||
public DateTime SelectedDate
|
|
||||||
{
|
|
||||||
get => GetValue(SelectedDateProperty);
|
|
||||||
set => SetValue(SelectedDateProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<DayOfWeek> FirstDayOfWeekProperty =
|
public static readonly StyledProperty<bool> IsTodayHighlightedProperty =
|
||||||
AvaloniaProperty.Register<CalendarDisplayControl, DayOfWeek>(nameof(FirstDayOfWeek),
|
DatePickerBase.IsTodayHighlightedProperty.AddOwner<CalendarDisplayControl>();
|
||||||
defaultValue: DateTimeHelper.GetCurrentDateTimeFormatInfo().FirstDayOfWeek);
|
|
||||||
public DayOfWeek FirstDayOfWeek
|
|
||||||
{
|
|
||||||
get => GetValue(FirstDayOfWeekProperty);
|
|
||||||
set => SetValue(FirstDayOfWeekProperty, value);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<bool> IsTodayHighlightedProperty = AvaloniaProperty.Register<CalendarDisplayControl, bool>(nameof(IsTodayHighlighted), true);
|
|
||||||
public bool IsTodayHighlighted
|
public bool IsTodayHighlighted
|
||||||
{
|
{
|
||||||
get => GetValue(IsTodayHighlightedProperty);
|
get => GetValue(IsTodayHighlightedProperty);
|
||||||
set => SetValue(IsTodayHighlightedProperty, value);
|
set => SetValue(IsTodayHighlightedProperty, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static readonly StyledProperty<AvaloniaList<DateRange>?> BlackoutDatesProperty =
|
public static readonly StyledProperty<DayOfWeek> FirstDayOfWeekProperty =
|
||||||
AvaloniaProperty.Register<CalendarDisplayControl, AvaloniaList<DateRange>?>(
|
DatePickerBase.FirstDayOfWeekProperty.AddOwner<CalendarDisplayControl>();
|
||||||
nameof(BlackoutDates));
|
|
||||||
|
public DayOfWeek FirstDayOfWeek
|
||||||
public AvaloniaList<DateRange>? BlackoutDates
|
|
||||||
{
|
{
|
||||||
get => GetValue(BlackoutDatesProperty);
|
get => GetValue(FirstDayOfWeekProperty);
|
||||||
set => SetValue(BlackoutDatesProperty, value);
|
set => SetValue(FirstDayOfWeekProperty, value);
|
||||||
}
|
|
||||||
|
|
||||||
public static readonly StyledProperty<IDateSelector?> BlackoutDateRuleProperty = AvaloniaProperty.Register<CalendarDisplayControl, IDateSelector?>(
|
|
||||||
nameof(BlackoutDateRule));
|
|
||||||
|
|
||||||
public IDateSelector? BlackoutDateRule
|
|
||||||
{
|
|
||||||
get => GetValue(BlackoutDateRuleProperty);
|
|
||||||
set => SetValue(BlackoutDateRuleProperty, value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private bool _isMonthMode = true;
|
private bool _isMonthMode = true;
|
||||||
@@ -92,16 +69,13 @@ public class CalendarDisplayControl: TemplatedControl
|
|||||||
set => SetAndRaise(IsMonthModeProperty, ref _isMonthMode, value);
|
set => SetAndRaise(IsMonthModeProperty, ref _isMonthMode, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
internal DateTime? StartDate;
|
|
||||||
internal DateTime? EndDate;
|
|
||||||
|
|
||||||
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
|
||||||
{
|
{
|
||||||
base.OnApplyTemplate(e);
|
base.OnApplyTemplate(e);
|
||||||
if (_monthView is not null)
|
if (_monthView is not null)
|
||||||
{
|
{
|
||||||
_monthView.OnDateSelected -= OnDateSelected;
|
_monthView.OnDateSelected -= OnMonthViewDateSelected;
|
||||||
_monthView.OnDatePreviewed -= OnDatePreviewed;
|
_monthView.OnDatePreviewed -= OnMonthViewDatePreviewed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_yearView is not null)
|
if (_yearView is not null)
|
||||||
@@ -118,8 +92,8 @@ public class CalendarDisplayControl: TemplatedControl
|
|||||||
_headerButton = e.NameScope.Find<Button>(PART_HeaderButton);
|
_headerButton = e.NameScope.Find<Button>(PART_HeaderButton);
|
||||||
if(_monthView is not null)
|
if(_monthView is not null)
|
||||||
{
|
{
|
||||||
_monthView.OnDateSelected += OnDateSelected;
|
_monthView.OnDateSelected += OnMonthViewDateSelected;
|
||||||
_monthView.OnDatePreviewed += OnDatePreviewed;
|
_monthView.OnDatePreviewed += OnMonthViewDatePreviewed;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_yearView is not null)
|
if (_yearView is not null)
|
||||||
@@ -131,6 +105,11 @@ public class CalendarDisplayControl: TemplatedControl
|
|||||||
Button.ClickEvent.AddHandler(OnHeaderButtonClick, _headerButton);
|
Button.ClickEvent.AddHandler(OnHeaderButtonClick, _headerButton);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Rule:
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="sender"></param>
|
||||||
|
/// <param name="e"></param>
|
||||||
private void OnHeaderButtonClick(object sender, RoutedEventArgs e)
|
private void OnHeaderButtonClick(object sender, RoutedEventArgs e)
|
||||||
{
|
{
|
||||||
if (_yearView?.Mode == CalendarYearViewMode.Month)
|
if (_yearView?.Mode == CalendarYearViewMode.Month)
|
||||||
|
|||||||
@@ -16,10 +16,10 @@ namespace Ursa.Controls;
|
|||||||
public class CalendarMonthView : TemplatedControl
|
public class CalendarMonthView : TemplatedControl
|
||||||
{
|
{
|
||||||
public const string PART_Grid = "PART_Grid";
|
public const string PART_Grid = "PART_Grid";
|
||||||
|
private const string ShortestDayName = "ShortestDayName";
|
||||||
|
|
||||||
public static readonly StyledProperty<DayOfWeek> FirstDayOfWeekProperty =
|
public static readonly StyledProperty<DayOfWeek> FirstDayOfWeekProperty =
|
||||||
AvaloniaProperty.Register<CalendarMonthView, DayOfWeek>(
|
DatePickerBase.FirstDayOfWeekProperty.AddOwner<CalendarMonthView>();
|
||||||
nameof(FirstDayOfWeek));
|
|
||||||
|
|
||||||
private readonly System.Globalization.Calendar _calendar = new GregorianCalendar();
|
private readonly System.Globalization.Calendar _calendar = new GregorianCalendar();
|
||||||
|
|
||||||
@@ -56,12 +56,14 @@ public class CalendarMonthView : TemplatedControl
|
|||||||
base.OnApplyTemplate(e);
|
base.OnApplyTemplate(e);
|
||||||
_grid = e.NameScope.Find<Grid>(PART_Grid);
|
_grid = e.NameScope.Find<Grid>(PART_Grid);
|
||||||
GenerateGridElements();
|
GenerateGridElements();
|
||||||
SetDayButtons(DateTime.Today);
|
SetDayButtons(ContextDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnDayOfWeekChanged(AvaloniaPropertyChangedEventArgs<DayOfWeek> args)
|
private void OnDayOfWeekChanged(AvaloniaPropertyChangedEventArgs<DayOfWeek> args)
|
||||||
{
|
{
|
||||||
// throw new NotImplementedException();
|
// throw new NotImplementedException();
|
||||||
|
UpdateGridElements();
|
||||||
|
SetDayButtons(ContextDate);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -75,7 +77,7 @@ public class CalendarMonthView : TemplatedControl
|
|||||||
for (var i = 0; i < 7; i++)
|
for (var i = 0; i < 7; i++)
|
||||||
{
|
{
|
||||||
var d = (dayOfWeek + i) % DateTimeHelper.NumberOfDaysPerWeek;
|
var d = (dayOfWeek + i) % DateTimeHelper.NumberOfDaysPerWeek;
|
||||||
var cell = new TextBlock { Text = info.ShortestDayNames[d] };
|
var cell = new TextBlock { Text = info.ShortestDayNames[d], Tag = ShortestDayName };
|
||||||
cell.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Center);
|
cell.SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Center);
|
||||||
cell.SetValue(Grid.RowProperty, 0);
|
cell.SetValue(Grid.RowProperty, 0);
|
||||||
cell.SetValue(Grid.ColumnProperty, i);
|
cell.SetValue(Grid.ColumnProperty, i);
|
||||||
@@ -97,6 +99,27 @@ public class CalendarMonthView : TemplatedControl
|
|||||||
_grid?.Children.AddRange(children);
|
_grid?.Children.AddRange(children);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void UpdateGridElements()
|
||||||
|
{
|
||||||
|
var count = 7 + 7 * 7;
|
||||||
|
// var children = new List<Control>(count);
|
||||||
|
var dayOfWeek = (int)FirstDayOfWeek;
|
||||||
|
var info = DateTimeHelper.GetCurrentDateTimeFormatInfo();
|
||||||
|
var textblocks = _grid?.Children.Where(a => a is TextBlock { Tag: ShortestDayName }).ToList();
|
||||||
|
if (textblocks is not null)
|
||||||
|
{
|
||||||
|
for (var i = 0; i < 7; i++)
|
||||||
|
{
|
||||||
|
var d = (dayOfWeek + i) % DateTimeHelper.NumberOfDaysPerWeek;
|
||||||
|
textblocks[i].SetValue(TextBlock.TextProperty, info.ShortestDayNames[d]);
|
||||||
|
textblocks[i].SetValue(HorizontalAlignmentProperty, HorizontalAlignment.Center);
|
||||||
|
textblocks[i].SetValue(Grid.RowProperty, 0);
|
||||||
|
textblocks[i].SetValue(Grid.ColumnProperty, i);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
SetDayButtons(ContextDate);
|
||||||
|
}
|
||||||
|
|
||||||
private void OnCellDatePreviewed(object sender, CalendarDayButtonEventArgs e)
|
private void OnCellDatePreviewed(object sender, CalendarDayButtonEventArgs e)
|
||||||
{
|
{
|
||||||
OnDatePreviewed?.Invoke(sender, e);
|
OnDatePreviewed?.Invoke(sender, e);
|
||||||
@@ -179,40 +202,7 @@ public class CalendarMonthView : TemplatedControl
|
|||||||
if (d == startDate && d == endDate) button.IsSelected = true;
|
if (d == startDate && d == endDate) button.IsSelected = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void MarkPreview(DateTime? start, DateTime? end)
|
|
||||||
{
|
|
||||||
if (_grid?.Children is null) return;
|
|
||||||
foreach (var child in _grid.Children)
|
|
||||||
{
|
|
||||||
if (child is not CalendarDayButton { DataContext: DateTime d } button) continue;
|
|
||||||
if (d == start)
|
|
||||||
{
|
|
||||||
button.IsPreviewStartDate = true;
|
|
||||||
button.IsPreviewEndDate = false;
|
|
||||||
button.IsInRange = false;
|
|
||||||
}
|
|
||||||
else if (d == end)
|
|
||||||
{
|
|
||||||
button.IsPreviewEndDate = true;
|
|
||||||
button.IsPreviewStartDate = false;
|
|
||||||
button.IsInRange = false;
|
|
||||||
}
|
|
||||||
else if (d > start && d < end)
|
|
||||||
{
|
|
||||||
button.IsInRange = true;
|
|
||||||
button.IsPreviewStartDate = false;
|
|
||||||
button.IsPreviewEndDate = false;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
button.IsPreviewStartDate = false;
|
|
||||||
button.IsPreviewEndDate = false;
|
|
||||||
button.IsInRange = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void ClearSelection()
|
public void ClearSelection()
|
||||||
{
|
{
|
||||||
if (_grid?.Children is null) return;
|
if (_grid?.Children is null) return;
|
||||||
|
|||||||
@@ -100,11 +100,11 @@ public class CalendarYearView: TemplatedControl
|
|||||||
child.SetValues(CalendarYearViewMode.Month, contextDate, month: i);
|
child.SetValues(CalendarYearViewMode.Month, contextDate, month: i);
|
||||||
break;
|
break;
|
||||||
case CalendarYearViewMode.Year:
|
case CalendarYearViewMode.Year:
|
||||||
child.SetValues(CalendarYearViewMode.Year, contextDate, year: ContextDate.Year + i);
|
child.SetValues(CalendarYearViewMode.Year, contextDate, year: ContextDate.Year / 10 * 10 + i - 1);
|
||||||
break;
|
break;
|
||||||
case CalendarYearViewMode.YearRange:
|
case CalendarYearViewMode.YearRange:
|
||||||
var startYear = ContextDate.Year - 1;
|
var startYear = (ContextDate.Year / 10 + i - 1) * 10;
|
||||||
var endYear = ContextDate.Year + 10;
|
var endYear = (ContextDate.Year / 10 + i - 1) * 10 + 10;
|
||||||
child.SetValues(CalendarYearViewMode.YearRange, contextDate, startYear: startYear, endYear: endYear);
|
child.SetValues(CalendarYearViewMode.YearRange, contextDate, startYear: startYear, endYear: endYear);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
namespace Ursa.Controls;
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
public class DatePicker: TemplatedControl
|
public class DatePicker: DatePickerBase
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
46
src/Ursa/Controls/DateTimePicker/DatePickerBase.cs
Normal file
46
src/Ursa/Controls/DateTimePicker/DatePickerBase.cs
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Collections;
|
||||||
|
using Avalonia.Controls.Primitives;
|
||||||
|
using Avalonia.Controls.Templates;
|
||||||
|
|
||||||
|
namespace Ursa.Controls;
|
||||||
|
|
||||||
|
public class DatePickerBase: TemplatedControl
|
||||||
|
{
|
||||||
|
public static readonly StyledProperty<AvaloniaList<DateRange>> BlackoutDatesProperty =
|
||||||
|
AvaloniaProperty.Register<DatePickerBase, AvaloniaList<DateRange>>(nameof(BlackoutDates));
|
||||||
|
|
||||||
|
public AvaloniaList<DateRange> BlackoutDates
|
||||||
|
{
|
||||||
|
get => GetValue(BlackoutDatesProperty);
|
||||||
|
set => SetValue(BlackoutDatesProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly StyledProperty<IDateSelector?> BlackoutDateRuleProperty =
|
||||||
|
AvaloniaProperty.Register<DatePickerBase, IDateSelector?>(nameof(BlackoutDateRule));
|
||||||
|
|
||||||
|
public IDateSelector? BlackoutDateRule
|
||||||
|
{
|
||||||
|
get => GetValue(BlackoutDateRuleProperty);
|
||||||
|
set => SetValue(BlackoutDateRuleProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly StyledProperty<DayOfWeek> FirstDayOfWeekProperty =
|
||||||
|
AvaloniaProperty.Register<DatePickerBase, DayOfWeek>(
|
||||||
|
nameof(FirstDayOfWeek), DateTimeHelper.GetCurrentDateTimeFormatInfo().FirstDayOfWeek);
|
||||||
|
|
||||||
|
public DayOfWeek FirstDayOfWeek
|
||||||
|
{
|
||||||
|
get => GetValue(FirstDayOfWeekProperty);
|
||||||
|
set => SetValue(FirstDayOfWeekProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static readonly StyledProperty<bool> IsTodayHighlightedProperty =
|
||||||
|
AvaloniaProperty.Register<DatePickerBase, bool>(nameof(IsTodayHighlighted), true);
|
||||||
|
public bool IsTodayHighlighted
|
||||||
|
{
|
||||||
|
get => GetValue(IsTodayHighlightedProperty);
|
||||||
|
set => SetValue(IsTodayHighlightedProperty, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -2,14 +2,14 @@
|
|||||||
|
|
||||||
public interface IDateSelector
|
public interface IDateSelector
|
||||||
{
|
{
|
||||||
public bool IsValid(DateTime? date);
|
public bool Match(DateTime? date);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class WeekendDateSelector: IDateSelector
|
public class WeekendDateSelector: IDateSelector
|
||||||
{
|
{
|
||||||
public static WeekendDateSelector Instance { get; } = new WeekendDateSelector();
|
public static WeekendDateSelector Instance { get; } = new WeekendDateSelector();
|
||||||
|
|
||||||
public bool IsValid(DateTime? date)
|
public bool Match(DateTime? date)
|
||||||
{
|
{
|
||||||
if (date is null) return false;
|
if (date is null) return false;
|
||||||
return date.Value.DayOfWeek == DayOfWeek.Saturday || date.Value.DayOfWeek == DayOfWeek.Sunday;
|
return date.Value.DayOfWeek == DayOfWeek.Saturday || date.Value.DayOfWeek == DayOfWeek.Sunday;
|
||||||
|
|||||||
Reference in New Issue
Block a user