Skip to content

Commit

Permalink
Limit visible sticky lines to text widget height
Browse files Browse the repository at this point in the history
When the editor is very small, the sticky lines should not be displayed or the amount of sticky lines should be reduced so that the original source code can be seen.
  • Loading branch information
Christopher-Hermann authored and BeckerWdf committed Jul 12, 2024
1 parent bb479a8 commit 337f4ba
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,15 @@
*/
public class StickyScrollingControl {

/**
* This threshold represents the minimum number of source lines that must remain visible in the
* editor. If the StickyScrollingControl's size would result in fewer than this number of lines
* being visible, the height of the StickyScrollingControl will be reduced to ensure visibility
* of at least this many lines. Thus, it guarantees a minimum visibility threshold for the
* source content in the editor underneath the StickyScrollingControl.
*/
private final static int MIN_VISIBLE_EDITOR_LINES_THRESHOLD= 3;

private List<StickyLine> stickyLines;

private ISourceViewer sourceViewer;
Expand All @@ -103,6 +112,8 @@ public class StickyScrollingControl {

private StickyScrollingHandler stickyScrollingHandler;

private int maximumVisibleStickyLines= Integer.MAX_VALUE;

public StickyScrollingControl(ISourceViewer sourceViewer, StickyScrollingControlSettings settings) {
this(sourceViewer, null, settings, null);
}
Expand Down Expand Up @@ -185,6 +196,7 @@ private void createControls() {
bottomSeparator.setEnabled(false);

layoutLineNumbers();
limitVisibleStickyLinesToTextWidgetHeight(sourceViewer.getTextWidget());

stickyLinesCanvas.pack();
stickyLinesCanvas.moveAbove(null);
Expand Down Expand Up @@ -379,7 +391,9 @@ private void ensureSourceViewerLineVisible(int line) {
}

private int getNumberStickyLines() {
return Math.min(settings.maxCountStickyLines(), this.stickyLines.size());
int numberStickyLines= Math.min(settings.maxCountStickyLines(), this.stickyLines.size());
numberStickyLines= Math.min(maximumVisibleStickyLines, numberStickyLines);
return numberStickyLines;
}

/**
Expand Down Expand Up @@ -412,7 +426,12 @@ private void addSourceViewerListeners() {
controlListener= new ControlListener() {
@Override
public void controlResized(ControlEvent e) {
StyledText textWidget= sourceViewer.getTextWidget();
limitVisibleStickyLinesToTextWidgetHeight(textWidget);
layoutStickyLines();
if (stickyScrollingHandler != null) {
stickyScrollingHandler.viewportChanged(textWidget.getTopPixel());
}
}

@Override
Expand All @@ -423,6 +442,16 @@ public void controlMoved(ControlEvent e) {
sourceViewer.getTextWidget().addControlListener(controlListener);
}

private void limitVisibleStickyLinesToTextWidgetHeight(StyledText textWidget) {
int lineHeight= textWidget.getLineHeight() + textWidget.getLineSpacing();
int textWidgetHeight= textWidget.getBounds().height;

int visibleLinesInTextWidget= textWidgetHeight / lineHeight;

maximumVisibleStickyLines= Math.max(0, visibleLinesInTextWidget - MIN_VISIBLE_EDITOR_LINES_THRESHOLD);
updateStickyScrollingControls();
}

/**
* Sets the cursor on the canvas to {@link SWT#CURSOR_HAND} and adds several mouse listeners to
* the canvas.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void setup() {
ruler = new CompositeRuler();
sourceViewer = new SourceViewer(shell, ruler, SWT.V_SCROLL | SWT.H_SCROLL);
sourceViewer.setDocument(new Document());
sourceViewer.getTextWidget().setBounds(0, 0, 200, 200);

lineNumberColor = new Color(0, 0, 0);
hoverColor = new Color(1, 1, 1);
Expand Down Expand Up @@ -116,12 +117,10 @@ public void testLimitStickyLinesCount() {
stickyScrollingControl.applySettings(settings);

StyledText stickyLineNumber = getStickyLineNumber();
String expLineNumber = """
10""";
String expLineNumber = "10";
assertEquals(expLineNumber, stickyLineNumber.getText());
StyledText stickyLineText = getStickyLineText();
String expStickyLineText = """
line 10""";
String expStickyLineText = "line 10";
assertEquals(expStickyLineText, stickyLineText.getText());
}

Expand Down Expand Up @@ -215,6 +214,7 @@ public void testNavigateToStickyLine() {

@Test
public void testVerticalScrollingIsDispatched() {
sourceViewer.getTextWidget().setBounds(0, 0, 200, 0);
Canvas stickyControlCanvas = getStickyControlCanvas(shell);
String text = """
line 1
Expand All @@ -232,6 +232,7 @@ public void testVerticalScrollingIsDispatched() {

@Test
public void testHorizontalScrollingIsDispatched() {
sourceViewer.getTextWidget().setBounds(0, 0, 0, 200);
Canvas stickyControlCanvas = getStickyControlCanvas(shell);
String text = """
line 1
Expand All @@ -247,6 +248,20 @@ public void testHorizontalScrollingIsDispatched() {
assertEquals(10, sourceViewer.getTextWidget().getHorizontalPixel());
}

@Test
public void limitStickyLinesToTextWidgetHeight() {
sourceViewer.getTextWidget().setBounds(0, 0, 200, 200);
List<StickyLine> stickyLines = List.of(new StickyLine("line 2", 1));
stickyScrollingControl.setStickyLines(stickyLines);

StyledText stickyLineText = getStickyLineText();
assertEquals("line 2", stickyLineText.getText());

sourceViewer.getTextWidget().setBounds(0, 0, 200, 20);
stickyLineText = getStickyLineText();
assertEquals("", stickyLineText.getText());
}

private Canvas getStickyControlCanvas(Composite composite) {
for (Control control : composite.getChildren()) {
if (control instanceof Canvas canvas) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void setup() {
ruler = new CompositeRuler();
sourceViewer = new SourceViewer(shell, ruler, SWT.None);
sourceViewer.setDocument(new Document());
sourceViewer.getTextWidget().setBounds(0, 0, 200, 200);

lineNumberColor = new Color(0, 0, 0);
hoverColor = new Color(1, 1, 1);
Expand Down

0 comments on commit 337f4ba

Please sign in to comment.