Skip to content

Commit 503bbee

Browse files
committed
feat: make git branch switch smoother to prevent flickering
1 parent f109cc4 commit 503bbee

1 file changed

Lines changed: 31 additions & 23 deletions

File tree

src/extensions/default/Git/src/Branch.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ define(function (require, exports) {
3333
$dropdown;
3434

3535
let lastRenderedBranchName = null;
36+
let lastRenderedProjectRoot = null;
3637
let $dropdownAnchor = null;
3738

3839
function renderList(branches) {
@@ -482,12 +483,12 @@ define(function (require, exports) {
482483
return Utils.getProjectRoot() !== projectRoot;
483484
}
484485

485-
// show info that branch is refreshing currently
486-
$gitBranchName
487-
.text("\u2026")
488-
.parent()
489-
.show();
490-
Panel.setBranchName("\u2026", "");
486+
// show the loading placeholder only when the displayed name can't be
487+
// trusted (first refresh or project switch), else keep it to avoid flicker
488+
if (projectRoot !== lastRenderedProjectRoot) {
489+
$gitBranchName.text("\u2026");
490+
Panel.setBranchName("\u2026", "");
491+
}
491492

492493
return Git.getGitRoot().then(function (gitRoot) {
493494
if (isStale()) { return; }
@@ -500,6 +501,7 @@ define(function (require, exports) {
500501
Preferences.set("currentGitSubfolder", "");
501502

502503
lastRenderedBranchName = null;
504+
lastRenderedProjectRoot = projectRoot;
503505
$gitBranchName
504506
.off("click")
505507
.text(Strings.GIT_NOT_A_REPO);
@@ -539,24 +541,29 @@ define(function (require, exports) {
539541

540542
const MAX_LEN = 18;
541543

544+
const changed = branchName !== lastRenderedBranchName ||
545+
projectRoot !== lastRenderedProjectRoot;
542546
lastRenderedBranchName = branchName;
543-
const tooltip = StringUtils.format(Strings.ON_BRANCH, branchName);
544-
const displayName = branchName.length > MAX_LEN
545-
? branchName.substring(0, MAX_LEN) + "\u2026"
546-
: branchName;
547-
// branch names may contain characters like "<", so set them
548-
// as text and never as html
549-
$gitBranchName
550-
.text(" " + displayName)
551-
.prepend('<i class="fas fa-code-branch"></i>')
552-
.attr("title", tooltip)
553-
.off("click")
554-
.on("click", toggleDropdown);
555-
Panel.setBranchName(displayName, tooltip);
556-
$("#git-panel .git-panel-branch")
557-
.addClass("clickable")
558-
.off("click")
559-
.on("click", toggleDropdown);
547+
lastRenderedProjectRoot = projectRoot;
548+
if (changed) {
549+
const tooltip = StringUtils.format(Strings.ON_BRANCH, branchName);
550+
const displayName = branchName.length > MAX_LEN
551+
? branchName.substring(0, MAX_LEN) + "\u2026"
552+
: branchName;
553+
// branch names may contain characters like "<", so set them
554+
// as text and never as html
555+
$gitBranchName
556+
.text(" " + displayName)
557+
.prepend('<i class="fas fa-code-branch"></i>')
558+
.attr("title", tooltip)
559+
.off("click")
560+
.on("click", toggleDropdown);
561+
Panel.setBranchName(displayName, tooltip);
562+
$("#git-panel .git-panel-branch")
563+
.addClass("clickable")
564+
.off("click")
565+
.on("click", toggleDropdown);
566+
}
560567
Panel.enable();
561568

562569
}).catch(function (err) {
@@ -567,6 +574,7 @@ define(function (require, exports) {
567574
if (isStale()) { return; }
568575
if (ErrorHandler.contains(ex, "unknown revision")) {
569576
lastRenderedBranchName = null;
577+
lastRenderedProjectRoot = projectRoot;
570578
$gitBranchName
571579
.off("click")
572580
.text(Strings.GIT_NO_BRANCH);

0 commit comments

Comments
 (0)