Skip to content

Commit 61bbc38

Browse files
Pieter12345cmaglie
authored andcommitted
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.
1 parent 3aa81b0 commit 61bbc38

File tree

4 files changed

+84
-91
lines changed

4 files changed

+84
-91
lines changed

app/src/processing/app/AbstractTextMonitor.java

+4-40
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88
import java.awt.Font;
99
import java.awt.event.ActionEvent;
1010
import java.awt.event.ActionListener;
11-
import java.awt.event.KeyEvent;
12-
import java.awt.event.KeyAdapter;
13-
import java.awt.event.MouseWheelEvent;
1411
import java.awt.event.WindowAdapter;
1512
import java.awt.event.WindowEvent;
1613
import java.text.SimpleDateFormat;
@@ -35,8 +32,6 @@
3532
@SuppressWarnings("serial")
3633
public abstract class AbstractTextMonitor extends AbstractMonitor {
3734

38-
private final Base base;
39-
4035
protected JLabel noLineEndingAlert;
4136
protected TextAreaFIFO textArea;
4237
protected JScrollPane scrollPane;
@@ -50,7 +45,10 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
5045

5146
public AbstractTextMonitor(Base base, BoardPort boardPort) {
5247
super(boardPort);
53-
this.base = base;
48+
49+
// Add font size adjustment listeners. This has to be done here due to
50+
// super(boardPort) invoking onCreateWindow(...) before we can store base.
51+
base.addEditorFontResizeListeners(textArea);
5452
}
5553

5654
@Override
@@ -67,40 +65,6 @@ protected void onCreateWindow(Container mainPane) {
6765
// whether or not to do so based on the autoscroll checkbox.
6866
((DefaultCaret) textArea.getCaret()).setUpdatePolicy(DefaultCaret.NEVER_UPDATE);
6967

70-
// Add "CTRL scroll" hotkey for font size adjustment.
71-
textArea.addMouseWheelListener((MouseWheelEvent e) -> {
72-
if (e.isControlDown()) {
73-
if (e.getWheelRotation() < 0) {
74-
base.handleFontSizeChange(1);
75-
} else {
76-
base.handleFontSizeChange(-1);
77-
}
78-
} else {
79-
e.getComponent().getParent().dispatchEvent(e);
80-
}
81-
});
82-
83-
// Add "CTRL (SHIFT) =/+" and "CTRL -" hotkeys for font size adjustment.
84-
textArea.addKeyListener(new KeyAdapter() {
85-
@Override
86-
public void keyPressed(KeyEvent e) {
87-
if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK
88-
|| e.getModifiersEx() == (KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK)) {
89-
switch (e.getKeyCode()) {
90-
case KeyEvent.VK_PLUS:
91-
case KeyEvent.VK_EQUALS:
92-
base.handleFontSizeChange(1);
93-
break;
94-
case KeyEvent.VK_MINUS:
95-
if (!e.isShiftDown()) {
96-
base.handleFontSizeChange(-1);
97-
}
98-
break;
99-
}
100-
}
101-
}
102-
});
103-
10468
scrollPane = new JScrollPane(textArea);
10569

10670
mainPane.add(scrollPane, BorderLayout.CENTER);

app/src/processing/app/Base.java

+75
Original file line numberDiff line numberDiff line change
@@ -1877,6 +1877,81 @@ public void handleFontSizeChange(int change) {
18771877
getEditors().forEach(Editor::applyPreferences);
18781878
}
18791879

1880+
private MouseWheelListener editorFontResizeMouseWheelListener = null;
1881+
private KeyListener editorFontResizeKeyListener = null;
1882+
1883+
/**
1884+
* Adds a {@link MouseWheelListener} and {@link KeyListener} to the given
1885+
* component that will make "CTRL scroll" and "CTRL +/-"
1886+
* (with optional SHIFT for +) increase/decrease the editor text size.
1887+
* This method is equivalent to calling
1888+
* {@link #addEditorFontResizeMouseWheelListener(Component)} and
1889+
* {@link #addEditorFontResizeKeyListener(Component)} on the given component.
1890+
* Note that this also affects components that use the editor font settings.
1891+
* @param comp - The component to add the listener to.
1892+
*/
1893+
public void addEditorFontResizeListeners(Component comp) {
1894+
this.addEditorFontResizeMouseWheelListener(comp);
1895+
this.addEditorFontResizeKeyListener(comp);
1896+
}
1897+
1898+
/**
1899+
* Adds a {@link MouseWheelListener} to the given component that will
1900+
* make "CTRL scroll" increase/decrease the editor text size.
1901+
* When CTRL is not pressed while scrolling, mouse wheel events are passed
1902+
* on to the parent of the given component.
1903+
* Note that this also affects components that use the editor font settings.
1904+
* @param comp - The component to add the listener to.
1905+
*/
1906+
public void addEditorFontResizeMouseWheelListener(Component comp) {
1907+
if (this.editorFontResizeMouseWheelListener == null) {
1908+
this.editorFontResizeMouseWheelListener = (MouseWheelEvent e) -> {
1909+
if (e.isControlDown()) {
1910+
if (e.getWheelRotation() < 0) {
1911+
this.handleFontSizeChange(1);
1912+
} else {
1913+
this.handleFontSizeChange(-1);
1914+
}
1915+
} else {
1916+
e.getComponent().getParent().dispatchEvent(e);
1917+
}
1918+
};
1919+
}
1920+
comp.addMouseWheelListener(this.editorFontResizeMouseWheelListener);
1921+
}
1922+
1923+
/**
1924+
* Adds a {@link KeyListener} to the given component that will make "CTRL +/-"
1925+
* (with optional SHIFT for +) increase/decrease the editor text size.
1926+
* Note that this also affects components that use the editor font settings.
1927+
* @param comp - The component to add the listener to.
1928+
*/
1929+
public void addEditorFontResizeKeyListener(Component comp) {
1930+
if (this.editorFontResizeKeyListener == null) {
1931+
this.editorFontResizeKeyListener = new KeyAdapter() {
1932+
@Override
1933+
public void keyPressed(KeyEvent e) {
1934+
if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK
1935+
|| e.getModifiersEx() == (KeyEvent.CTRL_DOWN_MASK
1936+
| KeyEvent.SHIFT_DOWN_MASK)) {
1937+
switch (e.getKeyCode()) {
1938+
case KeyEvent.VK_PLUS:
1939+
case KeyEvent.VK_EQUALS:
1940+
Base.this.handleFontSizeChange(1);
1941+
break;
1942+
case KeyEvent.VK_MINUS:
1943+
if (!e.isShiftDown()) {
1944+
Base.this.handleFontSizeChange(-1);
1945+
}
1946+
break;
1947+
}
1948+
}
1949+
}
1950+
};
1951+
}
1952+
comp.addKeyListener(this.editorFontResizeKeyListener);
1953+
}
1954+
18801955
public List<JMenu> getBoardsCustomMenus() {
18811956
return boardsCustomMenus;
18821957
}

app/src/processing/app/EditorConsole.java

+3-37
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,6 @@
2626
import javax.swing.*;
2727
import javax.swing.text.*;
2828
import java.awt.*;
29-
import java.awt.event.KeyAdapter;
30-
import java.awt.event.KeyEvent;
31-
import java.awt.event.MouseWheelEvent;
3229
import java.io.PrintStream;
3330

3431
import static processing.app.Theme.scale;
@@ -64,7 +61,7 @@ public static void setCurrentEditorConsole(EditorConsole console) {
6461
private SimpleAttributeSet stdOutStyle;
6562
private SimpleAttributeSet stdErrStyle;
6663

67-
public EditorConsole(final Base base) {
64+
public EditorConsole(Base base) {
6865
document = new DefaultStyledDocument();
6966

7067
consoleTextPane = new JTextPane(document);
@@ -114,39 +111,8 @@ public EditorConsole(final Base base) {
114111

115112
EditorConsole.init(stdOutStyle, System.out, stdErrStyle, System.err);
116113

117-
// Add "CTRL scroll" hotkey for font size adjustment.
118-
consoleTextPane.addMouseWheelListener((MouseWheelEvent e) -> {
119-
if (e.isControlDown()) {
120-
if (e.getWheelRotation() < 0) {
121-
base.handleFontSizeChange(1);
122-
} else {
123-
base.handleFontSizeChange(-1);
124-
}
125-
} else {
126-
e.getComponent().getParent().dispatchEvent(e);
127-
}
128-
});
129-
130-
// Add "CTRL (SHIFT) =/+" and "CTRL -" hotkeys for font size adjustment.
131-
consoleTextPane.addKeyListener(new KeyAdapter() {
132-
@Override
133-
public void keyPressed(KeyEvent e) {
134-
if (e.getModifiersEx() == KeyEvent.CTRL_DOWN_MASK
135-
|| e.getModifiersEx() == (KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK)) {
136-
switch (e.getKeyCode()) {
137-
case KeyEvent.VK_PLUS:
138-
case KeyEvent.VK_EQUALS:
139-
base.handleFontSizeChange(1);
140-
break;
141-
case KeyEvent.VK_MINUS:
142-
if (!e.isShiftDown()) {
143-
base.handleFontSizeChange(-1);
144-
}
145-
break;
146-
}
147-
}
148-
}
149-
});
114+
// Add font size adjustment listeners.
115+
base.addEditorFontResizeListeners(consoleTextPane);
150116
}
151117

152118
public void applyPreferences() {

app/src/processing/app/EditorTab.java

+2-14
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
/**
6969
* Single tab, editing a single file, in the main window.
7070
*/
71-
public class EditorTab extends JPanel implements SketchFile.TextStorage, MouseWheelListener {
71+
public class EditorTab extends JPanel implements SketchFile.TextStorage {
7272
protected Editor editor;
7373
protected SketchTextArea textarea;
7474
protected RTextScrollPane scrollPane;
@@ -110,7 +110,7 @@ public EditorTab(Editor editor, SketchFile file, String contents)
110110
file.setStorage(this);
111111
applyPreferences();
112112
add(scrollPane, BorderLayout.CENTER);
113-
textarea.addMouseWheelListener(this);
113+
editor.base.addEditorFontResizeMouseWheelListener(textarea);
114114
}
115115

116116
private RSyntaxDocument createDocument(String contents) {
@@ -182,18 +182,6 @@ private SketchTextArea createTextArea(RSyntaxDocument document)
182182
configurePopupMenu(textArea);
183183
return textArea;
184184
}
185-
186-
public void mouseWheelMoved(MouseWheelEvent e) {
187-
if (e.isControlDown()) {
188-
if (e.getWheelRotation() < 0) {
189-
editor.base.handleFontSizeChange(1);
190-
} else {
191-
editor.base.handleFontSizeChange(-1);
192-
}
193-
} else {
194-
e.getComponent().getParent().dispatchEvent(e);
195-
}
196-
}
197185

198186
private void configurePopupMenu(final SketchTextArea textarea){
199187

0 commit comments

Comments
 (0)