Skip to content

Commit

Permalink
Fix positioning of the target selection dialog.
Browse files Browse the repository at this point in the history
The logic must differ slightly between clicking the button in the inspection widget vs selecting it from a context menu. Using showUnderneathOf in the case of the context menu causes the popup to show underneath the entire menu which seems wrong.

(cherry picked from commit 0716735)
  • Loading branch information
Googler authored and mai93 committed Aug 14, 2023
1 parent c15c9fb commit 753059d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.idea.blaze.base.qsync;

import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper;
import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper.PopupPosititioner;
import com.google.idea.blaze.base.qsync.settings.QuerySyncConfigurable;
import com.google.idea.blaze.base.qsync.settings.QuerySyncSettings;
import com.google.idea.blaze.base.settings.Blaze;
Expand Down Expand Up @@ -86,10 +87,10 @@ public BuildDependencies(@NotNull Editor editor) {
buildDepsHelper = new BuildDependenciesHelper(editor.getProject());
}


@Override
public void actionPerformed(@NotNull AnActionEvent e) {
buildDepsHelper.enableAnalysis(e);
buildDepsHelper.enableAnalysis(
e, PopupPosititioner.showUnderneathClickedComponentOrCentered(e));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.idea.blaze.base.qsync.action;

import com.google.idea.blaze.base.actions.BlazeProjectAction;
import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper.PopupPosititioner;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.intellij.icons.AllIcons.Actions;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
Expand Down Expand Up @@ -72,6 +73,6 @@ protected void updateForBlazeProject(Project project, AnActionEvent e) {
@Override
protected void actionPerformedInBlazeProject(Project project, AnActionEvent e) {
BuildDependenciesHelper helper = new BuildDependenciesHelper(project);
helper.enableAnalysis(e);
helper.enableAnalysis(e, PopupPosititioner.showAtMousePointerOrCentered(e));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.google.common.collect.Iterables;
import com.google.idea.blaze.base.actions.BlazeProjectAction;
import com.google.idea.blaze.base.qsync.TargetsToBuild;
import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper.PopupPosititioner;
import com.google.idea.blaze.common.Label;
import com.intellij.openapi.actionSystem.ActionUpdateThread;
import com.intellij.openapi.actionSystem.AnActionEvent;
Expand Down Expand Up @@ -81,7 +82,7 @@ protected void actionPerformedInBlazeProject(Project project, AnActionEvent even
helper.chooseTargetToBuildFor(
targetsToBuild.get(ambiguousOne),
ambiguousOne,
event,
PopupPosititioner.showAtMousePointerOrCentered(event),
chosen ->
helper.enableAnalysis(
ImmutableSet.<Label>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.openapi.ui.popup.JBPopupFactory;
import com.intellij.openapi.ui.popup.ListPopup;
import com.intellij.openapi.ui.popup.PopupStep;
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.awt.RelativePoint;
import java.awt.event.MouseEvent;
import java.nio.file.Path;
import java.util.Comparator;
Expand All @@ -43,6 +45,46 @@
*/
public class BuildDependenciesHelper {

/** Encapsulates a relative position to show the target selection popup at. */
public interface PopupPosititioner {
void showInCorrectPosition(JBPopup popup);

/**
* Shows the popup at the location of the mount event, or centered it the screen if the event is
* not a mouse event (e.g. keyboard shortcut).
*
* <p>This is used e.g. when selecting "build dependencies" from a context menu.
*/
static PopupPosititioner showAtMousePointerOrCentered(AnActionEvent e) {
return popup -> {
if (e.getInputEvent() instanceof MouseEvent) {
popup.show(
RelativePoint.fromScreen(((MouseEvent) e.getInputEvent()).getLocationOnScreen()));
} else {
popup.showCenteredInCurrentWindow(e.getProject());
}
};
}

/**
* Shows the popup underneath the clicked UI component, or centered in tge screen if the event
* is not a mouse event.
*
* <p>This is used to show the popup underneath the inspection widget action.
*/
static PopupPosititioner showUnderneathClickedComponentOrCentered(AnActionEvent event) {
return popup -> {
if (event.getInputEvent() instanceof MouseEvent
&& event.getInputEvent().getComponent() != null) {
// if the user clicked the action button, show underneath that
popup.showUnderneathOf(event.getInputEvent().getComponent());
} else {
popup.showCenteredInCurrentWindow(event.getProject());
}
};
}
}

private final Project project;
private final QuerySyncManager syncManager;

Expand Down Expand Up @@ -88,7 +130,7 @@ public static VirtualFile getVirtualFile(AnActionEvent e) {
return e.getData(CommonDataKeys.VIRTUAL_FILE);
}

public void enableAnalysis(AnActionEvent e) {
public void enableAnalysis(AnActionEvent e, PopupPosititioner positioner) {
VirtualFile vfile = getVirtualFile(e);
TargetsToBuild toBuild = getTargetsToEnableAnalysisFor(vfile);
if (toBuild.isEmpty()) {
Expand All @@ -98,7 +140,8 @@ public void enableAnalysis(AnActionEvent e) {
syncManager.enableAnalysis(toBuild.targets());
return;
}
chooseTargetToBuildFor(vfile, toBuild, e, label -> enableAnalysis(ImmutableSet.of(label)));
chooseTargetToBuildFor(
vfile, toBuild, positioner, label -> enableAnalysis(ImmutableSet.of(label)));
}

void enableAnalysis(ImmutableSet<Label> targets) {
Expand All @@ -108,18 +151,12 @@ void enableAnalysis(ImmutableSet<Label> targets) {
public void chooseTargetToBuildFor(
VirtualFile vfile,
TargetsToBuild toBuild,
AnActionEvent event,
PopupPosititioner positioner,
Consumer<Label> chosenConsumer) {
JBPopupFactory factory = JBPopupFactory.getInstance();
ListPopup popup =
factory.createListPopup(SelectTargetPopupStep.create(toBuild, vfile, chosenConsumer));
if (event.getInputEvent() instanceof MouseEvent
&& event.getInputEvent().getComponent() != null) {
// if the user clicked the action button, show underneath that
popup.showUnderneathOf(event.getInputEvent().getComponent());
} else {
popup.showCenteredInCurrentWindow(event.getProject());
}
positioner.showInCorrectPosition(popup);
}

static class SelectTargetPopupStep extends BaseListPopupStep<Label> {
Expand Down

0 comments on commit 753059d

Please sign in to comment.