Skip to content

Commit

Permalink
Example usage of display executor
Browse files Browse the repository at this point in the history
  • Loading branch information
laeubi authored and mickaelistria committed Jan 23, 2023
1 parent 8c09461 commit 117ee89
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,25 @@ public ISourceViewer getViewer() {
*/
public void redraw() {
StyledText text= getTextWidget();
InlinedAnnotationSupport.runInUIThread(text, t -> {
Position pos= getPosition();
int offset= pos.getOffset();
ISourceViewer viewer= getViewer();
if (viewer instanceof ITextViewerExtension5) {
// adjust offset according folded content
offset= ((ITextViewerExtension5) viewer).modelOffset2WidgetOffset(offset);
if (text == null || text.isDisposed()) {
return;
}
text.getDisplay().execute(() -> {
if (text.isDisposed()) {
return;
}
try {
Position pos= getPosition();
int offset= pos.getOffset();
ISourceViewer viewer= getViewer();
if (viewer instanceof ITextViewerExtension5) {
// adjust offset according folded content
offset= ((ITextViewerExtension5) viewer).modelOffset2WidgetOffset(offset);
}
InlinedAnnotationDrawingStrategy.draw(this, null, text, offset, pos.getLength(), null);
} catch (RuntimeException e) {
// Ignore UI error
}
InlinedAnnotationDrawingStrategy.draw(this, null, t, offset, pos.getLength(), null);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -555,37 +555,6 @@ private AbstractInlinedAnnotation getInlinedAnnotationAtPoint(ISourceViewer view
return null;
}

/**
* Execute UI {@link StyledText} function which requires UI Thread.
*
* @param text the styled text
* @param fn the function to execute.
*/
static void runInUIThread(StyledText text, Consumer<StyledText> fn) {
if (text == null || text.isDisposed()) {
return;
}
Display display= text.getDisplay();
if (display.getThread() == Thread.currentThread()) {
try {
fn.accept(text);
} catch (Exception e) {
// Ignore UI error
}
} else {
display.asyncExec(() -> {
if (text.isDisposed()) {
return;
}
try {
fn.accept(text);
} catch (Exception e) {
// Ignore UI error
}
});
}
}

/**
* Return whether the given offset is in visible lines.
*
Expand Down

0 comments on commit 117ee89

Please sign in to comment.