From b6317fe47492d3ec3afdda43a99dc67e4992bee5 Mon Sep 17 00:00:00 2001 From: "Gregory.Shrago" Date: Mon, 25 Mar 2024 13:30:58 +0400 Subject: [PATCH] reuse update session to expand toolbar groups properly Fixes GTW-8175 Toolbar actions icons are missing sometimes in Run toolwindow GitOrigin-RevId: 6dd5a6158585d7552875bb1e25707084793f403f --- .../ide/actions/ToggleToolbarAction.java | 51 ++++++++++--------- 1 file changed, 27 insertions(+), 24 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/ide/actions/ToggleToolbarAction.java b/platform/platform-impl/src/com/intellij/ide/actions/ToggleToolbarAction.java index c1714fdbeca6..d369c0bb8250 100644 --- a/platform/platform-impl/src/com/intellij/ide/actions/ToggleToolbarAction.java +++ b/platform/platform-impl/src/com/intellij/ide/actions/ToggleToolbarAction.java @@ -15,8 +15,8 @@ import com.intellij.ui.content.Content; import com.intellij.ui.content.ContentManager; import com.intellij.ui.content.ContentManagerEvent; import com.intellij.ui.content.ContentManagerListener; -import com.intellij.util.SmartList; import com.intellij.util.containers.ContainerUtil; +import com.intellij.util.containers.JBIterable; import com.intellij.util.ui.UIUtil; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -24,6 +24,7 @@ import org.jetbrains.annotations.Nullable; import javax.swing.*; import java.awt.*; +import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -223,38 +224,40 @@ public final class ToggleToolbarAction extends ToggleAction implements DumbAware @Override public AnAction @NotNull [] getChildren(@Nullable AnActionEvent e) { if (e == null) return EMPTY_ARRAY; - return e.getUpdateSession() - .compute(this, "getChildrenImpl", ActionUpdateThread.EDT, this::getChildrenImpl); - } - - private AnAction @NotNull [] getChildrenImpl() { - ContentManager contentManager = myToolWindow.getContentManagerIfCreated(); - Content selectedContent = contentManager == null ? null : contentManager.getSelectedContent(); - JComponent contentComponent = selectedContent == null ? null : selectedContent.getComponent(); - if (contentComponent == null) return EMPTY_ARRAY; - List result = new SmartList<>(); - for (final ActionToolbar toolbar : iterateToolbars(Collections.singletonList(contentComponent))) { - JComponent c = toolbar.getComponent(); - if (c.isVisible() || !c.isValid()) continue; - if (!result.isEmpty() && !(ContainerUtil.getLastItem(result) instanceof Separator)) { - result.add(Separator.getInstance()); - } - - List actions = toolbar.getActions(); + UpdateSession updateSession = e.getUpdateSession(); + List groups = updateSession.compute( + this, "collectActionGroups", ActionUpdateThread.EDT, this::collectActionGroups); + if (groups.isEmpty()) return EMPTY_ARRAY; + List result = new ArrayList<>(); + for (ActionGroup group : groups) { + Iterable actions = updateSession.expandedChildren(group); for (AnAction action : actions) { if (action instanceof ToggleAction && !result.contains(action)) { result.add(action); } - else if (action instanceof Separator) { - if (!result.isEmpty() && !(ContainerUtil.getLastItem(result) instanceof Separator)) { - result.add(Separator.getInstance()); - } + else if (action instanceof Separator && !result.isEmpty() && + !(ContainerUtil.getLastItem(result) instanceof Separator)) { + result.add(Separator.getInstance()); } } } boolean popup = ContainerUtil.count(result, it -> !(it instanceof Separator)) > 3; if (!popup && !result.isEmpty()) result.add(Separator.getInstance()); - return result.toArray(AnAction.EMPTY_ARRAY); + return result.toArray(EMPTY_ARRAY); + } + + private @NotNull List collectActionGroups() { + ContentManager contentManager = myToolWindow.getContentManagerIfCreated(); + Content selectedContent = contentManager == null ? null : contentManager.getSelectedContent(); + JComponent contentComponent = selectedContent == null ? null : selectedContent.getComponent(); + if (contentComponent == null) return Collections.emptyList(); + return JBIterable.from(iterateToolbars(Collections.singletonList(contentComponent))) + .filterMap(toolbar -> { + JComponent c = toolbar.getComponent(); + if (c.isVisible() || !c.isValid()) return null; + return toolbar.getActionGroup(); + }) + .toList(); } } }