<commit_before>﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Pomo
{
    static class Program
    {
        enum State
        {
            Work,
            Rest
        }

        #region Private Members

        readonly static TimeSpan WorkPeriod = new TimeSpan(0, 25, 0);
        readonly static TimeSpan RestPeriod = new TimeSpan(0, 5, 0);

        static NotifyIcon _notifyIcon;
        static Timer _timer;
        static TimeSpan _currentInterval = WorkPeriod;
        static State _currentState;

        #endregion

        #region Entry Point

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            AddTrayIcon();
            SetupTimer();

            Application.Run();

            RemoveTrayIcon();
        }

        #endregion

        #region Private Methods

        static void SetupTimer()
        {
            _timer = new Timer()
            {
                Interval = 1000,
                Enabled = true
            };

            _timer.Tick += Timer_Tick;
        }

        static void StartTimer()
        {
            _timer.Start();
        }

        static void PauseTimer()
        {
            _timer.Stop();
        }

        static void ResetTimer()
        {
            _currentState = State.Work;
            _currentInterval = WorkPeriod;

            UpdateIcon();
        }

        static void AddTrayIcon()
        {
            var menu = new ContextMenu();

            var exitItem = new MenuItem { Text = "E&xit" };
            exitItem.Click += (sender, e) => { Application.Exit(); };
            var startItem = new MenuItem { Text = "&Start" };
            startItem.Click += (sender, e) => { StartTimer(); };
            var stopItem = new MenuItem { Text = "&Pause" };
            stopItem.Click += (sender, e) => { PauseTimer(); };
            var resetItem = new MenuItem { Text = "&Reset" };
            resetItem.Click += (sender, e) => { ResetTimer(); };

            menu.MenuItems.Add(resetItem);
            menu.MenuItems.Add(new MenuItem("-"));
            menu.MenuItems.Add(startItem);
            menu.MenuItems.Add(stopItem);
            menu.MenuItems.Add(new MenuItem("-"));
            menu.MenuItems.Add(exitItem);

            _notifyIcon = new NotifyIcon
            {
                Visible = true,
                ContextMenu = menu,
            };

            UpdateIcon();
        }

        static void RemoveTrayIcon()
        {
            _notifyIcon.Visible = false;
        }

        static Icon GetIcon(string text = "0")
        {
            var backgroundColor = _currentState == State.Work ? Color.FromArgb(0xa1, 0x1c, 0x1c) : Color.FromArgb(0x15, 0x4f, 0x12);

            using (var backgroundBrush = new SolidBrush(backgroundColor))
            using (var borderPen = new Pen(Color.White))
            using (var textBrush = new SolidBrush(Color.White))
            using (var font = new Font("Segoe UI", 8))
            {
                using (var bitmap = new Bitmap(16, 16))
                {
                    using (var graphics = Graphics.FromImage(bitmap))
                    {
                        var rect = new Rectangle(0, 0, 16, 16);
                        graphics.FillRectangle(backgroundBrush, rect);
                        graphics.DrawRectangle(borderPen, new Rectangle(0, 0, 15, 15));

                        var stringFormat = new StringFormat();
                        stringFormat.Alignment = StringAlignment.Center;
                        stringFormat.LineAlignment = StringAlignment.Center;

                        graphics.DrawString(text, font, textBrush, rect, stringFormat);

                        return Icon.FromHandle(bitmap.GetHicon());
                    }
                }
            }
        }

        static void UpdateIcon()
        {
            var text = _currentInterval.Seconds.ToString();
            if (_currentInterval.TotalSeconds > 60)
            {
                text = _currentInterval.Minutes.ToString();
            }

            if (_notifyIcon.Icon != null)
            {
                _notifyIcon.Icon.Dispose();
            }

            _notifyIcon.Icon = GetIcon(text);
        }

        #region Event Handlers

        static void Timer_Tick(object sender, EventArgs e)
        {
            _currentInterval = _currentInterval.Add(new TimeSpan(0, 0, -1));

            if (_currentInterval.TotalSeconds == 0)
            {
                switch (_currentState)
                {
                    case State.Work:
                        _notifyIcon.ShowBalloonTip(1500, "Pomodoro", "Break time!", ToolTipIcon.Info);

                        _currentState = State.Rest;
                        _currentInterval = RestPeriod;
                        break;

                    case State.Rest:
                        _notifyIcon.ShowBalloonTip(1500, "Pomodoro", "Work time!", ToolTipIcon.Info);

                        _currentState = State.Work;
                        _currentInterval = WorkPeriod;
                        break;
                }<commit_msg>Performance tweak<commit_after>﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace Pomo
{
    static class Program
    {
        enum State
        {
            Work,
            Rest
        }

        #region Private Members

        readonly static TimeSpan WorkPeriod = new TimeSpan(0, 25, 0);
        readonly static TimeSpan RestPeriod = new TimeSpan(0, 5, 0);

        static NotifyIcon _notifyIcon;
        static Timer _timer;
        static TimeSpan _currentInterval = WorkPeriod;
        static State _currentState;
        static IDictionary<string, Icon> _iconCache = new Dictionary<string, Icon>();

        #endregion

        #region Entry Point

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            AddTrayIcon();
            SetupTimer();

            Application.Run();

            RemoveTrayIcon();
        }

        #endregion

        #region Private Methods

        static void SetupTimer()
        {
            _timer = new Timer()
            {
                Interval = 1000,
                Enabled = true
            };

            _timer.Tick += Timer_Tick;
        }

        static void StartTimer()
        {
            _timer.Start();
        }

        static void PauseTimer()
        {
            _timer.Stop();
        }

        static void ResetTimer()
        {
            _currentState = State.Work;
            _currentInterval = WorkPeriod;

            UpdateIcon();
        }

        static void AddTrayIcon()
        {
            var menu = new ContextMenu();

            var exitItem = new MenuItem { Text = "E&xit" };
            exitItem.Click += (sender, e) => { Application.Exit(); };
            var startItem = new MenuItem { Text = "&Start" };
            startItem.Click += (sender, e) => { StartTimer(); };
            var stopItem = new MenuItem { Text = "&Pause" };
            stopItem.Click += (sender, e) => { PauseTimer(); };
            var resetItem = new MenuItem { Text = "&Reset" };
            resetItem.Click += (sender, e) => { ResetTimer(); };

            menu.MenuItems.Add(resetItem);
            menu.MenuItems.Add(new MenuItem("-"));
            menu.MenuItems.Add(startItem);
            menu.MenuItems.Add(stopItem);
            menu.MenuItems.Add(new MenuItem("-"));
            menu.MenuItems.Add(exitItem);

            _notifyIcon = new NotifyIcon
            {
                Visible = true,
                ContextMenu = menu,
            };

            UpdateIcon();
        }

        static void RemoveTrayIcon()
        {
            _notifyIcon.Visible = false;
        }

        static Icon GetIcon(string text = "0")
        {
            var key = string.Format("{0}:{1}", text, _currentState);

            var ret = _iconCache.ContainsKey(key) ? _iconCache[key] : null;
            if (ret == null)
            {
                var backgroundColor = _currentState == State.Work ? Color.FromArgb(0xa1, 0x1c, 0x1c) : Color.FromArgb(0x15, 0x4f, 0x12);

                using (var backgroundBrush = new SolidBrush(backgroundColor))
                using (var borderPen = new Pen(Color.White))
                using (var textBrush = new SolidBrush(Color.White))
                using (var font = new Font("Segoe UI", 8))
                {
                    using (var bitmap = new Bitmap(16, 16))
                    {
                        using (var graphics = Graphics.FromImage(bitmap))
                        {
                            var rect = new Rectangle(0, 0, 16, 16);
                            graphics.FillRectangle(backgroundBrush, rect);
                            graphics.DrawRectangle(borderPen, new Rectangle(0, 0, 15, 15));

                            using (var stringFormat = new StringFormat())
                            {
                                stringFormat.Alignment = StringAlignment.Center;
                                stringFormat.LineAlignment = StringAlignment.Center;

                                var textRect = new Rectangle(rect.X, rect.Y + 2, rect.Width, rect.Height);

                                graphics.DrawString(text, font, textBrush, textRect, stringFormat);
                            }

                            ret = Icon.FromHandle(bitmap.GetHicon());

                            _iconCache.Add(key, ret);
                        }
                    }
                }
            }

            return ret;
        }

        static void UpdateIcon()
        {
            var text = _currentInterval.Seconds.ToString();
            if (_currentInterval.TotalSeconds > 60)
            {
                text = _currentInterval.Minutes.ToString();
            }

            if (_notifyIcon.Icon != null)
            {
                _notifyIcon.Icon.Dispose();
            }

            try
            {
                _notifyIcon.Icon = GetIcon(text);
            }
            catch (ExternalException) { }
        }

        #region Event Handlers

        static void Timer_Tick(object sender, EventArgs e)
        {
            _currentInterval = _currentInterval.Add(new TimeSpan(0, 0, -1));

            if (_currentInterval.TotalSeconds == 0)
            {
                switch (_currentState)
                {
                    case State.Work:
                        _notifyIcon.ShowBalloonTip(1500, "Pomodoro", "Break time!", ToolTipIcon.Info);

                        _currentState = State.Rest;
                        _currentInterval = RestPeriod;
                        break;

                    case State.Rest:
                        _notifyIcon.ShowBalloonTip(1500, "Pomodoro", "Work time!", ToolTipIcon.Info);

                        _currentState = State.Work;
                        _currentInterval = WorkPeriod;
                        break;
                }
{'fraction_non_alphanumeric': 0.09402606489985131, 'fraction_numerical': 0.010233534505379166, 'mean_word_length': 1.3127022653721683, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1148465', 'n_tokens_mistral': 3000, 'n_tokens_neox': 2739, 'n_words': 655}