Skip to content

Commit

Permalink
Fixed a bug where the terminal would show an empty tooltip when hover…
Browse files Browse the repository at this point in the history
…ing over a region with no marking. Now it will not show up the tooltip in that case.
  • Loading branch information
neilccbrown committed Mar 1, 2024
1 parent a18de84 commit 5fd98c6
Showing 1 changed file with 48 additions and 16 deletions.
64 changes: 48 additions & 16 deletions bluej/src/main/java/bluej/terminal/TerminalTextPane.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public abstract class TerminalTextPane extends BaseEditorPane
private Pos anchorPos = new Pos(0, 0, 0);

private final Tooltip tooltip = new Tooltip();
private boolean tooltipInstalled = false;
private EditorPosition lastMousePos;

// A record holding a location in the terminal window.
Expand Down Expand Up @@ -202,25 +203,13 @@ public void scrollEventOnTextLine(ScrollEvent e, BaseEditorPane editorPane)
clear();

Tooltip.install(this, tooltip);
tooltipInstalled = true;

tooltip.setOnShowing(e -> {
if (lastMousePos != null)
{
for (Section s : currentSections)
{
if (s.start.line > lastMousePos.getLine())
continue; // We are before its start line
if (s.end.line >= 0 && s.end.line < lastMousePos.getLine())
continue; // We are after its end line
if (s.start.line == lastMousePos.getLine() && lastMousePos.getColumn() < s.start.column)
continue; // We are on the start line, but before its start column
if (s.end.line == lastMousePos.getLine() && lastMousePos.getColumn() > s.end.column)
continue; // We are on the end line, but after its end column
// We're inside!
tooltip.setText(s.title);
return;
}
tooltip.setText("");
String text = calculateTextForLastMousePos();
tooltip.setText(text);
}
});
// Because the tooltip is for the whole node, it will show even while you mouse around.
Expand All @@ -233,6 +222,19 @@ public void scrollEventOnTextLine(ScrollEvent e, BaseEditorPane editorPane)
{
Tooltip.uninstall(this, tooltip);
Tooltip.install(this, tooltip);
tooltipInstalled = true;
}
else if (calculateTextForLastMousePos().isEmpty())
{
// We don't want to risk showing an empty tooltip, so uninstall it now before tooltip is triggered there:
Tooltip.uninstall(this, tooltip);
tooltipInstalled = false;
}
else if (!tooltipInstalled)
{
// There is content to show, so put the tooltip back on ready to show after a hover:
Tooltip.install(this, tooltip);
tooltipInstalled = true;
}
});
addEventFilter(ScrollEvent.ANY, e -> {
Expand All @@ -246,7 +248,37 @@ public void scrollEventOnTextLine(ScrollEvent e, BaseEditorPane editorPane)
});

}


/**
* Calculates the tooltip that would be shown at lastMousePos. If the tooltip
* should be blank, either because there's no output at that location, or because
* the mouse isn't over a valid location, the empty string is returned (not null).
*/
private String calculateTextForLastMousePos()
{
String text = "";
if (lastMousePos == null)
return text;
for (Section s : currentSections)
{
if (s.start.line > lastMousePos.getLine())
continue; // We are before its start line
if (s.end.line >= 0 && s.end.line < lastMousePos.getLine())
continue; // We are after its end line
if (s.start.line == lastMousePos.getLine() && lastMousePos.getColumn() < s.start.column)
continue; // We are on the start line, but before its start column
if (s.end.line == lastMousePos.getLine() && lastMousePos.getColumn() >= s.end.column)
continue; // We are on the end line, but after its end column. Important to have >=
// because if a line ends at say column 10, and there's nothing at column 11
// we don't want to show a tooltip there, but the position will be clamped
// to column 10 by the lookup in the text pane (because column 11 doesn't exist).
// We're inside!
text = s.title;
break;
}
return text;
}


@Override
protected void keyPressed(KeyEvent event)
Expand Down

0 comments on commit 5fd98c6

Please sign in to comment.