|
| 1 | +using System.Drawing; |
| 2 | +using Terminal.Gui.Drawing; |
| 3 | +using Terminal.Gui.Input; |
| 4 | +using Terminal.Gui.ViewBase; |
| 5 | +using Terminal.Gui.Views; |
| 6 | +using Attribute = Terminal.Gui.Drawing.Attribute; |
| 7 | +using Color = Terminal.Gui.Drawing.Color; |
| 8 | + |
| 9 | +namespace Ted; |
| 10 | + |
| 11 | +/// <summary> |
| 12 | +/// A <see cref="Markdown" /> subclass that highlights the rendered line(s) corresponding to a |
| 13 | +/// source line in the editor and raises <see cref="SourceLineClicked" /> when the user clicks |
| 14 | +/// in the preview, enabling click-to-navigate back to the editor. |
| 15 | +/// </summary> |
| 16 | +internal sealed class MarkdownPreview : Markdown |
| 17 | +{ |
| 18 | + private int _highlightSourceLine = -1; |
| 19 | + private int _totalSourceLines; |
| 20 | + |
| 21 | + /// <summary> |
| 22 | + /// Gets or sets the 0-based source line number to highlight in the preview. |
| 23 | + /// Set to -1 to clear the highlight. |
| 24 | + /// </summary> |
| 25 | + public int HighlightSourceLine |
| 26 | + { |
| 27 | + get => _highlightSourceLine; |
| 28 | + set |
| 29 | + { |
| 30 | + if (_highlightSourceLine == value) |
| 31 | + { |
| 32 | + return; |
| 33 | + } |
| 34 | + |
| 35 | + _highlightSourceLine = value; |
| 36 | + SetNeedsDraw (); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// <summary> |
| 41 | + /// Gets or sets the total number of source lines in the document. |
| 42 | + /// Used for proportional mapping between source lines and rendered lines. |
| 43 | + /// </summary> |
| 44 | + public int TotalSourceLines |
| 45 | + { |
| 46 | + get => _totalSourceLines; |
| 47 | + set |
| 48 | + { |
| 49 | + if (_totalSourceLines == value) |
| 50 | + { |
| 51 | + return; |
| 52 | + } |
| 53 | + |
| 54 | + _totalSourceLines = value; |
| 55 | + SetNeedsDraw (); |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// Raised when the user clicks in the preview. The event arg carries the estimated 0-based |
| 61 | + /// source line number corresponding to the click position. |
| 62 | + /// </summary> |
| 63 | + public event EventHandler<SourceLineClickedEventArgs>? SourceLineClicked; |
| 64 | + |
| 65 | + /// <summary> |
| 66 | + /// Maps a 0-based source line number to the corresponding rendered line index using |
| 67 | + /// proportional mapping. |
| 68 | + /// </summary> |
| 69 | + private int MapSourceToRendered (int sourceLine) |
| 70 | + { |
| 71 | + if (_totalSourceLines <= 1 || LineCount <= 0) |
| 72 | + { |
| 73 | + return 0; |
| 74 | + } |
| 75 | + |
| 76 | + return (int)((long)sourceLine * (LineCount - 1) / (_totalSourceLines - 1)); |
| 77 | + } |
| 78 | + |
| 79 | + /// <summary> |
| 80 | + /// Maps a rendered line index back to an approximate 0-based source line number. |
| 81 | + /// </summary> |
| 82 | + private int MapRenderedToSource (int renderedLine) |
| 83 | + { |
| 84 | + if (LineCount <= 1 || _totalSourceLines <= 0) |
| 85 | + { |
| 86 | + return 0; |
| 87 | + } |
| 88 | + |
| 89 | + return (int)((long)renderedLine * (_totalSourceLines - 1) / (LineCount - 1)); |
| 90 | + } |
| 91 | + |
| 92 | + /// <inheritdoc /> |
| 93 | + protected override bool OnDrawingContent (DrawContext? context) |
| 94 | + { |
| 95 | + var result = base.OnDrawingContent (context); |
| 96 | + |
| 97 | + DrawHighlightBar (); |
| 98 | + |
| 99 | + return result; |
| 100 | + } |
| 101 | + |
| 102 | + /// <summary> |
| 103 | + /// Paints a subtle background highlight on the rendered row corresponding to |
| 104 | + /// <see cref="HighlightSourceLine" />. |
| 105 | + /// </summary> |
| 106 | + private void DrawHighlightBar () |
| 107 | + { |
| 108 | + if (_highlightSourceLine < 0 || _totalSourceLines <= 0 || LineCount <= 0) |
| 109 | + { |
| 110 | + return; |
| 111 | + } |
| 112 | + |
| 113 | + var renderedLine = MapSourceToRendered (_highlightSourceLine); |
| 114 | + |
| 115 | + // Check if the highlighted line is within the visible viewport. |
| 116 | + var drawRow = renderedLine - Viewport.Y; |
| 117 | + |
| 118 | + if (drawRow < 0 || drawRow >= Viewport.Height) |
| 119 | + { |
| 120 | + return; |
| 121 | + } |
| 122 | + |
| 123 | + // Compute a highlight attribute: shift the background slightly for contrast. |
| 124 | + Attribute normalAttr = GetAttributeForRole (VisualRole.Normal); |
| 125 | + Color highlightBg = normalAttr.Background.IsDarkColor () |
| 126 | + ? normalAttr.Background.GetDimmerColor (0.25, false) |
| 127 | + : normalAttr.Background.GetDimmerColor (0.15, true); |
| 128 | + |
| 129 | + // Paint the highlight over the full viewport width by reading existing screen content |
| 130 | + // and re-drawing with the highlight background. |
| 131 | + Cell[,]? contents = ScreenContents; |
| 132 | + |
| 133 | + if (contents is null) |
| 134 | + { |
| 135 | + return; |
| 136 | + } |
| 137 | + |
| 138 | + // Map the viewport-relative draw row to screen coordinates so we read the correct |
| 139 | + // cells regardless of horizontal scroll position (Viewport.X). |
| 140 | + Point screenOrigin = ViewportToScreen (new Point (0, drawRow)); |
| 141 | + var screenRow = screenOrigin.Y; |
| 142 | + var screenStartCol = screenOrigin.X; |
| 143 | + |
| 144 | + for (var col = 0; col < Viewport.Width; col++) |
| 145 | + { |
| 146 | + var sc = screenStartCol + col; |
| 147 | + |
| 148 | + if (screenRow < 0 || screenRow >= contents.GetLength (0) || sc < 0 || sc >= contents.GetLength (1)) |
| 149 | + { |
| 150 | + continue; |
| 151 | + } |
| 152 | + |
| 153 | + Cell cell = contents[screenRow, sc]; |
| 154 | + var grapheme = string.IsNullOrEmpty (cell.Grapheme) ? " " : cell.Grapheme; |
| 155 | + |
| 156 | + // Preserve the foreground color from the original cell but apply highlight background. |
| 157 | + Attribute cellAttr = (cell.Attribute ?? normalAttr) with { Background = highlightBg }; |
| 158 | + SetAttribute (cellAttr); |
| 159 | + AddStr (col, drawRow, grapheme); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + /// <inheritdoc /> |
| 164 | + protected override bool OnMouseEvent (Mouse mouse) |
| 165 | + { |
| 166 | + if (mouse.Flags.HasFlag (MouseFlags.LeftButtonClicked) && mouse.Position is { } pos) |
| 167 | + { |
| 168 | + var contentRow = Viewport.Y + pos.Y; |
| 169 | + |
| 170 | + if (contentRow >= 0 && contentRow < LineCount) |
| 171 | + { |
| 172 | + var sourceLine = MapRenderedToSource (contentRow); |
| 173 | + SourceLineClicked?.Invoke (this, new SourceLineClickedEventArgs (sourceLine)); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + return base.OnMouseEvent (mouse); |
| 178 | + } |
| 179 | +} |
0 commit comments