<commit_before>﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;

namespace Stylet.Xaml
{
    /// <summary>
    /// Turn a boolean value into a Visibility
    /// </summary>
    public class BoolToVisibilityConverter : DependencyObject, IValueConverter
    {
        /// <summary>
        /// Singleton instance of this converter. Usage e.g. Converter="{x:Static s:BoolToVisibilityConverter.Instance}"
        /// </summary>
        public static readonly BoolToVisibilityConverter Instance = new BoolToVisibilityConverter();

        /// <summary>
        /// Visibility to use if value is true
        /// </summary>
        public Visibility TrueVisibility
        {
            get { return (Visibility)GetValue(TrueVisibilityProperty); }
            set { SetValue(TrueVisibilityProperty, value); }
        }

        /// <summary>
        /// Property specifying the visibility to return when the parameter is true
        /// </summary>
        public static readonly DependencyProperty TrueVisibilityProperty =
            DependencyProperty.Register("TrueVisibility", typeof(Visibility), typeof(BoolToVisibilityConverter), new PropertyMetadata(Visibility.Visible));

        /// <summary>
        /// Visibility to use if value is false
        /// </summary>
        public Visibility FalseVisibility
        {
            get { return (Visibility)GetValue(FalseVisibilityProperty); }
            set { SetValue(FalseVisibilityProperty, value); }
        }

        /// <summary>
        /// Property specifying the visibility to return when the parameter is false
        /// </summary>
        public static readonly DependencyProperty FalseVisibilityProperty =
            DependencyProperty.Register("FalseVisibility", typeof(Visibility), typeof(BoolToVisibilityConverter), new PropertyMetadata(Visibility.Collapsed));


        /// <summary>
        /// Perform the conversion
        /// </summary>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool result;
            if (value == null)
            {
                result = false;
            }
            else if (value is bool)
            {
                result = (bool)value;
            }
            else if (value is IEnumerable)
            {
                result = ((IEnumerable)value).GetEnumerator().MoveNext();
            }
            else
            {
                // This fails if it an int can't be converter to it, or for many other reasons
                // Easiest is just to try it and see
                try
                {
                    result = !value.Equals(System.Convert.ChangeType((object)0, value.GetType()));
                }
                catch
                {
                    result = true; // Not null, didn't meet any other falsy behaviour
                }
            }

            return result ? this.TrueVisibility : this.FalseVisibility;
        }

        /// <summary>
        /// Perform the inverse conversion. Only valid if the value is bool
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(bool))
                throw new ArgumentException("Can't ConvertBack on BoolToVisibilityConverter when TargetType is not bool");

            if (!(value is Visibility))
                return null;

            var vis = (Visibility)value;

            if (vis == this.TrueVisibility)
                return true;<commit_msg>Update BoolToVisibilityConverter to be a bit more efficient<commit_after>﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Data;

namespace Stylet.Xaml
{
    /// <summary>
    /// Turn a boolean value into a Visibility
    /// </summary>
    public class BoolToVisibilityConverter : DependencyObject, IValueConverter
    {
        /// <summary>
        /// Singleton instance of this converter. Usage e.g. Converter="{x:Static s:BoolToVisibilityConverter.Instance}"
        /// </summary>
        public static readonly BoolToVisibilityConverter Instance = new BoolToVisibilityConverter();

        /// <summary>
        /// Visibility to use if value is true
        /// </summary>
        public Visibility TrueVisibility
        {
            get { return (Visibility)GetValue(TrueVisibilityProperty); }
            set { SetValue(TrueVisibilityProperty, value); }
        }

        /// <summary>
        /// Property specifying the visibility to return when the parameter is true
        /// </summary>
        public static readonly DependencyProperty TrueVisibilityProperty =
            DependencyProperty.Register("TrueVisibility", typeof(Visibility), typeof(BoolToVisibilityConverter), new PropertyMetadata(Visibility.Visible));

        /// <summary>
        /// Visibility to use if value is false
        /// </summary>
        public Visibility FalseVisibility
        {
            get { return (Visibility)GetValue(FalseVisibilityProperty); }
            set { SetValue(FalseVisibilityProperty, value); }
        }

        /// <summary>
        /// Property specifying the visibility to return when the parameter is false
        /// </summary>
        public static readonly DependencyProperty FalseVisibilityProperty =
            DependencyProperty.Register("FalseVisibility", typeof(Visibility), typeof(BoolToVisibilityConverter), new PropertyMetadata(Visibility.Collapsed));


        /// <summary>
        /// Perform the conversion
        /// </summary>
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            bool result;
            if (value == null)
            {
                result = false;
            }
            else if (value is bool)
            {
                result = (bool)value;
            }
            else if (value is IEnumerable)
            {
                result = ((IEnumerable)value).GetEnumerator().MoveNext();
            }
            else if (!(value is ValueType))
            {
                result = true; // Non-null non-enumerable reference type = true
            }
            // Value types from here on in
            else
            {
                // This fails if an int can't be converted to it, or for many other reasons
                // Easiest is just to try it and see
                try
                {
                    result = !value.Equals(System.Convert.ChangeType((object)0, value.GetType()));
                }
                catch
                {
                    result = true; // Not null, didn't meet any other falsy behaviour
                }
            }

            return result ? this.TrueVisibility : this.FalseVisibility;
        }

        /// <summary>
        /// Perform the inverse conversion. Only valid if the value is bool
        /// </summary>
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(bool))
                throw new ArgumentException("Can't ConvertBack on BoolToVisibilityConverter when TargetType is not bool");

            if (!(value is Visibility))
                return null;

            var vis = (Visibility)value;

            if (vis == this.TrueVisibility)
                return true;
{'fraction_non_alphanumeric': 0.08873538788522849, 'fraction_numerical': 0.00026567481402763017, 'mean_word_length': 1.7833641404805916, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 35, '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': '962569', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1639, 'n_words': 533}