Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 8df7e1955ffe44ffb8bc24708c17399cd34f7e5f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/*******************************************************************************
 * Copyright (c) 2018 Angelo ZERR.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * Angelo Zerr <angelo.zerr@gmail.com> - [minimap] Initialize minimap view - Bug 535450
 *******************************************************************************/
package org.eclipse.ui.internal.views.minimap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CaretEvent;
import org.eclipse.swt.custom.CaretListener;
import org.eclipse.swt.custom.StyleRange;
import org.eclipse.swt.custom.StyledText;
import org.eclipse.swt.custom.TextChangeListener;
import org.eclipse.swt.custom.TextChangedEvent;
import org.eclipse.swt.custom.TextChangingEvent;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.MouseWheelListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextPresentationListener;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.ITextViewerExtension4;
import org.eclipse.jface.text.ITextViewerExtension5;
import org.eclipse.jface.text.IViewportListener;
import org.eclipse.jface.text.JFaceTextUtil;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.TextViewer;

/**
 * Minimap widget which displays scaled content of the given text editor.
 *
 */
public class MinimapWidget {

	private static final float SCALE = 0.2f;

	private final ITextViewer fEditorViewer;

	private final StyledText fMinimapTextWidget;

	/**
	 * Editor tracker used to track text changed and styles changes of the
	 * editor content.
	 */
	class EditorTracker implements TextChangeListener, ControlListener, ITextPresentationListener, IViewportListener {

		private Map<Font, Font> fScaledFonts;

		private static final int RESETED = -1;

		private int scaledClientAreaHeight = RESETED;

		@Override
		public void textSet(TextChangedEvent event) {
			synchText();
		}

		@Override
		public void textChanging(TextChangingEvent event) {
			fMinimapTracker.replaceTextRange(event);
		}

		@Override
		public void textChanged(TextChangedEvent event) {
			// Do nothing
		}

		@Override
		public void applyTextPresentation(TextPresentation presentation) {
			addPresentation(presentation);
		}

		private StyleRange modelStyleRange2WidgetStyleRange(StyleRange range) {
			IRegion region = modelRange2WidgetRange(new Region(range.start, range.length));
			if (region != null) {
				StyleRange result = (StyleRange) range.clone();
				result.start = region.getOffset();
				result.length = region.getLength();
				return result;
			}
			return null;
		}

		private IRegion modelRange2WidgetRange(IRegion region) {
			if (fEditorViewer instanceof ITextViewerExtension5) {
				ITextViewerExtension5 extension = (ITextViewerExtension5) fEditorViewer;
				return extension.modelRange2WidgetRange(region);
			}
			if (fEditorViewer instanceof TextViewer) {
				return ((TextViewer) fEditorViewer).modelRange2WidgetRange(region);
			}
			IRegion visibleRegion = fEditorViewer.getVisibleRegion();
			int start = region.getOffset() - visibleRegion.getOffset();
			int end = start + region.getLength();
			if (end > visibleRegion.getLength())
				end = visibleRegion.getLength();

			return new Region(start, end - start);
		}

		private void addPresentation(TextPresentation presentation) {

			StyleRange range = presentation.getDefaultStyleRange();
			if (range != null) {

				range = modelStyleRange2WidgetStyleRange(range);
				if (range != null) {
					updateStyle(range);
					fMinimapTextWidget.setStyleRange(range);
				}
				List<StyleRange> ranges = new ArrayList<>(presentation.getDenumerableRanges());
				Iterator<StyleRange> e = presentation.getNonDefaultStyleRangeIterator();
				while (e.hasNext()) {
					range = e.next();
					range = modelStyleRange2WidgetStyleRange(range);
					if (range != null) {
						updateStyle(range);
						ranges.add(range);
					}
				}

				if (!ranges.isEmpty()) {
					fMinimapTextWidget.replaceStyleRanges(0, 0, ranges.toArray(new StyleRange[ranges.size()]));
				}

			} else {
				IRegion region = modelRange2WidgetRange(presentation.getCoverage());
				if (region == null)
					return;

				List<StyleRange> list = new ArrayList<>(presentation.getDenumerableRanges());
				Iterator<StyleRange> e = presentation.getAllStyleRangeIterator();
				while (e.hasNext()) {
					range = e.next();
					range = modelStyleRange2WidgetStyleRange(range);
					if (range != null) {
						updateStyle(range);
						list.add(range);
					}
				}

				if (!list.isEmpty()) {
					StyleRange[] ranges = new StyleRange[list.size()];
					list.toArray(ranges);
					fMinimapTextWidget.replaceStyleRanges(region.getOffset(), region.getLength(), ranges);
				}
			}
		}

		void updateStyle(StyleRange range) {
			// scale the font
			range.font = getScaledFont(range.font);
			// remove GlyphMetrics (created by code mining)
			range.metrics = null;
		}

		Font getScaledFont(Font editorFont) {
			if (editorFont == null) {
				return null;
			}
			Font scaledFont = fScaledFonts.get(editorFont);
			if (scaledFont != null) {
				return scaledFont;
			}
			FontData[] fontData = editorFont.getFontData();
			FontData fontDatum = fontData[0];

			scaledFont = new Font(editorFont.getDevice(), fontDatum.getName(),
					(int) Math.ceil(fontDatum.getHeight() * getScale()), fontDatum.getStyle());
			fScaledFonts.put(editorFont, scaledFont);
			return scaledFont;
		}

		@Override
		public void controlMoved(ControlEvent e) {
			// Do nothing
		}

		@Override
		public void controlResized(ControlEvent e) {
			scaledClientAreaHeight = RESETED;
			updateMinimap();
		}

		@Override
		public void viewportChanged(int verticalOffset) {
			fMinimapTextWidget.getDisplay().asyncExec(() -> {
				if (!fMinimapTextWidget.isDisposed()) {
					updateMinimap();
				}
			});
		}

		void updateMinimap() {
			StyledText editorTextWidget = fEditorViewer.getTextWidget();
			int editorTopIndex = JFaceTextUtil.getPartialTopIndex(editorTextWidget);
			int editorBottomIndex = JFaceTextUtil.getPartialBottomIndex(editorTextWidget);
			fMinimapTracker.updateMinimap(editorTopIndex, editorBottomIndex);
		}

		int getScaledClientAreaHeight() {
			if (scaledClientAreaHeight == RESETED) {
				scaledClientAreaHeight = Math.round(fEditorViewer.getTextWidget().getClientArea().height * getScale());
			}
			return scaledClientAreaHeight;
		}

		void install() {
			StyledText editorTextWidget = fEditorViewer.getTextWidget();
			fScaledFonts = new HashMap<>();
			// Compute scaled font
			Font scaledFont = getScaledFont(editorTextWidget.getFont());
			fMinimapTextWidget.setFont(scaledFont);
			// track changed content of styled text of the editor
			editorTextWidget.getContent().addTextChangeListener(this);
			// track changed styles of styled text of the editor
			fMinimapTextWidget.setBackground(editorTextWidget.getBackground());
			fMinimapTextWidget.setForeground(editorTextWidget.getForeground());
			if (fEditorViewer instanceof ITextViewerExtension4) {
				((ITextViewerExtension4) fEditorViewer).addTextPresentationListener(this);
			}
			// track changed of vertical bar scroll to update highlight
			// Viewport.
			fEditorViewer.addViewportListener(this);
			editorTextWidget.addControlListener(this);
			synchTextAndStyles();
		}

		void synchTextAndStyles() {
			synchText();
			synchStyles();
		}

		private void synchStyles() {
			StyledText editorTextWidget = fEditorViewer.getTextWidget();
			StyleRange[] ranges = editorTextWidget.getStyleRanges();
			if (ranges != null) {
				for (StyleRange range : ranges) {
					updateStyle(range);
				}
			}
			fMinimapTextWidget.setStyleRanges(ranges);
		}

		private void synchText() {
			StyledText editorTextWidget = fEditorViewer.getTextWidget();
			fMinimapTextWidget.setText(editorTextWidget.getText());
		}

		void uninstall() {
			StyledText editorTextWidget = fEditorViewer.getTextWidget();
			// untrack changed content of styled text of the editor
			if (editorTextWidget.getContent() != null) {
				editorTextWidget.getContent().removeTextChangeListener(this);
			}
			// untrack changed styles of styled text of the editor
			if (fEditorViewer instanceof ITextViewerExtension4) {
				((ITextViewerExtension4) fEditorViewer).removeTextPresentationListener(this);
			}
			// track changed of vertical bar scroll to update highlight
			// Viewport.
			fEditorViewer.removeViewportListener(this);
			editorTextWidget.removeControlListener(this);
			fScaledFonts.values().forEach(Font::dispose);
		}
	}

	/**
	 * Minimap tracker.
	 *
	 */
	class MinimapTracker implements CaretListener, SelectionListener, PaintListener, MouseWheelListener {

		private static final int NB_LINES_SCROLL = 10;

		private int fEditorTopIndex;

		private int fTopIndexY;

		private boolean fTextChanging;

		@Override
		public void caretMoved(CaretEvent event) {
			if (fTextChanging) {
				// When editor content changed, it updates the minimap styled
				// text content which update caret.
				// In this case, ignore it to avoid set the top index of the
				// editor viewer.
				return;
			}
			updateEditorTopIndex();
		}

		private void updateEditorTopIndex() {
			int caretOffset = fMinimapTextWidget.getCaretOffset();
			int lineAtOffset = fMinimapTextWidget.getLineAtOffset(caretOffset);
			int newTopIndex = lineAtOffset;
			if (fEditorViewer instanceof ITextViewerExtension5) {
				// adjust offset according folded content
				newTopIndex = ((ITextViewerExtension5) fEditorViewer).widgetLine2ModelLine(lineAtOffset);
			}
			fEditorViewer.setTopIndex(newTopIndex);
		}

		public void updateMinimap(int editorTopIndex, int editorBottomIndex) {
			if (editorTopIndex != fEditorTopIndex) {
				fEditorTopIndex = editorTopIndex;
				// Update the position of minimap styled text
				fMinimapTextWidget.setRedraw(false);
				int newMinimapTopIndex = editorTopIndex;
				if (editorTopIndex != 0
						&& editorBottomIndex != fMinimapTextWidget.getLineAtOffset(fMinimapTextWidget.getCharCount())) {
					// center the draw of square of editor client area
					int minimapTopIndex = JFaceTextUtil.getPartialTopIndex(fMinimapTextWidget);
					int minimapBottomIndex = JFaceTextUtil.getPartialBottomIndex(fMinimapTextWidget);
					int minimapVisibleLineCount = minimapBottomIndex - minimapTopIndex;
					int editorVisibleLineCount = editorBottomIndex - editorTopIndex;
					newMinimapTopIndex = Math.max(0,
							editorTopIndex + editorVisibleLineCount - minimapVisibleLineCount / 2);
				}
				fMinimapTextWidget.setTopIndex(newMinimapTopIndex);
				fTopIndexY = fMinimapTextWidget.getLinePixel(fEditorTopIndex);
				fMinimapTextWidget.setRedraw(true);
			}
		}

		public void replaceTextRange(TextChangingEvent event) {
			fTextChanging = true;
			int start = event.start;
			int length = event.replaceCharCount;
			String text = event.newText;
			if (event.newLineCount > 0 || event.replaceLineCount > 0) {
				Rectangle clientArea = fMinimapTextWidget.getClientArea();
				fMinimapTextWidget.setRedraw(false);
				fMinimapTextWidget.replaceTextRange(start, length, text);
				fMinimapTextWidget.redraw(0, fTopIndexY, clientArea.width, clientArea.height, false);
				fMinimapTextWidget.setRedraw(true);
			} else {
				fMinimapTextWidget.replaceTextRange(start, length, text);
			}
			fTextChanging = false;
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
			// Do nothing
		}

		@Override
		public void widgetSelected(SelectionEvent e) {
			// hack to avoid drawing selection of styled text while mouse move
			// of highlight viewport
			fMinimapTextWidget.setSelection(fMinimapTextWidget.getCaretOffset());
		}

		@Override
		public void paintControl(PaintEvent event) {
			GC gc = event.gc;
			int clientAreaHeight = fEditorTracker.getScaledClientAreaHeight();
			int clientAreaWidth = fMinimapTextWidget.getClientArea().width;
			gc.setBackground(fMinimapTextWidget.getSelectionBackground());
			Rectangle rect = new Rectangle(0, fTopIndexY, clientAreaWidth, clientAreaHeight);
			gc.drawRectangle(rect.x, rect.y, Math.max(1, rect.width - 1), Math.max(1, rect.height - 1));
			gc.setAdvanced(true);
			if (gc.getAdvanced()) {
				gc.setAlpha(20);
				gc.fillRectangle(rect);
				gc.setAdvanced(false);
			}
		}

		@Override
		public void mouseScrolled(MouseEvent e) {
			int caretOffset = fMinimapTextWidget.getOffsetAtPoint(new Point(0, fTopIndexY));
			int lineIndex = fMinimapTextWidget.getLineAtOffset(caretOffset);
			if (e.count > 0) {
				lineIndex = Math.max(0, lineIndex - NB_LINES_SCROLL);
			} else {
				lineIndex = Math.min(fMinimapTextWidget.getCharCount(), lineIndex + NB_LINES_SCROLL);
			}
			caretOffset = fMinimapTextWidget.getOffsetAtLine(lineIndex);
			fMinimapTextWidget.setCaretOffset(caretOffset);
		}

		void install() {
			fMinimapTextWidget.addCaretListener(this);
			fMinimapTextWidget.addSelectionListener(this);
			fMinimapTextWidget.addPaintListener(this);
			fMinimapTextWidget.addMouseWheelListener(this);
		}

		void uninstall() {
			if (!fMinimapTextWidget.isDisposed()) {
				fMinimapTextWidget.removeCaretListener(this);
				fMinimapTextWidget.removeSelectionListener(this);
				fMinimapTextWidget.removePaintListener(this);
				fMinimapTextWidget.removeMouseWheelListener(this);
			}
		}

	}

	private final EditorTracker fEditorTracker;

	private final MinimapTracker fMinimapTracker;

	public MinimapWidget(Composite parent, ITextViewer viewer) {
		fEditorViewer = viewer;

		// Create minimap styled text
		fMinimapTextWidget = new StyledText(parent, SWT.MULTI | SWT.READ_ONLY);
		fMinimapTextWidget.setEditable(false);
		fMinimapTextWidget.setCursor(fMinimapTextWidget.getDisplay().getSystemCursor(SWT.CURSOR_ARROW));

		// Initialize trackers
		fEditorTracker = new EditorTracker();
		fMinimapTracker = new MinimapTracker();
	}

	/**
	 * Install minimap widget.
	 */
	public void install() {
		fEditorTracker.install();
		fMinimapTracker.install();
	}

	public void uninstall() {
		fEditorTracker.uninstall();
		fMinimapTracker.uninstall();
	}

	public Control getControl() {
		return fMinimapTextWidget;
	}

	float getScale() {
		return SCALE;
	}
}

Back to the top