Skip to content

Commit d4beb23

Browse files
committed
list: clean drop, dragstart
1 parent c47c001 commit d4beb23

3 files changed

Lines changed: 62 additions & 49 deletions

File tree

src/vs/base/browser/ui/tree/abstractTree.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ import { IDragAndDropData } from 'vs/base/browser/dnd';
1717
import { range } from 'vs/base/common/arrays';
1818
import { ElementsDragAndDropData } from 'vs/base/browser/ui/list/listView';
1919

20+
function asTreeDragAndDropData<T, TFilterData>(data: IDragAndDropData): IDragAndDropData {
21+
if (data instanceof ElementsDragAndDropData) {
22+
const nodes = (data as ElementsDragAndDropData<ITreeNode<T, TFilterData>>).elements;
23+
return new ElementsDragAndDropData(nodes.map(node => node.element));
24+
}
25+
26+
return data;
27+
}
28+
2029
class TreeNodeListDragAndDrop<T, TFilterData, TRef> implements IListDragAndDrop<ITreeNode<T, TFilterData>> {
2130

2231
private autoExpandNode: ITreeNode<T, TFilterData> | undefined;
@@ -38,19 +47,12 @@ class TreeNodeListDragAndDrop<T, TFilterData, TRef> implements IListDragAndDrop<
3847

3948
onDragStart(data: IDragAndDropData, originalEvent: DragEvent): void {
4049
if (this.dnd.onDragStart) {
41-
this.dnd.onDragStart(data, originalEvent);
50+
this.dnd.onDragStart(asTreeDragAndDropData(data), originalEvent);
4251
}
4352
}
4453

4554
onDragOver(data: IDragAndDropData, targetNode: ITreeNode<T, TFilterData> | undefined, targetIndex: number | undefined, originalEvent: DragEvent, raw = true): boolean | IListDragOverReaction {
46-
let treeData: IDragAndDropData = data;
47-
48-
if (data instanceof ElementsDragAndDropData) {
49-
const nodes = (data as ElementsDragAndDropData<ITreeNode<T, TFilterData>>).elements;
50-
treeData = new ElementsDragAndDropData(nodes.map(node => node.element));
51-
}
52-
53-
const result = this.dnd.onDragOver(treeData, targetNode && targetNode.element, targetIndex, originalEvent);
55+
const result = this.dnd.onDragOver(asTreeDragAndDropData(data), targetNode && targetNode.element, targetIndex, originalEvent);
5456
const didChangeAutoExpandNode = this.autoExpandNode !== targetNode;
5557

5658
if (didChangeAutoExpandNode) {
@@ -102,7 +104,7 @@ class TreeNodeListDragAndDrop<T, TFilterData, TRef> implements IListDragAndDrop<
102104
}
103105

104106
drop(data: IDragAndDropData, targetNode: ITreeNode<T, TFilterData> | undefined, targetIndex: number | undefined, originalEvent: DragEvent): void {
105-
this.dnd.drop(data, targetNode && targetNode.element, targetIndex, originalEvent);
107+
this.dnd.drop(asTreeDragAndDropData(data), targetNode && targetNode.element, targetIndex, originalEvent);
106108
}
107109
}
108110

src/vs/base/browser/ui/tree/asyncDataTree.ts

Lines changed: 43 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55

66
import { ComposedTreeDelegate, IAbstractTreeOptions } from 'vs/base/browser/ui/tree/abstractTree';
77
import { ObjectTree, IObjectTreeOptions } from 'vs/base/browser/ui/tree/objectTree';
8-
import { IListVirtualDelegate, IIdentityProvider } from 'vs/base/browser/ui/list/list';
9-
import { ITreeElement, ITreeNode, ITreeRenderer, ITreeEvent, ITreeMouseEvent, ITreeContextMenuEvent, ITreeSorter, ICollapseStateChangeEvent, IAsyncDataSource } from 'vs/base/browser/ui/tree/tree';
8+
import { IListVirtualDelegate, IIdentityProvider, IListDragAndDrop, IListDragOverReaction } from 'vs/base/browser/ui/list/list';
9+
import { ITreeElement, ITreeNode, ITreeRenderer, ITreeEvent, ITreeMouseEvent, ITreeContextMenuEvent, ITreeSorter, ICollapseStateChangeEvent, IAsyncDataSource, ITreeDragAndDrop } from 'vs/base/browser/ui/tree/tree';
1010
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
1111
import { Emitter, Event } from 'vs/base/common/event';
1212
import { timeout, always } from 'vs/base/common/async';
@@ -124,6 +124,46 @@ export interface IChildrenResolutionEvent<T> {
124124
readonly reason: ChildrenResolutionReason;
125125
}
126126

127+
function asAsyncDataTreeDragAndDropData<TInput, T>(data: IDragAndDropData): IDragAndDropData {
128+
if (data instanceof ElementsDragAndDropData) {
129+
const nodes = (data as ElementsDragAndDropData<IAsyncDataTreeNode<TInput, T>>).elements;
130+
return new ElementsDragAndDropData(nodes.map(node => node.element));
131+
}
132+
133+
return data;
134+
}
135+
136+
class AsyncDataTreeNodeListDragAndDrop<TInput, T> implements IListDragAndDrop<IAsyncDataTreeNode<TInput, T>> {
137+
138+
constructor(private dnd: ITreeDragAndDrop<T>) { }
139+
140+
getDragURI(node: IAsyncDataTreeNode<TInput, T>): string | null {
141+
return this.dnd.getDragURI(node.element as T);
142+
}
143+
144+
getDragLabel(nodes: IAsyncDataTreeNode<TInput, T>[]): string | undefined {
145+
if (this.dnd.getDragLabel) {
146+
return this.dnd.getDragLabel(nodes.map(node => node.element as T));
147+
}
148+
149+
return undefined;
150+
}
151+
152+
onDragStart(data: IDragAndDropData, originalEvent: DragEvent): void {
153+
if (this.dnd.onDragStart) {
154+
this.dnd.onDragStart(asAsyncDataTreeDragAndDropData(data), originalEvent);
155+
}
156+
}
157+
158+
onDragOver(data: IDragAndDropData, targetNode: IAsyncDataTreeNode<TInput, T> | undefined, targetIndex: number | undefined, originalEvent: DragEvent, raw = true): boolean | IListDragOverReaction {
159+
return this.dnd.onDragOver(asAsyncDataTreeDragAndDropData(data), targetNode && targetNode.element as T, targetIndex, originalEvent);
160+
}
161+
162+
drop(data: IDragAndDropData, targetNode: IAsyncDataTreeNode<TInput, T> | undefined, targetIndex: number | undefined, originalEvent: DragEvent): void {
163+
this.dnd.drop(asAsyncDataTreeDragAndDropData(data), targetNode && targetNode.element as T, targetIndex, originalEvent);
164+
}
165+
}
166+
127167
function asObjectTreeOptions<TInput, T, TFilterData>(options?: IAsyncDataTreeOptions<T, TFilterData>): IObjectTreeOptions<IAsyncDataTreeNode<TInput, T>, TFilterData> | undefined {
128168
return options && {
129169
...options,
@@ -132,36 +172,7 @@ function asObjectTreeOptions<TInput, T, TFilterData>(options?: IAsyncDataTreeOpt
132172
return options.identityProvider!.getId(el.element as T);
133173
}
134174
},
135-
dnd: options.dnd && {
136-
getDragURI(node) {
137-
return options.dnd!.getDragURI(node.element as T);
138-
},
139-
getDragLabel(nodes) {
140-
if (options.dnd!.getDragLabel) {
141-
return options.dnd!.getDragLabel!(nodes.map(node => node.element as T));
142-
}
143-
144-
return undefined;
145-
},
146-
onDragStart(data, originalEvent) {
147-
if (options.dnd!.onDragStart) {
148-
options.dnd!.onDragStart!(data, originalEvent);
149-
}
150-
},
151-
onDragOver(data, targetNode, targetIndex, originalEvent) {
152-
let treeData: IDragAndDropData = data;
153-
154-
if (data instanceof ElementsDragAndDropData) {
155-
const nodes = (data as ElementsDragAndDropData<IAsyncDataTreeNode<TInput, T>>).elements;
156-
treeData = new ElementsDragAndDropData(nodes.map(node => node.element));
157-
}
158-
159-
return options.dnd!.onDragOver(treeData, targetNode && targetNode.element as T, targetIndex, originalEvent);
160-
},
161-
drop(data, targetNode, targetIndex, originalEvent) {
162-
options.dnd!.drop(data, targetNode && targetNode.element as T, targetIndex, originalEvent);
163-
}
164-
},
175+
dnd: options.dnd && new AsyncDataTreeNodeListDragAndDrop(options.dnd),
165176
multipleSelectionController: options.multipleSelectionController && {
166177
isSelectionSingleChangeEvent(e) {
167178
return options.multipleSelectionController!.isSelectionSingleChangeEvent({ ...e, element: e.element } as any);

src/vs/workbench/parts/debug/electron-browser/watchExpressionsView.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,13 +290,13 @@ class WatchExpressionsDragAndDrop implements ITreeDragAndDrop<IExpression> {
290290
}
291291

292292
drop(data: IDragAndDropData, targetElement: IExpression): void {
293-
const draggedData = data.getData();
294-
295-
if (Array.isArray(draggedData)) {
296-
const draggedElement = <IExpression>draggedData[0].element.element;
297-
const watches = this.debugService.getModel().getWatchExpressions();
298-
const position = targetElement instanceof Expression ? watches.indexOf(targetElement) : watches.length - 1;
299-
this.debugService.moveWatchExpression(draggedElement.getId(), position);
293+
if (!(data instanceof ElementsDragAndDropData)) {
294+
return;
300295
}
296+
297+
const draggedElement = (data as ElementsDragAndDropData<IExpression>).elements[0];
298+
const watches = this.debugService.getModel().getWatchExpressions();
299+
const position = targetElement instanceof Expression ? watches.indexOf(targetElement) : watches.length - 1;
300+
this.debugService.moveWatchExpression(draggedElement.getId(), position);
301301
}
302302
}

0 commit comments

Comments
 (0)