Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TextConsoleViewer Scrolling #719

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
Expand Down Expand Up @@ -63,6 +62,7 @@
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.ScrollBar;
import org.eclipse.ui.internal.console.ConsoleDocumentAdapter;
import org.eclipse.ui.internal.console.ConsoleHyperlinkPosition;
import org.eclipse.ui.progress.WorkbenchJob;
Expand Down Expand Up @@ -124,10 +124,7 @@ public void documentChanged(DocumentEvent event) {
}
};

// to store to user scroll lock action
private final AtomicBoolean userHoldsScrollLock = new AtomicBoolean(false);

WorkbenchJob revealJob = new WorkbenchJob("Reveal End of Document") {//$NON-NLS-1$
WorkbenchJob revealJob = new WorkbenchJob("Reveal End of Document") { //$NON-NLS-1$
@Override
public IStatus runInUIThread(IProgressMonitor monitor) {
scrollToEndOfDocument();
Expand All @@ -147,51 +144,41 @@ private void scrollToEndOfDocument() {

// set the scroll Lock setting for Console Viewer and Console View
private void setScrollLock(boolean lock) {
userHoldsScrollLock.set(lock);
if (scrollLockStateProvider != null && scrollLockStateProvider.getAutoScrollLock() != lock) {
scrollLockStateProvider.setAutoScrollLock(lock);
}
}

/*
* Checks if at the end of document
* Checks if last visible line is same as line count, implying end of the
* document.
*/
private boolean checkEndOfDocument() {
private boolean isAtEndOfDocument() {
StyledText textWidget = getTextWidget();
if (textWidget != null && !textWidget.isDisposed()) {
int partialBottomIndex = JFaceTextUtil.getPartialBottomIndex(textWidget);
int lastVisibleLineIndex = JFaceTextUtil.getPartialBottomIndex(textWidget);
int lineCount = textWidget.getLineCount();
int delta = textWidget.getVerticalBar().getIncrement();
return lineCount - partialBottomIndex < delta;
return lineCount == lastVisibleLineIndex + 1;
}
return false;
}

/*
* Check if user preference is enabled for auto scroll lock and the document is empty or the line count is smaller than each
* vertical scroll
* Checks if user preference is enabled for auto scroll lock. and if the view is
* empty or the content is minimal, implying no need for scrolling.
Comment on lines +167 to +168
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Checks if user preference is enabled for auto scroll lock. and if the view is
* empty or the content is minimal, implying no need for scrolling.
* Checks if the auto scrolling is not applicable based on:
<ul>
<li>Whether or not the appropriate user preference is enabled</li>
<li>Whether or not there is enough content to even require scrolling</li>
</ul>

*/
private boolean isAutoScrollLockNotApplicable() {
if (!consoleAutoScrollLock) {
return true;
}
StyledText textWidget = getTextWidget();
if (textWidget != null && !textWidget.isDisposed()) {
return (textWidget.getLineCount() <= textWidget.getVerticalBar().getIncrement());
}
return false;
}

/*
* Checks if at the start of document
*/
private boolean checkStartOfDocument() {
StyledText textWidget = getTextWidget();
if (textWidget != null && !textWidget.isDisposed()) {
int partialTopIndex = JFaceTextUtil.getPartialTopIndex(textWidget);
int lineCount = textWidget.getLineCount();
int delta = textWidget.getVerticalBar().getIncrement();
return lineCount - partialTopIndex < delta;
ScrollBar scrollBar = textWidget.getVerticalBar();
/**
* Value for pageIncrement is only set when lines exceed one page, otherwise it
* is 1
*/
return scrollBar.getPageIncrement() == 1;
}
return false;
}
Expand Down Expand Up @@ -253,30 +240,30 @@ public TextConsoleViewer(Composite parent, TextConsole console) {
styledText.addListener(SWT.MouseUp, mouseUpListener);
// event listener used to send event to vertical scroll bar
styledText.getVerticalBar().addSelectionListener(new SelectionAdapter() {

@Override
public void widgetSelected(SelectionEvent e) {
if (isAutoScrollLockNotApplicable()) {
return;
}
// scroll lock if vertical scroll bar dragged, OR selection on
// vertical bar used
if (e.detail == SWT.TOP || e.detail == SWT.HOME) {
if (e.detail == SWT.HOME || e.detail == SWT.TOP) {
// selecting TOP or HOME should lock
setScrollLock(true);
}
if (e.detail == SWT.ARROW_UP || e.detail == SWT.PAGE_UP) {
} else if (e.detail == SWT.PAGE_UP || e.detail == SWT.ARROW_UP) {
setScrollLock(true);
} else if (e.detail == SWT.END || e.detail == SWT.BOTTOM) {
// selecting BOTTOM or END from vertical scroll makes it
// reveal the end
setScrollLock(false);
} else if (e.detail == SWT.DRAG) {
if (checkEndOfDocument()) {
if (isAtEndOfDocument()) {
setScrollLock(false);
} else {
setScrollLock(true);
}
} else if ((e.detail == SWT.PAGE_DOWN || e.detail == SWT.ARROW_DOWN) && checkEndOfDocument()) {
} else if ((e.detail == SWT.PAGE_DOWN || e.detail == SWT.ARROW_DOWN) && isAtEndOfDocument()) {
// unlock if Down at the end of document
setScrollLock(false);
}
Expand All @@ -291,12 +278,12 @@ public void keyPressed(KeyEvent e) {
// lock the scroll if PAGE_UP ,HOME or TOP selected
if (e.keyCode == SWT.HOME || e.keyCode == SWT.TOP) {
setScrollLock(true);
} else if ((e.keyCode == SWT.PAGE_UP || e.keyCode == SWT.ARROW_UP) && !checkStartOfDocument()) {
} else if ((e.keyCode == SWT.PAGE_UP || e.keyCode == SWT.ARROW_UP)) {
setScrollLock(true);
} else if ((e.keyCode == SWT.END && (e.stateMask & SWT.CTRL) != 0) || e.keyCode == SWT.BOTTOM) {
// pressing CTRL+END reveals the end
setScrollLock(false);
} else if ((e.keyCode == SWT.PAGE_DOWN || e.keyCode == SWT.ARROW_DOWN) && checkEndOfDocument()) {
} else if ((e.keyCode == SWT.PAGE_DOWN || e.keyCode == SWT.ARROW_DOWN) && isAtEndOfDocument()) {
// unlock if Down at the end of document
setScrollLock(false);
}
Expand All @@ -306,11 +293,13 @@ public void keyPressed(KeyEvent e) {
if (isAutoScrollLockNotApplicable()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could rewrite this whole lambda like this:

if (isAuto... || e.count == 0) { 
    return; 
}
setScrollLock(e.count > 0 || !isAtEndOfDocument());

return;
}
if (e.count < 0) { // Mouse dragged down
if (checkEndOfDocument()) {
if (e.count < 0) { // Mouse scroll down
if (isAtEndOfDocument()) {
setScrollLock(false);
} else {
setScrollLock(true);
}
} else if (!userHoldsScrollLock.get()) {
} else if (e.count > 0) { // Mouse scroll up
setScrollLock(true);
}
});
Expand All @@ -331,6 +320,7 @@ public void keyPressed(KeyEvent e) {
document.addPositionUpdater(positionUpdater);
}


/**
* Sets the tab width used by this viewer.
*
Expand Down Expand Up @@ -854,4 +844,4 @@ protected void internalRevealRange(int start, int end) {
}


}
}
Loading