private void ScrollIntoView(IElement container, ScrollPattern scrollbar, bool horizontally, bool incrementally)
{
// TODO To make scrolling more efficient, make big steps in the beginning.
var step = incrementally ? ScrollAmount.SmallIncrement : ScrollAmount.SmallDecrement;
bool done = false;
do
{
// dimensions of partially displayed elements only reveal visible parts. (XAML-based only)
int before = horizontally ? this.Width : this.Height;
if (horizontally)
{
scrollbar.Scroll(step, ScrollAmount.NoAmount);
}
else
{
scrollbar.Scroll(ScrollAmount.NoAmount, step);
}
System.Threading.Thread.Sleep(20); // or the following readings could be stale
this.ResetCache();
int after = horizontally ? this.Width : this.Height;
if (horizontally)
{
if (incrementally)
{
done = this.X + this.Width <= container.X + container.Width;
}
else
{
done = this.X >= container.X;
}
}
else
{
if (incrementally)
{
done = this.Y + this.Height <= container.Y + container.Height;
}
else
{
done = this.Y >= container.Y;
}
}
done = done && (after <= before); // leaving the element
logger.Debug(
"Done? {0}; element [{1},{2}][{3},{4}] in the scrollable container [{5},{6}][{7},{8}]",
done, this.X, this.Y, this.Width, this.Height, container.X, container.Y, container.Width, container.Height);
}
while (!done);
}