Browse Source

feat: mark current line in editor (#51)

Use WindowsText color to mark the line for the caret.
Define the resource color LineNumberCurrent.
master
Gerhard Olsson 3 months ago
committed by GitHub
parent
commit
02a2aca8c9
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 1
      Project/Resources/Mode.xsd
  2. 1
      Project/Src/Document/HighlightingStrategy/DefaultHighlightingStrategy.cs
  3. 15
      Project/Src/Gui/AbstractMargin.cs
  4. 11
      Project/Src/Gui/GutterMargin.cs

1
Project/Resources/Mode.xsd

@ -47,6 +47,7 @@
<xsd:element name="CaretLine" type="EnvironmentEntry" minOccurs="0" maxOccurs="1" />
<xsd:element name="LineNumbers" type="EnvironmentEntry" minOccurs="0" maxOccurs="1" />
<xsd:element name="LineNumberSelected" type="EnvironmentEntry" minOccurs="0" maxOccurs="1" />
<xsd:element name="FoldLine" type="EnvironmentEntry" minOccurs="0" maxOccurs="1" />
<xsd:element name="FoldMarker" type="EnvironmentEntry" minOccurs="0" maxOccurs="1" />

1
Project/Src/Document/HighlightingStrategy/DefaultHighlightingStrategy.cs

@ -65,6 +65,7 @@ namespace ICSharpCode.TextEditor.Document
["CaretMarker"] = new HighlightColor(nameof(SystemColors.MenuBar), bold: false, italic: false),
["CaretLine"] = new HighlightBackground(nameof(SystemColors.ControlLight), nameof(SystemColors.Window), bold: false, italic: false),
["LineNumbers"] = new HighlightBackground(nameof(SystemColors.GrayText), nameof(SystemColors.Window), bold: false, italic: false),
["LineNumberSelected"] = new HighlightBackground(nameof(SystemColors.WindowText), nameof(SystemColors.Window), bold: true, italic: false),
["FoldLine"] = new HighlightColor(nameof(SystemColors.ControlDark), bold: false, italic: false),
["FoldMarker"] = new HighlightColor(nameof(SystemColors.WindowText), nameof(SystemColors.Window), bold: false, italic: false),
["SelectedFoldLine"] = new HighlightColor(nameof(SystemColors.WindowText), bold: false, italic: false),

15
Project/Src/Gui/AbstractMargin.cs

@ -21,6 +21,8 @@ namespace ICSharpCode.TextEditor
/// </summary>
public abstract class AbstractMargin
{
private int _caretLine = -1;
protected Rectangle drawingPosition = new Rectangle(x: 0, y: 0, width: 0, height: 0);
protected TextArea textArea;
@ -42,6 +44,8 @@ namespace ICSharpCode.TextEditor
public ITextEditorProperties TextEditorProperties => textArea.Document.TextEditorProperties;
public bool MarkSelectedLine { get; set; } = true;
public virtual Cursor Cursor { get; set; } = Cursors.Default;
public virtual int Width => -1;
@ -63,6 +67,17 @@ namespace ICSharpCode.TextEditor
MouseLeave?.Invoke(this, e);
}
public virtual void SelectedLineChanged(int line)
{
if (!IsVisible || _caretLine == line || !MarkSelectedLine)
{
return;
}
_caretLine = line;
textArea.Invalidate();
}
public virtual void Paint(Graphics g, Rectangle rect)
{
Painted?.Invoke(this, g, rect);

11
Project/Src/Gui/GutterMargin.cs

@ -57,11 +57,13 @@ namespace ICSharpCode.TextEditor
return;
var lineNumberPainterColor = textArea.Document.HighlightingStrategy.GetColorFor("LineNumbers");
var lineNumberCurrentPainterColor = textArea.Document.HighlightingStrategy.GetColorFor("LineNumberSelected");
var fontHeight = textArea.TextView.FontHeight;
var fillBrush = textArea.Enabled
? BrushRegistry.GetBrush(lineNumberPainterColor.BackgroundColor)
: SystemBrushes.InactiveBorder;
var drawBrush = BrushRegistry.GetBrush(lineNumberPainterColor.Color);
Brush currentLineBrush = BrushRegistry.GetBrush(lineNumberCurrentPainterColor.Color);
for (var y = 0; y < (drawingPosition.Height + textArea.TextView.VisibleLineDrawingRemainder)/fontHeight + 1; ++y)
{
@ -71,12 +73,17 @@ namespace ICSharpCode.TextEditor
{
g.FillRectangle(fillBrush, backgroundRectangle);
var curLine = textArea.Document.GetFirstLogicalLine(textArea.Document.GetVisibleLine(textArea.TextView.FirstVisibleLine) + y);
bool isCurrentLine = curLine == textArea.Caret.Line && MarkSelectedLine;
Brush lineBrush = isCurrentLine ? currentLineBrush : drawBrush;
Font font = isCurrentLine
? lineNumberCurrentPainterColor.GetFont(TextEditorProperties.FontContainer)
: lineNumberPainterColor.GetFont(TextEditorProperties.FontContainer);
if (curLine < textArea.Document.TotalNumberOfLines)
g.DrawString(
(curLine + 1).ToString(),
lineNumberPainterColor.GetFont(TextEditorProperties.FontContainer),
drawBrush,
font,
lineBrush,
backgroundRectangle,
numberStringFormat);
}

Loading…
Cancel
Save