From 73fc94c8ab7ee69fcf475960789e89c0182b8933 Mon Sep 17 00:00:00 2001 From: "Ilya.Kazakevich" Date: Thu, 7 Nov 2024 00:24:27 +0100 Subject: [PATCH] PY-75549: Refactor code to method to preserve history. Extract code to method to simplify next refactoring. (cherry picked from commit 7928c74167a0b72fb74ecc04ac509f02dba7b2ac) IJ-CR-149723 GitOrigin-RevId: 8fffd1bbb9ea8d130bf6dd17a9cf8ebaefa04b51 --- .../welcomeScreen/EmptyStateProjectsPanel.kt | 267 +++++++++--------- .../impl/welcomeScreen/ProjectsTabFactory.kt | 2 +- 2 files changed, 128 insertions(+), 141 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/EmptyStateProjectsPanel.kt b/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/EmptyStateProjectsPanel.kt index 0fb66e5da4e1..a5acb86b8a59 100644 --- a/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/EmptyStateProjectsPanel.kt +++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/EmptyStateProjectsPanel.kt @@ -9,167 +9,154 @@ import com.intellij.openapi.actionSystem.ex.ActionUtil import com.intellij.openapi.actionSystem.ex.CustomComponentAction import com.intellij.openapi.actionSystem.impl.ActionToolbarImpl import com.intellij.openapi.actionSystem.toolbarLayout.ToolbarLayoutStrategy -import com.intellij.openapi.ui.VerticalFlowLayout import com.intellij.openapi.ui.popup.JBPopupFactory import com.intellij.openapi.util.Key -import com.intellij.openapi.util.NlsContexts import com.intellij.openapi.wm.IdeFocusManager import com.intellij.ui.components.DropDownLink -import com.intellij.ui.components.JBLabel -import com.intellij.ui.components.panels.NonOpaquePanel -import com.intellij.ui.components.panels.Wrapper +import com.intellij.ui.dsl.builder.AlignX +import com.intellij.ui.dsl.builder.panel +import com.intellij.ui.dsl.gridLayout.UnscaledGaps import com.intellij.ui.scale.JBUIScale.scale +import com.intellij.util.concurrency.annotations.RequiresEdt import com.intellij.util.ui.FocusUtil -import com.intellij.util.ui.JBUI -import com.intellij.util.ui.UIUtil -import com.intellij.util.ui.components.BorderLayoutPanel -import java.awt.FlowLayout import java.awt.Font import javax.swing.JComponent -import javax.swing.JPanel -import javax.swing.SwingConstants -internal class EmptyStateProjectsPanel(parentDisposable: Disposable) : BorderLayoutPanel() { - init { - setBackground(WelcomeScreenUIManager.getMainAssociatedComponentBackground()) - val mainPanel: JPanel = NonOpaquePanel(VerticalFlowLayout()) - mainPanel.setBorder(JBUI.Borders.emptyTop(103)) - mainPanel.add(createTitle()) - mainPanel.add(createCommentLabel(IdeBundle.message("welcome.screen.empty.projects.create.comment"))) - mainPanel.add(createCommentLabel(IdeBundle.message("welcome.screen.empty.projects.open.comment"))) - - val (actionsToolbar: ActionToolbarImpl, moreToolbar) = createActionToolbars(parentDisposable) - - mainPanel.add(Wrapper(FlowLayout(), actionsToolbar.component)) - mainPanel.add(Wrapper(FlowLayout(), moreToolbar.component)) - addToCenter(mainPanel) +@RequiresEdt +internal fun emptyStateProjectPanel(disposable: Disposable): JComponent = panel { + row { + label(WelcomeScreenComponentFactory.getApplicationTitle()).applyToComponent { + font = font.deriveFont(font.getSize() + scale(13).toFloat()).deriveFont(Font.BOLD) + }.customize(UnscaledGaps(top = 103, bottom = 17)) + .align(AlignX.CENTER) } - - // Returns main actions, more actions - private fun createActionToolbars(parentDisposable: Disposable): Pair { - val actionManager = ActionManager.getInstance() - val baseGroup = actionManager.getAction(IdeActions.GROUP_WELCOME_SCREEN_QUICKSTART_EMPTY_STATE) as ActionGroup - val moreActionGroup = DefaultActionGroup(IdeBundle.message("welcome.screen.more.actions.link.text"), true) - - val toolbarGroup = object : ActionGroupWrapper(baseGroup) { - override fun postProcessVisibleChildren(e: AnActionEvent, visibleChildren: List): List { - moreActionGroup.removeAll() - val mapped = visibleChildren.mapIndexedNotNull { index, action -> - when { - index >= getWelcomeScreenPrimaryButtonsNum() -> { - moreActionGroup.add(action) - null - } - action is ActionGroup && action is ActionsWithPanelProvider -> { - val p = e.updateSession.presentation(action) - val wrapper = p.getClientProperty(ActionUtil.INLINE_ACTIONS)?.first() - ?: ActionGroupPanelWrapper.wrapGroups(action, parentDisposable).also { - p.putClientProperty(ActionUtil.INLINE_ACTIONS, listOf(it)) - } - e.updateSession.presentation(wrapper) - wrapper - } - else -> action - } - } - mapped.forEach { action -> - e.updateSession.presentation(action).putClientProperty( - ActionUtil.COMPONENT_PROVIDER, WelcomeScreenActionsUtil.createBigIconWithTextAction(action)) - } - moreActionGroup.templatePresentation.text = when { - moreActionGroup.childrenCount == 1 -> - e.updateSession.presentation(moreActionGroup.getChildren(e.actionManager)[0]).text - else -> IdeBundle.message("welcome.screen.more.actions.link.text") - } - return mapped - } + for (text in arrayOf( + IdeBundle.message("welcome.screen.empty.projects.create.comment"), + IdeBundle.message("welcome.screen.empty.projects.open.comment"))) { + row { + comment(text).align(AlignX.CENTER).customize(UnscaledGaps(2)) } - val actionsToolbar: ActionToolbarImpl = createActionsToolbar(toolbarGroup) - - moreActionGroup.templatePresentation.putClientProperty(ActionUtil.COMPONENT_PROVIDER, object : CustomComponentAction { - val ENABLED = Key.create("ENABLED") - var alienUpdateScheduled = false - override fun createCustomComponent(presentation: Presentation, place: String): JComponent { - return object : DropDownLink(presentation.text, { link: DropDownLink -> - JBPopupFactory.getInstance().createActionGroupPopup( - null, moreActionGroup, DataManager.getInstance().getDataContext(link), - JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, true) - }) { - override fun performAction() { - if (moreActionGroup.childrenCount == 1) { - WelcomeScreenActionsUtil.performAnActionForComponent(moreActionGroup.getChildren(actionManager)[0], this) - } - else { - super.performAction() - } - } - } - } - - override fun updateCustomComponent(component: JComponent, presentation: Presentation) { - component as? DropDownLink<*> ?: return - val single = moreActionGroup.getChildren(actionManager).singleOrNull() - if (!alienUpdateScheduled) { - alienUpdateScheduled = true - (single as? OpenAlienProjectAction)?.scheduleUpdate { enabled -> - component.isVisible = enabled - presentation.putClientProperty(ENABLED, enabled) - } - } - component.isVisible = moreActionGroup.childrenCount > 0 && - presentation.getClientProperty(ENABLED) != false - component.text = moreActionGroup.templateText - if (single != null) component.icon = null - else component.setDropDownLinkIcon() - } - }) - val moreToolbar = ActionToolbarImpl(ActionPlaces.WELCOME_SCREEN, DefaultActionGroup(moreActionGroup), true) - moreToolbar.setBorder(JBUI.Borders.emptyTop(5)) - moreToolbar.targetComponent = moreToolbar.component - moreToolbar.isOpaque = false - return Pair(actionsToolbar, moreToolbar) } + val (mainActions, moreActions) = createActionToolbars(disposable) + panel { + row { + cell(mainActions).align(AlignX.FILL) + } + }.align(AlignX.CENTER).customize(UnscaledGaps(27)) + row { + cell(moreActions).align(AlignX.CENTER) + } +}.apply { + background = WelcomeScreenUIManager.getMainAssociatedComponentBackground() +} - private fun createActionsToolbar(actionGroup: ActionGroup): ActionToolbarImpl { - val actionToolbar: ActionToolbarImpl = object : ActionToolbarImpl(ActionPlaces.WELCOME_SCREEN, actionGroup, true) { - private var wasFocusRequested = false - override fun isSecondaryAction(action: AnAction, actionIndex: Int): Boolean { - return actionIndex >= getWelcomeScreenPrimaryButtonsNum() +// Returns main actions, more actions +private fun createActionToolbars(parentDisposable: Disposable): Pair { + val actionManager = ActionManager.getInstance() + val baseGroup = actionManager.getAction(IdeActions.GROUP_WELCOME_SCREEN_QUICKSTART_EMPTY_STATE) as ActionGroup + val moreActionGroup = DefaultActionGroup(IdeBundle.message("welcome.screen.more.actions.link.text"), true) + + val toolbarGroup = object : ActionGroupWrapper(baseGroup) { + override fun postProcessVisibleChildren(e: AnActionEvent, visibleChildren: List): List { + moreActionGroup.removeAll() + val mapped = visibleChildren.mapIndexedNotNull { index, action -> + when { + index >= getWelcomeScreenPrimaryButtonsNum() -> { + moreActionGroup.add(action) + null + } + action is ActionGroup && action is ActionsWithPanelProvider -> { + val p = e.updateSession.presentation(action) + val wrapper = p.getClientProperty(ActionUtil.INLINE_ACTIONS)?.first() + ?: ActionGroupPanelWrapper.wrapGroups(action, parentDisposable).also { + p.putClientProperty(ActionUtil.INLINE_ACTIONS, listOf(it)) + } + e.updateSession.presentation(wrapper) + wrapper + } + else -> action + } } + mapped.forEach { action -> + e.updateSession.presentation(action).putClientProperty( + ActionUtil.COMPONENT_PROVIDER, WelcomeScreenActionsUtil.createBigIconWithTextAction(action)) + } + moreActionGroup.templatePresentation.text = when { + moreActionGroup.childrenCount == 1 -> + e.updateSession.presentation(moreActionGroup.getChildren(e.actionManager)[0]).text + else -> IdeBundle.message("welcome.screen.more.actions.link.text") + } + return mapped + } + } + val actionsToolbar: ActionToolbarImpl = createActionsToolbar(toolbarGroup) - override fun actionsUpdated(forced: Boolean, newVisibleActions: MutableList) { - super.actionsUpdated(forced, newVisibleActions) - if (forced && !newVisibleActions.isEmpty() && componentCount > 0 && !wasFocusRequested) { - val obj = FocusUtil.findFocusableComponentIn(components[0], null) - if (obj != null) { - wasFocusRequested = true - IdeFocusManager.getGlobalInstance().requestFocus(obj, true) + moreActionGroup.templatePresentation.putClientProperty(ActionUtil.COMPONENT_PROVIDER, object : CustomComponentAction { + val ENABLED = Key.create("ENABLED") + var alienUpdateScheduled = false + override fun createCustomComponent(presentation: Presentation, place: String): JComponent { + return object : DropDownLink(presentation.text, { link: DropDownLink -> + JBPopupFactory.getInstance().createActionGroupPopup( + null, moreActionGroup, DataManager.getInstance().getDataContext(link), + JBPopupFactory.ActionSelectionAid.SPEEDSEARCH, true) + }) { + override fun performAction() { + if (moreActionGroup.childrenCount == 1) { + WelcomeScreenActionsUtil.performAnActionForComponent(moreActionGroup.getChildren(actionManager)[0], this) + } + else { + super.performAction() } } } } - actionToolbar.setLayoutStrategy(ToolbarLayoutStrategy.NOWRAP_STRATEGY) - actionToolbar.setTargetComponent(actionToolbar.component) - actionToolbar.setBorder(JBUI.Borders.emptyTop(27)) - actionToolbar.setOpaque(false) - return actionToolbar - } - private fun createTitle(): JBLabel { - val titleLabel = JBLabel(WelcomeScreenComponentFactory.getApplicationTitle(), SwingConstants.CENTER) - titleLabel.setOpaque(false) - val componentFont = titleLabel.getFont() - titleLabel.setFont(componentFont.deriveFont(componentFont.getSize() + scale(13).toFloat()).deriveFont(Font.BOLD)) - titleLabel.setBorder(JBUI.Borders.emptyBottom(17)) - return titleLabel - } + override fun updateCustomComponent(component: JComponent, presentation: Presentation) { + component as? DropDownLink<*> ?: return + val single = moreActionGroup.getChildren(actionManager).singleOrNull() + if (!alienUpdateScheduled) { + alienUpdateScheduled = true + (single as? OpenAlienProjectAction)?.scheduleUpdate { enabled -> + component.isVisible = enabled + presentation.putClientProperty(ENABLED, enabled) + } + } + component.isVisible = moreActionGroup.childrenCount > 0 && + presentation.getClientProperty(ENABLED) != false + component.text = moreActionGroup.templateText + if (single != null) component.icon = null + else component.setDropDownLinkIcon() + } + }) + val moreToolbar = ActionToolbarImpl(ActionPlaces.WELCOME_SCREEN, DefaultActionGroup(moreActionGroup), true) + moreToolbar.targetComponent = moreToolbar.component + moreToolbar.isOpaque = false + return Pair(actionsToolbar, moreToolbar) +} - fun createCommentLabel(text: @NlsContexts.HintText String): JBLabel { - val commentFirstLabel = JBLabel(text, SwingConstants.CENTER) - commentFirstLabel.setOpaque(false) - commentFirstLabel.setForeground(UIUtil.getContextHelpForeground()) - return commentFirstLabel +private fun createActionsToolbar(actionGroup: ActionGroup): ActionToolbarImpl { + val actionToolbar: ActionToolbarImpl = object : ActionToolbarImpl(ActionPlaces.WELCOME_SCREEN, actionGroup, true) { + private var wasFocusRequested = false + + override fun isSecondaryAction(action: AnAction, actionIndex: Int): Boolean { + return actionIndex >= getWelcomeScreenPrimaryButtonsNum() + } + + override fun actionsUpdated(forced: Boolean, newVisibleActions: MutableList) { + super.actionsUpdated(forced, newVisibleActions) + if (forced && !newVisibleActions.isEmpty() && componentCount > 0 && !wasFocusRequested) { + val obj = FocusUtil.findFocusableComponentIn(components[0], null) + if (obj != null) { + wasFocusRequested = true + IdeFocusManager.getGlobalInstance().requestFocus(obj, true) + } + } + } } -} \ No newline at end of file + actionToolbar.setLayoutStrategy(ToolbarLayoutStrategy.WRAP_STRATEGY) + actionToolbar.setTargetComponent(actionToolbar.component) + actionToolbar.setOpaque(false) + return actionToolbar +} diff --git a/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/ProjectsTabFactory.kt b/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/ProjectsTabFactory.kt index 0a780160208b..1ee57ff55718 100644 --- a/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/ProjectsTabFactory.kt +++ b/platform/platform-impl/src/com/intellij/openapi/wm/impl/welcomeScreen/ProjectsTabFactory.kt @@ -195,7 +195,7 @@ internal class ProjectsTab(private val parentDisposable: Disposable) : DefaultWe } private fun createEmptyStatePanel(): JComponent { - val emptyStateProjectsPanel = EmptyStateProjectsPanel(parentDisposable) + val emptyStateProjectsPanel = emptyStateProjectPanel(parentDisposable) initDnD(emptyStateProjectsPanel) return emptyStateProjectsPanel }