1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-03-13 10:29:35 +01:00

Move editor font resize listener code to Base

- Move editor font resize listener code to the Base class.
- Cache and share the same listener for all components.
This commit is contained in:
Pieter12345 2019-03-26 16:45:33 +01:00 committed by Cristian Maglie
parent 3aa81b0a89
commit 61bbc382b5
4 changed files with 84 additions and 91 deletions

View File

@ -8,9 +8,6 @@ import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.MouseWheelEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.text.SimpleDateFormat;
@ -35,8 +32,6 @@ import cc.arduino.packages.BoardPort;
@SuppressWarnings("serial")
public abstract class AbstractTextMonitor extends AbstractMonitor {
private final Base base;
protected JLabel noLineEndingAlert;
protected TextAreaFIFO textArea;
protected JScrollPane scrollPane;
@ -50,7 +45,10 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
public AbstractTextMonitor(Base base, BoardPort boardPort) {
super(boardPort);
this.base = base;
// Add font size adjustment listeners. This has to be done here due to
// super(boardPort) invoking onCreateWindow(...) before we can store base.
base.addEditorFontResizeListeners(textArea);
}
@Override
@ -67,40 +65,6 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
// whether or not to do so based on the autoscroll checkbox.
((DefaultCaret) textArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
// Add "CTRL scroll" hotkey for font size adjustment.
textArea.addMouseWheelListener((MouseWheelEvent e) -> {
if (e.isControlDown()) {
if (e.getWheelRotation() < 0) {
base.handleFontSizeChange(1);
} else {
base.handleFontSizeChange(-1);
}
} else {
e.getComponent().getParent().dispatchEvent(e);
}
});
// Add "CTRL (SHIFT) =/+" and "CTRL -" hotkeys for font size adjustment.
textArea.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK
|| e.getModifiersEx() == (KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK)) {
switch (e.getKeyCode()) {
case KeyEvent.VK_PLUS:
case KeyEvent.VK_EQUALS:
base.handleFontSizeChange(1);
break;
case KeyEvent.VK_MINUS:
if (!e.isShiftDown()) {
base.handleFontSizeChange(-1);
}
break;
}
}
}
});
scrollPane = new JScrollPane(textArea);
mainPane.add(scrollPane, BorderLayout.CENTER);

View File

@ -1877,6 +1877,81 @@ public class Base {
getEditors().forEach(Editor::applyPreferences);
}
private MouseWheelListener editorFontResizeMouseWheelListener = null;
private KeyListener editorFontResizeKeyListener = null;
/**
* Adds a {@link MouseWheelListener} and {@link KeyListener} to the given
* component that will make "CTRL scroll" and "CTRL +/-"
* (with optional SHIFT for +) increase/decrease the editor text size.
* This method is equivalent to calling
* {@link #addEditorFontResizeMouseWheelListener(Component)} and
* {@link #addEditorFontResizeKeyListener(Component)} on the given component.
* Note that this also affects components that use the editor font settings.
* @param comp - The component to add the listener to.
*/
public void addEditorFontResizeListeners(Component comp) {
this.addEditorFontResizeMouseWheelListener(comp);
this.addEditorFontResizeKeyListener(comp);
}
/**
* Adds a {@link MouseWheelListener} to the given component that will
* make "CTRL scroll" increase/decrease the editor text size.
* When CTRL is not pressed while scrolling, mouse wheel events are passed
* on to the parent of the given component.
* Note that this also affects components that use the editor font settings.
* @param comp - The component to add the listener to.
*/
public void addEditorFontResizeMouseWheelListener(Component comp) {
if (this.editorFontResizeMouseWheelListener == null) {
this.editorFontResizeMouseWheelListener = (MouseWheelEvent e) -> {
if (e.isControlDown()) {
if (e.getWheelRotation() < 0) {
this.handleFontSizeChange(1);
} else {
this.handleFontSizeChange(-1);
}
} else {
e.getComponent().getParent().dispatchEvent(e);
}
};
}
comp.addMouseWheelListener(this.editorFontResizeMouseWheelListener);
}
/**
* Adds a {@link KeyListener} to the given component that will make "CTRL +/-"
* (with optional SHIFT for +) increase/decrease the editor text size.
* Note that this also affects components that use the editor font settings.
* @param comp - The component to add the listener to.
*/
public void addEditorFontResizeKeyListener(Component comp) {
if (this.editorFontResizeKeyListener == null) {
this.editorFontResizeKeyListener = new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK
|| e.getModifiersEx() == (KeyEvent.CTRL_DOWN_MASK
| KeyEvent.SHIFT_DOWN_MASK)) {
switch (e.getKeyCode()) {
case KeyEvent.VK_PLUS:
case KeyEvent.VK_EQUALS:
Base.this.handleFontSizeChange(1);
break;
case KeyEvent.VK_MINUS:
if (!e.isShiftDown()) {
Base.this.handleFontSizeChange(-1);
}
break;
}
}
}
};
}
comp.addKeyListener(this.editorFontResizeKeyListener);
}
public List<JMenu> getBoardsCustomMenus() {
return boardsCustomMenus;
}

View File

@ -26,9 +26,6 @@ import cc.arduino.ConsoleOutputStream;
import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseWheelEvent;
import java.io.PrintStream;
import static processing.app.Theme.scale;
@ -64,7 +61,7 @@ public class EditorConsole extends JScrollPane {
private SimpleAttributeSet stdOutStyle;
private SimpleAttributeSet stdErrStyle;
public EditorConsole(final Base base) {
public EditorConsole(Base base) {
document = new DefaultStyledDocument();
consoleTextPane = new JTextPane(document);
@ -114,39 +111,8 @@ public class EditorConsole extends JScrollPane {
EditorConsole.init(stdOutStyle, System.out, stdErrStyle, System.err);
// Add "CTRL scroll" hotkey for font size adjustment.
consoleTextPane.addMouseWheelListener((MouseWheelEvent e) -> {
if (e.isControlDown()) {
if (e.getWheelRotation() < 0) {
base.handleFontSizeChange(1);
} else {
base.handleFontSizeChange(-1);
}
} else {
e.getComponent().getParent().dispatchEvent(e);
}
});
// Add "CTRL (SHIFT) =/+" and "CTRL -" hotkeys for font size adjustment.
consoleTextPane.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK
|| e.getModifiersEx() == (KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK)) {
switch (e.getKeyCode()) {
case KeyEvent.VK_PLUS:
case KeyEvent.VK_EQUALS:
base.handleFontSizeChange(1);
break;
case KeyEvent.VK_MINUS:
if (!e.isShiftDown()) {
base.handleFontSizeChange(-1);
}
break;
}
}
}
});
// Add font size adjustment listeners.
base.addEditorFontResizeListeners(consoleTextPane);
}
public void applyPreferences() {

View File

@ -68,7 +68,7 @@ import processing.app.tools.DiscourseFormat;
/**
* Single tab, editing a single file, in the main window.
*/
public class EditorTab extends JPanel implements SketchFile.TextStorage, MouseWheelListener {
public class EditorTab extends JPanel implements SketchFile.TextStorage {
protected Editor editor;
protected SketchTextArea textarea;
protected RTextScrollPane scrollPane;
@ -110,7 +110,7 @@ public class EditorTab extends JPanel implements SketchFile.TextStorage, MouseWh
file.setStorage(this);
applyPreferences();
add(scrollPane, BorderLayout.CENTER);
textarea.addMouseWheelListener(this);
editor.base.addEditorFontResizeMouseWheelListener(textarea);
}
private RSyntaxDocument createDocument(String contents) {
@ -182,18 +182,6 @@ public class EditorTab extends JPanel implements SketchFile.TextStorage, MouseWh
configurePopupMenu(textArea);
return textArea;
}
public void mouseWheelMoved(MouseWheelEvent e) {
if (e.isControlDown()) {
if (e.getWheelRotation() < 0) {
editor.base.handleFontSizeChange(1);
} else {
editor.base.handleFontSizeChange(-1);
}
} else {
e.getComponent().getParent().dispatchEvent(e);
}
}
private void configurePopupMenu(final SketchTextArea textarea){