Skip to content

Commit

Permalink
Merge "ui: Add TrackNode.clone() method" into main
Browse files Browse the repository at this point in the history
  • Loading branch information
stevegolton authored and Gerrit Code Review committed Dec 16, 2024
2 parents 1cfff13 + c004f3f commit 57f2ef8
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
20 changes: 20 additions & 0 deletions ui/src/plugins/com.example.ExampleNestedTracks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,25 @@ export default class implements PerfettoPlugin {
track1.addChildLast(track12);
track12.addChildLast(track121);
track2.addChildLast(track21);

ctx.commands.registerCommand({
id: 'com.example.ExampleNestedTracks#CloneTracksToNewWorkspace',
name: 'Clone track to new workspace',
callback: () => {
const ws = ctx.workspaces.createEmptyWorkspace('New workspace');
ws.addChildLast(trackRoot.clone());
ctx.workspaces.switchWorkspace(ws);
},
});

ctx.commands.registerCommand({
id: 'com.example.ExampleNestedTracks#DeepCloneTracksToNewWorkspace',
name: 'Clone all tracks to new workspace',
callback: () => {
const ws = ctx.workspaces.createEmptyWorkspace('Deep workspace');
ws.addChildLast(trackRoot.clone(true));
ctx.workspaces.switchWorkspace(ws);
},
});
}
}
16 changes: 16 additions & 0 deletions ui/src/public/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,22 @@ export class TrackNode {
return this.tracksByUri.get(uri);
}

/**
* Creates a copy of this node with a new ID.
*
* @param deep - If true, children are copied too.
* @returns - A copy of this node.
*/
clone(deep = false): TrackNode {
const cloned = new TrackNode({...this, id: undefined});
if (deep) {
this.children.forEach((c) => {
cloned.addChildLast(c.clone(deep));
});
}
return cloned;
}

private adopt(child: TrackNode): void {
if (child.parent) {
child.parent.removeChild(child);
Expand Down
46 changes: 46 additions & 0 deletions ui/src/public/workspace_unittest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,3 +224,49 @@ test('TrackNode::flatTracks', () => {
);
expect(root.flatTracks.length).toBe(4);
});

test('TrackNode::clone', () => {
const root = new TrackNode();
const childA = new TrackNode();
root.addChildLast(childA);

const childB = new TrackNode();
root.addChildLast(childB);

const cloned = root.clone();

expect(cloned.id).not.toBe(root.id); // id should be different
expect(cloned.uri).toBe(root.uri);
expect(cloned.expanded).toBe(root.expanded);
expect(cloned.title).toBe(root.title);
expect(cloned.headless).toBe(root.headless);
expect(cloned.isSummary).toBe(root.isSummary);
expect(cloned.removable).toBe(root.removable);
expect(cloned.children).toStrictEqual([]); // Children should not be copied
});

test('TrackNode::clone(deep)', () => {
const root = new TrackNode();
const childA = new TrackNode();
root.addChildLast(childA);

const childB = new TrackNode();
root.addChildLast(childB);

const cloned = root.clone(true);

expect(cloned.id).not.toBe(root.id); // id should be different
expect(cloned.uri).toBe(root.uri);
expect(cloned.expanded).toBe(root.expanded);
expect(cloned.title).toBe(root.title);
expect(cloned.headless).toBe(root.headless);
expect(cloned.isSummary).toBe(root.isSummary);
expect(cloned.removable).toBe(root.removable);
expect(cloned.children).toHaveLength(2);

expect(cloned.children[0].title).toBe(childA.title);
expect(cloned.children[0].uri).toBe(childA.uri);

expect(cloned.children[1].title).toBe(childB.title);
expect(cloned.children[1].uri).toBe(childB.uri);
});

0 comments on commit 57f2ef8

Please sign in to comment.