From 5fd98c6dab3981cc9aad1296bc7ae6b0bd4a5d22 Mon Sep 17 00:00:00 2001 From: Neil Brown Date: Fri, 1 Mar 2024 13:29:09 +0000 Subject: [PATCH] Fixed a bug where the terminal would show an empty tooltip when hovering over a region with no marking. Now it will not show up the tooltip in that case. --- .../java/bluej/terminal/TerminalTextPane.java | 64 ++++++++++++++----- 1 file changed, 48 insertions(+), 16 deletions(-) diff --git a/bluej/src/main/java/bluej/terminal/TerminalTextPane.java b/bluej/src/main/java/bluej/terminal/TerminalTextPane.java index ad259d6b8..a5019226c 100644 --- a/bluej/src/main/java/bluej/terminal/TerminalTextPane.java +++ b/bluej/src/main/java/bluej/terminal/TerminalTextPane.java @@ -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. @@ -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. @@ -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 -> { @@ -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)