From 61bbc382b50e36e391858762a88cdbda15461100 Mon Sep 17 00:00:00 2001 From: Pieter12345 Date: Tue, 26 Mar 2019 16:45:33 +0100 Subject: [PATCH] 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. --- .../processing/app/AbstractTextMonitor.java | 44 +---------- app/src/processing/app/Base.java | 75 +++++++++++++++++++ app/src/processing/app/EditorConsole.java | 40 +--------- app/src/processing/app/EditorTab.java | 16 +--- 4 files changed, 84 insertions(+), 91 deletions(-) diff --git a/app/src/processing/app/AbstractTextMonitor.java b/app/src/processing/app/AbstractTextMonitor.java index 16df86558..64a0928e5 100644 --- a/app/src/processing/app/AbstractTextMonitor.java +++ b/app/src/processing/app/AbstractTextMonitor.java @@ -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); diff --git a/app/src/processing/app/Base.java b/app/src/processing/app/Base.java index 62ebfc2ac..8544e6c41 100644 --- a/app/src/processing/app/Base.java +++ b/app/src/processing/app/Base.java @@ -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 getBoardsCustomMenus() { return boardsCustomMenus; } diff --git a/app/src/processing/app/EditorConsole.java b/app/src/processing/app/EditorConsole.java index 1b8efc1cf..15c78df2d 100644 --- a/app/src/processing/app/EditorConsole.java +++ b/app/src/processing/app/EditorConsole.java @@ -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() { diff --git a/app/src/processing/app/EditorTab.java b/app/src/processing/app/EditorTab.java index 1ac16cf6d..25c45d9d8 100644 --- a/app/src/processing/app/EditorTab.java +++ b/app/src/processing/app/EditorTab.java @@ -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){