<commit_before>﻿using System;
using System.Timers;
using System.Windows;
using System.Windows.Input;

namespace SpotiPeek.App
{
	public partial class MainWindow : Window
    {
        private SpotifyManager _sm;
        private App _app = (App)Application.Current;
        private Timer _albumArtTimer = new Timer();
		private bool _isAlbumArtVisible = false;

        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            RestoreStateFromSettings();
			InitSpotifyConnection();
			InitUI();
        }

		private void InitSpotifyConnection()
		{
			EnsureSpotifyIsInstalled();
			_sm = new SpotifyManager();
			CheckAndRespondToErrorState();
			RefreshContent();

			_sm.TrackChanged += OnTrackChanged;
            _sm.PlayStateChanged += OnPlayStateChanged;
            _sm.ErrorStateChanged += OnErrorStateChanged;
		}

		private void InitUI()
		{
			Opacity = 0.4;
            _albumArtTimer.Interval = 5000;
			_albumArtTimer.Elapsed += AlbumArtTimer_Elapsed;
			ImageStackPanel.Visibility = Visibility.Collapsed;

			ContextMenuExit.Click += OnContextMenuExit;
			ContextMenuRefresh.Click += OnContextMenuRefresh;
			MouseLeftButtonDown += OnAfterDragWindow;
			MouseLeftButtonUp += OnSingleClick;
			MouseDoubleClick += OnDoubleClick;
		}

		private void EnsureSpotifyIsInstalled()
        {
            if (!SpotifyManager.IsSpotifyInstalled())
            {
                MessageBox.Show("Spotify must be installed to run SpotiPeek", "Can't find Spotify");
                _app.Shutdown(-1);
            }
        }

        private void OnContextMenuRefresh(object sender, RoutedEventArgs e)
        {
            RefreshContent();
        }

        private void OnContextMenuExit(object sender, RoutedEventArgs e)
        {
            _app.Shutdown(0);
        }

        private void RestoreStateFromSettings()
        {
            RestoreStartupWindowLocation();
        }

        private void RestoreStartupWindowLocation()
        {
            Left = Math.Abs(_app.Settings.Data.WindowLocationLeft);
            Top = 0;
        }

        private void OnAfterDragWindow(object sender, MouseButtonEventArgs e)
        {
            DragMove();
            Top = 0;
            SaveStateToSettings();
        }

        private void SaveStateToSettings()
        {
            _app.Settings.Data.WindowLocationLeft = (int)Math.Abs(Left);
            _app.Settings.Save();
        }

        private void OnErrorStateChanged(object sender, EventArgs e)
        {
            CheckAndRespondToErrorState();
        }

		private void OnDoubleClick(object sender, MouseButtonEventArgs e)
		{
			_sm.TogglePlayPauseState();
		}

		private void OnSingleClick(object sender, MouseButtonEventArgs e)
		{
			if (_isAlbumArtVisible)
			{
				HideAlbumArt();
			}
		}

        private void OnPlayStateChanged(object sender, EventArgs e)
        {
            RefreshContent();
            ShowAlbumArt();
        }

        private void OnTrackChanged(object sender, EventArgs e)
        {
            RefreshContent();
            ShowAlbumArt();
        }

        private void RefreshContent()
        {
            Dispatcher.Invoke(() =>
            {
                _sm.UpdateStatus();
                TrackInfoLabel.Content = _sm.CurrentTrackInfo;
                AlbumArtImage.Source = _sm.CurrentAlbumImage;
            });
        }

        private void ShowAlbumArt()
        {
            Dispatcher.Invoke(() =>
            {
                this.Opacity = 1.0;
                ImageStackPanel.Visibility = Visibility.Visible;
            });

            _albumArtTimer.Stop();
            _albumArtTimer.Start();

			_isAlbumArtVisible = true;
		}

		private void HideAlbumArt()
		{
			Dispatcher.Invoke(() =>
			{
				this.Opacity = 0.4;
				ImageStackPanel.Visibility = Visibility.Collapsed;<commit_msg>Extracted out a few constants<commit_after>﻿using System;
using System.Timers;
using System.Windows;
using System.Windows.Input;

namespace SpotiPeek.App
{
	public partial class MainWindow : Window
    {
        private SpotifyManager _sm;
        private App _app = (App)Application.Current;
        private Timer _albumArtTimer = new Timer();
		private bool _isAlbumArtVisible = false;
        private const double _transparentOpacity = 0.4;
        private const double _solidOpacity = 1.0;
        private const double _albumArtDisplayTime = 5000;

        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnInitialized(EventArgs e)
        {
            base.OnInitialized(e);

            RestoreStateFromSettings();
			InitSpotifyConnection();
			InitUI();
        }

		private void InitSpotifyConnection()
		{
			EnsureSpotifyIsInstalled();
			_sm = new SpotifyManager();
			CheckAndRespondToErrorState();
			RefreshContent();

			_sm.TrackChanged += OnTrackChanged;
            _sm.PlayStateChanged += OnPlayStateChanged;
            _sm.ErrorStateChanged += OnErrorStateChanged;
		}

		private void InitUI()
		{
            Opacity = _transparentOpacity;
            _albumArtTimer.Interval = _albumArtDisplayTime;
			_albumArtTimer.Elapsed += AlbumArtTimer_Elapsed;
			ImageStackPanel.Visibility = Visibility.Collapsed;

			ContextMenuExit.Click += OnContextMenuExit;
			ContextMenuRefresh.Click += OnContextMenuRefresh;
			MouseLeftButtonDown += OnAfterDragWindow;
			MouseLeftButtonUp += OnSingleClick;
			MouseDoubleClick += OnDoubleClick;
		}

		private void EnsureSpotifyIsInstalled()
        {
            if (!SpotifyManager.IsSpotifyInstalled())
            {
                MessageBox.Show("Spotify must be installed to run SpotiPeek", "Can't find Spotify");
                _app.Shutdown(-1);
            }
        }

        private void OnContextMenuRefresh(object sender, RoutedEventArgs e)
        {
            RefreshContent();
        }

        private void OnContextMenuExit(object sender, RoutedEventArgs e)
        {
            _app.Shutdown(0);
        }

        private void RestoreStateFromSettings()
        {
            RestoreStartupWindowLocation();
        }

        private void RestoreStartupWindowLocation()
        {
            Left = Math.Abs(_app.Settings.Data.WindowLocationLeft);
            Top = 0;
        }

        private void OnAfterDragWindow(object sender, MouseButtonEventArgs e)
        {
            DragMove();
            Top = 0;
            SaveStateToSettings();
        }

        private void SaveStateToSettings()
        {
            _app.Settings.Data.WindowLocationLeft = (int)Math.Abs(Left);
            _app.Settings.Save();
        }

        private void OnErrorStateChanged(object sender, EventArgs e)
        {
            CheckAndRespondToErrorState();
        }

		private void OnDoubleClick(object sender, MouseButtonEventArgs e)
		{
			_sm.TogglePlayPauseState();
		}

		private void OnSingleClick(object sender, MouseButtonEventArgs e)
		{
			if (_isAlbumArtVisible)
			{
				HideAlbumArt();
			}
		}

        private void OnPlayStateChanged(object sender, EventArgs e)
        {
            RefreshContent();
            ShowAlbumArt();
        }

        private void OnTrackChanged(object sender, EventArgs e)
        {
            RefreshContent();
            ShowAlbumArt();
        }

        private void RefreshContent()
        {
            Dispatcher.Invoke(() =>
            {
                _sm.UpdateStatus();
                TrackInfoLabel.Content = _sm.CurrentTrackInfo;
                AlbumArtImage.Source = _sm.CurrentAlbumImage;
            });
        }

        private void ShowAlbumArt()
        {
            Dispatcher.Invoke(() =>
            {
                Opacity = _solidOpacity;
                ImageStackPanel.Visibility = Visibility.Visible;
            });

            _albumArtTimer.Stop();
            _albumArtTimer.Start();

			_isAlbumArtVisible = true;
		}

		private void HideAlbumArt()
		{
			Dispatcher.Invoke(() =>
			{
				Opacity = _transparentOpacity;
				ImageStackPanel.Visibility = Visibility.Collapsed;
{'fraction_non_alphanumeric': 0.08352696587990706, 'fraction_numerical': 0.003179650238473768, 'mean_word_length': 2.0480805068952663, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, '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': '2127936', 'n_tokens_mistral': 2388, 'n_tokens_neox': 2115, 'n_words': 426}