diff --git a/src/DesktopMagic/BuiltInPlugins/DatePlugin.cs b/src/DesktopMagic/BuiltInPlugins/DatePlugin.cs index c0d6f35..8cdda27 100644 --- a/src/DesktopMagic/BuiltInPlugins/DatePlugin.cs +++ b/src/DesktopMagic/BuiltInPlugins/DatePlugin.cs @@ -4,13 +4,28 @@ using DesktopMagic.Api.Settings; using System; using System.Drawing; using System.Drawing.Text; +using System.Globalization; +using System.Linq; +using System.Text.RegularExpressions; namespace DesktopMagic.BuiltInPlugins; -internal class DatePlugin : Plugin +internal partial class DatePlugin : Plugin { - [Setting("short-date", "Short date")] - private readonly CheckBox shortDateCheckBox = new CheckBox(true); + [Setting("week-day-style", "Week Day Style")] + public ComboBox weekDayStyleComboBox = new ComboBox([.. Enum.GetValues().Select(e => e.ToString())]); + + [Setting("day-style", "Day Style")] + public ComboBox dayStyleComboBox = new ComboBox([.. Enum.GetValues().Select(e => e.ToString())]); + + [Setting("month-style", "Month Style")] + public ComboBox monthStyleComboBox = new ComboBox([.. Enum.GetValues().Select(e => e.ToString())]); + + [Setting("year-style", "Year Style")] + public ComboBox yearStyleComboBox = new ComboBox([.. Enum.GetValues().Select(e => e.ToString())]); + + [Setting("use-date-separators", "Use Date Separators")] + public CheckBox useDateSeparators = new CheckBox(true); private DateTime oldDateTime = DateTime.MinValue; private bool themeChanged = false; @@ -27,7 +42,64 @@ internal class DatePlugin : Plugin oldDateTime = DateTime.Now; themeChanged = false; - string date = shortDateCheckBox.Value ? DateTime.Now.ToShortDateString() : DateTime.Now.ToLongDateString(); + CultureInfo culture = CultureInfo.CurrentCulture; + DateTime dateTime = DateTime.Now; + + DateTimeFormatInfo dtf = culture.DateTimeFormat; + + string pattern = dtf.LongDatePattern; + + WeekdayStyle weekdayStyle = Enum.Parse(weekDayStyleComboBox.Value); + DayStyle dayStyle = Enum.Parse(dayStyleComboBox.Value); + MonthStyle monthStyle = Enum.Parse(monthStyleComboBox.Value); + YearStyle yearStyle = Enum.Parse(yearStyleComboBox.Value); + + // Weekday: ddd, dddd + pattern = WeekdayRegex().Replace(pattern, weekdayStyle switch + { + WeekdayStyle.Long => "dddd", + WeekdayStyle.Short => "ddd", + _ => "" + }); + + // Day: d, dd + pattern = DayRegex().Replace(pattern, dayStyle switch + { + DayStyle.Numeric => "dd", + _ => "" + }); + + // Month: M, MM, MMM, MMMM + pattern = MonthRegex().Replace(pattern, monthStyle switch + { + MonthStyle.Long => "MMMM", + MonthStyle.Short => "MMM", + MonthStyle.Numeric => "MM", + _ => "" + }); + + // Year: yy, yyyy + pattern = YearRegex().Replace(pattern, yearStyle switch + { + YearStyle.FourDigit => "yyyy", + YearStyle.TwoDigit => "yy", + _ => "" + }); + + // Clean up leftover punctuation/spacing + pattern = CleanupRegex().Replace(pattern, " ").Trim(); + + if (string.IsNullOrWhiteSpace(pattern)) + { + pattern = dtf.LongDatePattern; + } + + string date = dateTime.ToString(pattern, culture); + + if (useDateSeparators.Value) + { + date = DateSeparatorsRegex().Replace(date, dtf.DateSeparator); + } using Font font = new Font(Application.Theme.Font, 200); @@ -59,4 +131,27 @@ internal class DatePlugin : Plugin { themeChanged = true; } -} \ No newline at end of file + + [GeneratedRegex(@"d{3,4}")] + private static partial Regex WeekdayRegex(); + + [GeneratedRegex(@"\b(?