Skip to content

Commit

Permalink
Move PopupPositioner to top-level class
Browse files Browse the repository at this point in the history
The interface is used in a number of other classes.

PiperOrigin-RevId: 588096110
  • Loading branch information
Googler authored and copybara-github committed Dec 6, 2023
1 parent 8d9bd98 commit e8a81d5
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper;
import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper.DepsBuildType;
import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper.PopupPosititioner;
import com.google.idea.blaze.base.qsync.action.PopupPositioner;
import com.google.idea.blaze.base.qsync.settings.QuerySyncConfigurableProvider;
import com.google.idea.blaze.base.qsync.settings.QuerySyncSettings;
import com.google.idea.blaze.base.settings.Blaze;
Expand Down Expand Up @@ -91,7 +91,7 @@ public BuildDependencies(@NotNull Editor editor) {
@Override
public void actionPerformed(@NotNull AnActionEvent e) {
buildDepsHelper.enableAnalysis(
e, PopupPosititioner.showUnderneathClickedComponentOrCentered(e));
e, PopupPositioner.showUnderneathClickedComponentOrCentered(e));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import com.google.idea.blaze.base.qsync.QuerySyncManager;
import com.google.idea.blaze.base.qsync.QuerySyncManager.TaskOrigin;
import com.google.idea.blaze.base.qsync.QuerySyncProject;
import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper.PopupPosititioner;
import com.google.idea.blaze.exception.BuildException;
import com.intellij.notification.NotificationGroupManager;
import com.intellij.notification.NotificationType;
Expand Down Expand Up @@ -103,17 +102,17 @@ public static class Performer {
private final Path workspacePathToAdd;
@Nullable private final AnActionEvent event;

private final PopupPosititioner popupPosititioner;
private final PopupPositioner popupPositioner;

public Performer(
Project project,
Path workspacePathToAdd,
@Nullable AnActionEvent event,
PopupPosititioner popupPosititioner) {
PopupPositioner popupPositioner) {
this.project = project;
this.workspacePathToAdd = workspacePathToAdd;
this.event = event;
this.popupPosititioner = popupPosititioner;
this.popupPositioner = popupPositioner;
qsManager = QuerySyncManager.getInstance(project);
}

Expand Down Expand Up @@ -230,7 +229,7 @@ protected ImmutableList<CandidatePackage> compute(
.createListPopup(
SelectPackagePopupStep.create(
candidatePackages, Performer.this::doAddToProjectView));
popupPosititioner.showInCorrectPosition(popup);
popupPositioner.showInCorrectPosition(popup);
}
} catch (BuildException e) {
notify(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.idea.blaze.base.actions.BlazeProjectAction;
import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper.DepsBuildType;
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 @@ -76,6 +75,6 @@ protected void updateForBlazeProject(Project project, AnActionEvent e) {
protected void actionPerformedInBlazeProject(Project project, AnActionEvent e) {
BuildDependenciesHelper helper =
new BuildDependenciesHelper(project, getClass(), DepsBuildType.SELF);
helper.enableAnalysis(e, PopupPosititioner.showAtMousePointerOrCentered(e));
helper.enableAnalysis(e, PopupPositioner.showAtMousePointerOrCentered(e));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import com.google.idea.blaze.base.actions.BlazeProjectAction;
import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper.DepsBuildType;
import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper.PopupPosititioner;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.project.Project;
Expand Down Expand Up @@ -49,7 +48,7 @@ protected void actionPerformedInBlazeProject(Project project, AnActionEvent e) {
if (relativePath.isEmpty()) {
return;
}
helper.enableAnalysis(e, PopupPosititioner.showAtMousePointerOrCentered(e));
helper.enableAnalysis(e, PopupPositioner.showAtMousePointerOrCentered(e));
}

private BuildDependenciesHelper createHelper(Project project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.google.idea.blaze.base.logging.utils.querysync.QuerySyncActionStatsScope;
import com.google.idea.blaze.base.qsync.TargetsToBuild;
import com.google.idea.blaze.base.qsync.action.BuildDependenciesHelper.DepsBuildType;
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 @@ -82,7 +81,7 @@ protected void actionPerformedInBlazeProject(Project project, AnActionEvent even
helper.chooseTargetToBuildFor(
ambiguousOne.sourceFilePath().orElseThrow(),
ambiguousOne,
PopupPosititioner.showAtMousePointerOrCentered(event),
PopupPositioner.showAtMousePointerOrCentered(event),
chosen ->
helper.enableAnalysis(
ImmutableSet.<Label>builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,11 @@
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;
import java.util.Optional;
Expand All @@ -60,46 +57,6 @@ public enum DepsBuildType {
REVERSE_DEPS
}

/** 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;
private final Class<?> actionClass;
Expand Down Expand Up @@ -180,7 +137,7 @@ public ImmutableSet<Label> getAffectedTargetsForPaths(ImmutableSet<Path> paths)
return disambiguator.unambiguousTargets;
}

public void enableAnalysis(AnActionEvent e, PopupPosititioner popupPosititioner) {
public void enableAnalysis(AnActionEvent e, PopupPositioner popupPositioner) {
ImmutableSet<Label> additionalTargets;
if (QuerySyncSettings.getInstance().buildWorkingSet()) {
try {
Expand All @@ -194,11 +151,11 @@ public void enableAnalysis(AnActionEvent e, PopupPosititioner popupPosititioner)
} else {
additionalTargets = ImmutableSet.of();
}
enableAnalysis(e, popupPosititioner, additionalTargets);
enableAnalysis(e, popupPositioner, additionalTargets);
}

public void enableAnalysis(
AnActionEvent e, PopupPosititioner positioner, ImmutableSet<Label> additionalTargetsToBuild) {
AnActionEvent e, PopupPositioner positioner, ImmutableSet<Label> additionalTargetsToBuild) {
VirtualFile vfile = getVirtualFile(e);
determineTargetsAndRun(
vfile,
Expand All @@ -215,13 +172,13 @@ public void enableAnalysis(
}

public void determineTargetsAndRun(
VirtualFile vf, PopupPosititioner positioner, Consumer<ImmutableSet<Label>> consumer) {
VirtualFile vf, PopupPositioner positioner, Consumer<ImmutableSet<Label>> consumer) {
determineTargetsAndRun(vf, positioner, consumer, ImmutableSet.of());
}

public void determineTargetsAndRun(
VirtualFile vf,
PopupPosititioner positioner,
PopupPositioner positioner,
Consumer<ImmutableSet<Label>> consumer,
ImmutableSet<Label> additionalTargetsToBuild) {
TargetsToBuild toBuild = getTargetsToEnableAnalysisFor(vf);
Expand Down Expand Up @@ -269,7 +226,7 @@ void enableAnalysis(ImmutableSet<Label> targets, QuerySyncActionStatsScope query
public void chooseTargetToBuildFor(
Path workspaceRelativePath,
TargetsToBuild toBuild,
PopupPosititioner positioner,
PopupPositioner positioner,
Consumer<Label> chosenConsumer) {
chooseTargetToBuildFor(
WorkspaceRoot.fromProject(project).path().resolve(workspaceRelativePath).toString(),
Expand All @@ -281,7 +238,7 @@ public void chooseTargetToBuildFor(
public void chooseTargetToBuildFor(
String fileName,
TargetsToBuild toBuild,
PopupPosititioner positioner,
PopupPositioner positioner,
Consumer<Label> chosenConsumer) {
JBPopupFactory factory = JBPopupFactory.getInstance();
ListPopup popup =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright 2023 The Bazel Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.idea.blaze.base.qsync.action;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.ui.popup.JBPopup;
import com.intellij.ui.awt.RelativePoint;
import java.awt.event.MouseEvent;

/** Encapsulates a relative position to show a popup. */
public interface PopupPositioner {
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 PopupPositioner 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 PopupPositioner 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());
}
};
}
}

0 comments on commit e8a81d5

Please sign in to comment.