1
0
mirror of https://github.com/arduino/Arduino.git synced 2025-01-30 19:52:13 +01:00

Make text monitor output area font size dynamically adjustable

Add CTRL +/- and CTRL scroll shortcuts to increase/decrease serial/network monitor output text size. This font size is shared with the editor and adjusting either will update both.
Partially fixes #8615
This commit is contained in:
Pieter12345 2019-03-25 19:34:08 +01:00 committed by Cristian Maglie
parent 49242bed02
commit 1a6d55480c
6 changed files with 84 additions and 19 deletions

View File

@ -30,23 +30,24 @@
package cc.arduino.packages;
import processing.app.AbstractMonitor;
import processing.app.Base;
import processing.app.NetworkMonitor;
import processing.app.SerialMonitor;
public class MonitorFactory {
public AbstractMonitor newMonitor(BoardPort port) {
public AbstractMonitor newMonitor(Base base, BoardPort port) {
if ("network".equals(port.getProtocol())) {
if ("yes".equals(port.getPrefs().get("ssh_upload"))) {
// the board is SSH capable
return new NetworkMonitor(port);
return new NetworkMonitor(base, port);
} else {
// SSH not supported, no monitor support
return null;
}
}
return new SerialMonitor(port);
return new SerialMonitor(base, port);
}
}

View File

@ -173,4 +173,13 @@ public abstract class AbstractMonitor extends JFrame implements ActionListener {
message(s);
}
}
/**
* Read and apply new values from the preferences, either because
* the app is just starting up, or the user just finished messing
* with things in the Preferences window.
*/
public void applyPreferences() {
// Empty.
};
}

View File

@ -8,6 +8,9 @@ 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;
@ -32,6 +35,8 @@ 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;
@ -43,14 +48,12 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
protected JComboBox lineEndings;
protected JComboBox serialRates;
public AbstractTextMonitor(BoardPort boardPort) {
public AbstractTextMonitor(Base base, BoardPort boardPort) {
super(boardPort);
this.base = base;
}
protected void onCreateWindow(Container mainPane) {
Font consoleFont = Theme.getFont("console.font");
Font editorFont = PreferencesData.getFont("editor.font");
Font font = Theme.scale(new Font(consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize()));
mainPane.setLayout(new BorderLayout());
@ -58,12 +61,45 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
textArea.setRows(16);
textArea.setColumns(40);
textArea.setEditable(false);
textArea.setFont(font);
// don't automatically update the caret. that way we can manually decide
// 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);
@ -110,12 +146,6 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
noLineEndingAlert.setForeground(pane.getBackground());
}
});
if (PreferencesData.get("serial.line_ending") != null) {
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
}
if (PreferencesData.get("serial.show_timestamp") != null) {
addTimeStampBox.setSelected(PreferencesData.getBoolean("serial.show_timestamp"));
}
addTimeStampBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
PreferencesData.setBoolean("serial.show_timestamp", addTimeStampBox.isSelected());
@ -142,6 +172,8 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
pane.add(Box.createRigidArea(new Dimension(8, 0)));
pane.add(clearButton);
applyPreferences();
mainPane.add(pane, BorderLayout.SOUTH);
}
@ -189,6 +221,26 @@ public abstract class AbstractTextMonitor extends AbstractMonitor {
}
}
@Override
public void applyPreferences() {
// Apply font.
Font consoleFont = Theme.getFont("console.font");
Font editorFont = PreferencesData.getFont("editor.font");
textArea.setFont(Theme.scale(new Font(
consoleFont.getName(), consoleFont.getStyle(), editorFont.getSize())));
// Apply line endings.
if (PreferencesData.get("serial.line_ending") != null) {
lineEndings.setSelectedIndex(PreferencesData.getInteger("serial.line_ending"));
}
// Apply timestamp visibility.
if (PreferencesData.get("serial.show_timestamp") != null) {
addTimeStampBox.setSelected(PreferencesData.getBoolean("serial.show_timestamp"));
}
}
private String addTimestamps(String text) {
String now = new SimpleDateFormat("HH:mm:ss.SSS -> ").format(new Date());
final StringBuilder sb = new StringBuilder(text.length() + now.length());

View File

@ -495,6 +495,9 @@ public class Editor extends JFrame implements RunnerListener {
tab.applyPreferences();
}
console.applyPreferences();
if (serialMonitor != null) {
serialMonitor.applyPreferences();
}
}
@ -2211,7 +2214,7 @@ public class Editor extends JFrame implements RunnerListener {
return;
}
serialMonitor = new MonitorFactory().newMonitor(port);
serialMonitor = new MonitorFactory().newMonitor(base, port);
if (serialMonitor == null) {
String board = port.getPrefs().get("board");

View File

@ -31,8 +31,8 @@ public class NetworkMonitor extends AbstractTextMonitor implements MessageConsum
private Channel channel;
private int connectionAttempts;
public NetworkMonitor(BoardPort port) {
super(port);
public NetworkMonitor(Base base, BoardPort port) {
super(base, port);
onSendCommand(new ActionListener() {
public void actionPerformed(ActionEvent event) {

View File

@ -33,8 +33,8 @@ public class SerialMonitor extends AbstractTextMonitor {
private Serial serial;
private int serialRate;
public SerialMonitor(BoardPort port) {
super(port);
public SerialMonitor(Base base, BoardPort port) {
super(base, port);
serialRate = PreferencesData.getInteger("serial.debug_rate");
serialRates.setSelectedItem(serialRate + " " + tr("baud"));