<commit_before>﻿using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Interop;
using System.Windows.Media;
using MahApps.Metro.Controls;
using MahApps.Metro.Native;
using Microsoft.Windows.Shell;

namespace MahApps.Metro.Behaviours
{
    /// <summary>
    /// With this class we can make custom window styles.
    /// </summary>
    public class BorderlessWindowBehavior : Behavior<Window>
    {
        private IntPtr handle;
        private HwndSource hwndSource;
        private WindowChrome windowChrome;
        private Thickness? savedBorderThickness = null;
        private Brush nonActiveBorderColor;
        private Brush savedBorderBrush = null;

        protected override void OnAttached()
        {
            // maybe this can change to set via window from a dependency property
            this.nonActiveBorderColor = new SolidColorBrush(Colors.Gray);
            this.nonActiveBorderColor.Freeze();

            windowChrome = new WindowChrome();
            windowChrome.ResizeBorderThickness = SystemParameters2.Current.WindowResizeBorderThickness;
            windowChrome.CaptionHeight = 0;
            windowChrome.CornerRadius = new CornerRadius(0);
            windowChrome.GlassFrameThickness = new Thickness(0);
            windowChrome.UseAeroCaptionButtons = false;
            var metroWindow = AssociatedObject as MetroWindow;
            windowChrome.IgnoreTaskbarOnMaximize = metroWindow != null && metroWindow.IgnoreTaskbarOnMaximize;
            AssociatedObject.SetValue(WindowChrome.WindowChromeProperty, windowChrome);

            // no transparany, because it hase more then one unwanted issues
            AssociatedObject.AllowsTransparency = false;
            AssociatedObject.WindowStyle = WindowStyle.None;
            savedBorderThickness = AssociatedObject.BorderThickness;

            AssociatedObject.Loaded += AssociatedObject_Loaded;
            AssociatedObject.Unloaded += AssociatedObject_Unloaded;
            AssociatedObject.SourceInitialized += AssociatedObject_SourceInitialized;
            AssociatedObject.StateChanged += AssociatedObject_StateChanged;
            AssociatedObject.Activated += AssociatedObject_Activated;
            AssociatedObject.Deactivated += AssociatedObject_Deactivated;

            // handle size to content (thanks @lynnx)
            var autoSizeToContent = AssociatedObject.SizeToContent == SizeToContent.WidthAndHeight;
            AssociatedObject.SizeToContent = SizeToContent.Height;
            AssociatedObject.SizeToContent = autoSizeToContent ? SizeToContent.WidthAndHeight : SizeToContent.Manual;
            AssociatedObject.IsVisibleChanged += AssociatedObject_IsVisibleChanged;

            // handle resize mode after loading the window
            Window.ResizeModeProperty.AddOwner(AssociatedObject.GetType(), new FrameworkPropertyMetadata(ResizeMode.CanResize, FrameworkPropertyMetadataOptions.AffectsMeasure, this.ResizeModePropertyChangedCallback));

            base.OnAttached();
        }

        private void ResizeModePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            var metroWindow = dependencyObject as MetroWindow;
            if (metroWindow != null && e.NewValue != e.OldValue)
            {
                this.HandleResizeMode(metroWindow, (ResizeMode)e.NewValue);
            }
        }

        /// <summary>
        /// handle the resize mode for the window
        /// </summary>
        private void HandleResizeMode(MetroWindow window, ResizeMode resizeMode)
        {
            switch (resizeMode)
            {
                case ResizeMode.NoResize:
                    window.ShowMaxRestoreButton = false;
                    window.ShowMinButton = false;
                    break;
                case ResizeMode.CanMinimize:
                    window.ShowMaxRestoreButton = false;
                    window.ShowMinButton = true;
                    break;

                case ResizeMode.CanResize:
                case ResizeMode.CanResizeWithGrip:
                    window.ShowMaxRestoreButton = true;
                    window.ShowMinButton = true;
                    break;
            }
        }

        private bool isCleanedUp;

        private void Cleanup()
        {
            if (!isCleanedUp)
            {
                isCleanedUp = true;

                // clean up events
                AssociatedObject.Loaded -= AssociatedObject_Loaded;
                AssociatedObject.Unloaded -= AssociatedObject_Unloaded;
                AssociatedObject.SourceInitialized -= AssociatedObject_SourceInitialized;
                AssociatedObject.StateChanged -= AssociatedObject_StateChanged;
                AssociatedObject.Activated -= AssociatedObject_Activated;
                AssociatedObject.Deactivated -= AssociatedObject_Deactivated;
                AssociatedObject.IsVisibleChanged -= AssociatedObject_IsVisibleChanged;
                if (hwndSource != null)
                {
                    hwndSource.RemoveHook(WindowProc);
                }
                windowChrome = null;
            }
        }

        protected override void OnDetaching()
        {
            Cleanup();
            base.OnDetaching();
        }

        private void AssociatedObject_Unloaded(object sender, RoutedEventArgs e)<commit_msg>fix adding the ResizeMode change callback (AddOwner is the wrong way)<commit_after>﻿using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Interop;
using System.Windows.Media;
using MahApps.Metro.Controls;
using MahApps.Metro.Native;
using Microsoft.Windows.Shell;

namespace MahApps.Metro.Behaviours
{
    /// <summary>
    /// With this class we can make custom window styles.
    /// </summary>
    public class BorderlessWindowBehavior : Behavior<Window>
    {
        private IntPtr handle;
        private HwndSource hwndSource;
        private WindowChrome windowChrome;
        private Thickness? savedBorderThickness = null;
        private Brush nonActiveBorderColor;
        private Brush savedBorderBrush = null;

        protected override void OnAttached()
        {
            // maybe this can change to set via window from a dependency property
            this.nonActiveBorderColor = new SolidColorBrush(Colors.Gray);
            this.nonActiveBorderColor.Freeze();

            windowChrome = new WindowChrome();
            windowChrome.ResizeBorderThickness = SystemParameters2.Current.WindowResizeBorderThickness;
            windowChrome.CaptionHeight = 0;
            windowChrome.CornerRadius = new CornerRadius(0);
            windowChrome.GlassFrameThickness = new Thickness(0);
            windowChrome.UseAeroCaptionButtons = false;
            var metroWindow = AssociatedObject as MetroWindow;
            windowChrome.IgnoreTaskbarOnMaximize = metroWindow != null && metroWindow.IgnoreTaskbarOnMaximize;
            AssociatedObject.SetValue(WindowChrome.WindowChromeProperty, windowChrome);

            // no transparany, because it hase more then one unwanted issues
            AssociatedObject.AllowsTransparency = false;
            AssociatedObject.WindowStyle = WindowStyle.None;
            savedBorderThickness = AssociatedObject.BorderThickness;

            AssociatedObject.Loaded += AssociatedObject_Loaded;
            AssociatedObject.Unloaded += AssociatedObject_Unloaded;
            AssociatedObject.SourceInitialized += AssociatedObject_SourceInitialized;
            AssociatedObject.StateChanged += AssociatedObject_StateChanged;
            AssociatedObject.Activated += AssociatedObject_Activated;
            AssociatedObject.Deactivated += AssociatedObject_Deactivated;

            // handle size to content (thanks @lynnx)
            var autoSizeToContent = AssociatedObject.SizeToContent == SizeToContent.WidthAndHeight;
            AssociatedObject.SizeToContent = SizeToContent.Height;
            AssociatedObject.SizeToContent = autoSizeToContent ? SizeToContent.WidthAndHeight : SizeToContent.Manual;
            AssociatedObject.IsVisibleChanged += AssociatedObject_IsVisibleChanged;

            // handle resize mode after loading the window
            System.ComponentModel.DependencyPropertyDescriptor.FromProperty(Window.ResizeModeProperty, typeof(Window)). AddValueChanged(AssociatedObject, ResizeModePropertyChangedCallback);
            
            base.OnAttached();
        }

        private void ResizeModePropertyChangedCallback(object sender, EventArgs e)
        {
            var metroWindow = sender as MetroWindow;
            if (metroWindow != null)
            {
                this.HandleResizeMode(metroWindow, metroWindow.ResizeMode);
            }
        }

        /// <summary>
        /// handle the resize mode for the window
        /// </summary>
        private void HandleResizeMode(MetroWindow window, ResizeMode resizeMode)
        {
            switch (resizeMode)
            {
                case ResizeMode.NoResize:
                    window.ShowMaxRestoreButton = false;
                    window.ShowMinButton = false;
                    break;
                case ResizeMode.CanMinimize:
                    window.ShowMaxRestoreButton = false;
                    window.ShowMinButton = true;
                    break;

                case ResizeMode.CanResize:
                case ResizeMode.CanResizeWithGrip:
                    window.ShowMaxRestoreButton = true;
                    window.ShowMinButton = true;
                    break;
            }
        }

        private bool isCleanedUp;

        private void Cleanup()
        {
            if (!isCleanedUp)
            {
                isCleanedUp = true;

                // clean up events
                System.ComponentModel.DependencyPropertyDescriptor.FromProperty(Window.ResizeModeProperty, typeof(Window)).RemoveValueChanged(AssociatedObject, ResizeModePropertyChangedCallback);
                AssociatedObject.Loaded -= AssociatedObject_Loaded;
                AssociatedObject.Unloaded -= AssociatedObject_Unloaded;
                AssociatedObject.SourceInitialized -= AssociatedObject_SourceInitialized;
                AssociatedObject.StateChanged -= AssociatedObject_StateChanged;
                AssociatedObject.Activated -= AssociatedObject_Activated;
                AssociatedObject.Deactivated -= AssociatedObject_Deactivated;
                AssociatedObject.IsVisibleChanged -= AssociatedObject_IsVisibleChanged;
                if (hwndSource != null)
                {
                    hwndSource.RemoveHook(WindowProc);
                }
                windowChrome = null;
            }
        }

        protected override void OnDetaching()
        {
            Cleanup();
            base.OnDetaching();
        }

        private void AssociatedObject_Unloaded(object sender, RoutedEventArgs e)
{'fraction_non_alphanumeric': 0.06387764282501125, 'fraction_numerical': 0.0007197480881691408, 'mean_word_length': 2.2304562627143274, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '4038094', 'n_tokens_mistral': 2716, 'n_tokens_neox': 2563, 'n_words': 539}