102 lines
3.1 KiB
C#
102 lines
3.1 KiB
C#
using System.Globalization;
|
||
using Microsoft.Maui.Controls.Shapes;
|
||
|
||
namespace CORP_MAUI.Controls;
|
||
|
||
public partial class DesktopCalendarPanel : ContentView
|
||
{
|
||
private DateTime _currentMonth;
|
||
private static readonly CultureInfo RuCulture = new("ru-RU");
|
||
|
||
public DesktopCalendarPanel()
|
||
{
|
||
InitializeComponent();
|
||
_currentMonth = DateTime.Today;
|
||
UpdateCalendar();
|
||
}
|
||
|
||
private void UpdateCalendar()
|
||
{
|
||
MonthYearLabel.Text = _currentMonth.ToString("MMMM yyyy", RuCulture);
|
||
MonthYearLabel.Text = char.ToUpper(MonthYearLabel.Text[0]) + MonthYearLabel.Text[1..];
|
||
CalendarGrid.Children.Clear();
|
||
|
||
var firstDay = new DateTime(_currentMonth.Year, _currentMonth.Month, 1);
|
||
int startColumn = ((int)firstDay.DayOfWeek + 6) % 7;
|
||
int daysInMonth = DateTime.DaysInMonth(_currentMonth.Year, _currentMonth.Month);
|
||
|
||
int row = 0;
|
||
int col = startColumn;
|
||
|
||
for (int day = 1; day <= daysInMonth; day++)
|
||
{
|
||
var dayDate = new DateTime(_currentMonth.Year, _currentMonth.Month, day);
|
||
bool isToday = dayDate.Date == DateTime.Today;
|
||
bool isWeekend = col >= 5;
|
||
|
||
var dayLabel = new Label
|
||
{
|
||
Text = day.ToString(),
|
||
FontSize = 14,
|
||
HorizontalOptions = LayoutOptions.Center,
|
||
VerticalOptions = LayoutOptions.Center,
|
||
TextColor = isToday ? Colors.White : (isWeekend ? Color.FromArgb("#0D6EFD") : Color.FromArgb("#333333"))
|
||
};
|
||
|
||
View dayView;
|
||
if (isToday)
|
||
{
|
||
var border = new Border
|
||
{
|
||
StrokeShape = new RoundRectangle { CornerRadius = 17 },
|
||
BackgroundColor = Color.FromArgb("#0D6EFD"),
|
||
HeightRequest = 34,
|
||
WidthRequest = 34,
|
||
Content = dayLabel
|
||
};
|
||
dayView = border;
|
||
}
|
||
else
|
||
{
|
||
dayView = dayLabel;
|
||
}
|
||
|
||
Grid.SetRow(dayView, row);
|
||
Grid.SetColumn(dayView, col);
|
||
CalendarGrid.Children.Add(dayView);
|
||
|
||
col++;
|
||
if (col > 6) { col = 0; row++; }
|
||
}
|
||
|
||
UpdateWeekRange();
|
||
}
|
||
|
||
private void UpdateWeekRange()
|
||
{
|
||
var today = DateTime.Today;
|
||
int diff = ((int)today.DayOfWeek + 6) % 7;
|
||
var weekStart = today.AddDays(-diff);
|
||
var weekEnd = weekStart.AddDays(6);
|
||
WeekRangeLabel.Text = $"{weekStart:d MMMM} – {weekEnd:d MMMM}";
|
||
}
|
||
|
||
private void OnPreviousMonthClicked(object? sender, EventArgs e)
|
||
{
|
||
_currentMonth = _currentMonth.AddMonths(-1);
|
||
UpdateCalendar();
|
||
}
|
||
|
||
private void OnNextMonthClicked(object? sender, EventArgs e)
|
||
{
|
||
_currentMonth = _currentMonth.AddMonths(1);
|
||
UpdateCalendar();
|
||
}
|
||
|
||
private void OnTodayClicked(object? sender, EventArgs e)
|
||
{
|
||
_currentMonth = DateTime.Today;
|
||
UpdateCalendar();
|
||
}
|
||
}
|