AvaloniaUI / AvaloniaEdit

Avalonia-based text editor (port of AvalonEdit)
MIT License
774 stars 149 forks source link

Unimplemented ScrollToHorizontalOffset and ScrollToVerticalOffset #461

Open stiggronnestad opened 2 weeks ago

stiggronnestad commented 2 weeks ago

Version: 11.1.0

During an attempt to lock 2 AvaloniaEdit vertical/horizontal offsets I encountered some issues. It seems that ScrollToHorizontalOffset and ScrollToVerticalOffset was never implemented (or commented out) at some point. Avalonia 11.2 ScrollViewer does not have this API currently.

The underlying ScrollViewer is internal, so I'm not sure what the best approach is.

        /// <summary>
        /// Scrolls to the specified position in the document.
        /// </summary>
        public void ScrollToHorizontalOffset(double offset)
        {
            ApplyTemplate(); // ensure scrollViewer is created
            //if (scrollViewer != null)
            //    scrollViewer.ScrollToHorizontalOffset(offset);
        }

        /// <summary>
        /// Scrolls to the specified position in the document.
        /// </summary>
        public void ScrollToVerticalOffset(double offset)
        {
            ApplyTemplate(); // ensure scrollViewer is created
            //if (scrollViewer != null)
            //    scrollViewer.ScrollToVerticalOffset(offset);
        }

Edit: I tested a simple solution which works fine.

        /// <summary>
        /// Scrolls to the specified position in the document.
        /// </summary>
        public void ScrollToHorizontalOffset(double offset)
        {
            ApplyTemplate(); // ensure scrollViewer is created
            if (ScrollViewer != null)
                ScrollViewer.SetValue(ScrollViewer.OffsetProperty, new Vector(offset, ScrollViewer.Offset.Y));
        }

        /// <summary>
        /// Scrolls to the specified position in the document.
        /// </summary>
        public void ScrollToVerticalOffset(double offset)
        {
            ApplyTemplate(); // ensure scrollViewer is created
            if (ScrollViewer != null)
                ScrollViewer.SetValue(ScrollViewer.OffsetProperty, new Vector(ScrollViewer.Offset.X, offset));
        }