Use 0-based index in tab positions (fixes #46)

This commit is contained in:
Thomas Brouard 2022-05-25 10:53:37 +02:00
parent 15812d2e1f
commit 5fc15ab42b
5 changed files with 20 additions and 33 deletions

View file

@ -81,7 +81,7 @@ export class Tab extends EventTarget {
getBadge(): string;
setIcon(iconURL: string, icon: string): this;
getIcon(): string;
setPosition(newPosition: number): this;
setPosition(newPosition: number): void;
getPosition(fromRight?: boolean): number;
activate(): this;
show(flag?: boolean): this;

File diff suppressed because one or more lines are too long

17
dist/electron-tabs.js vendored
View file

@ -2831,18 +2831,16 @@ class $eda442ba39f881a8$var$Tab extends EventTarget {
}
setPosition(newPosition) {
const tabContainer = this.tabGroup.tabContainer;
const tabs = tabContainer.children;
const oldPosition = this.getPosition() - 1;
const length = tabContainer.childElementCount;
const thisPosition = this.getPosition();
const tabs = Array.from(tabContainer.children);
tabs.splice(thisPosition, 1);
if (newPosition < 0) {
newPosition += tabContainer.childElementCount;
newPosition += length;
if (newPosition < 0) newPosition = 0;
} else {
if (newPosition > tabContainer.childElementCount) newPosition = tabContainer.childElementCount;
// Make 1 be leftmost position
newPosition--;
}
if (newPosition > oldPosition) newPosition++;
tabContainer.insertBefore(tabs[oldPosition], tabs[newPosition]);
if (newPosition < length) tabContainer.insertBefore(this.tab, tabs[newPosition]);
else tabContainer.appendChild(this.tab);
return this;
}
getPosition(fromRight = false) {
@ -2850,7 +2848,6 @@ class $eda442ba39f881a8$var$Tab extends EventTarget {
let tab = this.tab;
while((tab = tab.previousSibling) != null)position++;
if (fromRight === true) position -= this.tabGroup.tabContainer.childElementCount;
if (position >= 0) position++;
return position;
}
activate() {

File diff suppressed because one or more lines are too long

View file

@ -460,30 +460,24 @@ class Tab extends EventTarget {
setPosition(newPosition: number) {
const tabContainer = this.tabGroup.tabContainer;
const tabs = tabContainer.children;
const oldPosition = this.getPosition() - 1;
const length = tabContainer.childElementCount;
const thisPosition = this.getPosition();
const tabs = Array.from(tabContainer.children)
tabs.splice(thisPosition, 1);
if (newPosition < 0) {
newPosition += tabContainer.childElementCount;
newPosition += length;
if (newPosition < 0) {
newPosition = 0;
}
}
if (newPosition < length) {
tabContainer.insertBefore(this.tab, tabs[newPosition]);
} else {
if (newPosition > tabContainer.childElementCount) {
newPosition = tabContainer.childElementCount;
tabContainer.appendChild(this.tab);
}
// Make 1 be leftmost position
newPosition--;
}
if (newPosition > oldPosition) {
newPosition++;
}
tabContainer.insertBefore(tabs[oldPosition], tabs[newPosition]);
return this;
}
@ -496,10 +490,6 @@ class Tab extends EventTarget {
position -= this.tabGroup.tabContainer.childElementCount;
}
if (position >= 0) {
position++;
}
return position;
}