Skip to content

Commit 802201a

Browse files
committed
Support font zoom change on compare editors
This commits adds support to change font size on compare editors through basic zooming shortcuts (cmd^+ or cmd^- or pinch to zoom like other editors
1 parent bdfe771 commit 802201a

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

team/bundles/org.eclipse.compare/compare/org/eclipse/compare/contentmergeviewer/TextMergeViewer.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*******************************************************************************
2-
* Copyright (c) 2000, 2024 IBM Corporation and others.
2+
* Copyright (c) 2000, 2025 IBM Corporation and others.
33
*
44
* This program and the accompanying materials
55
* are made available under the terms of the Eclipse Public License 2.0
@@ -105,7 +105,9 @@
105105
import org.eclipse.jface.preference.IPreferenceStore;
106106
import org.eclipse.jface.preference.PreferenceConverter;
107107
import org.eclipse.jface.resource.ColorRegistry;
108+
import org.eclipse.jface.resource.FontDescriptor;
108109
import org.eclipse.jface.resource.JFaceResources;
110+
import org.eclipse.jface.resource.LocalResourceManager;
109111
import org.eclipse.jface.text.BadLocationException;
110112
import org.eclipse.jface.text.BadPositionCategoryException;
111113
import org.eclipse.jface.text.CursorLinePainter;
@@ -166,6 +168,7 @@
166168
import org.eclipse.swt.graphics.Color;
167169
import org.eclipse.swt.graphics.Cursor;
168170
import org.eclipse.swt.graphics.Font;
171+
import org.eclipse.swt.graphics.FontData;
169172
import org.eclipse.swt.graphics.GC;
170173
import org.eclipse.swt.graphics.Image;
171174
import org.eclipse.swt.graphics.Point;
@@ -494,6 +497,12 @@ private static RGB interpolate(RGB fg, RGB bg, double scale) {
494497
private boolean isConfigured = false;
495498
private boolean fRedoDiff = false;
496499

500+
private double fCurrMagni = 0;
501+
502+
private LocalResourceManager fLocalResManager;
503+
504+
private int fCurrentHeight;
505+
497506
private final class InternalOutlineViewerCreator extends OutlineViewerCreator implements ISelectionChangedListener {
498507
@Override
499508
public Viewer findStructureViewer(Viewer oldViewer,
@@ -3171,6 +3180,7 @@ protected void updateContent(Object ancestor, Object left, Object right) {
31713180
private void configureSourceViewer(SourceViewer sourceViewer, boolean editable, ContributorInfo contributor) {
31723181
setEditable(sourceViewer, editable);
31733182
configureTextViewer(sourceViewer);
3183+
31743184
if (editable && contributor != null) {
31753185
IDocument document = sourceViewer.getDocument();
31763186
if (document != null) {
@@ -3180,6 +3190,31 @@ private void configureSourceViewer(SourceViewer sourceViewer, boolean editable,
31803190
if (!isCursorLinePainterInstalled(sourceViewer)) {
31813191
getSourceViewerDecorationSupport(sourceViewer).install(fPreferenceStore);
31823192
}
3193+
3194+
StyledText styleText = sourceViewer.getTextWidget();
3195+
fLocalResManager = new LocalResourceManager(JFaceResources.getResources(), styleText);
3196+
styleText.addKeyListener(new KeyAdapter() {
3197+
@Override
3198+
public void keyPressed(KeyEvent e) {
3199+
if ((e.stateMask & SWT.COMMAND) != 0 && (e.keyCode == '=' || e.keyCode == '+')) {
3200+
updateFontSize(true);
3201+
}
3202+
if ((e.stateMask & SWT.COMMAND) != 0 && e.keyCode == '-') {
3203+
updateFontSize(false);
3204+
}
3205+
}
3206+
});
3207+
styleText.addListener(SWT.Gesture, event -> {
3208+
if (event.detail == SWT.GESTURE_MAGNIFY) {
3209+
if (event.magnification > fCurrMagni) {
3210+
fCurrMagni = event.magnification;
3211+
updateFontSize(true);
3212+
} else if (event.magnification < fCurrMagni) {
3213+
fCurrMagni = event.magnification;
3214+
updateFontSize(false);
3215+
}
3216+
}
3217+
});
31833218
}
31843219

31853220
private boolean isCursorLinePainterInstalled(SourceViewer viewer) {
@@ -6019,4 +6054,29 @@ private void updateStructure(char leg) {
60196054
Assert.isNotNull(key);
60206055
getCompareConfiguration().setProperty(key, null);
60216056
}
6057+
6058+
private void updateFontSize(boolean increment) {
6059+
StyledText rightStyle = fRight.getSourceViewer().getTextWidget();
6060+
StyledText leftStyle = fLeft.getSourceViewer().getTextWidget();
6061+
6062+
Font rightFont = rightStyle.getFont();
6063+
for (FontData fd : rightFont.getFontData()) {
6064+
if (increment) {
6065+
fCurrentHeight = Math.max(5, fd.getHeight() + 1);
6066+
} else {
6067+
fCurrentHeight = Math.max(5, fd.getHeight() - 1);
6068+
}
6069+
}
6070+
6071+
FontData[] baseData = JFaceResources.getTextFont().getFontData();
6072+
for (FontData fd : baseData) {
6073+
fd.setHeight(fCurrentHeight);
6074+
}
6075+
FontDescriptor desc = FontDescriptor.createFrom(baseData);
6076+
Font newFont = fLocalResManager.create(desc);
6077+
leftStyle.setFont(newFont);
6078+
rightStyle.setFont(newFont);
6079+
doDiff();
6080+
}
6081+
60226082
}

0 commit comments

Comments
 (0)