<commit_before>﻿using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using WinTile.Model;

namespace WinTile
{
    public partial class MainWindow : Window
    {
        private ViewModel viewModel = new ViewModel();
        private Map<WindowTile, ToggleButton> Windows = new Map<WindowTile, ToggleButton>();

        public MainWindow()
        {
            KeyDown += (sender, args) =>
            {
                viewModel.updateHotKey(args);
            };

            viewModel.WindowAdded += AddWindow;
            viewModel.WindowRemoved += removeWindow;
            viewModel.WindowChanged += WindowChanged;

            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                DataContext = viewModel;
            }

            Loaded += (sender, args) =>
            {
                viewModel.Load();
            };
        }

        private void AddWindowButton_OnClick(object sender, RoutedEventArgs e)
        {
            viewModel.AddWindow();
        }

        private void AddWindow(WindowTile window)
        {

            var button = new ToggleButton();
            activateToggle(window, button);

            Windows.Add(window, button);

            button.Click += (sender, args) =>
            {
                selectWindow(button);
            };

            Canvas.Children.Add(button);
        }

        private void selectWindow(ToggleButton button)
        {
            if (CurrentToggle() == button && button.IsChecked == false)
            {
                viewModel.Selected = null;
            }
            else
            {
                // Deselect other buttons
                Canvas.SetZIndex(button, 1000);
                Windows.Values.ToList()
                    .Where(b => b != button)
                    .ForEach(b =>
                    {
                        b.IsChecked = false;
                        Canvas.SetZIndex(b, 0);
                    });

                viewModel.Selected = Windows.Reverse[button];
            }
        }

        private void removeWindow(WindowTile window)
        {
            var toggle = Windows.Forward[window];
            Canvas.Children.Remove(toggle);
        }

        private void activateToggle(WindowTile window, ToggleButton button)
        {
            var scaledWindow = window.tile.extend(Canvas.ActualWidth, Canvas.ActualHeight);
            Canvas.SetLeft(button, scaledWindow.Left);
            Canvas.SetTop(button, scaledWindow.Top);
            button.Width = scaledWindow.Width;
            button.Height = scaledWindow.Height;
            button.Content = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(window.hotkey.ToString());
        }

        private void WindowChanged(WindowTile window)
        {
            var toggle = CurrentToggle();
            if(toggle!=null) activateToggle(window, toggle);
        }

        private ToggleButton CurrentToggle()
        {
            return Windows.Values.FirstOrDefault(b => b.IsChecked == true);
        }

        private void RemoveWindowButton_OnClick(object sender, RoutedEventArgs e)
        {
            var toggle = CurrentToggle();
            if (toggle != null) viewModel.removeWindow(Windows.Reverse[toggle]);
        }

        private void ApplyLayoutButton_OnClick(object sender, RoutedEventArgs e)
        {
            viewModel.Save();
        }

        private void PrevWindow_OnClick(object sender, RoutedEventArgs e)
        {
            SelectNextToggle(-1);
        }

        private void NextWindow_OnClick(object sender, RoutedEventArgs e)
        {
            SelectNextToggle(1);
        }

        private void SelectNextToggle(int direction)
        {
            var toggles = Windows.Values.ToList();
            var index = toggles.FindIndex(b => b.IsChecked == true);
            var toggle = toggles[(index + direction + toggles.Count) % toggles.Count];
            toggle.IsChecked = true;
            toggle.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
        }

        private void ExportButton_OnClick(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog()
            {
                FileName = "Wintile Layout Profile",<commit_msg>Oder tiles when cycling between windows.<commit_after>﻿using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using WinTile.Model;

namespace WinTile
{
    public partial class MainWindow : Window
    {
        private ViewModel viewModel = new ViewModel();
        private Map<WindowTile, ToggleButton> Windows = new Map<WindowTile, ToggleButton>();

        public MainWindow()
        {
            KeyDown += (sender, args) =>
            {
                viewModel.updateHotKey(args);
            };

            viewModel.WindowAdded += AddWindow;
            viewModel.WindowRemoved += removeWindow;
            viewModel.WindowChanged += WindowChanged;

            InitializeComponent();

            if (!DesignerProperties.GetIsInDesignMode(this))
            {
                DataContext = viewModel;
            }

            Loaded += (sender, args) =>
            {
                viewModel.Load();
            };
        }

        private void AddWindowButton_OnClick(object sender, RoutedEventArgs e)
        {
            viewModel.AddWindow();
        }

        private void AddWindow(WindowTile window)
        {

            var button = new ToggleButton();
            activateToggle(window, button);

            Windows.Add(window, button);

            button.Click += (sender, args) =>
            {
                selectWindow(button);
            };

            Canvas.Children.Add(button);
        }

        private void selectWindow(ToggleButton button)
        {
            if (CurrentToggle() == button && button.IsChecked == false)
            {
                viewModel.Selected = null;
            }
            else
            {
                // Deselect other buttons
                Canvas.SetZIndex(button, 1000);
                Windows.Values.ToList()
                    .Where(b => b != button)
                    .ForEach(b =>
                    {
                        b.IsChecked = false;
                        Canvas.SetZIndex(b, 0);
                    });

                viewModel.Selected = Windows.Reverse[button];
            }
        }

        private void removeWindow(WindowTile window)
        {
            var toggle = Windows.Forward[window];
            Canvas.Children.Remove(toggle);
        }

        private void activateToggle(WindowTile window, ToggleButton button)
        {
            var scaledWindow = window.tile.extend(Canvas.ActualWidth, Canvas.ActualHeight);
            Canvas.SetLeft(button, scaledWindow.Left);
            Canvas.SetTop(button, scaledWindow.Top);
            button.Width = scaledWindow.Width;
            button.Height = scaledWindow.Height;
            button.Content = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(window.hotkey.ToString());
        }

        private void WindowChanged(WindowTile window)
        {
            var toggle = CurrentToggle();
            if(toggle!=null) activateToggle(window, toggle);
        }

        private ToggleButton CurrentToggle()
        {
            return Windows.Values.FirstOrDefault(b => b.IsChecked == true);
        }

        private void RemoveWindowButton_OnClick(object sender, RoutedEventArgs e)
        {
            var toggle = CurrentToggle();
            if (toggle != null) viewModel.removeWindow(Windows.Reverse[toggle]);
        }

        private void ApplyLayoutButton_OnClick(object sender, RoutedEventArgs e)
        {
            viewModel.Save();
        }

        private void PrevWindow_OnClick(object sender, RoutedEventArgs e)
        {
            SelectNextToggle(-1);
        }

        private void NextWindow_OnClick(object sender, RoutedEventArgs e)
        {
            SelectNextToggle(1);
        }

        private void SelectNextToggle(int direction)
        {
            var toggles = Windows.Values
                .OrderBy( key =>
                {
                    var tile = Windows.Reverse[key].tile;
                    return tile.Left + tile.Top;
                })
                .ToList();

            var index = toggles.FindIndex(b => b.IsChecked == true);
            var toggle = toggles[(index + direction + toggles.Count) % toggles.Count];
            toggle.IsChecked = true;
            toggle.RaiseEvent(new RoutedEventArgs(ButtonBase.ClickEvent));
        }

        private void ExportButton_OnClick(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog()
            {
                FileName = "Wintile Layout Profile",
{'fraction_non_alphanumeric': 0.08890845070422536, 'fraction_numerical': 0.0024207746478873238, 'mean_word_length': 1.6521739130434783, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 22, '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': '4508452', 'n_tokens_mistral': 2301, 'n_tokens_neox': 2169, 'n_words': 455}