<commit_before>﻿// Copyright (c) 2007-2016 ppy Pty Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using osu.Framework.Desktop.Input.Handlers.Keyboard;
using osu.Framework.Desktop.Input.Handlers.Mouse;
using osu.Framework.Desktop.Platform.Windows.Native;
using osu.Framework.Input.Handlers;
using OpenTK.Graphics;
using NativeWindow = OpenTK.NativeWindow;

namespace osu.Framework.Desktop.Platform.Windows
{
    public class WindowsGameHost : DesktopGameHost
    {
        private TimePeriod timePeriod;

        internal WindowsGameHost(GraphicsContextFlags flags)
        {
            IsActive = Window != null && GetForegroundWindow().Equals(Window.Handle);

            timePeriod = new TimePeriod(1) { Active = true };

            Architecture.SetIncludePath();

            Window = new WindowsGameWindow(flags);
            Window.Activated += OnActivated;
            Window.Deactivated += OnDeactivated;

            Application.EnableVisualStyles();
        }

        public override IEnumerable<InputHandler> GetInputHandlers()
        {
            //todo: figure why opentk input handlers aren't working.
            return new InputHandler[] {
                new CursorMouseHandler(), //handles cursor position
                new FormMouseHandler(),   //handles button states
                new FormKeyboardHandler(),
            };
        }

        protected override void Dispose(bool isDisposing)
        {
            timePeriod.Dispose();
            base.Dispose(isDisposing);
        }

        protected override void OnActivated(object sender, EventArgs args)
        {
            timePeriod.Active = true;

            Execution.SetThreadExecutionState(Execution.ExecutionState.Continuous | Execution.ExecutionState.SystemRequired | Execution.ExecutionState.DisplayRequired);
            base.OnActivated(sender, args);
        }

        protected override void OnDeactivated(object sender, EventArgs args)
        {
            timePeriod.Active = false;

            Execution.SetThreadExecutionState(Execution.ExecutionState.Continuous);
            base.OnDeactivated(sender, args);
        }

        public override void Run()
        {
            //not sure this is still needed
            NativeWindow.OsuWindowHandle = Window.Handle;

            base.Run();
        }

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
    }
}
<commit_msg>Add explanatory comment about IsActive assignment in WindowsGameHost.<commit_after>﻿// Copyright (c) 2007-2016 ppy Pty Ltd <<EMAIL>>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using osu.Framework.Desktop.Input.Handlers.Keyboard;
using osu.Framework.Desktop.Input.Handlers.Mouse;
using osu.Framework.Desktop.Platform.Windows.Native;
using osu.Framework.Input.Handlers;
using OpenTK.Graphics;
using NativeWindow = OpenTK.NativeWindow;

namespace osu.Framework.Desktop.Platform.Windows
{
    public class WindowsGameHost : DesktopGameHost
    {
        private TimePeriod timePeriod;

        internal WindowsGameHost(GraphicsContextFlags flags)
        {
            // OnActivate / OnDeactivate may not fire, so the initial activity state may be unknown here.
            // In order to be certain we have the correct activity state we are querying the Windows API here.
            IsActive = Window != null && GetForegroundWindow().Equals(Window.Handle);

            timePeriod = new TimePeriod(1) { Active = true };

            Architecture.SetIncludePath();

            Window = new WindowsGameWindow(flags);
            Window.Activated += OnActivated;
            Window.Deactivated += OnDeactivated;

            Application.EnableVisualStyles();
        }

        public override IEnumerable<InputHandler> GetInputHandlers()
        {
            //todo: figure why opentk input handlers aren't working.
            return new InputHandler[] {
                new CursorMouseHandler(), //handles cursor position
                new FormMouseHandler(),   //handles button states
                new FormKeyboardHandler(),
            };
        }

        protected override void Dispose(bool isDisposing)
        {
            timePeriod.Dispose();
            base.Dispose(isDisposing);
        }

        protected override void OnActivated(object sender, EventArgs args)
        {
            timePeriod.Active = true;

            Execution.SetThreadExecutionState(Execution.ExecutionState.Continuous | Execution.ExecutionState.SystemRequired | Execution.ExecutionState.DisplayRequired);
            base.OnActivated(sender, args);
        }

        protected override void OnDeactivated(object sender, EventArgs args)
        {
            timePeriod.Active = false;

            Execution.SetThreadExecutionState(Execution.ExecutionState.Continuous);
            base.OnDeactivated(sender, args);
        }

        public override void Run()
        {
            //not sure this is still needed
            NativeWindow.OsuWindowHandle = Window.Handle;

            base.Run();
        }

        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
    }
}

{'fraction_non_alphanumeric': 0.08143437555202261, 'fraction_numerical': 0.0038862391803568272, 'mean_word_length': 2.226210826210826, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 2, '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': '370769', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1362, 'n_words': 327}