<commit_before>﻿using System;
using Avalonia.Controls.Primitives;
using Avalonia.Media;

namespace Avalonia.Controls.Presenters
{
    public class ThingamybobPresenter : Decorator, IItemsPresenter, IScrollable
    {
        private IVirtualizingPanel _panel;
        private int _firstIndex;
        private int _lastIndex;

        public override void ApplyTemplate()
        {
            if (_panel == null)
            {
                _panel = new VirtualizingStackPanel();
                _panel.ArrangeCompleted = CheckPanel;
                Child = _panel;
                CheckPanel();
            }
        }

        public IPanel Panel => _panel;

        Action IScrollable.InvalidateScroll { get; set; }

        Size IScrollable.Extent => new Size(1, 100 * AverageItemSize );

        Vector IScrollable.Offset
        {
            get
            {
                return new Vector(0, _firstIndex * AverageItemSize);
            }

            set
            {
                var count = _lastIndex - _firstIndex;
                _firstIndex = (int)(value.Y / AverageItemSize);
                _lastIndex = _firstIndex + count;
                _panel.PixelOffset = value.Y % AverageItemSize;
                Renumber();
            }
        }

        Size IScrollable.Viewport => new Size(1, (_lastIndex - _firstIndex) * AverageItemSize);
        Size IScrollable.ScrollSize => new Size(0, 1);
        Size IScrollable.PageScrollSize => new Size(0, 1);

        private double AverageItemSize => _panel?.AverageItemSize ?? 1;

        protected override Size ArrangeOverride(Size finalSize)
        {
            var result = base.ArrangeOverride(finalSize);
            CreateItems();
            ((IScrollable)this).InvalidateScroll();
            return result;
        }

        private void CreateItems()
        {
            var randomColor = Color.FromUInt32(
                (uint)(0xff000000 + new Random().Next(0xffffff)));

            while (!_panel.IsFull)
            {
                _panel.Children.Add(new TextBlock
                {
                    Text = "Item " + ++_lastIndex,
                    Background = new SolidColorBrush(randomColor),
                });
            }
        }

        private void RemoveItems()
        {
            var remove = _panel.OverflowCount;

            _panel.Children.RemoveRange(
                _panel.Children.Count - remove,
                _panel.OverflowCount);
            _lastIndex -= remove;
        }

        private void Renumber()
        {
            var index = _firstIndex;

            foreach (TextBlock child in _panel.Children)
            {
                child.Text = "Item " + ++index;
            }
        }

        private void CheckPanel()
        {
            if (!_panel.IsFull)
            {
                CreateItems();
            }
            else if (_panel.OverflowCount > 0)
            {
                RemoveItems();
            }
        }
    }
}<commit_msg>Leave one container materialized.<commit_after>﻿using System;
using Avalonia.Controls.Primitives;
using Avalonia.Media;

namespace Avalonia.Controls.Presenters
{
    public class ThingamybobPresenter : Decorator, IItemsPresenter, IScrollable
    {
        private IVirtualizingPanel _panel;
        private int _firstIndex;
        private int _lastIndex;

        public override void ApplyTemplate()
        {
            if (_panel == null)
            {
                _panel = new VirtualizingStackPanel();
                _panel.ArrangeCompleted = CheckPanel;
                Child = _panel;
                CheckPanel();
            }
        }

        public IPanel Panel => _panel;

        Action IScrollable.InvalidateScroll { get; set; }

        Size IScrollable.Extent => new Size(1, 100 * AverageItemSize );

        Vector IScrollable.Offset
        {
            get
            {
                return new Vector(0, _firstIndex * AverageItemSize);
            }

            set
            {
                var count = _lastIndex - _firstIndex;
                _firstIndex = (int)(value.Y / AverageItemSize);
                _lastIndex = _firstIndex + count;
                _panel.PixelOffset = value.Y % AverageItemSize;
                Renumber();
            }
        }

        Size IScrollable.Viewport => new Size(1, (_lastIndex - _firstIndex) * AverageItemSize);
        Size IScrollable.ScrollSize => new Size(0, 1);
        Size IScrollable.PageScrollSize => new Size(0, 1);

        private double AverageItemSize => _panel?.AverageItemSize ?? 1;

        protected override Size ArrangeOverride(Size finalSize)
        {
            var result = base.ArrangeOverride(finalSize);
            CreateItems();
            ((IScrollable)this).InvalidateScroll();
            return result;
        }

        private void CreateItems()
        {
            var randomColor = Color.FromUInt32(
                (uint)(0xff000000 + new Random().Next(0xffffff)));

            while (!_panel.IsFull)
            {
                _panel.Children.Add(new TextBlock
                {
                    Text = "Item " + ++_lastIndex,
                    Background = new SolidColorBrush(randomColor),
                });
            }
        }

        private void RemoveItems()
        {
            var remove = _panel.OverflowCount;

            _panel.Children.RemoveRange(
                _panel.Children.Count - remove,
                _panel.OverflowCount);
            _lastIndex -= remove;
        }

        private void Renumber()
        {
            var index = _firstIndex;

            foreach (TextBlock child in _panel.Children)
            {
                child.Text = "Item " + ++index;
            }
        }

        private void CheckPanel()
        {
            if (!_panel.IsFull)
            {
                CreateItems();
            }
            else if (_panel.OverflowCount > 1)
            {
                RemoveItems();
            }
        }
    }
}
{'fraction_non_alphanumeric': 0.08352706741946198, 'fraction_numerical': 0.0073065426768515445, 'mean_word_length': 1.3872374157748713, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7246410', 'n_tokens_mistral': 1581, 'n_tokens_neox': 1483, 'n_words': 333}