<commit_before>﻿using System;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using MahApps.Metro.Native;

namespace MahApps.Metro.Controls
{
    public interface IWindowPlacementSettings
    {
        WINDOWPLACEMENT? Placement { get; set; }

        /// <summary>
        /// Refreshes the application settings property values from persistent storage.
        /// </summary>
        void Reload();

        /// <summary>
        /// Upgrades the application settings on loading.
        /// </summary>
        [DefaultValue(true)]
        bool UpgradeSettings { get; set; }

        /// <summary>
        /// Updates application settings to reflect a more recent installation of the application.
        /// </summary>
        void Upgrade();

        /// <summary>
        /// Stores the current values of the settings properties.
        /// </summary>
        void Save();
    }

    /// <summary>
    /// this settings class is the default way to save the placement of the window
    /// </summary>
    internal class WindowApplicationSettings : ApplicationSettingsBase, IWindowPlacementSettings
    {
        public WindowApplicationSettings(Window window)
            : base(window.GetType().FullName) {
        }

        [UserScopedSetting]
        public WINDOWPLACEMENT? Placement {
            get {
                if (this["Placement"] != null) {
                    return ((WINDOWPLACEMENT)this["Placement"]);
                }
                return null;
            }
            set {
                this["Placement"] = value;
            }
        }

        /// <summary>
        /// Upgrades the application settings on loading.
        /// </summary>
        [DefaultValue(true)]
        public bool UpgradeSettings { get; set; }
    }
    
    public class WindowSettings
    {
        public static readonly DependencyProperty WindowPlacementSettingsProperty = DependencyProperty.RegisterAttached("WindowPlacementSettings", typeof(IWindowPlacementSettings), typeof(WindowSettings), new FrameworkPropertyMetadata(OnWindowPlacementSettingsInvalidated));

        public static void SetSave(DependencyObject dependencyObject, IWindowPlacementSettings windowPlacementSettings)
        {
            dependencyObject.SetValue(WindowPlacementSettingsProperty, windowPlacementSettings);
        }

        private static void OnWindowPlacementSettingsInvalidated(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) {
            var window = dependencyObject as Window;
            if (window == null || !(e.NewValue is IWindowPlacementSettings))
                return;

            var windowSettings = new WindowSettings(window, (IWindowPlacementSettings)e.NewValue);
            windowSettings.Attach();
        }

        private Window _window;
        private IWindowPlacementSettings _settings;

        public WindowSettings(Window window, IWindowPlacementSettings windowPlacementSettings)
        {
            _window = window;
            _settings = windowPlacementSettings;
        }

        protected virtual void LoadWindowState()
        {
            if (_settings == null) return;
            _settings.Reload();

            // check for existing placement and prevent empty bounds
            if (_settings.Placement == null || _settings.Placement.Value.normalPosition.IsEmpty) return;

            try
            {
                var wp = _settings.Placement.Value;

                wp.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
                wp.flags = 0;
                wp.showCmd = (wp.showCmd == (int)Constants.ShowWindowCommands.SW_SHOWMINIMIZED ? (int)Constants.ShowWindowCommands.SW_SHOWNORMAL : wp.showCmd);
                var hwnd = new WindowInteropHelper(_window).Handle;
                UnsafeNativeMethods.SetWindowPlacement(hwnd, ref wp);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to load window state:\r\n{0}", ex);
            }
        }

        protected virtual void SaveWindowState()
        {
            if (_settings == null) return;
            var hwnd = new WindowInteropHelper(_window).Handle;
            var wp = new WINDOWPLACEMENT();
            wp.length = Marshal.SizeOf(wp);
            UnsafeNativeMethods.GetWindowPlacement(hwnd, ref wp);
            // check for saveable values
            if (wp.showCmd != (int)Constants.ShowWindowCommands.SW_HIDE && wp.length > 0 && !wp.normalPosition.IsEmpty)
            {
                _settings.Placement = wp;
            }
            _settings.Save();
        }

        private void Attach()
        {
            if (_window != null)
            {<commit_msg>use GetWindowRect to save possible snapped window<commit_after>﻿using System;
using System.ComponentModel;
using System.Configuration;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using MahApps.Metro.Native;

namespace MahApps.Metro.Controls
{
    public interface IWindowPlacementSettings
    {
        WINDOWPLACEMENT? Placement { get; set; }

        /// <summary>
        /// Refreshes the application settings property values from persistent storage.
        /// </summary>
        void Reload();

        /// <summary>
        /// Upgrades the application settings on loading.
        /// </summary>
        [DefaultValue(true)]
        bool UpgradeSettings { get; set; }

        /// <summary>
        /// Updates application settings to reflect a more recent installation of the application.
        /// </summary>
        void Upgrade();

        /// <summary>
        /// Stores the current values of the settings properties.
        /// </summary>
        void Save();
    }

    /// <summary>
    /// this settings class is the default way to save the placement of the window
    /// </summary>
    internal class WindowApplicationSettings : ApplicationSettingsBase, IWindowPlacementSettings
    {
        public WindowApplicationSettings(Window window)
            : base(window.GetType().FullName) {
        }

        [UserScopedSetting]
        public WINDOWPLACEMENT? Placement {
            get {
                if (this["Placement"] != null) {
                    return ((WINDOWPLACEMENT)this["Placement"]);
                }
                return null;
            }
            set {
                this["Placement"] = value;
            }
        }

        /// <summary>
        /// Upgrades the application settings on loading.
        /// </summary>
        [DefaultValue(true)]
        public bool UpgradeSettings { get; set; }
    }
    
    public class WindowSettings
    {
        public static readonly DependencyProperty WindowPlacementSettingsProperty = DependencyProperty.RegisterAttached("WindowPlacementSettings", typeof(IWindowPlacementSettings), typeof(WindowSettings), new FrameworkPropertyMetadata(OnWindowPlacementSettingsInvalidated));

        public static void SetSave(DependencyObject dependencyObject, IWindowPlacementSettings windowPlacementSettings)
        {
            dependencyObject.SetValue(WindowPlacementSettingsProperty, windowPlacementSettings);
        }

        private static void OnWindowPlacementSettingsInvalidated(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e) {
            var window = dependencyObject as Window;
            if (window == null || !(e.NewValue is IWindowPlacementSettings))
                return;

            var windowSettings = new WindowSettings(window, (IWindowPlacementSettings)e.NewValue);
            windowSettings.Attach();
        }

        private Window _window;
        private IWindowPlacementSettings _settings;

        public WindowSettings(Window window, IWindowPlacementSettings windowPlacementSettings)
        {
            _window = window;
            _settings = windowPlacementSettings;
        }

        protected virtual void LoadWindowState()
        {
            if (_settings == null) return;
            _settings.Reload();

            // check for existing placement and prevent empty bounds
            if (_settings.Placement == null || _settings.Placement.Value.normalPosition.IsEmpty) return;

            try
            {
                var wp = _settings.Placement.Value;

                wp.length = Marshal.SizeOf(typeof(WINDOWPLACEMENT));
                wp.flags = 0;
                wp.showCmd = (wp.showCmd == (int)Constants.ShowWindowCommands.SW_SHOWMINIMIZED ? (int)Constants.ShowWindowCommands.SW_SHOWNORMAL : wp.showCmd);
                var hwnd = new WindowInteropHelper(_window).Handle;
                UnsafeNativeMethods.SetWindowPlacement(hwnd, ref wp);
            }
            catch (Exception ex)
            {
                Debug.WriteLine("Failed to load window state:\r\n{0}", ex);
            }
        }

        protected virtual void SaveWindowState()
        {
            if (_settings == null) return;
            var hwnd = new WindowInteropHelper(_window).Handle;
            var wp = new WINDOWPLACEMENT();
            wp.length = Marshal.SizeOf(wp);
            UnsafeNativeMethods.GetWindowPlacement(hwnd, ref wp);
            // check for saveable values
            if (wp.showCmd != (int)Constants.ShowWindowCommands.SW_HIDE && wp.length > 0)
            {
                if (wp.showCmd == (int)Constants.ShowWindowCommands.SW_NORMAL)
                {
                    RECT rect;
                    if (UnsafeNativeMethods.GetWindowRect(hwnd, out rect))
                    {
                        wp.normalPosition = rect;
                    }
                }
                if (!wp.normalPosition.IsEmpty)
                {
                    _settings.Placement = wp;
                }
            }
            _settings.Save();
        }

        private void Attach()
        {
            if (_window != null)
            {
{'fraction_non_alphanumeric': 0.08608478802992518, 'fraction_numerical': 0.000598503740648379, 'mean_word_length': 1.9531664212076583, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 29, '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': '2125539', 'n_tokens_mistral': 2455, 'n_tokens_neox': 2385, 'n_words': 590}