<commit_before>﻿// -----------------------------------------------------------------------
// Copyright (c) <NAME>. All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using AudioSwitcher.ApplicationModel;
using AudioSwitcher.ComponentModel;

namespace AudioSwitcher.Presentation
{
    [Export(typeof(PresenterManager))]
    internal class PresenterManager : IDisposable
    {
        private readonly IApplication _application;
        private readonly ExportFactory<IPresenter, IPresenterMetadata>[] _presenters;
        private readonly List<IDisposable> _lifetimesToDispose = new List<IDisposable>();
        private PresenterLifetime<ContextMenuPresenter> _current;

        [ImportingConstructor]
        public PresenterManager(IApplication application, [ImportMany]ExportFactory<IPresenter, IPresenterMetadata>[] presenters)
        {
            _application = application;
            _application.Idle += OnApplicationIdle;
            _presenters = presenters;
        }

        public void ShowNonModal(string id)
        {
            if (id == null)
                throw new ArgumentNullException("id");

            PresenterLifetime<NonModalPresenter> presenter = CreatePresenter<NonModalPresenter>(id);
            presenter.Instance.Closed += (sender, e) => presenter.Dispose();
            presenter.Instance.ShowNonModal();
        }

        public void ShowContextMenu(string id, Point screenLocation)
        {
            if (id == null)
                throw new ArgumentNullException("id");

            if (CloseOpenContextMenu(id))
                return;

            PresenterLifetime<ContextMenuPresenter> presenter = _current = CreatePresenter<ContextMenuPresenter>(id);
            presenter.Instance.Closed += (sender, e) =>
            {
                Debug.Assert(_current == presenter);
                _current = null;
                _lifetimesToDispose.Add(presenter);
            };

            presenter.Instance.Show(screenLocation);
        }

        private bool CloseOpenContextMenu(string id)
        {
            PresenterLifetime<ContextMenuPresenter> current = _current;
            if (current != null)
            {
                current.Instance.Close();

                // Current context menu is a toggle and is already
                // open, let's close it instead opening another
                if (current.Metadata.Id == id && current.Metadata.IsToggle)
                    return true;<commit_msg>Fixed grammatical error<commit_after>﻿// -----------------------------------------------------------------------
// Copyright (c) <NAME>. All rights reserved.
// -----------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using AudioSwitcher.ApplicationModel;
using AudioSwitcher.ComponentModel;

namespace AudioSwitcher.Presentation
{
    [Export(typeof(PresenterManager))]
    internal class PresenterManager : IDisposable
    {
        private readonly IApplication _application;
        private readonly ExportFactory<IPresenter, IPresenterMetadata>[] _presenters;
        private readonly List<IDisposable> _lifetimesToDispose = new List<IDisposable>();
        private PresenterLifetime<ContextMenuPresenter> _current;

        [ImportingConstructor]
        public PresenterManager(IApplication application, [ImportMany]ExportFactory<IPresenter, IPresenterMetadata>[] presenters)
        {
            _application = application;
            _application.Idle += OnApplicationIdle;
            _presenters = presenters;
        }

        public void ShowNonModal(string id)
        {
            if (id == null)
                throw new ArgumentNullException("id");

            PresenterLifetime<NonModalPresenter> presenter = CreatePresenter<NonModalPresenter>(id);
            presenter.Instance.Closed += (sender, e) => presenter.Dispose();
            presenter.Instance.ShowNonModal();
        }

        public void ShowContextMenu(string id, Point screenLocation)
        {
            if (id == null)
                throw new ArgumentNullException("id");

            if (CloseOpenContextMenu(id))
                return;

            PresenterLifetime<ContextMenuPresenter> presenter = _current = CreatePresenter<ContextMenuPresenter>(id);
            presenter.Instance.Closed += (sender, e) =>
            {
                Debug.Assert(_current == presenter);
                _current = null;
                _lifetimesToDispose.Add(presenter);
            };

            presenter.Instance.Show(screenLocation);
        }

        private bool CloseOpenContextMenu(string id)
        {
            PresenterLifetime<ContextMenuPresenter> current = _current;
            if (current != null)
            {
                current.Instance.Close();

                // Current context menu is a toggle and is already
                // open, let's close it instead of opening another
                if (current.Metadata.Id == id && current.Metadata.IsToggle)
                    return true;
{'fraction_non_alphanumeric': 0.1323719726381956, 'fraction_numerical': 0.0, 'mean_word_length': 2.0088987764182424, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, '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': '7053291', 'n_tokens_mistral': 1286, 'n_tokens_neox': 1239, 'n_words': 286}