ChangeScreen2: Scroll to make last viewed file visible When navigating up from a file view back to the change, scroll to make the last viewed file visible in the "Files" section of the screen. This is useful on changes with incredibly large commit messages like I2d572279ac978e644772b1cedbecc080746a2306. The files table is immediately visible and the user can navigate to another file within the change. Change-Id: I4aa99c1632e60deb7254803f3730df45d6cfc46b
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/Gerrit.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/Gerrit.java index b23e192..59dd849 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/Gerrit.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/Gerrit.java
@@ -115,6 +115,7 @@ private static ViewSite<Screen> body; private static PatchScreen patchScreen; private static String lastChangeListToken; + private static String lastViewToken; static { SYSTEM_SVC = GWT.create(SystemInfoService.class); @@ -143,6 +144,10 @@ } } + public static String getPriorView() { + return lastViewToken; + } + /** * Load the screen at the given location, displaying when ready. * <p> @@ -490,8 +495,9 @@ body = new ViewSite<Screen>() { @Override protected void onShowView(Screen view) { - final String token = view.getToken(); - if (!token.equals(History.getToken())) { + lastViewToken = History.getToken(); + String token = view.getToken(); + if (!token.equals(lastViewToken)) { History.newItem(token, false); dispatchHistoryHooks(token); }
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ChangeScreen2.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ChangeScreen2.java index a879caa..2e16be9 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ChangeScreen2.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/ChangeScreen2.java
@@ -72,6 +72,7 @@ import com.google.gwtexpui.globalkey.client.GlobalKey; import com.google.gwtexpui.globalkey.client.KeyCommand; import com.google.gwtexpui.globalkey.client.KeyCommandSet; +import com.google.gwtorm.client.KeyUtil; import java.util.ArrayList; import java.util.Collections; @@ -212,6 +213,43 @@ files.registerKeys(); } + @Override + public void onShowView() { + super.onShowView(); + + String prior = Gerrit.getPriorView(); + if (prior != null && prior.startsWith("/c/")) { + scrollToPath(prior.substring(3)); + } + } + + private void scrollToPath(String token) { + int s = token.indexOf('/'); + try { + if (s < 0 || !changeId.equals(Change.Id.parse(token.substring(0, s)))) { + return; // Unrelated URL, do not scroll. + } + } catch (IllegalArgumentException e) { + return; + } + + s = token.indexOf('/', s + 1); + if (s < 0) { + return; // URL does not name a file. + } + + int c = token.lastIndexOf(','); + if (0 <= c) { + token = token.substring(s + 1, c); + } else { + token = token.substring(s + 1); + } + + if (!token.isEmpty()) { + files.scrollToPath(KeyUtil.decode(token)); + } + } + @UiHandler("star") void onToggleStar(ValueChangeEvent<Boolean> e) { StarredChanges.toggleStar(changeId, e.getValue());
diff --git a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/FileTable.java b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/FileTable.java index 1e30587..c5005ef 100644 --- a/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/FileTable.java +++ b/gerrit-gwtui/src/main/java/com/google/gerrit/client/change/FileTable.java
@@ -127,6 +127,7 @@ private MyTable table; private boolean register; private JsArrayString reviewed; + private String scrollToPath; @Override protected void onLoad() { @@ -176,10 +177,19 @@ } } + void scrollToPath(String path) { + if (table != null) { + table.scrollToPath(path); + } else { + scrollToPath = path; + } + } + private void setTable(MyTable table) { clear(); add(table); this.table = table; + if (register) { table.setRegisterKeys(true); } @@ -187,6 +197,10 @@ table.markReviewed(reviewed); reviewed = null; } + if (scrollToPath != null) { + table.scrollToPath(scrollToPath); + scrollToPath = null; + } } private String url(FileInfo info) { @@ -267,6 +281,13 @@ return InputElement.as(e.getFirstChildElement()); } + void scrollToPath(String path) { + FileInfo info = map.get(path); + if (info != null) { + movePointerTo(1 + info._row(), true); + } + } + @Override protected Object getRowItemKey(FileInfo item) { return item.path();