Skip to content

Commit 91fc80b

Browse files
Pieter12345cmaglie
authored andcommitted
Properly update editor console font size on change
Make the editor console font properly update with editor font size changes. Before this commit, only newly added text would have an adjusted size, making it possible to have multiple text sizes in the editor console.
1 parent e3f129a commit 91fc80b

File tree

1 file changed

+36
-2
lines changed

1 file changed

+36
-2
lines changed

app/src/processing/app/EditorConsole.java

+36-2
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,49 @@ public EditorConsole() {
113113
}
114114

115115
public void applyPreferences() {
116+
117+
// Update the console text pane font from the preferences.
116118
Font consoleFont = Theme.getFont("console.font");
117119
Font editorFont = PreferencesData.getFont("editor.font");
118120
Font actualFont = new Font(consoleFont.getName(), consoleFont.getStyle(), scale(editorFont.getSize()));
119121

122+
AttributeSet stdOutStyleOld = stdOutStyle.copyAttributes();
123+
AttributeSet stdErrStyleOld = stdErrStyle.copyAttributes();
120124
StyleConstants.setFontSize(stdOutStyle, actualFont.getSize());
121125
StyleConstants.setFontSize(stdErrStyle, actualFont.getSize());
122126

123-
out.setAttibutes(stdOutStyle);
124-
err.setAttibutes(stdErrStyle);
127+
// Re-insert console text with the new preferences if there were changes.
128+
// This assumes that the document has single-child paragraphs (default).
129+
if (!stdOutStyle.isEqual(stdOutStyleOld) || !stdErrStyle.isEqual(stdOutStyleOld)) {
130+
out.setAttibutes(stdOutStyle);
131+
err.setAttibutes(stdErrStyle);
132+
133+
int start;
134+
for (int end = document.getLength() - 1; end >= 0; end = start - 1) {
135+
Element elem = document.getParagraphElement(end);
136+
start = elem.getStartOffset();
137+
AttributeSet attrs = elem.getElement(0).getAttributes();
138+
AttributeSet newAttrs;
139+
if (attrs.isEqual(stdErrStyleOld)) {
140+
newAttrs = stdErrStyle;
141+
} else if (attrs.isEqual(stdOutStyleOld)) {
142+
newAttrs = stdOutStyle;
143+
} else {
144+
continue;
145+
}
146+
try {
147+
String text = document.getText(start, end - start);
148+
document.remove(start, end - start);
149+
document.insertString(start, text, newAttrs);
150+
} catch (BadLocationException e) {
151+
// Should only happen when text is async removed (through clear()).
152+
// Accept this case, but throw an error when text could mess up.
153+
if (document.getLength() != 0) {
154+
throw new Error(e);
155+
}
156+
}
157+
}
158+
}
125159
}
126160

127161
public void clear() {

0 commit comments

Comments
 (0)