Skip to content

Commit 82ea6f6

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 c6507cb commit 82ea6f6

File tree

1 file changed

+25
-17
lines changed

1 file changed

+25
-17
lines changed

packages/widgets/src/tabbar.ts

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,16 +1028,25 @@ export class TabBar<T> extends Widget {
10281028
// Add the document mouse up listener.
10291029
this.document.addEventListener('pointerup', this, true);
10301030

1031+
if (scrollBeforeButtonClicked || scrollAfterButtonClicked) {
1032+
this.beginScrolling(scrollBeforeButtonClicked ? '-' : '+');
1033+
return;
1034+
}
1035+
1036+
this._clickedTabIndex = index;
1037+
10311038
// Do nothing else if the middle button or add button is clicked.
10321039
if (event.button === 1 || addButtonClicked) {
10331040
return;
10341041
}
1035-
if (scrollBeforeButtonClicked || scrollAfterButtonClicked) {
1036-
this.beginScrolling(scrollBeforeButtonClicked ? '-' : '+');
1042+
1043+
// Do nothing else if the close icon is clicked.
1044+
let icon = tabs[index].querySelector(this.renderer.closeIconSelector);
1045+
if (icon && icon.contains(event.target as HTMLElement)) {
10371046
return;
10381047
}
10391048

1040-
// Initialize the non-measured parts of the drag data.
1049+
// Initialize the non-measured parts of the drag data,
10411050
this._dragData = {
10421051
tab: tabs[index] as HTMLElement,
10431052
index: index,
@@ -1207,14 +1216,7 @@ export class TabBar<T> extends Widget {
12071216
return;
12081217
}
12091218

1210-
// Do nothing if no drag is in progress.
12111219
const data = this._dragData;
1212-
if (!data) {
1213-
if (this._scrollData) {
1214-
this.stopScrolling();
1215-
}
1216-
return;
1217-
}
12181220

12191221
// Stop the event propagation.
12201222
event.preventDefault();
@@ -1223,17 +1225,22 @@ export class TabBar<T> extends Widget {
12231225
// Remove the extra mouse event listeners.
12241226
this.document.removeEventListener('pointermove', this, true);
12251227
this.document.removeEventListener('pointerup', this, true);
1226-
this.document.removeEventListener('keydown', this, true);
1227-
this.document.removeEventListener('contextmenu', this, true);
12281228

1229-
// Handle a release when the drag is not active.
1230-
if (!data.dragActive) {
1229+
// Remove extra mouse event listeners which are only added when drag is in progress.
1230+
if (data) {
1231+
this.document.removeEventListener('pointermove', this, true);
1232+
this.document.removeEventListener('keydown', this, true);
1233+
this.document.removeEventListener('contextmenu', this, true);
1234+
}
1235+
1236+
// Handle a release when the drag is not active or not in progress.
1237+
if (!data || !data.dragActive) {
12311238
// Clear the drag data.
12321239
this._dragData = null;
12331240

1241+
// Handle mouse release if scrolling was in progress.
12341242
if (this._scrollData) {
12351243
this.stopScrolling();
1236-
return;
12371244
}
12381245
// Handle clicking the add button.
12391246
let addButtonClicked =
@@ -1253,12 +1260,12 @@ export class TabBar<T> extends Widget {
12531260
});
12541261

12551262
// Do nothing if the release is not on the original pressed tab.
1256-
if (index !== data.index) {
1263+
if (index !== this._clickedTabIndex) {
12571264
return;
12581265
}
12591266

12601267
// Do nothing if neither press nor release was on a tab.
1261-
if (index === -1 && data.index === -1) {
1268+
if (index === -1 && this._clickedTabIndex === -1) {
12621269
return;
12631270
}
12641271

@@ -1524,6 +1531,7 @@ export class TabBar<T> extends Widget {
15241531
}
15251532

15261533
private _name: string;
1534+
private _clickedTabIndex: number = -1;
15271535
private _currentIndex = -1;
15281536
private _titles: Title<T>[] = [];
15291537
private _orientation: TabBar.Orientation;

0 commit comments

Comments
 (0)