public static async Task ShowDialogAsync()

in Python/Product/PythonTools/PythonTools/Environments/AddEnvironmentDialog.xaml.cs [194:319]


        public static async Task ShowDialogAsync(
            PageKind activePage,
            IServiceProvider site,
            PythonProjectNode project,
            IPythonWorkspaceContext workspace,
            string existingCondaEnvName,
            string environmentYmlPath,
            string requirementsTxtPath,
            CancellationToken ct = default(CancellationToken)
        ) {
            if (site == null) {
                throw new ArgumentNullException(nameof(site));
            }

            ProjectView[] projectViews;
            ProjectView selectedProjectView;

            if (workspace != null) {
                var registryService = site.GetComponentModel().GetService<IInterpreterRegistryService>();
                var optionsService = site.GetComponentModel().GetService<IInterpreterOptionsService>();
                selectedProjectView = new ProjectView(workspace);
                projectViews = new ProjectView[] { selectedProjectView };
            } else {
                try {
                    var sln = (IVsSolution)site.GetService(typeof(SVsSolution));
                    var projects = sln?.EnumerateLoadedPythonProjects().ToArray() ?? Array.Empty<PythonProjectNode>();

                    projectViews = projects
                        .Select((projectNode) => new ProjectView(projectNode))
                        .ToArray();

                    selectedProjectView = projectViews.SingleOrDefault(pv => pv.Node == project);
                } catch (InvalidOperationException ex) {
                    Debug.Fail(ex.ToUnhandledExceptionMessage(typeof(AddEnvironmentDialog)));
                    projectViews = Array.Empty<ProjectView>();
                    selectedProjectView = null;
                }
            }

            if (selectedProjectView != null) {
                if (existingCondaEnvName != null) {
                    selectedProjectView.MissingCondaEnvName = existingCondaEnvName;
                }

                if (environmentYmlPath != null) {
                    selectedProjectView.EnvironmentYmlPath = environmentYmlPath;
                }

                if (requirementsTxtPath != null) {
                    selectedProjectView.RequirementsTxtPath = requirementsTxtPath;
                }
            }

            var addVirtualView = new AddVirtualEnvironmentView(
                site,
                projectViews,
                selectedProjectView
            );

            var addCondaView = new AddCondaEnvironmentView(
                site,
                projectViews,
                selectedProjectView
            );

            var addExistingView = new AddExistingEnvironmentView(
                site,
                projectViews,
                selectedProjectView
            );

            var addInstalledView = new AddInstalledEnvironmentView(
                site,
                projectViews,
                selectedProjectView
            );

            EnvironmentViewBase activeView;
            switch (activePage) {
                case PageKind.VirtualEnvironment:
                    activeView = addVirtualView;
                    break;
                case PageKind.CondaEnvironment:
                    activeView = addCondaView;
                    break;
                case PageKind.ExistingEnvironment:
                    activeView = addExistingView;
                    break;
                case PageKind.InstalledEnvironment:
                    activeView = addInstalledView;
                    break;
                default:
                    Debug.Assert(false, string.Format("Unknown page kind '{0}'", activePage));
                    activeView = null;
                    break;
            }

            using (var dlg = new AddEnvironmentDialog(
                new EnvironmentViewBase[] {
                    addVirtualView,
                    addCondaView,
                    addExistingView,
                    addInstalledView,
                },
                activeView
            )) {
                try {
                    WindowHelper.ShowModal(dlg);
                } catch (Exception) {
                    dlg.Close();
                    throw;
                }

                if (dlg.DialogResult ?? false) {
                    var view = dlg.View.PagesView.CurrentItem as EnvironmentViewBase;
                    Debug.Assert(view != null);
                    if (view != null) {
                        try {
                            await view.ApplyAsync();
                        } catch (Exception ex) when (!ex.IsCriticalException()) {
                            Debug.Fail(ex.ToUnhandledExceptionMessage(typeof(AddEnvironmentDialog)), Strings.ProductTitle);
                        }
                    }
                }
            }
        }