SideBySide2: Batch updates to CodeMirror3 during onShowView

Colby recently observed opening files is sluggish.  Digging into
profile data shows CM3 doing multiple relayouts if its internal
DOM during onShowView(), once for each method being invoked. Batch
these together into an operation() call, allowing CM3 to buffer
the updates and update its DOM only once.

Change-Id: Iefbc112a37016c4031515b4422a02368e5af16c1
This commit is contained in:
Shawn Pearce
2013-11-21 12:40:57 -08:00
parent a8ecfa3ea3
commit 5b83d94271
2 changed files with 29 additions and 9 deletions

View File

@@ -231,17 +231,29 @@ public class SideBySide2 extends Screen {
@Override
public void onShowView() {
super.onShowView();
resizeCodeMirror();
Window.enableScrolling(false);
cmA.setOption("viewportMargin", 10);
cmB.setOption("viewportMargin", 10);
final int height = getCodeMirrorHeight();
onShowView(cmA, height);
onShowView(cmB, height);
diffTable.sidePanel.adjustGutters(cmB);
cmB.setCursor(LineCharacter.create(0));
cmB.focus();
prefetchNextFile();
}
private void onShowView(final CodeMirror cm, final int height) {
cm.operation(new Runnable() {
@Override
public void run() {
cm.setHeight(height);
cm.setOption("viewportMargin", 10);
cm.refresh();
}
});
}
@Override
protected void onUnload() {
super.onUnload();
@@ -1239,16 +1251,18 @@ public class SideBySide2 extends Screen {
}
void resizeCodeMirror() {
int height = getCodeMirrorHeight();
cmA.setHeight(height);
cmB.setHeight(height);
diffTable.sidePanel.adjustGutters(cmB);
}
private int getCodeMirrorHeight() {
int rest = Gerrit.getHeaderFooterHeight()
+ header.getOffsetHeight()
+ diffTable.getHeaderHeight()
+ 5; // Estimate
int h = Window.getClientHeight() - rest;
cmA.setHeight(h);
cmB.setHeight(h);
cmA.refresh();
cmB.refresh();
diffTable.sidePanel.adjustGutters(cmB);
return Window.getClientHeight() - rest;
}
static void setHeightInPx(Element ele, double height) {

View File

@@ -166,6 +166,12 @@ public class CodeMirror extends JavaScriptObject {
this.state.oldViewportSize = lines;
}-*/;
public final native void operation(Runnable thunk) /*-{
this.operation(function() {
thunk.@java.lang.Runnable::run()();
});
}-*/;
public final native void on(String event, Runnable thunk) /*-{
this.on(event, $entry(function() {
thunk.@java.lang.Runnable::run()();