Skip to content

Commit 74720eb

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 74720eb

1 file changed

Lines changed: 156 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: 156 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,26 @@ public class ConsoleView extends PageBookView implements IConsoleView, IConsoleL
131135

132136
private boolean updateConsoleIcon;
133137

138+
/**
139+
* Custom font created for font zoom. Must be disposed when no longer needed.
140+
*/
141+
private Font fCustomFont;
142+
143+
/**
144+
* Minimum font size for console zoom.
145+
*/
146+
private static final int MIN_FONT_SIZE = 6;
147+
148+
/**
149+
* Maximum font size for console zoom.
150+
*/
151+
private static final int MAX_FONT_SIZE = 72;
152+
153+
/**
154+
* Font size change step for zoom in/out.
155+
*/
156+
private static final int FONT_SIZE_STEP = 1;
157+
134158
private boolean isAvailable() {
135159
PageBook pageBook = getPageBook();
136160
return pageBook != null && !pageBook.isDisposed();
@@ -410,6 +434,18 @@ public void handleException(Throwable exception) {
410434
});
411435
}
412436

437+
// Install font zoom key listener on the StyledText widget
438+
StyledText textWidget = null;
439+
if (page instanceof TextConsolePage textPage) {
440+
TextConsoleViewer viewer = textPage.getViewer();
441+
if (viewer != null) {
442+
textWidget = viewer.getTextWidget();
443+
}
444+
}
445+
if (textWidget != null) {
446+
installFontZoomKeyListener(textWidget);
447+
}
448+
413449
PageRec rec = new PageRec(dummyPart, page);
414450
return rec;
415451
}
@@ -441,6 +477,13 @@ public void dispose() {
441477
localResManager.dispose();
442478
localResManager = null;
443479
}
480+
481+
// Dispose custom font if we created one
482+
if (fCustomFont != null && !fCustomFont.isDisposed()) {
483+
fCustomFont.dispose();
484+
fCustomFont = null;
485+
}
486+
444487
fConsoleToPageParticipants.clear();
445488
fStack.clear();
446489
fConsoleToPart.clear();
@@ -909,4 +952,117 @@ public boolean getAutoScrollLock() {
909952
}
910953
return fScrollLock;
911954
}
955+
956+
/**
957+
* Installs the font zoom key listener on the given control.
958+
*
959+
* @param control the control to install the key listener on
960+
*/
961+
private void installFontZoomKeyListener(Control control) {
962+
control.addKeyListener(KeyListener.keyPressedAdapter(event -> {
963+
// Check for Ctrl key first.
964+
if ((event.stateMask & SWT.MOD1) == 0) {
965+
return;
966+
}
967+
968+
// Check for + or - keys (including numpad)
969+
boolean isPlus = event.character == '+' || event.keyCode == SWT.KEYPAD_ADD
970+
|| (event.character == '=' && (event.stateMask & SWT.SHIFT) != 0);
971+
boolean isMinus = event.character == '-' || event.keyCode == SWT.KEYPAD_SUBTRACT;
972+
973+
if (isPlus) {
974+
increaseFontSize();
975+
event.doit = false;
976+
} else if (isMinus) {
977+
decreaseFontSize();
978+
event.doit = false;
979+
}
980+
}));
981+
}
982+
983+
/**
984+
* Increases the font size of the console by one step.
985+
*/
986+
private void increaseFontSize() {
987+
changeFontSize(FONT_SIZE_STEP);
988+
}
989+
990+
/**
991+
* Decreases the font size of the console by one step.
992+
*/
993+
private void decreaseFontSize() {
994+
changeFontSize(-FONT_SIZE_STEP);
995+
}
996+
997+
/**
998+
* Changes the font size of the console by the given delta.
999+
*
1000+
* @param delta the amount to change the font size (positive to increase,
1001+
* negative to decrease)
1002+
*/
1003+
private void changeFontSize(int delta) {
1004+
StyledText styledText = getConsoleStyledText();
1005+
if (styledText == null || styledText.isDisposed()) {
1006+
return;
1007+
}
1008+
1009+
Font currentFont = styledText.getFont();
1010+
if (currentFont == null || currentFont.isDisposed()) {
1011+
return;
1012+
}
1013+
1014+
FontData[] fontData = currentFont.getFontData();
1015+
if (fontData == null || fontData.length == 0) {
1016+
return;
1017+
}
1018+
1019+
int currentHeight = fontData[0].getHeight();
1020+
int newHeight = Math.max(MIN_FONT_SIZE, Math.min(MAX_FONT_SIZE, currentHeight + delta));
1021+
1022+
if (newHeight == currentHeight) {
1023+
return;
1024+
}
1025+
1026+
// Create new font data with the new height
1027+
FontData[] newFontData = new FontData[fontData.length];
1028+
for (int i = 0; i < fontData.length; i++) {
1029+
newFontData[i] = new FontData(fontData[i].getName(), newHeight, fontData[i].getStyle());
1030+
}
1031+
1032+
// Dispose old custom font if we created one
1033+
Font oldCustomFont = fCustomFont;
1034+
1035+
// Create and set the new font
1036+
fCustomFont = new Font(styledText.getDisplay(), newFontData);
1037+
styledText.setFont(fCustomFont);
1038+
1039+
// Dispose the old custom font after setting the new one
1040+
if (oldCustomFont != null && !oldCustomFont.isDisposed()) {
1041+
oldCustomFont.dispose();
1042+
}
1043+
}
1044+
1045+
/**
1046+
* Returns the StyledText control of the current console page, or null if not
1047+
* available.
1048+
*
1049+
* @return the StyledText control or null
1050+
*/
1051+
private StyledText getConsoleStyledText() {
1052+
IPage page = getCurrentPage();
1053+
if (page != null) {
1054+
Control control = page.getControl();
1055+
if (control instanceof StyledText styledText) {
1056+
return styledText;
1057+
}
1058+
// For TextConsolePage, get the viewer's text widget
1059+
if (page instanceof TextConsolePage textPage) {
1060+
TextConsoleViewer viewer = textPage.getViewer();
1061+
if (viewer != null) {
1062+
return viewer.getTextWidget();
1063+
}
1064+
}
1065+
}
1066+
return null;
1067+
}
9121068
}

0 commit comments

Comments
 (0)