Skip to content

Coordinates

Pavel edited this page Oct 19, 2026 · 3 revisions

English | Русский

Your application draws its own things over the document or next to it: buttons in a margin, frames, lines between two panes. The viewer tells where pieces of text and source lines are on the screen — right in the coordinates of your element — and when the layout changes.

Buttons of the application at the source lines

One entry point

GetCoordinates(element) gives MdCoordinates — the geometry of the document in the coordinates of the element. It is computed at every call from the current layout.

Member What it gives
RangeRects(range), RangeBounds(range) the rectangles of a piece of the text map screen line by screen line and their common frame; only for created blocks
LineTop(line) the top of a source line; a fractional part is a part of the line
LinesRect(first, last) the band of source lines across the width of the viewer: from the top of the first to the top of the line after the last
LineAt(point) the source line under a point; a fractional part is a part of the line
PositionAt(point, snapToText) the position of the text map under a point; with snapToText — at the nearest text, without it outside the text — null
VisibleLines() the source lines at the top and the bottom edges of the visible area of the scrolling
VisibleRows() the source lines that have their own place in the visible area — where the margin puts their numbers: the top and the height of each

A line inside a paragraph stands where it starts on the screen: a long source line that wraps into several screen lines does not shift the next one. The same answers in the viewer's own coordinates are GetRangeRects, GetRangeBounds, GetSourceLineOffset, GetSourceLineAt and GetPositionFromPoint — see Selection and Source Lines and Two Panes.

When to draw again

ViewportChanged comes when the layout of the visible part may have changed: the scrolling moved outside the viewer or inside a block — a code block or a table scrolled sideways; heights of blocks became known; a new text came. Draw your things again on it.

Buttons in the margin

A column of your application next to the viewer, in the same scrolling. Buttons are created only for the visible lines, stand at their lines and move again on ViewportChanged.

MainWindow.xaml:

<ScrollViewer>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="28" />
            <ColumnDefinition />
        </Grid.ColumnDefinitions>
        <Canvas x:Name="Marks" />
        <md:MarkdownViewer x:Name="Viewer"
                            Grid.Column="1"
                            Markdown="{Binding Text}"
                            ViewportChanged="OnViewportChanged" />
    </Grid>
</ScrollViewer>

MainWindow.xaml.cs, in the window class (using System.Windows.Controls.Primitives;):

private readonly HashSet<int> _bookmarks = [];

private void OnViewportChanged(object? sender, EventArgs e)
{
    Marks.Children.Clear();
    foreach (var (line, top, height) in Viewer.GetCoordinates(Marks).VisibleRows())
    {
        var button = new ToggleButton { Width = 12, Height = 12, IsChecked = _bookmarks.Contains(line), Tag = line };
        button.Click += OnBookmarkClick;
        Canvas.SetLeft(button, 8);
        Canvas.SetTop(button, top + (height - button.Height) / 2);
        Marks.Children.Add(button);
    }
}

private void OnBookmarkClick(object sender, RoutedEventArgs e)
{
    var line = (int)((ToggleButton)sender).Tag;
    if (!_bookmarks.Remove(line))
    {
        _bookmarks.Add(line);
    }
}

The showcase scenario «Margin buttons» shows the bookmarks as marks of its own layer.

Lines between two panes

Two viewers in their own scrolling and a canvas between them: a changed piece is a figure from the band of its lines on the left to the band on the right. Both bands come from the coordinates of one canvas.

Lines between changed pieces of two panes

MainWindow.xaml.cs, drawn again on ViewportChanged of both viewers (using System.Windows.Shapes;); _hunks are the changed pieces of your diff — the first and the last line on each side:

private void DrawConnections()
{
    Connections.Children.Clear();
    var left = LeftViewer.GetCoordinates(Connections);
    var right = RightViewer.GetCoordinates(Connections);

    foreach (var (leftFirst, leftLast, rightFirst, rightLast) in _hunks)
    {
        if (left.LinesRect(leftFirst, leftLast) is { } from && right.LinesRect(rightFirst, rightLast) is { } to)
        {
            Connections.Children.Add(new Polygon
            {
                Points = { new(0, from.Top), new(Connections.ActualWidth, to.Top), new(Connections.ActualWidth, to.Bottom), new(0, from.Bottom) },
                Fill = (Brush)FindResource("Md.Mark.Changed.Brush")
            });
        }
    }
}

A piece that is empty on one side is a point there: LineTop of the line it stands before. The full example is the showcase scenario «Two panes»; see also two panes.

Beresta.Markdown.Wpf

Basics

Working with Text

Appearance

Integration

Editing

Your Own Syntax

Ready-made Extensions


По-русски

Основы

Работа с текстом

Оформление

Встраивание в приложение

Правка

Свой синтаксис

Готовые расширения

Clone this wiki locally