in plugin/src/software/aws/toolkits/eclipse/amazonq/inlineChat/InlineChatUIManager.java [73:214]
public CompletableFuture<Void> showUserInputPrompt() {
CompletableFuture<Void> future = new CompletableFuture<>();
Display.getDefault().syncExec(() -> {
if (inputBox != null) {
inputBox.close();
}
if (viewer == null || viewer.getTextWidget() == null) {
future.completeExceptionally(new IllegalStateException("Text widget not available"));
return;
}
var widget = viewer.getTextWidget();
inputBox = new PopupDialog(widget.getShell(), PopupDialog.INFOPOPUP_SHELLSTYLE, false, false, true, false, false, null, null) {
private Point screenLocation;
private Text inputField;
@Override
public int open() {
int result = super.open();
Display.getCurrent().asyncExec(() -> {
if (inputField != null && !inputField.isDisposed()) {
inputField.setFocus();
}
});
return result;
}
@Override
protected Point getInitialLocation(final Point initialSize) {
if (screenLocation == null) {
try {
int visualOffset;
if (viewer instanceof ITextViewerExtension5) {
visualOffset = ((ITextViewerExtension5) viewer).modelOffset2WidgetOffset(task.getSelectionOffset());
} else if (viewer instanceof ProjectionViewer) {
visualOffset = ((ProjectionViewer) viewer).modelOffset2WidgetOffset(task.getSelectionOffset());
} else {
visualOffset = task.getSelectionOffset();
}
int indentedOffset = calculateIndentOffset(widget, visualOffset);
Point location = widget.getLocationAtOffset(indentedOffset);
// Move input bar up as to not block the selected code
location.y -= widget.getLineHeight() * 2.5;
screenLocation = Display.getCurrent().map(widget, null, location);
} catch (Exception e) {
Activator.getLogger().error("Exception positioning input prompt: " + e.getMessage(), e);
if (widget != null) {
Point location = widget.getLocationAtOffset(widget.getCaretOffset());
location.y -= widget.getLineHeight() * 2.5;
screenLocation = Display.getCurrent().map(widget, null, location);
}
}
}
return screenLocation;
}
@Override
protected Control createDialogArea(final Composite parent) {
var composite = (Composite) super.createDialogArea(parent);
composite.setLayout(new GridLayout(1, false));
inputField = new Text(composite, SWT.SEARCH | SWT.BORDER | SWT.SINGLE);
if (PluginUtils.getPlatform() == PluginPlatform.WINDOWS) {
Display.getDefault().asyncExec(() -> {
inputField.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_GRAY));
inputField.setText(inputPromptMessage);
});
inputField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent e) {
// If this is the first character being typed
boolean backspace = (e.keyCode == SWT.DEL || e.keyCode == SWT.BS);
if (inputField.getText().equals(inputPromptMessage)) {
if (!backspace) {
inputField.setText("");
inputField.setForeground(isDarkTheme
? Display.getDefault().getSystemColor(SWT.COLOR_WHITE)
: Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
}
e.doit = !backspace;
} else if (backspace && inputField.getText().length() <= 1) {
inputField.setText(inputPromptMessage);
inputField.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_GRAY));
}
}
});
} else {
inputField.setMessage(inputPromptMessage);
}
GridData gridData = new GridData(GridData.FILL_HORIZONTAL);
gridData.widthHint = 350;
inputField.setLayoutData(gridData);
inputField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(final KeyEvent e) {
if (e.character == SWT.CR || e.character == SWT.LF) {
// Gather inputs and send back to controller
var userInput = inputField.getText();
if (userInputIsValid(userInput)) {
var cursorState = getSelectionRangeCursorState().get();
task.setCursorState(cursorState);
task.setPrompt(userInput);
future.complete(null);
inputBox.close();
}
}
}
});
// Close prompt if user scrolls away
widget.getDisplay().addFilter(SWT.MouseVerticalWheel, event -> {
close();
if (!future.isDone()) {
future.complete(null);
}
});
// Disposal before future completes indicates user hit ESC to cancel
getShell().addDisposeListener(e -> {
widget.getDisplay().removeFilter(SWT.MouseVerticalWheel, event -> {
});
if (!future.isDone()) {
future.complete(null);
}
});
composite.layout(true, true);
return composite;
}
};
inputBox.setBlockOnOpen(true);
inputBox.open();
});
return future;
}