<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using EyeXFramework.Wpf;
using Tobii.EyeX.Framework;
using TobiiEyeX.Utils;

namespace TobiiEyeX {
    /// <summary>
    /// Interaction logic for Keyboard.xaml
    /// </summary>
    public partial class Keyboard : Window {

        private WpfEyeXHost eyeXHost;
        private bool isShiftPressed = false;
        private bool isCapsLockPressed = false;
        private Dictionary<Type, int> keyTypes = new Dictionary<Type, int> {
            { typeof(DefaultKey), 0 },
            { typeof(SpaceKey), 1 },
            { typeof(EnterKey), 2 },
            { typeof(LeftShiftKey), 3 },
            { typeof(RightShiftKey), 4 },
            { typeof(ControlKey), 5 },
            { typeof(TabKey), 6 },
            { typeof(CapslockKey), 7 },
            { typeof(BackspaceKey), 8 }
        };

        private Timer timer;

        public Keyboard() {
            InitializeComponent();
            eyeXHost = new WpfEyeXHost();
            eyeXHost.Start();
            inputBox.Focus();
        }

        private void onHasGazeChanged(object sender, RoutedEventArgs e) {
            // Stopping timer and reseting highlight
            if (timer != null) timer.Stop();
            AbstractKey aKey = e.Source as AbstractKey;
            aKey.resetHighlight();

            // Launching timer
            timer = new Timer((int)Application.Current.Resources["Threshold"], 100, true);
            object source = e.Source;
            toggleKey(source);
            timer.OnElapsed += delegate () {
                captureKey(source);
                aKey.resetHighlight();
            };
            timer.OnTick += delegate (double ratio) {
                aKey.progressHighlight(ratio);
            };
            timer.Start();
        }

        private void captureKey(object source) {
            bool wasEdited = true;
            switch (keyTypes[source.GetType()]) {
                case 0:
                    // Default
                    DefaultKey commonKey = source as DefaultKey;
                    if (commonKey.IsAlphabet) {
                        if (isShiftPressed || isCapsLockPressed) inputBox.Text += commonKey.TopLegend.ToUpper();
                        else inputBox.Text += commonKey.TopLegend.ToLower();
                    }
                    else {
                        if (isShiftPressed) inputBox.Text += commonKey.TopLegend;
                        else inputBox.Text += commonKey.BotLegend;
                    }
                    break;
                case 1:<commit_msg>Fix keys reset<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using EyeXFramework.Wpf;
using Tobii.EyeX.Framework;
using TobiiEyeX.Utils;

namespace TobiiEyeX {
    /// <summary>
    /// Interaction logic for Keyboard.xaml
    /// </summary>
    public partial class Keyboard : Window {

        private WpfEyeXHost eyeXHost;
        private bool isShiftPressed = false;
        private bool isCapsLockPressed = false;
        private Dictionary<Type, int> keyTypes = new Dictionary<Type, int> {
            { typeof(DefaultKey), 0 },
            { typeof(SpaceKey), 1 },
            { typeof(EnterKey), 2 },
            { typeof(LeftShiftKey), 3 },
            { typeof(RightShiftKey), 4 },
            { typeof(ControlKey), 5 },
            { typeof(TabKey), 6 },
            { typeof(CapslockKey), 7 },
            { typeof(BackspaceKey), 8 }
        };

        private Timer timer;

        public Keyboard() {
            InitializeComponent();
            eyeXHost = new WpfEyeXHost();
            eyeXHost.Start();
            inputBox.Focus();
        }

        private void onHasGazeChanged(object sender, RoutedEventArgs e) {
            if (timer != null && timer.isRunning) {
                // If first initialization or timer is already running
                // Stop it and refresh key, because we moving to a new one
                timer.Stop();
                AbstractKey aKey = e.Source as AbstractKey;
                aKey.resetHighlight();
            }
            else {
                // If timer is not running 
                // Just run it
                timer = new Timer((int)Application.Current.Resources["Threshold"], 100, true);
                object source = e.Source;
                AbstractKey aKey = e.Source as AbstractKey;
                toggleKey(source);
                timer.OnElapsed += delegate () {
                    captureKey(source);
                    aKey.resetHighlight();
                };
                timer.OnTick += delegate (double ratio) {
                    aKey.progressHighlight(ratio);
                };
                timer.Start();
            } 
        }

        private void captureKey(object source) {
            bool wasEdited = true;
            switch (keyTypes[source.GetType()]) {
                case 0:
                    // Default
                    DefaultKey commonKey = source as DefaultKey;
                    if (commonKey.IsAlphabet) {
                        if (isShiftPressed || isCapsLockPressed) inputBox.Text += commonKey.TopLegend.ToUpper();
                        else inputBox.Text += commonKey.TopLegend.ToLower();
                    }
                    else {
                        if (isShiftPressed) inputBox.Text += commonKey.TopLegend;
                        else inputBox.Text += commonKey.BotLegend;
                    }
                    break;
                case 1:
{'fraction_non_alphanumeric': 0.09616644823066842, 'fraction_numerical': 0.0045871559633027525, 'mean_word_length': 1.6179245283018868, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3797434', 'n_tokens_mistral': 1614, 'n_tokens_neox': 1503, 'n_words': 373}