Skip to content

Commit

Permalink
Position package select popup next to editor notification
Browse files Browse the repository at this point in the history
(cherry picked from commit 0e40238)
  • Loading branch information
Googler authored and mai93 committed Dec 5, 2023
1 parent e4e82db commit 7dc8544
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.google.idea.blaze.base.projectview.ProjectViewSet;
import com.google.idea.blaze.base.qsync.QuerySyncManager;
import com.google.idea.blaze.base.qsync.QuerySyncProject;
import com.google.idea.blaze.base.qsync.action.AddToProjectAction;
import com.google.idea.blaze.base.scope.BlazeContext;
import com.google.idea.blaze.base.settings.Blaze;
import com.google.idea.blaze.base.settings.BlazeImportSettings;
Expand All @@ -54,7 +55,6 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.EditorNotificationPanel;
import com.intellij.ui.EditorNotifications;
import com.intellij.ui.HyperlinkLabel;
import java.io.File;
import java.nio.file.Path;
import java.util.HashSet;
Expand Down Expand Up @@ -224,10 +224,10 @@ private EditorNotificationPanel createNotificationPanelForQuerySync(VirtualFile

return createPanel(
virtualFile,
p -> {
HyperlinkLabel unused =
p.createActionLabel("Add file to project", "Blaze.AddToQuerySyncProjectView");
});
p ->
p.createActionLabel(
"Add file to project",
() -> AddToProjectAction.Performer.create(project, virtualFile, p).perform()));
}

private EditorNotificationPanel createPanel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
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 All @@ -51,9 +52,14 @@
import com.intellij.openapi.ui.popup.util.BaseListPopupStep;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.EditorNotificationPanel;
import com.intellij.ui.awt.RelativePoint;
import com.intellij.ui.popup.list.ListPopupImpl;
import java.awt.Point;
import java.nio.file.Path;
import java.util.Optional;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.jetbrains.annotations.NotNull;

/** Query sync specific action to add a new directory to the project view. */
Expand All @@ -76,12 +82,12 @@ protected QuerySyncStatus querySyncSupport() {

@Override
protected void actionPerformedInBlazeProject(Project project, AnActionEvent e) {
new Performer(project, e).perform();
Performer.create(project, e).perform();
}

@Override
protected void updateForBlazeProject(Project project, AnActionEvent e) {
Performer performer = new Performer(project, e);
Performer performer = Performer.create(project, e);
Presentation presentation = e.getPresentation();
if (!performer.canPerform()) {
presentation.setEnabledAndVisible(false);
Expand All @@ -90,22 +96,69 @@ protected void updateForBlazeProject(Project project, AnActionEvent e) {
presentation.setEnabled(true);
}

static class Performer {
/** Class that handles identifying the package to add and requesting a project update. */
public static class Performer {
private final Project project;
private final AnActionEvent event;
private final QuerySyncManager qsManager;
private final Path workspacePathToAdd;
@Nullable private final AnActionEvent event;

Performer(Project project, AnActionEvent event) {
private final PopupPosititioner popupPosititioner;

public Performer(
Project project,
Path workspacePathToAdd,
@Nullable AnActionEvent event,
PopupPosititioner popupPosititioner) {
this.project = project;
this.workspacePathToAdd = workspacePathToAdd;
this.event = event;
this.popupPosititioner = popupPosititioner;
qsManager = QuerySyncManager.getInstance(project);
workspacePathToAdd =
getWorkspaceFile(WorkspaceRoot.fromProject(project).path(), event).orElse(null);
}

public static Performer create(Project project, AnActionEvent event) {
return new Performer(
project,
getWorkspaceFile(WorkspaceRoot.fromProject(project).path(), event).orElse(null),
event,
popup -> popup.showInBestPositionFor(event.getDataContext()));
}

/**
* Places the package selection popup under the editor notification, on the right side of the
* window.
*
* <p>For the positioning logic, see {@link
* com.intellij.openapi.roots.ui.configuration.SdkPopupImpl#showUnderneathToTheRightOf}
*/
public static Performer create(
Project project, VirtualFile virtualFile, EditorNotificationPanel panel) {
return new Performer(
project,
getWorkspaceFile(WorkspaceRoot.fromProject(project).path(), virtualFile).orElse(null),
null,
popup -> {
if (popup instanceof ListPopupImpl) {
int width = (int) ((ListPopupImpl) popup).getList().getPreferredSize().getWidth();
popup.show(
new RelativePoint(panel, new Point(panel.getWidth() - width, panel.getHeight())));
} else {
popup.showUnderneathOf(panel);
}
});
}

private static Optional<Path> getWorkspaceFile(Path workspaceRoot, AnActionEvent event) {
if (event == null) {
return Optional.empty();
}
VirtualFile virtualFile = event.getData(CommonDataKeys.VIRTUAL_FILE);
return getWorkspaceFile(workspaceRoot, virtualFile);
}

private static Optional<Path> getWorkspaceFile(
Path workspaceRoot, @Nullable VirtualFile virtualFile) {
if (virtualFile == null) {
return Optional.empty();
}
Expand Down Expand Up @@ -141,7 +194,7 @@ boolean canPerform() {
return true;
}

void perform() {
public void perform() {
if (!canPerform()) {
// This shouldn't happen, but could rarely if there's a race between updateForBlazeProject
// and actionPerformedInBlazeProject which may be possible. Fail gracefully.
Expand Down Expand Up @@ -177,7 +230,7 @@ protected ImmutableList<CandidatePackage> compute(
.createListPopup(
SelectPackagePopupStep.create(
candidatePackages, Performer.this::doAddToProjectView));
popup.showInBestPositionFor(event.getDataContext());
popupPosititioner.showInCorrectPosition(popup);
}
} catch (BuildException e) {
notify(
Expand Down

0 comments on commit 7dc8544

Please sign in to comment.