From f83a2e1c937fbafac8511725e3fcf658326de9e3 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Tue, 6 Apr 2021 21:11:41 +0200 Subject: [PATCH] make IdeStarter more clear GitOrigin-RevId: e45d609ffa8c5c6a4cae3a939ad3621174a3c4fe --- .../com/intellij/idea/ApplicationLoader.kt | 4 +- .../src/com/intellij/idea/IdeStarter.kt | 139 +++++++++++------- 2 files changed, 86 insertions(+), 57 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/idea/ApplicationLoader.kt b/platform/platform-impl/src/com/intellij/idea/ApplicationLoader.kt index ded9f4d0a520..6a11f65aa706 100644 --- a/platform/platform-impl/src/com/intellij/idea/ApplicationLoader.kt +++ b/platform/platform-impl/src/com/intellij/idea/ApplicationLoader.kt @@ -32,6 +32,7 @@ import com.intellij.ui.AnimatedIcon import com.intellij.ui.AppIcon import com.intellij.ui.mac.foundation.Foundation import com.intellij.ui.mac.touchbar.TouchBarsManager +import com.intellij.util.PlatformUtils import com.intellij.util.TimeoutUtil import com.intellij.util.io.createDirectories import com.intellij.util.io.storage.HeavyProcessLatch @@ -106,7 +107,8 @@ private fun executeInitAppInEdt(args: List, // `ApplicationStarter` is an extension, so to find a starter extensions must be registered first registerComponentFuture .thenRun { - val starter = findStarter(args.first()) ?: IdeStarter() + val starter = findStarter(args.first()) + ?: if (PlatformUtils.getPlatformPrefix() == "LightEdit") IdeStarter.StandaloneLightEditStarter() else IdeStarter() if (Main.isHeadless() && !starter.isHeadless) { val commandName = starter.commandName val message = IdeBundle.message( diff --git a/platform/platform-impl/src/com/intellij/idea/IdeStarter.kt b/platform/platform-impl/src/com/intellij/idea/IdeStarter.kt index fb728940960d..1afad87674c7 100644 --- a/platform/platform-impl/src/com/intellij/idea/IdeStarter.kt +++ b/platform/platform-impl/src/com/intellij/idea/IdeStarter.kt @@ -1,4 +1,5 @@ // Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +@file:Suppress("ReplaceNegatedIsEmptyWithIsNotEmpty") package com.intellij.idea import com.intellij.diagnostic.* @@ -20,6 +21,7 @@ import com.intellij.notification.NotificationType import com.intellij.openapi.actionSystem.ActionManager import com.intellij.openapi.actionSystem.IdeActions import com.intellij.openapi.application.* +import com.intellij.openapi.application.ex.ApplicationEx import com.intellij.openapi.application.ex.ApplicationManagerEx import com.intellij.openapi.application.impl.ApplicationInfoImpl import com.intellij.openapi.diagnostic.Logger @@ -33,9 +35,7 @@ import com.intellij.openapi.wm.impl.SystemDock import com.intellij.openapi.wm.impl.welcomeScreen.WelcomeFrame import com.intellij.ui.AppUIUtil import com.intellij.ui.mac.touchbar.TouchBarsManager -import com.intellij.util.PlatformUtils import com.intellij.util.concurrency.AppExecutorUtil -import com.intellij.util.concurrency.NonUrgentExecutor import com.intellij.util.ui.accessibility.ScreenReader import java.awt.EventQueue import java.beans.PropertyChangeListener @@ -88,60 +88,7 @@ open class IdeStarter : ApplicationStarter { } val lifecyclePublisher = app.messageBus.syncPublisher(AppLifecycleListener.TOPIC) - val isStandaloneLightEdit = PlatformUtils.getPlatformPrefix() == "LightEdit" - val needToOpenProject: Boolean - if (isStandaloneLightEdit) { - needToOpenProject = true - } - else { - frameInitActivity.runChild("app frame created callback") { - lifecyclePublisher.appFrameCreated(args) - } - - // must be after appFrameCreated because some listeners can mutate state of RecentProjectsManager - if (app.isHeadlessEnvironment) { - needToOpenProject = false - } - else { - val willOpenProject = (args.isNotEmpty() || filesToLoad.isNotEmpty() || RecentProjectsManager.getInstance().willReopenProjectOnStart()) - && JetBrainsProtocolHandler.getCommand() == null - needToOpenProject = showWizardAndWelcomeFrame(lifecyclePublisher, willOpenProject) - } - - frameInitActivity.end() - - NonUrgentExecutor.getInstance().execute { - LifecycleUsageTriggerCollector.onIdeStart() - } - } - - val project = when { - !needToOpenProject -> null - filesToLoad.isNotEmpty() -> ProjectUtil.tryOpenFiles(null, filesToLoad, "MacMenu") - args.isNotEmpty() -> loadProjectFromExternalCommandLine(args) - else -> null - } - - lifecyclePublisher.appStarting(project) - - if (needToOpenProject && project == null && !JetBrainsProtocolHandler.appStartedWithCommand()) { - val recentProjectManager = RecentProjectsManager.getInstance() - var openLightEditFrame = isStandaloneLightEdit - if (recentProjectManager.willReopenProjectOnStart()) { - if (recentProjectManager.reopenLastProjectsOnStart()) { - openLightEditFrame = false - } - else if (!isStandaloneLightEdit) { - WelcomeFrame.showIfNoProjectOpened() - } - } - - if (openLightEditFrame) { - ApplicationManager.getApplication().invokeLater { - LightEditService.getInstance().showEditorWindow() - } - } - } + openProjectIfNeeded(args, app, frameInitActivity, lifecyclePublisher) reportPluginErrors() @@ -157,6 +104,64 @@ open class IdeStarter : ApplicationStarter { } } + protected open fun openProjectIfNeeded(args: List, + app: ApplicationEx, + frameInitActivity: Activity, + lifecyclePublisher: AppLifecycleListener) { + frameInitActivity.runChild("app frame created callback") { + lifecyclePublisher.appFrameCreated(args) + } + + // must be after appFrameCreated because some listeners can mutate state of RecentProjectsManager + if (app.isHeadlessEnvironment) { + frameInitActivity.end() + + LifecycleUsageTriggerCollector.onIdeStart() + lifecyclePublisher.appStarting(null) + return + } + + if (JetBrainsProtocolHandler.appStartedWithCommand()) { + val needToOpenProject = showWizardAndWelcomeFrame(lifecyclePublisher, willOpenProject = false) + frameInitActivity.end() + LifecycleUsageTriggerCollector.onIdeStart() + + val project = when { + !needToOpenProject -> null + !filesToLoad.isEmpty() -> ProjectUtil.tryOpenFiles(null, filesToLoad, "MacMenu") + !args.isEmpty() -> loadProjectFromExternalCommandLine(args) + else -> null + } + lifecyclePublisher.appStarting(project) + } + else { + val recentProjectManager = RecentProjectsManager.getInstance() + val willReopenRecentProjectOnStart = recentProjectManager.willReopenProjectOnStart() + val willOpenProject = willReopenRecentProjectOnStart || !args.isEmpty() || !filesToLoad.isEmpty() + val needToOpenProject = showWizardAndWelcomeFrame(lifecyclePublisher, willOpenProject) + frameInitActivity.end() + ForkJoinPool.commonPool().execute { + LifecycleUsageTriggerCollector.onIdeStart() + } + + if (needToOpenProject) { + val project = when { + !filesToLoad.isEmpty() -> ProjectUtil.tryOpenFiles(null, filesToLoad, "MacMenu") + !args.isEmpty() -> loadProjectFromExternalCommandLine(args) + else -> null + } + lifecyclePublisher.appStarting(project) + + if (project == null && willReopenRecentProjectOnStart && !recentProjectManager.reopenLastProjectsOnStart()) { + WelcomeFrame.showIfNoProjectOpened() + } + } + else { + lifecyclePublisher.appStarting(null) + } + } + } + private fun showWizardAndWelcomeFrame(lifecyclePublisher: AppLifecycleListener, willOpenProject: Boolean): Boolean { val doShowWelcomeFrame = if (willOpenProject) null else WelcomeFrame.prepareToShow() // do not show Customize IDE Wizard [IDEA-249516] @@ -201,6 +206,28 @@ open class IdeStarter : ApplicationStarter { } return false } + + internal class StandaloneLightEditStarter : IdeStarter() { + override fun openProjectIfNeeded(args: List, + app: ApplicationEx, + frameInitActivity: Activity, + lifecyclePublisher: AppLifecycleListener) { + val project = when { + !filesToLoad.isEmpty() -> ProjectUtil.tryOpenFiles(null, filesToLoad, "MacMenu") + !args.isEmpty() -> loadProjectFromExternalCommandLine(args) + else -> null + } + + if (project == null && !JetBrainsProtocolHandler.appStartedWithCommand()) { + val recentProjectManager = RecentProjectsManager.getInstance() + if (!recentProjectManager.willReopenProjectOnStart() || !recentProjectManager.reopenLastProjectsOnStart()) { + ApplicationManager.getApplication().invokeLater { + LightEditService.getInstance().showEditorWindow() + } + } + } + } + } } private fun loadProjectFromExternalCommandLine(commandLineArgs: List): Project? {