SideMenuScrollerToOpacityMask.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. using Avalonia;
  5. using Avalonia.Data.Converters;
  6. using Avalonia.Media;
  7. namespace SukiUI.Converters
  8. {
  9. public class SideMenuScrollerToOpacityMask : IMultiValueConverter
  10. {
  11. private readonly Func<double, double, IBrush?> _func;
  12. public static SideMenuScrollerToOpacityMask Top { get; } = new((x,y) => x > y ? TopBrush : Brushes.White);
  13. public static SideMenuScrollerToOpacityMask Bottom { get; } = new((x,y) => x < y ? BottomBrush : Brushes.White);
  14. private static readonly LinearGradientBrush BottomBrush = new()
  15. {
  16. StartPoint = new RelativePoint(0.5, 0, RelativeUnit.Relative),
  17. EndPoint = new RelativePoint(0.5, 0.95, RelativeUnit.Relative),
  18. GradientStops = new GradientStops()
  19. {
  20. new(Colors.Black, 0.9),
  21. new(Colors.Transparent,1 )
  22. }
  23. };
  24. private static readonly LinearGradientBrush TopBrush = new()
  25. {
  26. StartPoint = new RelativePoint(0.5, 1, RelativeUnit.Relative),
  27. EndPoint = new RelativePoint(0.5, 0.05, RelativeUnit.Relative),
  28. GradientStops = new GradientStops()
  29. {
  30. new(Colors.Black, 0.9),
  31. new(Colors.Transparent,1 )
  32. }
  33. };
  34. public SideMenuScrollerToOpacityMask(Func<double, double, IBrush?> func)
  35. {
  36. _func = func;
  37. }
  38. public object? Convert(IList<object?> values, Type targetType, object? parameter, CultureInfo culture)
  39. {
  40. if (values.Count != 2) return null;
  41. if (values[0] is not double valOne) return null;
  42. if (values[1] is not double valTwo) return null;
  43. return _func(valOne, valTwo);
  44. }
  45. }
  46. }