Skip to content

Commit 21d419f

Browse files
committed
Add font zoom functionality to console view
-Add key listener on text widget of console view which listens ctrl plus and control minus(including numpad +/-) -Zoom in/out of the console view text in steps based on the keys pressed. see #2578
1 parent 519a3b0 commit 21d419f

1 file changed

Lines changed: 165 additions & 0 deletions

File tree

  • debug/org.eclipse.ui.console/src/org/eclipse/ui/internal/console

debug/org.eclipse.ui.console/src/org/eclipse/ui/internal/console/ConsoleView.java

Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@
3434
import org.eclipse.jface.util.IPropertyChangeListener;
3535
import org.eclipse.jface.util.PropertyChangeEvent;
3636
import org.eclipse.jface.viewers.IBasicPropertyConstants;
37+
import org.eclipse.swt.SWT;
3738
import org.eclipse.swt.custom.StyledText;
39+
import org.eclipse.swt.events.KeyListener;
3840
import org.eclipse.swt.events.MouseAdapter;
3941
import org.eclipse.swt.events.MouseEvent;
42+
import org.eclipse.swt.graphics.Font;
43+
import org.eclipse.swt.graphics.FontData;
4044
import org.eclipse.swt.graphics.Image;
4145
import org.eclipse.swt.graphics.Point;
4246
import org.eclipse.swt.widgets.Composite;
@@ -131,6 +135,28 @@ public class ConsoleView extends PageBookView implements IConsoleView, IConsoleL
131135

132136
private boolean updateConsoleIcon;
133137

138+
/**
139+
* Data key used to store a custom zoom font on a StyledText widget. The font
140+
* stored under this key is managed per-widget and disposed via a
141+
* DisposeListener on that widget.
142+
*/
143+
private static final String ZOOM_FONT_KEY = ConsoleView.class.getName() + ".zoomFont"; //$NON-NLS-1$
144+
145+
/**
146+
* Minimum font size for console zoom.
147+
*/
148+
private static final int MIN_FONT_SIZE = 6;
149+
150+
/**
151+
* Maximum font size for console zoom.
152+
*/
153+
private static final int MAX_FONT_SIZE = 72;
154+
155+
/**
156+
* Font size change step for zoom in/out.
157+
*/
158+
private static final int FONT_SIZE_STEP = 1;
159+
134160
private boolean isAvailable() {
135161
PageBook pageBook = getPageBook();
136162
return pageBook != null && !pageBook.isDisposed();
@@ -410,6 +436,18 @@ public void handleException(Throwable exception) {
410436
});
411437
}
412438

439+
// Install font zoom key listener on the StyledText widget
440+
StyledText textWidget = null;
441+
if (page instanceof TextConsolePage textPage) {
442+
TextConsoleViewer viewer = textPage.getViewer();
443+
if (viewer != null) {
444+
textWidget = viewer.getTextWidget();
445+
}
446+
}
447+
if (textWidget != null) {
448+
installFontZoomKeyListener(textWidget);
449+
}
450+
413451
PageRec rec = new PageRec(dummyPart, page);
414452
return rec;
415453
}
@@ -441,6 +479,7 @@ public void dispose() {
441479
localResManager.dispose();
442480
localResManager = null;
443481
}
482+
444483
fConsoleToPageParticipants.clear();
445484
fStack.clear();
446485
fConsoleToPart.clear();
@@ -909,4 +948,130 @@ public boolean getAutoScrollLock() {
909948
}
910949
return fScrollLock;
911950
}
951+
952+
/**
953+
* Installs the font zoom key listener on the given control.
954+
*
955+
* @param control the control to install the key listener on
956+
*/
957+
private void installFontZoomKeyListener(Control control) {
958+
control.addKeyListener(KeyListener.keyPressedAdapter(event -> {
959+
// Check for Ctrl key first.
960+
if ((event.stateMask & SWT.MOD1) == 0) {
961+
return;
962+
}
963+
964+
// Check for + or - keys (including numpad)
965+
boolean isPlus = event.character == '+' || event.keyCode == SWT.KEYPAD_ADD
966+
|| (event.character == '=' && (event.stateMask & SWT.SHIFT) != 0);
967+
boolean isMinus = event.character == '-' || event.keyCode == SWT.KEYPAD_SUBTRACT;
968+
969+
if (isPlus) {
970+
increaseFontSize();
971+
event.doit = false;
972+
} else if (isMinus) {
973+
decreaseFontSize();
974+
event.doit = false;
975+
}
976+
}));
977+
}
978+
979+
/**
980+
* Increases the font size of the console by one step.
981+
*/
982+
private void increaseFontSize() {
983+
changeFontSize(FONT_SIZE_STEP);
984+
}
985+
986+
/**
987+
* Decreases the font size of the console by one step.
988+
*/
989+
private void decreaseFontSize() {
990+
changeFontSize(-FONT_SIZE_STEP);
991+
}
992+
993+
/**
994+
* Changes the font size of the console by the given delta.
995+
*
996+
* @param delta the amount to change the font size (positive to increase,
997+
* negative to decrease)
998+
*/
999+
private void changeFontSize(int delta) {
1000+
StyledText styledText = getConsoleStyledText();
1001+
if (styledText == null || styledText.isDisposed()) {
1002+
return;
1003+
}
1004+
1005+
Font currentFont = styledText.getFont();
1006+
if (currentFont == null || currentFont.isDisposed()) {
1007+
return;
1008+
}
1009+
1010+
FontData[] fontData = currentFont.getFontData();
1011+
if (fontData == null || fontData.length == 0) {
1012+
return;
1013+
}
1014+
1015+
int currentHeight = fontData[0].getHeight();
1016+
int newHeight = Math.max(MIN_FONT_SIZE, Math.min(MAX_FONT_SIZE, currentHeight + delta));
1017+
1018+
if (newHeight == currentHeight) {
1019+
return;
1020+
}
1021+
1022+
// Create new font data with the new height
1023+
FontData[] newFontData = new FontData[fontData.length];
1024+
for (int i = 0; i < fontData.length; i++) {
1025+
newFontData[i] = new FontData(fontData[i].getName(), newHeight, fontData[i].getStyle());
1026+
}
1027+
1028+
// Get any existing zoom font for this specific StyledText
1029+
Font oldZoomFont = (Font) styledText.getData(ZOOM_FONT_KEY);
1030+
1031+
// Create and set the new font
1032+
Font newZoomFont = new Font(styledText.getDisplay(), newFontData);
1033+
styledText.setFont(newZoomFont);
1034+
1035+
// Store the new zoom font on this specific StyledText
1036+
styledText.setData(ZOOM_FONT_KEY, newZoomFont);
1037+
1038+
// Install DisposeListener if this is the first zoom font for this widget
1039+
if (oldZoomFont == null) {
1040+
styledText.addDisposeListener(e -> {
1041+
Font zoomFont = (Font) styledText.getData(ZOOM_FONT_KEY);
1042+
if (zoomFont != null && !zoomFont.isDisposed()) {
1043+
zoomFont.dispose();
1044+
}
1045+
});
1046+
}
1047+
1048+
// Dispose the old zoom font for this widget after setting the new one
1049+
if (oldZoomFont != null && !oldZoomFont.isDisposed()) {
1050+
oldZoomFont.dispose();
1051+
}
1052+
}
1053+
1054+
/**
1055+
* Returns the StyledText control of the current console page, or null if not
1056+
* available.
1057+
*
1058+
* @return the StyledText control or null
1059+
*/
1060+
private StyledText getConsoleStyledText() {
1061+
IPage page = getCurrentPage();
1062+
if (page != null) {
1063+
Control control = page.getControl();
1064+
if (control instanceof StyledText styledText) {
1065+
return styledText;
1066+
}
1067+
// For TextConsolePage, get the viewer's text widget
1068+
if (page instanceof TextConsolePage textPage) {
1069+
TextConsoleViewer viewer = textPage.getViewer();
1070+
if (viewer != null) {
1071+
return viewer.getTextWidget();
1072+
}
1073+
}
1074+
}
1075+
return null;
1076+
}
9121077
}

0 commit comments

Comments
 (0)