Skip to content

Commit 24253c9

Browse files
committed
Decouple drag target index from click target index
Reducing the complexity of the logic and fixing tab add/close actions
1 parent c35906f commit 24253c9

File tree

1 file changed

+26
-25
lines changed

1 file changed

+26
-25
lines changed

packages/widgets/src/tabbar.ts

Lines changed: 26 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1046,16 +1046,25 @@ export class TabBar<T> extends Widget {
10461046
this.document.addEventListener('mouseup', this, true); // <DEPRECATED>
10471047
this.document.addEventListener('pointerup', this, true);
10481048

1049+
if (scrollBeforeButtonClicked || scrollAfterButtonClicked) {
1050+
this.beginScrolling(scrollBeforeButtonClicked ? '-' : '+');
1051+
return;
1052+
}
1053+
1054+
this._clickedTabIndex = index;
1055+
10491056
// Do nothing else if the middle button or add button is clicked.
10501057
if (event.button === 1 || addButtonClicked) {
10511058
return;
10521059
}
1053-
if (scrollBeforeButtonClicked || scrollAfterButtonClicked) {
1054-
this.beginScrolling(scrollBeforeButtonClicked ? '-' : '+');
1060+
1061+
// Do nothing else if the close icon is clicked.
1062+
let icon = tabs[index].querySelector(this.renderer.closeIconSelector);
1063+
if (icon && icon.contains(event.target as HTMLElement)) {
10551064
return;
10561065
}
10571066

1058-
// Initialize the non-measured parts of the drag data.
1067+
// Initialize the non-measured parts of the drag data,
10591068
this._dragData = {
10601069
tab: tabs[index] as HTMLElement,
10611070
index: index,
@@ -1077,12 +1086,6 @@ export class TabBar<T> extends Widget {
10771086
detachRequested: false
10781087
};
10791088

1080-
// Do nothing else if the close icon is clicked.
1081-
let icon = tabs[index].querySelector(this.renderer.closeIconSelector);
1082-
if (icon && icon.contains(event.target as HTMLElement)) {
1083-
return;
1084-
}
1085-
10861089
// Add the extra listeners if the tabs are movable.
10871090
if (this.tabsMovable) {
10881091
this.document.addEventListener('mousemove', this, true); // <DEPRECATED>
@@ -1218,35 +1221,32 @@ export class TabBar<T> extends Widget {
12181221
return;
12191222
}
12201223

1221-
// Do nothing if no drag is in progress.
12221224
const data = this._dragData;
1223-
if (!data) {
1224-
if (this._scrollData) {
1225-
this.stopScrolling();
1226-
}
1227-
return;
1228-
}
12291225

12301226
// Stop the event propagation.
12311227
event.preventDefault();
12321228
event.stopPropagation();
12331229

12341230
// Remove the extra mouse event listeners.
1235-
this.document.removeEventListener('mousemove', this, true); // <DEPRECATED>
12361231
this.document.removeEventListener('mouseup', this, true); // <DEPRECATED>
1237-
this.document.removeEventListener('pointermove', this, true);
12381232
this.document.removeEventListener('pointerup', this, true);
1239-
this.document.removeEventListener('keydown', this, true);
1240-
this.document.removeEventListener('contextmenu', this, true);
12411233

1242-
// Handle a release when the drag is not active.
1243-
if (!data.dragActive) {
1234+
// Remove extra mouse event listeners which are only added when drag is in progress.
1235+
if (data) {
1236+
this.document.removeEventListener('mousemove', this, true); // <DEPRECATED>
1237+
this.document.removeEventListener('pointermove', this, true);
1238+
this.document.removeEventListener('keydown', this, true);
1239+
this.document.removeEventListener('contextmenu', this, true);
1240+
}
1241+
1242+
// Handle a release when the drag is not active or not in progress.
1243+
if (!data || !data.dragActive) {
12441244
// Clear the drag data.
12451245
this._dragData = null;
12461246

1247+
// Handle mouse release if scrolling was in progress.
12471248
if (this._scrollData) {
12481249
this.stopScrolling();
1249-
return;
12501250
}
12511251
// Handle clicking the add button.
12521252
let addButtonClicked =
@@ -1266,12 +1266,12 @@ export class TabBar<T> extends Widget {
12661266
});
12671267

12681268
// Do nothing if the release is not on the original pressed tab.
1269-
if (index !== data.index) {
1269+
if (index !== this._clickedTabIndex) {
12701270
return;
12711271
}
12721272

12731273
// Do nothing if neither press nor release was on a tab.
1274-
if (index === -1 && data.index === -1) {
1274+
if (index === -1 && this._clickedTabIndex === -1) {
12751275
return;
12761276
}
12771277

@@ -1549,6 +1549,7 @@ export class TabBar<T> extends Widget {
15491549
}
15501550

15511551
private _name: string;
1552+
private _clickedTabIndex: number = -1;
15521553
private _currentIndex = -1;
15531554
private _titles: Title<T>[] = [];
15541555
private _orientation: TabBar.Orientation;

0 commit comments

Comments
 (0)