RSyntaxTextArea
RSyntaxTextArea copied to clipboard
Flickering of focusable tooltip
Hi,
I wanted to resize font of the text area on a CTRL+{mouse wheel}. Important part is to show a tooltip with the zoom percent on a fixed location (or close to the mouse pointer, this doesn't matter).
I was successful with the resizing, but I'm having trouble with properly showing the tooltip - on a mouse wheel it is flickering. I use the following code:
TextEditorPane textPane = new TextEditorPane(RTextArea.INSERT_MODE, true);
FocusableTip zoomTip = new FocusableTip(textPane, null);
textPane.addMouseWheelListener(e -> {
int wheelRotation = e.getWheelRotation();
if (wheelRotation != 0 && (e.getModifiersEx() & CTRL_DOWN_MASK) != 0) {
// font resizing
Font font = textPane.getFont();
int currentSize = font.getSize();
float newSize = (wheelRotation > 0) ? Math.max(currentSize - 1, FONT_DEFAULT_SIZE) : (currentSize + 1);
textPane.setFont(font.deriveFont(newSize));
// now the tooltip!
int percent = (int)(newSize / FONT_DEFAULT_SIZE * 100.0);
zoomTip.toolTipRequested(e, percent + "%");
}
});
Note that maybe I'm using the FocusableTip
in a wrong way, but I cannot find proper usage of it (lack of documentation).
My analysis is as follows - the problem might be in the order of repaint events in Swing. Upon a font change the whole text area view is repainted (if the text is long enough) and this might happen after the tooltip is being painted. I think a double buffering of the tooltip might help.
EDIT: I'm using Linux, Java 11
Thanks, I'll take a look. Also note I probably should add this as a built-in feature, at least the mouse wheel changing the font size, along with it firing an event to let folks do things like display a tool tip.
Yes, very good idea. User should be able to enable/disable the behavior when constructing the text editor. Thanks!