<commit_before>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Wox.Core.Plugin;
using Wox.Plugin;
using Wox.Helper;
using Application = System.Windows.Forms.Application;
using File = System.IO.File;
using MessageBox = System.Windows.MessageBox;
using System.Windows.Data;
using System.Windows.Forms;
using Microsoft.Win32;
using Wox.Core.i18n;
using Wox.Core.Theme;
using Wox.Core.Updater;
using Wox.Core.UserSettings;
using Wox.Infrastructure;
using CheckBox = System.Windows.Controls.CheckBox;
using Control = System.Windows.Controls.Control;
using Cursors = System.Windows.Input.Cursors;
using HorizontalAlignment = System.Windows.HorizontalAlignment;

namespace Wox
{
    public partial class SettingWindow : Window
    {
        public readonly MainWindow MainWindow;
        bool settingsLoaded = false;
        private Dictionary<ISettingProvider, Control> featureControls = new Dictionary<ISettingProvider, Control>();
        private bool themeTabLoaded = false;

        public SettingWindow(MainWindow mainWindow)
        {
            this.MainWindow = mainWindow;
            InitializeComponent();
            Loaded += Setting_Loaded;
        }

        private void Setting_Loaded(object sender, RoutedEventArgs ev)
        {
            #region General

            cbHideWhenDeactive.Checked += (o, e) =>
            {
                UserSettingStorage.Instance.HideWhenDeactive = true;
                UserSettingStorage.Instance.Save();
            };

            cbHideWhenDeactive.Unchecked += (o, e) =>
            {
                UserSettingStorage.Instance.HideWhenDeactive = false;
                UserSettingStorage.Instance.Save();
            };

            cbRememberLastLocation.Checked += (o, e) =>
            {
                UserSettingStorage.Instance.RememberLastLaunchLocation = true;
                UserSettingStorage.Instance.Save();
            };

            cbRememberLastLocation.Unchecked += (o, e) =>
            {
                UserSettingStorage.Instance.RememberLastLaunchLocation = false;
                UserSettingStorage.Instance.Save();
            };

            cbDontPromptUpdateMsg.Checked += (o, e) =>
            {
                UserSettingStorage.Instance.DontPromptUpdateMsg = true;
                UserSettingStorage.Instance.Save();
            };

            cbDontPromptUpdateMsg.Unchecked += (o, e) =>
            {
                UserSettingStorage.Instance.DontPromptUpdateMsg = false;
                UserSettingStorage.Instance.Save();
            };

            cbStartWithWindows.IsChecked = CheckApplicationIsStartupWithWindow();
            comboMaxResultsToShow.SelectionChanged += (o, e) =>
            {
                UserSettingStorage.Instance.MaxResultsToShow = (int)comboMaxResultsToShow.SelectedItem;
                UserSettingStorage.Instance.Save();
                MainWindow.pnlResult.lbResults.GetBindingExpression(MaxHeightProperty).UpdateTarget();
            };

            cbHideWhenDeactive.IsChecked = UserSettingStorage.Instance.HideWhenDeactive;
            cbDontPromptUpdateMsg.IsChecked = UserSettingStorage.Instance.DontPromptUpdateMsg;
            cbRememberLastLocation.IsChecked = UserSettingStorage.Instance.RememberLastLaunchLocation;

            LoadLanguages();
            comboMaxResultsToShow.ItemsSource = Enumerable.Range(2, 16);
            var maxResults = UserSettingStorage.Instance.MaxResultsToShow;
            comboMaxResultsToShow.SelectedItem = maxResults == 0 ? 6 : maxResults;

            #endregion

            #region Proxy

            cbEnableProxy.Checked += (o, e) => EnableProxy();
            cbEnableProxy.Unchecked += (o, e) => DisableProxy();
            cbEnableProxy.IsChecked = UserSettingStorage.Instance.ProxyEnabled;
            tbProxyServer.Text = UserSettingStorage.Instance.ProxyServer;
            if (UserSettingStorage.Instance.ProxyPort != 0) {
                tbProxyPort.Text = UserSettingStorage.Instance.ProxyPort.ToString();
            }
            tbProxyUserName.Text = UserSettingStorage.Instance.ProxyUserName;
            tbProxyPassword.Password = UserSettingStorage.Instance.ProxyPassword;
            if (UserSettingStorage.Instance.ProxyEnabled)
            {
                EnableProxy();
            }
            else
            {
                DisableProxy();
            }

            #endregion

            #region About

            tbVersion.Text = UpdaterManager.Instance.CurrentVersion.ToString();
            string activateTimes = string.Format(InternationalizationManager.Instance.GetTranslation("about_activate_times"),
                UserSettingStorage.Instance.ActivateTimes);
            tbActivatedTimes.Text = activateTimes;

            #endregion

            settingsLoaded = true;
        }

        public void SwitchTo(string tabName)
        {
            switch (tabName)
            {
                case "general":
                    settingTab.SelectedIndex = 0;
                    break;
                case "plugin":
                    settingTab.SelectedIndex = 1;
                    break;
                case "theme":
                    settingTab.SelectedIndex = 2;
                    break;
                case "hotkey":
                    settingTab.SelectedIndex = 3;
                    break;
                case "proxy":
                    settingTab.SelectedIndex = 4;
                    break;
                case "about":
                    settingTab.SelectedIndex = 5;
                    break;
            }
        }

        private void settingTab_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Update controls inside the selected tab

            if (tabPlugin.IsSelected)
            {
                OnPluginTabSelected();
            }
            else if (tabTheme.IsSelected)<commit_msg>Fixed commit #334 multiple SelectionChanged firing<commit_after>﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Wox.Core.Plugin;
using Wox.Plugin;
using Wox.Helper;
using Application = System.Windows.Forms.Application;
using File = System.IO.File;
using MessageBox = System.Windows.MessageBox;
using System.Windows.Data;
using System.Windows.Forms;
using Microsoft.Win32;
using Wox.Core.i18n;
using Wox.Core.Theme;
using Wox.Core.Updater;
using Wox.Core.UserSettings;
using Wox.Infrastructure;
using CheckBox = System.Windows.Controls.CheckBox;
using Control = System.Windows.Controls.Control;
using Cursors = System.Windows.Input.Cursors;
using HorizontalAlignment = System.Windows.HorizontalAlignment;

namespace Wox
{
    public partial class SettingWindow : Window
    {
        public readonly MainWindow MainWindow;
        bool settingsLoaded = false;
        private Dictionary<ISettingProvider, Control> featureControls = new Dictionary<ISettingProvider, Control>();
        private bool themeTabLoaded = false;

        public SettingWindow(MainWindow mainWindow)
        {
            this.MainWindow = mainWindow;
            InitializeComponent();
            Loaded += Setting_Loaded;
        }

        private void Setting_Loaded(object sender, RoutedEventArgs ev)
        {
            #region General

            cbHideWhenDeactive.Checked += (o, e) =>
            {
                UserSettingStorage.Instance.HideWhenDeactive = true;
                UserSettingStorage.Instance.Save();
            };

            cbHideWhenDeactive.Unchecked += (o, e) =>
            {
                UserSettingStorage.Instance.HideWhenDeactive = false;
                UserSettingStorage.Instance.Save();
            };

            cbRememberLastLocation.Checked += (o, e) =>
            {
                UserSettingStorage.Instance.RememberLastLaunchLocation = true;
                UserSettingStorage.Instance.Save();
            };

            cbRememberLastLocation.Unchecked += (o, e) =>
            {
                UserSettingStorage.Instance.RememberLastLaunchLocation = false;
                UserSettingStorage.Instance.Save();
            };

            cbDontPromptUpdateMsg.Checked += (o, e) =>
            {
                UserSettingStorage.Instance.DontPromptUpdateMsg = true;
                UserSettingStorage.Instance.Save();
            };

            cbDontPromptUpdateMsg.Unchecked += (o, e) =>
            {
                UserSettingStorage.Instance.DontPromptUpdateMsg = false;
                UserSettingStorage.Instance.Save();
            };

            cbStartWithWindows.IsChecked = CheckApplicationIsStartupWithWindow();
            comboMaxResultsToShow.SelectionChanged += (o, e) =>
            {
                UserSettingStorage.Instance.MaxResultsToShow = (int)comboMaxResultsToShow.SelectedItem;
                UserSettingStorage.Instance.Save();
                MainWindow.pnlResult.lbResults.GetBindingExpression(MaxHeightProperty).UpdateTarget();
            };

            cbHideWhenDeactive.IsChecked = UserSettingStorage.Instance.HideWhenDeactive;
            cbDontPromptUpdateMsg.IsChecked = UserSettingStorage.Instance.DontPromptUpdateMsg;
            cbRememberLastLocation.IsChecked = UserSettingStorage.Instance.RememberLastLaunchLocation;

            LoadLanguages();
            comboMaxResultsToShow.ItemsSource = Enumerable.Range(2, 16);
            var maxResults = UserSettingStorage.Instance.MaxResultsToShow;
            comboMaxResultsToShow.SelectedItem = maxResults == 0 ? 6 : maxResults;

            #endregion

            #region Proxy

            cbEnableProxy.Checked += (o, e) => EnableProxy();
            cbEnableProxy.Unchecked += (o, e) => DisableProxy();
            cbEnableProxy.IsChecked = UserSettingStorage.Instance.ProxyEnabled;
            tbProxyServer.Text = UserSettingStorage.Instance.ProxyServer;
            if (UserSettingStorage.Instance.ProxyPort != 0) {
                tbProxyPort.Text = UserSettingStorage.Instance.ProxyPort.ToString();
            }
            tbProxyUserName.Text = UserSettingStorage.Instance.ProxyUserName;
            tbProxyPassword.Password = <PASSWORD>.ProxyPassword;
            if (UserSettingStorage.Instance.ProxyEnabled)
            {
                EnableProxy();
            }
            else
            {
                DisableProxy();
            }

            #endregion

            #region About

            tbVersion.Text = UpdaterManager.Instance.CurrentVersion.ToString();
            string activateTimes = string.Format(InternationalizationManager.Instance.GetTranslation("about_activate_times"),
                UserSettingStorage.Instance.ActivateTimes);
            tbActivatedTimes.Text = activateTimes;

            #endregion

            settingsLoaded = true;
        }

        public void SwitchTo(string tabName)
        {
            switch (tabName)
            {
                case "general":
                    settingTab.SelectedIndex = 0;
                    break;
                case "plugin":
                    settingTab.SelectedIndex = 1;
                    break;
                case "theme":
                    settingTab.SelectedIndex = 2;
                    break;
                case "hotkey":
                    settingTab.SelectedIndex = 3;
                    break;
                case "proxy":
                    settingTab.SelectedIndex = 4;
                    break;
                case "about":
                    settingTab.SelectedIndex = 5;
                    break;
            }
        }

        private void settingTab_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Update controls inside the selected tab
            if (e.OriginalSource != settingTab) return;

            if (tabPlugin.IsSelected)
            {
                OnPluginTabSelected();
            }
            else if (tabTheme.IsSelected)
{'fraction_non_alphanumeric': 0.0805854913945633, 'fraction_numerical': 0.0028148624738619913, 'mean_word_length': 1.952980289717407, 'pattern_counts': {'":': 12, '<': 8, '<?xml version=': 0, '>': 26, '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': '3593315', 'n_tokens_mistral': 3066, 'n_tokens_neox': 2910, 'n_words': 548}