UnplayedCountIndicator.cs 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using System.Globalization;
  2. using MediaBrowser.Model.Drawing;
  3. using SkiaSharp;
  4. namespace Jellyfin.Drawing.Skia;
  5. /// <summary>
  6. /// Static helper class for drawing unplayed count indicators.
  7. /// </summary>
  8. public static class UnplayedCountIndicator
  9. {
  10. /// <summary>
  11. /// The x-offset used when drawing an unplayed count indicator.
  12. /// </summary>
  13. private const int OffsetFromTopRightCorner = 38;
  14. /// <summary>
  15. /// Draw an unplayed count indicator in the top right corner of a canvas.
  16. /// </summary>
  17. /// <param name="canvas">The canvas to draw the indicator on.</param>
  18. /// <param name="imageSize">
  19. /// The dimensions of the image to draw the indicator on. The width is used to determine the x-position of the
  20. /// indicator.
  21. /// </param>
  22. /// <param name="count">The number to draw in the indicator.</param>
  23. public static void DrawUnplayedCountIndicator(SKCanvas canvas, ImageDimensions imageSize, int count)
  24. {
  25. var x = imageSize.Width - OffsetFromTopRightCorner;
  26. var text = count.ToString(CultureInfo.InvariantCulture);
  27. using var paint = new SKPaint
  28. {
  29. Color = SKColor.Parse("#CC00A4DC"),
  30. Style = SKPaintStyle.Fill
  31. };
  32. canvas.DrawCircle(x, OffsetFromTopRightCorner, 20, paint);
  33. paint.Color = new SKColor(255, 255, 255, 255);
  34. paint.TextSize = 24;
  35. paint.IsAntialias = true;
  36. var y = OffsetFromTopRightCorner + 9;
  37. if (text.Length == 1)
  38. {
  39. x -= 7;
  40. }
  41. if (text.Length == 2)
  42. {
  43. x -= 13;
  44. }
  45. else if (text.Length >= 3)
  46. {
  47. x -= 15;
  48. y -= 2;
  49. paint.TextSize = 18;
  50. }
  51. canvas.DrawText(text, x, y, paint);
  52. }
  53. }