Files

114 lines
3.6 KiB
C#
Raw Permalink Normal View History

2026-03-12 11:28:41 +01:00
using CommunityToolkit.WinUI.Controls;
2026-03-10 16:50:16 +01:00
using Microsoft.Extensions.DependencyInjection;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
2026-03-11 19:26:37 +01:00
using Wino.Core.Domain;
2026-03-10 16:50:16 +01:00
namespace Wino.Mail.WinUI.Controls;
2026-03-12 11:28:41 +01:00
public sealed partial class AppModeFooterSwitcherControl : Segmented
2026-03-10 16:50:16 +01:00
{
2026-03-12 11:28:41 +01:00
private const double VerticalItemExtent = 44;
2026-03-10 16:50:16 +01:00
private readonly IStatePersistanceService _statePersistenceService;
private readonly INavigationService _navigationService;
private bool _isUpdatingSelection;
2026-03-12 11:28:41 +01:00
public static readonly DependencyProperty OrientationProperty = DependencyProperty.Register(
nameof(Orientation),
typeof(Orientation),
typeof(AppModeFooterSwitcherControl),
new PropertyMetadata(Orientation.Horizontal, OnOrientationChanged));
public Orientation Orientation
{
get => (Orientation)GetValue(OrientationProperty);
set => SetValue(OrientationProperty, value);
}
2026-03-10 16:50:16 +01:00
public AppModeFooterSwitcherControl()
{
_statePersistenceService = WinoApplication.Current.Services.GetRequiredService<IStatePersistanceService>();
_navigationService = WinoApplication.Current.Services.GetRequiredService<INavigationService>();
InitializeComponent();
}
2026-03-12 11:28:41 +01:00
private static void OnOrientationChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs _)
{
((AppModeFooterSwitcherControl)dependencyObject).UpdateOrientationState();
}
2026-03-10 16:50:16 +01:00
private void ControlLoaded(object sender, RoutedEventArgs e)
{
_statePersistenceService.StatePropertyChanged += StatePropertyChanged;
2026-03-12 11:28:41 +01:00
UpdateOrientationState();
2026-03-10 16:50:16 +01:00
UpdateSelection(_statePersistenceService.ApplicationMode);
}
private void ControlUnloaded(object sender, RoutedEventArgs e)
{
_statePersistenceService.StatePropertyChanged -= StatePropertyChanged;
}
private void StatePropertyChanged(object? sender, string propertyName)
{
if (propertyName != nameof(IStatePersistanceService.ApplicationMode))
return;
DispatcherQueue.TryEnqueue(() => UpdateSelection(_statePersistenceService.ApplicationMode));
}
private void ModeSegmentedControlSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_isUpdatingSelection)
return;
2026-03-12 11:28:41 +01:00
var selectedMode = SelectedIndex switch
2026-03-10 16:50:16 +01:00
{
1 => WinoApplicationMode.Calendar,
2 => WinoApplicationMode.Contacts,
2026-03-12 19:04:47 +01:00
3 => WinoApplicationMode.Settings,
2026-03-10 16:50:16 +01:00
_ => WinoApplicationMode.Mail
};
if (selectedMode == _statePersistenceService.ApplicationMode)
return;
_navigationService.ChangeApplicationMode(selectedMode);
}
private void UpdateSelection(WinoApplicationMode mode)
{
_isUpdatingSelection = true;
2026-03-12 11:28:41 +01:00
SelectedIndex = mode switch
2026-03-10 16:50:16 +01:00
{
WinoApplicationMode.Calendar => 1,
WinoApplicationMode.Contacts => 2,
2026-03-12 19:04:47 +01:00
WinoApplicationMode.Settings => 3,
2026-03-10 16:50:16 +01:00
_ => 0
};
_isUpdatingSelection = false;
}
2026-03-12 11:28:41 +01:00
private void UpdateOrientationState()
{
foreach (var item in Items)
{
if (item is not SegmentedItem segmentedItem)
continue;
if (Orientation == Orientation.Vertical)
{
segmentedItem.Width = VerticalItemExtent;
segmentedItem.Height = VerticalItemExtent;
}
else
{
segmentedItem.ClearValue(WidthProperty);
segmentedItem.ClearValue(HeightProperty);
}
}
}
2026-03-10 16:50:16 +01:00
}