<commit_before>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eto.Forms;
using Eto.Drawing;
using sw = System.Windows;
using swi = System.Windows.Input;
using swc = System.Windows.Controls;
using swm = System.Windows.Media;

namespace Eto.Platform.Wpf.Forms
{
	public interface IWpfFrameworkElement
	{
		sw.Size GetPreferredSize (sw.Size? constraint);
		sw.FrameworkElement ContainerControl { get; }
	}

	public static class ControlExtensions
	{
		public static sw.FrameworkElement GetContainerControl (this Control control)
		{
			var handler = control.Handler as IWpfFrameworkElement;
			if (handler != null)
				return handler.ContainerControl;
			else
				return control.ControlObject as sw.FrameworkElement;
		}

        public static sw.Size GetPreferredSize (this Control control, sw.Size? available = null)
        {
			if (control != null) {
				var handler = control.Handler as IWpfFrameworkElement;
				if (handler != null)
					return handler.GetPreferredSize (available);
			}
			return sw.Size.Empty;
        }
	}

    public static class WpfFrameworkElementHelper
    {
        public static bool ShouldCaptureMouse;
    }

	public abstract class WpfFrameworkElement<T, W> : WidgetHandler<T, W>, IControl, IWpfFrameworkElement
		where T : System.Windows.FrameworkElement
		where W : Control
	{
		Size? size;
		double preferredWidth = double.NaN;
		double preferredHeight = double.NaN;
		Size? newSize;
		Cursor cursor;
		bool loaded;
		bool isMouseOver;
		bool isMouseCaptured;

		public abstract Color BackgroundColor
		{
			get;
			set;
		}

		public virtual bool UseMousePreview { get { return false; } }

		public virtual sw.FrameworkElement ContainerControl
		{
			get { return this.Control; }
		}

		public virtual Size Size
		{
			get {
				var newSize = this.newSize;
				if (!Widget.Loaded && size != null) return size.Value;
				else if (newSize != null) return newSize.Value;
				else return Conversions.GetSize (Control); 
			}
			set {
				size = value;
				preferredWidth = value.Width == -1 ? double.NaN : (double)value.Width;
				preferredHeight = value.Height == -1 ? double.NaN : (double)value.Height;
				Conversions.SetSize (Control, value); 
			}
		}

		public virtual sw.Size GetPreferredSize (sw.Size? constraint = null)
		{
			if (double.IsNaN(preferredWidth) || double.IsNaN(preferredHeight) || constraint != null) {
				ContainerControl.Measure (constraint ?? new sw.Size (double.PositiveInfinity, double.PositiveInfinity));
				if (double.IsNaN (preferredWidth))
					preferredWidth = ContainerControl.DesiredSize.Width;
				if (double.IsNaN (preferredHeight))
					preferredHeight = ContainerControl.DesiredSize.Height;
			}
			return new sw.Size (preferredWidth, preferredHeight);
		}<commit_msg>Wpf: Fix getting size of control before it is loaded/shown<commit_after>using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Eto.Forms;
using Eto.Drawing;
using sw = System.Windows;
using swi = System.Windows.Input;
using swc = System.Windows.Controls;
using swm = System.Windows.Media;

namespace Eto.Platform.Wpf.Forms
{
	public interface IWpfFrameworkElement
	{
		sw.Size GetPreferredSize (sw.Size? constraint);
		sw.FrameworkElement ContainerControl { get; }
	}

	public static class ControlExtensions
	{
		public static sw.FrameworkElement GetContainerControl (this Control control)
		{
			var handler = control.Handler as IWpfFrameworkElement;
			if (handler != null)
				return handler.ContainerControl;
			else
				return control.ControlObject as sw.FrameworkElement;
		}

        public static sw.Size GetPreferredSize (this Control control, sw.Size? available = null)
        {
			if (control != null) {
				var handler = control.Handler as IWpfFrameworkElement;
				if (handler != null)
					return handler.GetPreferredSize (available);
			}
			return sw.Size.Empty;
        }
	}

    public static class WpfFrameworkElementHelper
    {
        public static bool ShouldCaptureMouse;
    }

	public abstract class WpfFrameworkElement<T, W> : WidgetHandler<T, W>, IControl, IWpfFrameworkElement
		where T : System.Windows.FrameworkElement
		where W : Control
	{
		Size? size;
		double preferredWidth = double.NaN;
		double preferredHeight = double.NaN;
		Size? newSize;
		Cursor cursor;
		bool loaded;
		bool isMouseOver;
		bool isMouseCaptured;

		public abstract Color BackgroundColor
		{
			get;
			set;
		}

		public virtual bool UseMousePreview { get { return false; } }

		public virtual sw.FrameworkElement ContainerControl
		{
			get { return this.Control; }
		}

		public virtual Size Size
		{
			get {
				var newSize = this.newSize;
				if (!Control.IsLoaded && size != null) return size.Value;
				else if (newSize != null) return newSize.Value;
				else return Conversions.GetSize (Control); 
			}
			set {
				size = value;
				preferredWidth = value.Width == -1 ? double.NaN : (double)value.Width;
				preferredHeight = value.Height == -1 ? double.NaN : (double)value.Height;
				Conversions.SetSize (Control, value); 
			}
		}

		public virtual sw.Size GetPreferredSize (sw.Size? constraint = null)
		{
			if (double.IsNaN(preferredWidth) || double.IsNaN(preferredHeight) || constraint != null) {
				ContainerControl.Measure (constraint ?? new sw.Size (double.PositiveInfinity, double.PositiveInfinity));
				if (double.IsNaN (preferredWidth))
					preferredWidth = ContainerControl.DesiredSize.Width;
				if (double.IsNaN (preferredHeight))
					preferredHeight = ContainerControl.DesiredSize.Height;
			}
			return new sw.Size (preferredWidth, preferredHeight);
		}
{'fraction_non_alphanumeric': 0.09291562833748665, 'fraction_numerical': 0.000711997152011392, 'mean_word_length': 3.6399669694467383, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '5825983', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1641, 'n_words': 453}