Add custom highligh style for single calendar event

This commit is contained in:
d.piccinini 2023-06-22 12:12:43 +02:00 committed by Patrik Svensson
parent 32361d3f15
commit 32384f7b8d
3 changed files with 34 additions and 20 deletions

View File

@ -10,10 +10,11 @@ public static class CalendarExtensions
/// </summary>
/// <param name="calendar">The calendar to add the calendar event to.</param>
/// <param name="date">The calendar event date.</param>
/// <param name="customEventHighlightStyle">The calendar event custom highlight style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Calendar AddCalendarEvent(this Calendar calendar, DateTime date)
public static Calendar AddCalendarEvent(this Calendar calendar, DateTime date, Style? customEventHighlightStyle = null)
{
return AddCalendarEvent(calendar, string.Empty, date.Year, date.Month, date.Day);
return AddCalendarEvent(calendar, string.Empty, date.Year, date.Month, date.Day, customEventHighlightStyle);
}
/// <summary>
@ -22,10 +23,11 @@ public static class CalendarExtensions
/// <param name="calendar">The calendar to add the calendar event to.</param>
/// <param name="description">The calendar event description.</param>
/// <param name="date">The calendar event date.</param>
/// <param name="customEventHighlightStyle">The calendar event custom highlight style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Calendar AddCalendarEvent(this Calendar calendar, string description, DateTime date)
public static Calendar AddCalendarEvent(this Calendar calendar, string description, DateTime date, Style? customEventHighlightStyle = null)
{
return AddCalendarEvent(calendar, description, date.Year, date.Month, date.Day);
return AddCalendarEvent(calendar, description, date.Year, date.Month, date.Day, customEventHighlightStyle);
}
/// <summary>
@ -35,10 +37,11 @@ public static class CalendarExtensions
/// <param name="year">The year of the calendar event.</param>
/// <param name="month">The month of the calendar event.</param>
/// <param name="day">The day of the calendar event.</param>
/// <param name="customEventHighlightStyle">The calendar event custom highlight style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Calendar AddCalendarEvent(this Calendar calendar, int year, int month, int day)
public static Calendar AddCalendarEvent(this Calendar calendar, int year, int month, int day, Style? customEventHighlightStyle = null)
{
return AddCalendarEvent(calendar, string.Empty, year, month, day);
return AddCalendarEvent(calendar, string.Empty, year, month, day, customEventHighlightStyle);
}
/// <summary>
@ -49,15 +52,16 @@ public static class CalendarExtensions
/// <param name="year">The year of the calendar event.</param>
/// <param name="month">The month of the calendar event.</param>
/// <param name="day">The day of the calendar event.</param>
/// <param name="customEventHighlightStyle">The calendar event custom highlight style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Calendar AddCalendarEvent(this Calendar calendar, string description, int year, int month, int day)
public static Calendar AddCalendarEvent(this Calendar calendar, string description, int year, int month, int day, Style? customEventHighlightStyle = null)
{
if (calendar is null)
{
throw new ArgumentNullException(nameof(calendar));
}
calendar.CalendarEvents.Add(new CalendarEvent(description, year, month, day));
calendar.CalendarEvents.Add(new CalendarEvent(description, year, month, day, customEventHighlightStyle));
return calendar;
}
@ -65,7 +69,7 @@ public static class CalendarExtensions
/// Sets the calendar's highlight <see cref="Style"/>.
/// </summary>
/// <param name="calendar">The calendar.</param>
/// <param name="style">The highlight style.</param>
/// <param name="style">The default highlight style.</param>
/// <returns>The same instance so that multiple calls can be chained.</returns>
public static Calendar HighlightStyle(this Calendar calendar, Style? style)
{

View File

@ -195,10 +195,11 @@ public sealed class Calendar : JustInTimeRenderable, IHasCulture, IHasTableBorde
while (currentDay <= daysInMonth)
{
if (weekdays[currentDay - 1] == weekday)
{
if (_calendarEvents.Any(e => e.Month == Month && e.Day == currentDay))
{
var todayEvent = _calendarEvents.LastOrDefault(e => e.Month == Month && e.Day == currentDay);
if (todayEvent != null)
{
row.Add(new Markup(currentDay.ToString(CultureInfo.InvariantCulture) + "*", _highlightStyle));
row.Add(new Markup(currentDay.ToString(CultureInfo.InvariantCulture) + "*", todayEvent.CustomHighlightStyle ?? _highlightStyle));
}
else
{

View File

@ -25,29 +25,38 @@ public sealed class CalendarEvent
/// </summary>
public int Day { get; }
/// <summary>
/// Gets the custom highlight style of the calendar event.
/// </summary>
public Style? CustomHighlightStyle { get; }
/// <summary>
/// Initializes a new instance of the <see cref="CalendarEvent"/> class.
/// </summary>
/// <param name="year">The year of the calendar event.</param>
/// <param name="month">The month of the calendar event.</param>
/// <param name="day">The day of the calendar event.</param>
public CalendarEvent(int year, int month, int day)
: this(string.Empty, year, month, day)
/// <param name="day">The day of the calendar event.</param>
/// <param name="customHighlightStyle">The custom highlight style of the calendar event.</param>
public CalendarEvent(int year, int month, int day, Style? customHighlightStyle = null)
: this(string.Empty, year, month, day, customHighlightStyle)
{
}
}
/// <summary>
/// Initializes a new instance of the <see cref="CalendarEvent"/> class.
/// </summary>
/// <param name="description">The calendar event description.</param>
/// <param name="year">The year of the calendar event.</param>
/// <param name="month">The month of the calendar event.</param>
/// <param name="day">The day of the calendar event.</param>
public CalendarEvent(string description, int year, int month, int day)
/// <param name="day">The day of the calendar event.</param>
/// <param name="customHighlightStyle">The custom highlight style of the calendar event.</param>
public CalendarEvent(string description, int year, int month, int day, Style? customHighlightStyle = null)
{
Description = description ?? string.Empty;
Year = year;
Month = month;
Day = day;
Day = day;
CustomHighlightStyle = customHighlightStyle;
}
}