<commit_before>using System;
using System.Linq;
using Eto.Forms;
using swc = System.Windows.Controls;
using sw = System.Windows;
using swd = System.Windows.Data;
using swm = System.Windows.Media;
using Eto.Drawing;

namespace Eto.Wpf.Forms
{
	public class TableLayoutHandler : WpfLayout<swc.Grid, TableLayout, TableLayout.ICallback>, TableLayout.IHandler
	{
		readonly swc.Border border;
		Size spacing;
		Control[,] controls;
		bool[] columnScale;
		bool[] rowScale;
		int lastColumnScale;
		int lastRowScale;
		bool inGroupBox;

		public TableLayoutHandler()
		{
			Control = new swc.Grid { SnapsToDevicePixels = true };
			border = new swc.Border();
			#pragma warning disable 612,618
			spacing = TableLayout.DefaultSpacing;
			border.Padding = TableLayout.DefaultPadding.ToWpf();
			#pragma warning restore 612,618
		}

		public Size Adjust { get; set; }

		public override sw.FrameworkElement ContainerControl
		{
			get { return border; }
		}

		public override Color BackgroundColor
		{
			get { return border.Background.ToEtoColor(); }
			set { border.Background = value.ToWpfBrush(Control.Background); }
		}

		public override sw.Size GetPreferredSize(sw.Size constraint)
		{
			if (columnScale == null || rowScale == null)
				return base.GetPreferredSize(constraint);
			var widths = new double[columnScale.Length];
			double height = 0;
			for (int y = 0; y < rowScale.Length; y++)
			{
				double maxHeight = 0;
				for (int x = 0; x < widths.Length; x++)
				{
					var childControl = controls[x, y].GetWpfFrameworkElement();
					if (childControl != null)
					{
						var preferredSize = childControl.GetPreferredSize(Conversions.PositiveInfinitySize);
						var margin = childControl.ContainerControl.Margin;
						widths[x] = Math.Max(widths[x], preferredSize.Width + margin.Horizontal());
						maxHeight = Math.Max(maxHeight, preferredSize.Height + margin.Vertical());
					}
				}
				height += maxHeight;
			}

			var size = new sw.Size(widths.Sum() + border.Padding.Horizontal(), height + border.Padding.Vertical());
			if (!double.IsNaN(PreferredSize.Width))
				size.Width = Math.Max(size.Width, PreferredSize.Width);
			if (!double.IsNaN(PreferredSize.Height))
				size.Height = Math.Max(size.Height, PreferredSize.Height);
			return size;
		}

		public void CreateControl(int cols, int rows)
		{
			controls = new Control[cols, rows];
			columnScale = new bool[cols];
			rowScale = new bool[rows];
			lastColumnScale = cols - 1;
			lastRowScale = rows - 1;
			for (int i = 0; i < cols; i++) Control.ColumnDefinitions.Add(new swc.ColumnDefinition { Width = GetColumnWidth(i) });
			for (int i = 0; i < rows; i++) Control.RowDefinitions.Add(new swc.RowDefinition { Height = GetRowHeight(i) });

			for (int y = 0; y < rows; y++)
				for (int x = 0; x < cols; x++)
					Control.Children.Add(EmptyCell(x, y));

			border.Child = Control;

			Control.SizeChanged += Control_SizeChanged;
			Control.Loaded += Control_SizeChanged;
		}

		void Control_SizeChanged(object sender, EventArgs e)
		{
			SetChildrenSizes();
		}

		sw.FrameworkElement EmptyCell(int x, int y)
		{
			var empty = new sw.FrameworkElement();
			swc.Grid.SetColumn(empty, x);
			swc.Grid.SetRow(empty, y);
			SetMargins(empty, x, y);
			return empty;
		}

		public override void OnLoadComplete(EventArgs e)
		{
			base.OnLoadComplete(e);
			inGroupBox = Widget.FindParent<GroupBox>() != null;
			SetMargins();

			if (Control.IsLoaded)
				SetScale();
		}

		public override void SetScale(bool xscale, bool yscale)
		{
			if (Widget.Loaded)
			{
				base.SetScale(xscale, yscale);
				SetScale();<commit_msg>Wpf: TableLayout should update child sizes each time layout is performed, not just when size changes.<commit_after>using System;
using System.Linq;
using Eto.Forms;
using swc = System.Windows.Controls;
using sw = System.Windows;
using swd = System.Windows.Data;
using swm = System.Windows.Media;
using Eto.Drawing;

namespace Eto.Wpf.Forms
{
	public class TableLayoutHandler : WpfLayout<swc.Grid, TableLayout, TableLayout.ICallback>, TableLayout.IHandler
	{
		readonly swc.Border border;
		Size spacing;
		Control[,] controls;
		bool[] columnScale;
		bool[] rowScale;
		int lastColumnScale;
		int lastRowScale;
		bool inGroupBox;

		public TableLayoutHandler()
		{
			Control = new swc.Grid { SnapsToDevicePixels = true };
			border = new swc.Border();
			#pragma warning disable 612,618
			spacing = TableLayout.DefaultSpacing;
			border.Padding = TableLayout.DefaultPadding.ToWpf();
			#pragma warning restore 612,618
		}

		public Size Adjust { get; set; }

		public override sw.FrameworkElement ContainerControl
		{
			get { return border; }
		}

		public override Color BackgroundColor
		{
			get { return border.Background.ToEtoColor(); }
			set { border.Background = value.ToWpfBrush(Control.Background); }
		}

		public override sw.Size GetPreferredSize(sw.Size constraint)
		{
			if (columnScale == null || rowScale == null)
				return base.GetPreferredSize(constraint);
			var widths = new double[columnScale.Length];
			double height = 0;
			for (int y = 0; y < rowScale.Length; y++)
			{
				double maxHeight = 0;
				for (int x = 0; x < widths.Length; x++)
				{
					var childControl = controls[x, y].GetWpfFrameworkElement();
					if (childControl != null)
					{
						var preferredSize = childControl.GetPreferredSize(Conversions.PositiveInfinitySize);
						var margin = childControl.ContainerControl.Margin;
						widths[x] = Math.Max(widths[x], preferredSize.Width + margin.Horizontal());
						maxHeight = Math.Max(maxHeight, preferredSize.Height + margin.Vertical());
					}
				}
				height += maxHeight;
			}

			var size = new sw.Size(widths.Sum() + border.Padding.Horizontal(), height + border.Padding.Vertical());
			if (!double.IsNaN(PreferredSize.Width))
				size.Width = Math.Max(size.Width, PreferredSize.Width);
			if (!double.IsNaN(PreferredSize.Height))
				size.Height = Math.Max(size.Height, PreferredSize.Height);
			return size;
		}

		public void CreateControl(int cols, int rows)
		{
			controls = new Control[cols, rows];
			columnScale = new bool[cols];
			rowScale = new bool[rows];
			lastColumnScale = cols - 1;
			lastRowScale = rows - 1;
			for (int i = 0; i < cols; i++) Control.ColumnDefinitions.Add(new swc.ColumnDefinition { Width = GetColumnWidth(i) });
			for (int i = 0; i < rows; i++) Control.RowDefinitions.Add(new swc.RowDefinition { Height = GetRowHeight(i) });

			for (int y = 0; y < rows; y++)
				for (int x = 0; x < cols; x++)
					Control.Children.Add(EmptyCell(x, y));

			border.Child = Control;

			Control.LayoutUpdated += (sender, args) => SetChildrenSizes();
		}

		sw.FrameworkElement EmptyCell(int x, int y)
		{
			var empty = new sw.FrameworkElement();
			swc.Grid.SetColumn(empty, x);
			swc.Grid.SetRow(empty, y);
			SetMargins(empty, x, y);
			return empty;
		}

		public override void OnLoadComplete(EventArgs e)
		{
			base.OnLoadComplete(e);
			inGroupBox = Widget.FindParent<GroupBox>() != null;
			SetMargins();

			if (Control.IsLoaded)
				SetScale();
		}

		public override void SetScale(bool xscale, bool yscale)
		{
			if (Widget.Loaded)
			{
				base.SetScale(xscale, yscale);
				SetScale();
{'fraction_non_alphanumeric': 0.12256267409470752, 'fraction_numerical': 0.006128133704735376, 'mean_word_length': 3.6299161831076723, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 8, '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': '6634821', 'n_tokens_mistral': 2749, 'n_tokens_neox': 2272, 'n_words': 574}