Files
Wino-Mail/Wino.Core.UWP/Controls/WinoInfoBar.cs

91 lines
3.3 KiB
C#
Raw Normal View History

2024-04-18 01:44:37 +02:00
using System;
using System.Numerics;
using CommunityToolkit.WinUI.Animations;
using Microsoft.UI.Xaml.Controls;
using Windows.UI.Xaml;
using Wino.Core.Domain.Enums;
2025-02-16 11:54:23 +01:00
namespace Wino.Core.UWP.Controls;
public partial class WinoInfoBar : InfoBar
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
public static readonly DependencyProperty AnimationTypeProperty = DependencyProperty.Register(nameof(AnimationType), typeof(InfoBarAnimationType), typeof(WinoInfoBar), new PropertyMetadata(InfoBarAnimationType.SlideFromRightToLeft));
public static readonly DependencyProperty DismissIntervalProperty = DependencyProperty.Register(nameof(DismissInterval), typeof(int), typeof(WinoInfoBar), new PropertyMetadata(5, new PropertyChangedCallback(OnDismissIntervalChanged)));
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public InfoBarAnimationType AnimationType
{
get { return (InfoBarAnimationType)GetValue(AnimationTypeProperty); }
set { SetValue(AnimationTypeProperty, value); }
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public int DismissInterval
{
get { return (int)GetValue(DismissIntervalProperty); }
set { SetValue(DismissIntervalProperty, value); }
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private readonly DispatcherTimer _dispatcherTimer = new DispatcherTimer();
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
public WinoInfoBar()
{
RegisterPropertyChangedCallback(IsOpenProperty, IsOpenChanged);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
_dispatcherTimer.Interval = TimeSpan.FromSeconds(DismissInterval);
}
2025-02-16 11:54:23 +01:00
private static void OnDismissIntervalChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args)
{
if (obj is WinoInfoBar bar)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
bar.UpdateInterval(bar.DismissInterval);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private void UpdateInterval(int seconds) => _dispatcherTimer.Interval = TimeSpan.FromSeconds(seconds);
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
private async void IsOpenChanged(DependencyObject sender, DependencyProperty dp)
{
if (sender is WinoInfoBar control)
2024-04-18 01:44:37 +02:00
{
2025-02-16 11:54:23 +01:00
// Message shown.
if (!control.IsOpen) return;
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
Opacity = 1;
2024-04-18 01:44:37 +02:00
_dispatcherTimer.Stop();
2025-02-16 11:54:23 +01:00
_dispatcherTimer.Tick -= TimerTick;
2025-02-16 11:54:23 +01:00
_dispatcherTimer.Tick += TimerTick;
2025-02-16 11:35:43 +01:00
2025-02-16 11:54:23 +01:00
_dispatcherTimer.Start();
// Slide from right.
2024-04-18 01:44:37 +02:00
if (AnimationType == InfoBarAnimationType.SlideFromRightToLeft)
{
2025-02-16 11:54:23 +01:00
await AnimationBuilder.Create().Translation(new Vector3(0, 0, 0), new Vector3(150, 0, 0), null, TimeSpan.FromSeconds(0.5)).StartAsync(this);
2024-04-18 01:44:37 +02:00
}
else if (AnimationType == InfoBarAnimationType.SlideFromBottomToTop)
{
2025-02-16 11:54:23 +01:00
await AnimationBuilder.Create().Translation(new Vector3(0, 0, 0), new Vector3(0, 50, 0), null, TimeSpan.FromSeconds(0.5)).StartAsync(this);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
}
}
private async void TimerTick(object sender, object e)
{
_dispatcherTimer.Stop();
_dispatcherTimer.Tick -= TimerTick;
2024-04-18 01:44:37 +02:00
2025-02-16 11:54:23 +01:00
if (AnimationType == InfoBarAnimationType.SlideFromRightToLeft)
{
await AnimationBuilder.Create().Translation(new Vector3((float)ActualWidth, 0, 0), new Vector3(0, 0, 0), null, TimeSpan.FromSeconds(0.5)).StartAsync(this);
}
else if (AnimationType == InfoBarAnimationType.SlideFromBottomToTop)
{
await AnimationBuilder.Create().Translation(new Vector3(0, (float)ActualHeight, 0), new Vector3(0, 0, 0), null, TimeSpan.FromSeconds(0.5)).StartAsync(this);
2024-04-18 01:44:37 +02:00
}
2025-02-16 11:54:23 +01:00
IsOpen = false;
2024-04-18 01:44:37 +02:00
}
}