Calendar stuff.

This commit is contained in:
Burak Kaan Köse
2026-02-13 03:09:13 +01:00
parent e936c431a2
commit 884f000058
22 changed files with 470 additions and 115 deletions
@@ -1,7 +1,8 @@
using System.Linq;
using System.Linq;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Automation.Peers;
using Microsoft.UI.Xaml.Controls;
using Wino.Core.Domain.Enums;
using Wino.Mail.WinUI.Controls.CalendarFlipView;
namespace Wino.Calendar.Controls;
@@ -11,27 +12,56 @@ namespace Wino.Calendar.Controls;
/// </summary>
public partial class CustomCalendarFlipView : FlipView
{
private const string PART_PreviousButton = "PreviousButtonHorizontal";
private const string PART_NextButton = "NextButtonHorizontal";
private const string PART_PreviousButtonHorizontal = "PreviousButtonHorizontal";
private const string PART_NextButtonHorizontal = "NextButtonHorizontal";
private const string PART_PreviousButtonVertical = "PreviousButtonVertical";
private const string PART_NextButtonVertical = "NextButtonVertical";
private Button? PreviousButton;
private Button? NextButton;
public static readonly DependencyProperty DisplayTypeProperty = DependencyProperty.Register(
nameof(DisplayType),
typeof(CalendarDisplayType),
typeof(CustomCalendarFlipView),
new PropertyMetadata(CalendarDisplayType.Week));
public CalendarDisplayType DisplayType
{
get => (CalendarDisplayType)GetValue(DisplayTypeProperty);
set => SetValue(DisplayTypeProperty, value);
}
private Button? PreviousButtonHorizontal;
private Button? NextButtonHorizontal;
private Button? PreviousButtonVertical;
private Button? NextButtonVertical;
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
PreviousButton = (Button)GetTemplateChild(PART_PreviousButton);
NextButton = (Button)GetTemplateChild(PART_NextButton);
PreviousButtonHorizontal = GetTemplateChild(PART_PreviousButtonHorizontal) as Button;
NextButtonHorizontal = GetTemplateChild(PART_NextButtonHorizontal) as Button;
PreviousButtonVertical = GetTemplateChild(PART_PreviousButtonVertical) as Button;
NextButtonVertical = GetTemplateChild(PART_NextButtonVertical) as Button;
// Hide navigation buttons
PreviousButton.Opacity = NextButton.Opacity = 0;
PreviousButton.IsHitTestVisible = NextButton.IsHitTestVisible = false;
HideButton(PreviousButtonHorizontal);
HideButton(NextButtonHorizontal);
HideButton(PreviousButtonVertical);
HideButton(NextButtonVertical);
this.SelectionChanged += FlipViewSelectionChanged;
SelectionChanged += FlipViewSelectionChanged;
}
private void FlipViewSelectionChanged(object sender, SelectionChangedEventArgs e) => OnSelectedItemChanged(e.RemovedItems.FirstOrDefault(), e.AddedItems.FirstOrDefault());
private static void HideButton(Button? button)
{
if (button == null) return;
button.Opacity = 0;
button.IsHitTestVisible = false;
}
private void FlipViewSelectionChanged(object sender, SelectionChangedEventArgs e)
=> OnSelectedItemChanged(e.RemovedItems.FirstOrDefault(), e.AddedItems.FirstOrDefault());
protected virtual void OnSelectedItemChanged(object oldValue, object newValue) { }
@@ -47,13 +77,25 @@ public partial class CustomCalendarFlipView : FlipView
public void GoPreviousFlip()
{
var backPeer = new ButtonAutomationPeer(PreviousButton);
var previousButton = DisplayType == CalendarDisplayType.Month
? PreviousButtonVertical ?? PreviousButtonHorizontal
: PreviousButtonHorizontal ?? PreviousButtonVertical;
if (previousButton == null) return;
var backPeer = new ButtonAutomationPeer(previousButton);
backPeer.Invoke();
}
public void GoNextFlip()
{
var nextPeer = new ButtonAutomationPeer(NextButton);
var nextButton = DisplayType == CalendarDisplayType.Month
? NextButtonVertical ?? NextButtonHorizontal
: NextButtonHorizontal ?? NextButtonVertical;
if (nextButton == null) return;
var nextPeer = new ButtonAutomationPeer(nextButton);
nextPeer.Invoke();
}
}
@@ -1,6 +1,9 @@
using System;
using System;
using System.Collections.Specialized;
using System.Linq;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Wino.Core.Domain.Collections;
using Wino.Core.Domain.Models.Calendar;
namespace Wino.Calendar.Controls;
@@ -10,7 +13,6 @@ public partial class DayColumnControl : Control
private const string PART_HeaderDateDayText = nameof(PART_HeaderDateDayText);
private const string PART_IsTodayBorder = nameof(PART_IsTodayBorder);
private const string PART_ColumnHeaderText = nameof(PART_ColumnHeaderText);
private const string PART_AllDayItemsControl = nameof(PART_AllDayItemsControl);
private const string TodayState = nameof(TodayState);
@@ -20,6 +22,7 @@ public partial class DayColumnControl : Control
private TextBlock ColumnHeaderText;
private Border IsTodayBorder;
private ItemsControl AllDayItemsControl;
private CalendarEventCollection _boundEventsCollection;
public CalendarDayModel DayModel
{
@@ -27,11 +30,16 @@ public partial class DayColumnControl : Control
set { SetValue(DayModelProperty, value); }
}
public static readonly DependencyProperty DayModelProperty = DependencyProperty.Register(nameof(DayModel), typeof(CalendarDayModel), typeof(DayColumnControl), new PropertyMetadata(null, new PropertyChangedCallback(OnRenderingPropertiesChanged)));
public static readonly DependencyProperty DayModelProperty = DependencyProperty.Register(
nameof(DayModel),
typeof(CalendarDayModel),
typeof(DayColumnControl),
new PropertyMetadata(null, new PropertyChangedCallback(OnRenderingPropertiesChanged)));
public DayColumnControl()
{
DefaultStyleKey = typeof(DayColumnControl);
Unloaded += OnUnloaded;
}
protected override void OnApplyTemplate()
@@ -43,6 +51,7 @@ public partial class DayColumnControl : Control
IsTodayBorder = GetTemplateChild(PART_IsTodayBorder) as Border;
AllDayItemsControl = GetTemplateChild(PART_AllDayItemsControl) as ItemsControl;
RegisterEventsCollectionHandlers();
UpdateValues();
}
@@ -50,15 +59,78 @@ public partial class DayColumnControl : Control
{
if (control is DayColumnControl columnControl)
{
columnControl.RegisterEventsCollectionHandlers();
columnControl.UpdateValues();
}
}
private void OnUnloaded(object sender, RoutedEventArgs e)
{
DeregisterEventsCollectionHandlers();
}
private bool IsMonthlyTemplate() => ColumnHeaderText == null;
private void RegisterEventsCollectionHandlers()
{
var nextCollection = DayModel?.EventsCollection;
if (ReferenceEquals(_boundEventsCollection, nextCollection))
return;
DeregisterEventsCollectionHandlers();
_boundEventsCollection = nextCollection;
if (_boundEventsCollection == null)
return;
((INotifyCollectionChanged)_boundEventsCollection.AllDayEvents).CollectionChanged += EventsCollectionChanged;
((INotifyCollectionChanged)_boundEventsCollection.RegularEvents).CollectionChanged += EventsCollectionChanged;
}
private void DeregisterEventsCollectionHandlers()
{
if (_boundEventsCollection == null)
return;
((INotifyCollectionChanged)_boundEventsCollection.AllDayEvents).CollectionChanged -= EventsCollectionChanged;
((INotifyCollectionChanged)_boundEventsCollection.RegularEvents).CollectionChanged -= EventsCollectionChanged;
_boundEventsCollection = null;
}
private void EventsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
UpdateEventItemsSource();
}
private void UpdateEventItemsSource()
{
if (AllDayItemsControl == null || DayModel == null) return;
if (IsMonthlyTemplate())
{
// Month cells should show all events for the day, not only all-day/multi-day.
var monthlyItems = DayModel.EventsCollection.AllDayEvents
.Concat(DayModel.EventsCollection.RegularEvents)
.GroupBy(a => a.Id)
.Select(g => g.First())
.OrderBy(a => a.StartDate)
.ToList();
AllDayItemsControl.ItemsSource = monthlyItems;
return;
}
AllDayItemsControl.ItemsSource = DayModel.EventsCollection.AllDayEvents;
}
private void UpdateValues()
{
if (HeaderDateDayText == null || IsTodayBorder == null || DayModel == null) return;
if (DayModel == null) return;
HeaderDateDayText.Text = DayModel.RepresentingDate.Day.ToString();
if (HeaderDateDayText != null)
{
HeaderDateDayText.Text = DayModel.RepresentingDate.Day.ToString();
}
// Monthly template does not use it.
if (ColumnHeaderText != null)
@@ -66,8 +138,9 @@ public partial class DayColumnControl : Control
ColumnHeaderText.Text = DayModel.RepresentingDate.ToString("dddd", DayModel.CalendarRenderOptions.CalendarSettings.CultureInfo);
}
AllDayItemsControl.ItemsSource = DayModel.EventsCollection.AllDayEvents;
UpdateEventItemsSource();
if (IsTodayBorder == null) return;
bool isToday = DayModel.RepresentingDate.Date == DateTime.Now.Date;
VisualStateManager.GoToState(this, isToday ? TodayState : NotTodayState, false);
@@ -87,6 +87,9 @@ public partial class WinoCalendarControl : Control
partial void OnOrientationChanged(CalendarOrientation newValue)
=> ManageCalendarOrientation();
partial void OnDisplayTypeChanged(CalendarDisplayType newValue)
=> ManageDisplayType();
partial void OnIsFlipIdleChanged(bool newValue)
=> UpdateIdleState();
@@ -131,6 +134,13 @@ public partial class WinoCalendarControl : Control
InternalFlipView.ItemsPanel = Orientation == CalendarOrientation.Horizontal ? HorizontalItemsPanelTemplate : VerticalItemsPanelTemplate;
}
private void ManageDisplayType()
{
if (InternalFlipView == null) return;
InternalFlipView.DisplayType = DisplayType;
}
private void ManageHighlightedDateRange()
=> SelectedFlipViewDayRange = InternalFlipView.SelectedItem as DayRangeRenderModel;
@@ -185,6 +195,7 @@ public partial class WinoCalendarControl : Control
UpdateIdleState();
ManageCalendarOrientation();
ManageDisplayType();
}
private void UpdateIdleState()
@@ -12,9 +12,12 @@ public partial class WinoCalendarTypeSelectorControl : Control
private const string PART_DayToggle = nameof(PART_DayToggle);
private const string PART_WeekToggle = nameof(PART_WeekToggle);
private const string PART_MonthToggle = nameof(PART_MonthToggle);
private const string PART_YearToggle = nameof(PART_YearToggle);
public static readonly DependencyProperty SelectedTypeProperty = DependencyProperty.Register(nameof(SelectedType), typeof(CalendarDisplayType), typeof(WinoCalendarTypeSelectorControl), new PropertyMetadata(CalendarDisplayType.Week));
public static readonly DependencyProperty SelectedTypeProperty = DependencyProperty.Register(
nameof(SelectedType),
typeof(CalendarDisplayType),
typeof(WinoCalendarTypeSelectorControl),
new PropertyMetadata(CalendarDisplayType.Week, OnSelectedTypeChanged));
public static readonly DependencyProperty DisplayDayCountProperty = DependencyProperty.Register(nameof(DisplayDayCount), typeof(int), typeof(WinoCalendarTypeSelectorControl), new PropertyMetadata(0));
public static readonly DependencyProperty TodayClickedCommandProperty = DependencyProperty.Register(nameof(TodayClickedCommand), typeof(ICommand), typeof(WinoCalendarTypeSelectorControl), new PropertyMetadata(null));
@@ -40,7 +43,6 @@ public partial class WinoCalendarTypeSelectorControl : Control
private AppBarToggleButton _dayToggle;
private AppBarToggleButton _weekToggle;
private AppBarToggleButton _monthToggle;
private AppBarToggleButton _yearToggle;
public WinoCalendarTypeSelectorControl()
{
@@ -51,24 +53,23 @@ public partial class WinoCalendarTypeSelectorControl : Control
{
base.OnApplyTemplate();
UnregisterHandlers();
_todayButton = GetTemplateChild(PART_TodayButton) as AppBarButton;
_dayToggle = GetTemplateChild(PART_DayToggle) as AppBarToggleButton;
_weekToggle = GetTemplateChild(PART_WeekToggle) as AppBarToggleButton;
_monthToggle = GetTemplateChild(PART_MonthToggle) as AppBarToggleButton;
_yearToggle = GetTemplateChild(PART_YearToggle) as AppBarToggleButton;
Guard.IsNotNull(_todayButton, nameof(_todayButton));
Guard.IsNotNull(_dayToggle, nameof(_dayToggle));
Guard.IsNotNull(_weekToggle, nameof(_weekToggle));
Guard.IsNotNull(_monthToggle, nameof(_monthToggle));
Guard.IsNotNull(_yearToggle, nameof(_yearToggle));
_todayButton.Click += TodayClicked;
_dayToggle.Click += (s, e) => { SetSelectedType(CalendarDisplayType.Day); };
_weekToggle.Click += (s, e) => { SetSelectedType(CalendarDisplayType.Week); };
_monthToggle.Click += (s, e) => { SetSelectedType(CalendarDisplayType.Month); };
_yearToggle.Click += (s, e) => { SetSelectedType(CalendarDisplayType.Year); };
UpdateToggleButtonStates();
}
@@ -81,11 +82,29 @@ public partial class WinoCalendarTypeSelectorControl : Control
UpdateToggleButtonStates();
}
private static void OnSelectedTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var control = d as WinoCalendarTypeSelectorControl;
control?.UpdateToggleButtonStates();
}
private void UnregisterHandlers()
{
if (_todayButton != null)
{
_todayButton.Click -= TodayClicked;
}
}
private void UpdateToggleButtonStates()
{
if (_dayToggle == null || _weekToggle == null || _monthToggle == null)
{
return;
}
_dayToggle.IsChecked = SelectedType == CalendarDisplayType.Day;
_weekToggle.IsChecked = SelectedType == CalendarDisplayType.Week;
_monthToggle.IsChecked = SelectedType == CalendarDisplayType.Month;
_yearToggle.IsChecked = SelectedType == CalendarDisplayType.Year;
}
}
@@ -22,8 +22,6 @@ public partial class WinoCalendarItemTemplateSelector : DataTemplateSelector
return DayWeekWorkWeekTemplate;
case CalendarDisplayType.Month:
return MonthlyTemplate;
case CalendarDisplayType.Year:
break;
default:
break;
}
@@ -22,7 +22,7 @@ public class StatePersistenceService : ObservableObject, IStatePersistanceServic
_openPaneLength = _configurationService.Get(OpenPaneLengthKey, 320d);
_mailListPaneLength = _configurationService.Get(MailListPaneLengthKey, 420d);
_calendarDisplayType = _configurationService.Get(nameof(CalendarDisplayType), CalendarDisplayType.Week);
_calendarDisplayType = EnsureValidCalendarDisplayType(_configurationService.Get(nameof(CalendarDisplayType), CalendarDisplayType.Week));
_dayDisplayCount = _configurationService.Get(nameof(DayDisplayCount), 1);
PropertyChanged += ServicePropertyChanged;
@@ -176,9 +176,11 @@ public class StatePersistenceService : ObservableObject, IStatePersistanceServic
get => _calendarDisplayType;
set
{
if (SetProperty(ref _calendarDisplayType, value))
var validValue = EnsureValidCalendarDisplayType(value);
if (SetProperty(ref _calendarDisplayType, validValue))
{
_configurationService.Set(nameof(CalendarDisplayType), value);
_configurationService.Set(nameof(CalendarDisplayType), validValue);
}
}
}
@@ -197,4 +199,11 @@ public class StatePersistenceService : ObservableObject, IStatePersistanceServic
}
private void UpdateAppCoreWindowTitle() => WinoApplication.MainWindow.Title = CoreWindowTitle;
private static CalendarDisplayType EnsureValidCalendarDisplayType(CalendarDisplayType displayType)
{
return Enum.IsDefined(typeof(CalendarDisplayType), displayType)
? displayType
: CalendarDisplayType.Week;
}
}
@@ -390,6 +390,9 @@
<RowDefinition Height="*" MinHeight="35" />
</Grid.RowDefinitions>
<!-- Day number -->
<TextBlock x:Name="PART_HeaderDateDayText" FontSize="17" />
<!-- Extras -->
<StackPanel Grid.Column="1" HorizontalAlignment="Right" />
@@ -61,16 +61,6 @@
</AppBarToggleButton.Icon>
</AppBarToggleButton>
<!-- Year -->
<AppBarToggleButton
x:Name="PART_YearToggle"
Foreground="{ThemeResource ApplicationForegroundThemeBrush}"
Label="Year">
<AppBarToggleButton.Icon>
<controls1:WinoFontIcon Icon="CalendarYear" />
</AppBarToggleButton.Icon>
</AppBarToggleButton>
</CommandBar.PrimaryCommands>
</CommandBar>
</ControlTemplate>
@@ -1,4 +1,4 @@
<abstract:CalendarAppShellAbstract
<abstract:CalendarAppShellAbstract
x:Class="Wino.Mail.WinUI.Views.Calendar.CalendarAppShell"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
@@ -85,6 +85,7 @@
FontSize="14"
FontWeight="Normal"
IsHitTestVisible="False"
DisplayType="{x:Bind ViewModel.StatePersistenceService.CalendarDisplayType, Mode=OneWay}"
ItemsSource="{x:Bind ViewModel.DateNavigationHeaderItems}"
SelectedIndex="{x:Bind ViewModel.SelectedDateNavigationHeaderIndex, Mode=OneWay}">
<FlipView.ItemTemplate>
@@ -19,13 +19,13 @@ public sealed partial class CalendarAppShell : CalendarAppShellAbstract,
InitializeComponent();
// Window.Current.SetTitleBar(DragArea);
ManageCalendarDisplayType();
ManageCalendarDisplayType(ViewModel.StatePersistenceService.CalendarDisplayType);
}
private void ManageCalendarDisplayType()
private void ManageCalendarDisplayType(Core.Domain.Enums.CalendarDisplayType displayType)
{
// Go to different states based on the display type.
if (ViewModel.IsVerticalCalendar)
if (displayType == Core.Domain.Enums.CalendarDisplayType.Month)
{
VisualStateManager.GoToState(this, STATE_VerticalCalendar, false);
}
@@ -41,7 +41,7 @@ public sealed partial class CalendarAppShell : CalendarAppShellAbstract,
public void Receive(CalendarDisplayTypeChangedMessage message)
{
ManageCalendarDisplayType();
ManageCalendarDisplayType(message.NewDisplayType);
}
//private void ShellFrameContentNavigated(object sender, Microsoft.UI.Xaml.Navigation.NavigationEventArgs e)