<commit_before>﻿using System;
using System.ComponentModel;
using System.Composition.Hosting;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Xml.Linq;
using Avalon.Windows.Controls;
using Microsoft.Practices.ServiceLocation;
using RoslynPad.UI;
using RoslynPad.Utilities;
using Xceed.Wpf.AvalonDock;
using Xceed.Wpf.AvalonDock.Layout.Serialization;

namespace RoslynPad
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private readonly MainViewModel _viewModel;
        private bool _isClosing;
        private bool _isClosed;

        internal MainWindow()
        {
            var container = new ContainerConfiguration()
                .WithAssembly(typeof(MainViewModel).Assembly)   // RoslynPad.Common.UI
                .WithAssembly(typeof(MainWindow).Assembly);     // RoslynPad
            var locator = container.CreateContainer().GetExport<IServiceLocator>();

            _viewModel = locator.GetInstance<MainViewModel>();
            _viewModel.Initialize();

            DataContext = _viewModel;
            InitializeComponent();
            DocumentsPane.ToggleAutoHide();

            LoadWindowLayout();
            LoadDockLayout();
        }

        protected override async void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            if (!_isClosing)
            {
                SaveDockLayout();
                SaveWindowLayout();
                Properties.Settings.Default.Save();

                _isClosing = true;
                IsEnabled = false;
                e.Cancel = true;

                try
                {
                    await Task.Run(() => _viewModel.OnExit()).ConfigureAwait(true);
                }
                catch
                {
                    // ignored
                }

                _isClosed = true;
                Close();
            }
            else
            {
                e.Cancel = !_isClosed;
            }
        }

        private void LoadWindowLayout()
        {
            var boundsString = Properties.Settings.Default.WindowBounds;
            if (!string.IsNullOrEmpty(boundsString))
            {
                var bounds = Rect.Parse(boundsString);
                if (bounds != default(Rect))
                {
                    Left = bounds.Left;
                    Top = bounds.Top;
                    Width = bounds.Width;
                    Height = bounds.Height;
                }
            }

            WindowState state;
            if (Enum.TryParse(Properties.Settings.Default.WindowState, out state) &&
                state != WindowState.Minimized)
            {
                WindowState = state;
            }
        }

        private void SaveWindowLayout()
        {
            Properties.Settings.Default.WindowBounds = RestoreBounds.ToString();
            Properties.Settings.Default.WindowState = WindowState.ToString();
        }

        private void LoadDockLayout()
        {
            var layout = Properties.Settings.Default.DockLayout;
            if (string.IsNullOrEmpty(layout)) return;

            var serializer = new XmlLayoutSerializer(DockingManager);
            var reader = new StringReader(layout);
            serializer.Deserialize(reader);
        }

        private void SaveDockLayout()
        {
            var serializer = new XmlLayoutSerializer(DockingManager);
            var document = new XDocument();
            using (var writer = document.CreateWriter())
            {
                serializer.Serialize(writer);
            }
            document.Root?.Element("FloatingWindows")?.Remove();
            Properties.Settings.Default.DockLayout = document.ToString();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            Application.Current.Shutdown();<commit_msg>Use InvariantCulture when calling Rect.ToString()<commit_after>﻿using System;
using System.ComponentModel;
using System.Composition.Hosting;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Xml.Linq;
using Avalon.Windows.Controls;
using Microsoft.Practices.ServiceLocation;
using RoslynPad.UI;
using RoslynPad.Utilities;
using Xceed.Wpf.AvalonDock;
using Xceed.Wpf.AvalonDock.Layout.Serialization;

namespace RoslynPad
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private readonly MainViewModel _viewModel;
        private bool _isClosing;
        private bool _isClosed;

        internal MainWindow()
        {
            var container = new ContainerConfiguration()
                .WithAssembly(typeof(MainViewModel).Assembly)   // RoslynPad.Common.UI
                .WithAssembly(typeof(MainWindow).Assembly);     // RoslynPad
            var locator = container.CreateContainer().GetExport<IServiceLocator>();

            _viewModel = locator.GetInstance<MainViewModel>();
            _viewModel.Initialize();

            DataContext = _viewModel;
            InitializeComponent();
            DocumentsPane.ToggleAutoHide();

            LoadWindowLayout();
            LoadDockLayout();
        }

        protected override async void OnClosing(CancelEventArgs e)
        {
            base.OnClosing(e);

            if (!_isClosing)
            {
                SaveDockLayout();
                SaveWindowLayout();
                Properties.Settings.Default.Save();

                _isClosing = true;
                IsEnabled = false;
                e.Cancel = true;

                try
                {
                    await Task.Run(() => _viewModel.OnExit()).ConfigureAwait(true);
                }
                catch
                {
                    // ignored
                }

                _isClosed = true;
                Close();
            }
            else
            {
                e.Cancel = !_isClosed;
            }
        }

        private void LoadWindowLayout()
        {
            var boundsString = Properties.Settings.Default.WindowBounds;
            if (!string.IsNullOrEmpty(boundsString))
            {
                var bounds = Rect.Parse(boundsString);
                if (bounds != default(Rect))
                {
                    Left = bounds.Left;
                    Top = bounds.Top;
                    Width = bounds.Width;
                    Height = bounds.Height;
                }
            }

            WindowState state;
            if (Enum.TryParse(Properties.Settings.Default.WindowState, out state) &&
                state != WindowState.Minimized)
            {
                WindowState = state;
            }
        }

        private void SaveWindowLayout()
        {
            Properties.Settings.Default.WindowBounds = RestoreBounds.ToString(CultureInfo.InvariantCulture);
            Properties.Settings.Default.WindowState = WindowState.ToString();
        }

        private void LoadDockLayout()
        {
            var layout = Properties.Settings.Default.DockLayout;
            if (string.IsNullOrEmpty(layout)) return;

            var serializer = new XmlLayoutSerializer(DockingManager);
            var reader = new StringReader(layout);
            serializer.Deserialize(reader);
        }

        private void SaveDockLayout()
        {
            var serializer = new XmlLayoutSerializer(DockingManager);
            var document = new XDocument();
            using (var writer = document.CreateWriter())
            {
                serializer.Serialize(writer);
            }
            document.Root?.Element("FloatingWindows")?.Remove();
            Properties.Settings.Default.DockLayout = document.ToString();
        }

        protected override void OnClosed(EventArgs e)
        {
            base.OnClosed(e);

            Application.Current.Shutdown();
{'fraction_non_alphanumeric': 0.08733087330873308, 'fraction_numerical': 0.0, 'mean_word_length': 1.5911408540471639, 'pattern_counts': {'":': 0, '<': 11, '<?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': '2469213', 'n_tokens_mistral': 2012, 'n_tokens_neox': 1913, 'n_words': 379}