<commit_before>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using ConsoleFramework.Controls;
using ConsoleFramework.Core;
using ConsoleFramework.Events;
using ConsoleFramework.Native;
using ConsoleFramework.Rendering;
#if !WIN32
using Mono.Unix;
using Mono.Unix.Native;
#endif
using Xaml;

namespace ConsoleFramework
{
    public class TerminalSizeChangedEventArgs : EventArgs
    {
        public readonly int Width;
        public readonly int Height;

        public TerminalSizeChangedEventArgs( int width, int height ) {
            Width = width;
            Height = height;
        }
    }

    public delegate void TerminalSizeChangedHandler( object sender, TerminalSizeChangedEventArgs args );

    /// <summary>
    /// Console application entry point.
    /// Encapsulates messages loop and application lifecycle.
    /// Supports Windows and Linux.
    /// </summary>
    public sealed class ConsoleApplication : IDisposable
    {
        private bool maximized;
        private Size savedBufferSize;
        private Rect savedWindowRect;

        private IntPtr consoleWindowHwnd;
        private IntPtr getConsoleWindowHwnd( ) {
            if ( IntPtr.Zero == consoleWindowHwnd ) {
                consoleWindowHwnd = Win32.GetConsoleWindow(  );
            }
            return consoleWindowHwnd;
        }

        /// <summary>
        /// Maximizes the terminal window size and terminal buffer size.
        /// Current size is stored.
        /// </summary>
        public void Maximize( ) {
            if ( usingLinux ) {
				// Doesn't work in Konsole
				Console.Write ("\x1B[9;1t");
				return;
			}
            
            if ( maximized ) return;
            //
            savedBufferSize = new Size(Console.BufferWidth, Console.BufferHeight);
            Win32.SendMessage(getConsoleWindowHwnd(), Win32.WM_SYSCOMMAND,
                Win32.SC_MAXIMIZE, IntPtr.Zero);
            int maxWidth = Console.LargestWindowWidth;
            int maxHeight = Console.LargestWindowHeight;
            Console.SetWindowPosition( 0, 0 );
            Console.SetBufferSize(maxWidth, maxHeight);
            Console.SetWindowSize(maxWidth, maxHeight);

            // Apply new sizes to Canvas
            CanvasSize = new Size(maxWidth, maxHeight);
            renderer.RootElementRect = new Rect(canvas.Size);
            renderer.UpdateLayout();

            maximized = true;
        }

        /// <summary>
        /// Restores the terminal window size and terminal buffer to stored state.
        /// </summary>
        public void Restore( ) {
            if (usingLinux) {
				// Doesn't work in Konsole
				Console.Write ("\x1B[9;0t");
				return;
			}

            if ( !maximized ) return;
            //
            Win32.SendMessage(getConsoleWindowHwnd(), Win32.WM_SYSCOMMAND, 
                Win32.SC_RESTORE, IntPtr.Zero);
            Console.SetWindowPosition( 0, 0 );

            // Get largest size again - because resolution of screen can change
            // between maximize and restore calls
            int maxWidth = Console.LargestWindowWidth;
            int maxHeight = Console.LargestWindowHeight;

            Console.SetWindowSize(
                Math.Min( savedWindowRect.Width, maxWidth),
                Math.Min(savedWindowRect.Height, maxHeight));
            Console.SetWindowPosition(savedWindowRect.Left, savedWindowRect.Top);

            // Duo issue with console in Windows 10 Technical Preview (unexpected decreasing Console.BufferSize)
            // we should take Max of window width. See https://github.com/elw00d/consoleframework/issues/21
            Console.SetBufferSize(
                Math.Max( savedBufferSize.Width, Console.WindowWidth ),
                Math.Max( savedBufferSize.Height, Console.WindowHeight )
            );

            // Apply new sizes to Canvas
            CanvasSize = new Size(savedWindowRect.Width, savedWindowRect.Height);
            renderer.RootElementRect = new Rect(canvas.Size);
            renderer.UpdateLayout();

            maximized = false;
        }

        /// <summary>
        /// Fires when console buffer size is changed.
        /// </summary>
        public event TerminalSizeChangedHandler TerminalSizeChanged;

        /// <summary>
        /// Default TerminalSizeChanged event handler. Invoked when
        /// initial CanvasSize and RootElementRect are empty and no another
        /// TerminalSizeChanged handler is attached.
        /// </summary><commit_msg>Removed workaround for Windows 10 Technical Preview (seems fixed in Windows 10 Release).<commit_after>﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;
using ConsoleFramework.Controls;
using ConsoleFramework.Core;
using ConsoleFramework.Events;
using ConsoleFramework.Native;
using ConsoleFramework.Rendering;
#if !WIN32
using Mono.Unix;
using Mono.Unix.Native;
#endif
using Xaml;

namespace ConsoleFramework
{
    public class TerminalSizeChangedEventArgs : EventArgs
    {
        public readonly int Width;
        public readonly int Height;

        public TerminalSizeChangedEventArgs( int width, int height ) {
            Width = width;
            Height = height;
        }
    }

    public delegate void TerminalSizeChangedHandler( object sender, TerminalSizeChangedEventArgs args );

    /// <summary>
    /// Console application entry point.
    /// Encapsulates messages loop and application lifecycle.
    /// Supports Windows and Linux.
    /// </summary>
    public sealed class ConsoleApplication : IDisposable
    {
        private bool maximized;
        private Size savedBufferSize;
        private Rect savedWindowRect;

        private IntPtr consoleWindowHwnd;
        private IntPtr getConsoleWindowHwnd( ) {
            if ( IntPtr.Zero == consoleWindowHwnd ) {
                consoleWindowHwnd = Win32.GetConsoleWindow(  );
            }
            return consoleWindowHwnd;
        }

        /// <summary>
        /// Maximizes the terminal window size and terminal buffer size.
        /// Current size is stored.
        /// </summary>
        public void Maximize( ) {
            if ( usingLinux ) {
				// Doesn't work in Konsole
				Console.Write ("\x1B[9;1t");
				return;
			}
            
            if ( maximized ) return;
            //
            savedBufferSize = new Size(Console.BufferWidth, Console.BufferHeight);
            Win32.SendMessage(getConsoleWindowHwnd(), Win32.WM_SYSCOMMAND,
                Win32.SC_MAXIMIZE, IntPtr.Zero);
            int maxWidth = Console.LargestWindowWidth;
            int maxHeight = Console.LargestWindowHeight;
            Console.SetWindowPosition( 0, 0 );
            Console.SetBufferSize(maxWidth, maxHeight);
            Console.SetWindowSize(maxWidth, maxHeight);

            // Apply new sizes to Canvas
            CanvasSize = new Size(maxWidth, maxHeight);
            renderer.RootElementRect = new Rect(canvas.Size);
            renderer.UpdateLayout();

            maximized = true;
        }

        /// <summary>
        /// Restores the terminal window size and terminal buffer to stored state.
        /// </summary>
        public void Restore( ) {
            if (usingLinux) {
				// Doesn't work in Konsole
				Console.Write ("\x1B[9;0t");
				return;
			}

            if ( !maximized ) return;
            //
            Win32.SendMessage(getConsoleWindowHwnd(), Win32.WM_SYSCOMMAND, 
                Win32.SC_RESTORE, IntPtr.Zero);
            Console.SetWindowPosition( 0, 0 );

            // Get largest size again - because resolution of screen can change
            // between maximize and restore calls
            int maxWidth = Console.LargestWindowWidth;
            int maxHeight = Console.LargestWindowHeight;

            Console.SetWindowSize(
                Math.Min( savedWindowRect.Width, maxWidth),
                Math.Min(savedWindowRect.Height, maxHeight));
            Console.SetWindowPosition(savedWindowRect.Left, savedWindowRect.Top);

            // Apply new sizes to Canvas
            CanvasSize = new Size(savedWindowRect.Width, savedWindowRect.Height);
            renderer.RootElementRect = new Rect(canvas.Size);
            renderer.UpdateLayout();

            maximized = false;
        }

        /// <summary>
        /// Fires when console buffer size is changed.
        /// </summary>
        public event TerminalSizeChangedHandler TerminalSizeChanged;

        /// <summary>
        /// Default TerminalSizeChanged event handler. Invoked when
        /// initial CanvasSize and RootElementRect are empty and no another
        /// TerminalSizeChanged handler is attached.
        /// </summary>
{'fraction_non_alphanumeric': 0.08440532611148725, 'fraction_numerical': 0.00699616339426766, 'mean_word_length': 2.0752949340735602, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 23, 'https://': 1, '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': '3089777', 'n_tokens_mistral': 2351, 'n_tokens_neox': 2195, 'n_words': 609}