ToolTipMessage.axaml.cs 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using System.Reactive.Linq;
  2. using Avalonia.Controls;
  3. using PicView.Avalonia.Helpers;
  4. using ReactiveUI;
  5. namespace PicView.Avalonia.Views.UC;
  6. public partial class ToolTipMessage : UserControl
  7. {
  8. private bool _isRunning;
  9. public ToolTipMessage()
  10. {
  11. InitializeComponent();
  12. // Subscribe to the ToolTipMessageText.Text changes
  13. this.WhenAnyValue(x => x.ToolTipMessageText.Text)
  14. .Throttle(TimeSpan.FromMilliseconds(100)) // Avoid rapid consecutive changes
  15. .Where(text => !string.IsNullOrEmpty(text))
  16. .ObserveOn(RxApp.MainThreadScheduler)
  17. .Select(async _ =>
  18. {
  19. await DoAnimation();
  20. })
  21. .Subscribe();
  22. }
  23. private async Task DoAnimation()
  24. {
  25. if (_isRunning)
  26. {
  27. return;
  28. }
  29. _isRunning = true;
  30. // ReSharper disable once CompareOfFloatsByEqualityOperator
  31. if (Opacity != 1)
  32. {
  33. var fadeInAnimation = AnimationsHelper.OpacityAnimation(from: 0, to: 1, 1.5);
  34. await fadeInAnimation.RunAsync(this);
  35. }
  36. // Wait for the duration before fading out
  37. await Task.Delay(TimeSpan.FromSeconds(1.5));
  38. _isRunning = false;
  39. var fadeOutAnimation = AnimationsHelper.OpacityAnimation(from: 1, to: 0, 1.5);
  40. await fadeOutAnimation.RunAsync(this);
  41. }
  42. }