| 1 | #nullable enable |
| 2 | using CascadeIDE.Views.Chat.Skia; |
| 3 | using SkiaSharp; |
| 4 | |
| 5 | namespace CascadeIDE.Views.SkiaKit; |
| 6 | |
| 7 | /// <summary>Схлопнутый mono-блок кода в ленте чата (ADR 0123 фаза 3).</summary> |
| 8 | internal static class SkiaMonoCodeStrip |
| 9 | { |
| 10 | public const float Padding = 8f; |
| 11 | public const float LineHeight = 14f; |
| 12 | public const int DefaultMaxLines = 8; |
| 13 | public const float CornerRadius = 6f; |
| 14 | |
| 15 | public static float MeasureHeight(string code, float contentWidth, int maxLines = DefaultMaxLines) |
| 16 | { |
| 17 | var innerWidth = Math.Max(40f, contentWidth - Padding * 2); |
| 18 | var rich = SkiaRichTextKitMarkdown.TryMeasurePlain( |
| 19 | code, |
| 20 | innerWidth, |
| 21 | fontSize: 10.5f, |
| 22 | color: new SKColor(220, 225, 235), |
| 23 | maxLines: maxLines, |
| 24 | lineHeight: LineHeight, |
| 25 | fontFamily: "Cascadia Mono"); |
| 26 | return Padding * 2 + (rich?.BodyHeight ?? LineHeight); |
| 27 | } |
| 28 | |
| 29 | public static void Draw( |
| 30 | SKCanvas canvas, |
| 31 | SKRect bounds, |
| 32 | ISkiaKitPaintTheme theme, |
| 33 | string code, |
| 34 | float contentWidth, |
| 35 | int maxLines = DefaultMaxLines) |
| 36 | { |
| 37 | using var fill = new SKPaint |
| 38 | { |
| 39 | Color = SkiaKitColor.Blend(theme.Surface, theme.Border, 0.55f), |
| 40 | IsAntialias = true, |
| 41 | Style = SKPaintStyle.Fill, |
| 42 | }; |
| 43 | canvas.DrawRoundRect(bounds, CornerRadius, CornerRadius, fill); |
| 44 | |
| 45 | using var border = new SKPaint |
| 46 | { |
| 47 | Color = theme.Border, |
| 48 | IsAntialias = true, |
| 49 | Style = SKPaintStyle.Stroke, |
| 50 | StrokeWidth = 1f, |
| 51 | }; |
| 52 | canvas.DrawRoundRect(bounds, CornerRadius, CornerRadius, border); |
| 53 | |
| 54 | var innerWidth = Math.Max(40f, contentWidth - Padding * 2); |
| 55 | var rich = SkiaRichTextKitMarkdown.TryMeasurePlain( |
| 56 | code, |
| 57 | innerWidth, |
| 58 | 10.5f, |
| 59 | theme.Content, |
| 60 | maxLines, |
| 61 | LineHeight, |
| 62 | "Cascadia Mono"); |
| 63 | if (rich is null) |
| 64 | return; |
| 65 | |
| 66 | SkiaRichTextKitMarkdown.Paint( |
| 67 | canvas, |
| 68 | new SKPoint(bounds.Left + Padding, bounds.Top + Padding + LineHeight - 3f - 10.5f * 0.85f), |
| 69 | rich, |
| 70 | theme.Content, |
| 71 | theme.Content); |
| 72 | } |
| 73 | } |
| 74 | |