feat: disabled date related feature.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Collections;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Interactivity;
|
||||
|
||||
@@ -12,8 +13,10 @@ public class Calendar: TemplatedControl
|
||||
get => GetValue(SelectedDateProperty);
|
||||
set => SetValue(SelectedDateProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<DayOfWeek> FirstDayOfWeekProperty = AvaloniaProperty.Register<Calendar, DayOfWeek>(nameof(FirstDayOfWeek), DayOfWeek.Sunday);
|
||||
|
||||
public static readonly StyledProperty<DayOfWeek> FirstDayOfWeekProperty =
|
||||
AvaloniaProperty.Register<Calendar, DayOfWeek>(nameof(FirstDayOfWeek),
|
||||
defaultValue: DateTimeHelper.GetCurrentDateTimeFormatInfo().FirstDayOfWeek);
|
||||
public DayOfWeek FirstDayOfWeek
|
||||
{
|
||||
get => GetValue(FirstDayOfWeekProperty);
|
||||
@@ -26,4 +29,23 @@ public class Calendar: TemplatedControl
|
||||
get => GetValue(IsTodayHighlightedProperty);
|
||||
set => SetValue(IsTodayHighlightedProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<AvaloniaList<DateRange>?> DisabledDatesProperty =
|
||||
AvaloniaProperty.Register<Calendar, AvaloniaList<DateRange>?>(
|
||||
nameof(DisabledDates));
|
||||
|
||||
public AvaloniaList<DateRange>? DisabledDates
|
||||
{
|
||||
get => GetValue(DisabledDatesProperty);
|
||||
set => SetValue(DisabledDatesProperty, value);
|
||||
}
|
||||
|
||||
public static readonly StyledProperty<IDateSelector?> DisabledDateRuleProperty = AvaloniaProperty.Register<Calendar, IDateSelector?>(
|
||||
nameof(DisabledDateRule));
|
||||
|
||||
public IDateSelector? DisabledDateRule
|
||||
{
|
||||
get => GetValue(DisabledDateRuleProperty);
|
||||
set => SetValue(DisabledDateRuleProperty, value);
|
||||
}
|
||||
}
|
||||
8
src/Ursa/Controls/DateTimePicker/CalendarDayButton.cs
Normal file
8
src/Ursa/Controls/DateTimePicker/CalendarDayButton.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using Avalonia.Controls;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public class CalendarDayButton: Button
|
||||
{
|
||||
|
||||
}
|
||||
36
src/Ursa/Controls/DateTimePicker/DateRange.cs
Normal file
36
src/Ursa/Controls/DateTimePicker/DateRange.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
namespace Ursa.Controls;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a date range. It can be a single day or a range of days. The range is inclusive.
|
||||
/// </summary>
|
||||
public sealed record DateRange
|
||||
{
|
||||
public DateRange(DateTime day)
|
||||
{
|
||||
Start = day.Date;
|
||||
End = day.Date;
|
||||
}
|
||||
|
||||
public DateRange(DateTime start, DateTime end)
|
||||
{
|
||||
if (DateTime.Compare(end, start) >= 0)
|
||||
{
|
||||
Start = start.Date;
|
||||
End = end.Date;
|
||||
}
|
||||
else
|
||||
{
|
||||
Start = start.Date;
|
||||
End = start.Date;
|
||||
}
|
||||
}
|
||||
|
||||
public DateTime Start { get; private set; }
|
||||
public DateTime End { get; private set; }
|
||||
|
||||
public bool Contains(DateTime? date)
|
||||
{
|
||||
if (date is null) return false;
|
||||
return date >= Start && date <= End;
|
||||
}
|
||||
}
|
||||
17
src/Ursa/Controls/DateTimePicker/DateTimeHelper.cs
Normal file
17
src/Ursa/Controls/DateTimePicker/DateTimeHelper.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
using System.Globalization;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
internal static class DateTimeHelper
|
||||
{
|
||||
public static DateTimeFormatInfo GetCurrentDateTimeFormatInfo()
|
||||
{
|
||||
if (CultureInfo.CurrentCulture.Calendar is GregorianCalendar) return CultureInfo.CurrentCulture.DateTimeFormat;
|
||||
System.Globalization.Calendar? calendar =
|
||||
CultureInfo.CurrentCulture.OptionalCalendars.OfType<GregorianCalendar>().FirstOrDefault();
|
||||
string cultureName = calendar is null ? CultureInfo.InvariantCulture.Name : CultureInfo.CurrentCulture.Name;
|
||||
var dt = new CultureInfo(cultureName).DateTimeFormat;
|
||||
dt.Calendar = calendar ?? new GregorianCalendar();
|
||||
return dt;
|
||||
}
|
||||
}
|
||||
17
src/Ursa/Controls/DateTimePicker/IDateSelector.cs
Normal file
17
src/Ursa/Controls/DateTimePicker/IDateSelector.cs
Normal file
@@ -0,0 +1,17 @@
|
||||
namespace Ursa.Controls;
|
||||
|
||||
public interface IDateSelector
|
||||
{
|
||||
public bool IsValid(DateTime? date);
|
||||
}
|
||||
|
||||
public class WeekendDateSelector: IDateSelector
|
||||
{
|
||||
public static WeekendDateSelector Instance { get; } = new WeekendDateSelector();
|
||||
|
||||
public bool IsValid(DateTime? date)
|
||||
{
|
||||
if (date is null) return false;
|
||||
return date.Value.DayOfWeek == DayOfWeek.Saturday || date.Value.DayOfWeek == DayOfWeek.Sunday;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user