Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synchronize scroll position between two controls - Scroll Position not updating #454

Open
voidray opened this issue Sep 10, 2024 · 3 comments

Comments

@voidray
Copy link

voidray commented Sep 10, 2024

I try to sync the scroll position of two AvaloniaEdit controls that are places beside eachother in a grid.
The line to scroll to is actually determined correctly. I did very that by writing currentLineSource to the console.
This is the code behind of the View (Constructor calls SetupEditors()):

private void SetupEditors()
{
   leftEditor.TextArea.TextView.ScrollOffsetChanged += (s, e) => SyncScroll(leftEditor, rightEditor);
   rightEditor.TextArea.TextView.ScrollOffsetChanged += (s, e) => SyncScroll(rightEditor, leftEditor);
}

private void SyncScroll(TextEditor source, TextEditor target)
{
   var currentLineSource = GetLineNumberFromVerticalOffset(source, source.VerticalOffset);
   target.ScrollToLine(currentLineSource);
}

public int GetLineNumberFromVerticalOffset(TextEditor editor, double verticalOffset)
{
	var textView = editor.TextArea.TextView;
	int estimatedLineNumber = Math.Max(1, (int)(verticalOffset / textView.DefaultLineHeight) + 1);
	estimatedLineNumber = Math.Min(estimatedLineNumber, editor.Document.LineCount);
}
@voidray voidray changed the title Synchronize scroll position between two controls Synchronize scroll position between two controls - Scroll Position not updating Sep 10, 2024
@danipen
Copy link
Collaborator

danipen commented Sep 10, 2024

I recommend you to synchronize using the scroll offset

@voidray
Copy link
Author

voidray commented Sep 11, 2024

With ScrollToVerticalOffset also nothing happens:

var offset = (currentLineSource - 1) * target.TextArea.TextView.DefaultLineHeight;
target.ScrollToVerticalOffset(offset);

This is the View I'm using:

<UserControl xmlns="https://github.com/avaloniaui"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:vm="using:TestProject.Av.ViewModels"
             xmlns:avedit="clr-namespace:AvaloniaEdit;assembly=AvaloniaEdit"
             mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
             x:Class="TestProject.Av.Views.TextView">
   <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <TextBlock Text="test" Grid.Row="0" />
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <avedit:TextEditor x:Name="leftEditor" Grid.Column="0" FontSize="14" ShowLineNumbers="True">
            </avedit:TextEditor>

            <avedit:TextEditor x:Name="rightEditor" Grid.Column="1" FontSize="14" ShowLineNumbers="True">
            </avedit:TextEditor>
        </Grid>
    </Grid>
    
</UserControl>

@voidray
Copy link
Author

voidray commented Sep 12, 2024

I did some more testing by creating a blank project with just two AvaloniaEdit controls and a button.
When I use the button to call ScrollToVerticalOffset then nothing happens.
ScrollToLine works. The issue with the code I posted above was that the expected line position is not at the top but in the center (vertically).
(The code also the issue that when setting the position of the other control, the event gets fired again and tries to update the control where the user scrolled. It's not relevant to the topic here however. The issue with ScrollToVerticalOffset can be reproduced with a simple Button)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants