Skip to content

Commit d5e53da

Browse files
authored
Merge pull request #450 from sugarlabs/gsoc-dmp-2025/week-7-8/saumya
GSoC/DMP Week 7-8: feat(masonry): Add tower disconnection and dragging of tower in Workspace
2 parents ad3df75 + aa78c4d commit d5e53da

File tree

12 files changed

+1128
-461
lines changed

12 files changed

+1128
-461
lines changed

modules/masonry/playground/pages/workspace/WorkspaceCanvas.tsx

Lines changed: 0 additions & 221 deletions
This file was deleted.

modules/masonry/playground/pages/workspace/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { useState } from 'react';
22
import PaletteWrapper from '../../../src/palette/components/paletteWrapper';
33
import '../../../src/palette/palette.css';
4-
import WorkspaceCanvas from './WorkspaceCanvas';
4+
import WorkSpaceView from '../../../src/workspace/view/components/WorkspaceView';
55

66
export default function App() {
77
const [showCollisionTest, setShowCollisionTest] = useState(false);
@@ -45,7 +45,7 @@ export default function App() {
4545
</aside>
4646

4747
{/* Playground canvas */}
48-
<WorkspaceCanvas />
48+
<WorkSpaceView />
4949
</div>
5050
);
5151
}

modules/masonry/src/brick/model/model.ts

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import type { TConnectionPoints as TCP } from '../../tree/model/model';
1616
import { generateBrickData } from '../utils/path';
1717
import type { TInputUnion } from '../utils/path';
1818
import { getLabelWidth } from '../utils/textMeasurement';
19+
import type { ExtendedTowerNode } from '../../tower/view/components/TowerView';
20+
import { calculateCompleteSubtreeDimensions } from '../../tower/utils/towerUtils';
1921

2022
export abstract class BrickModel implements IBrick {
2123
protected _uuid: string;
@@ -386,34 +388,54 @@ export default class CompoundBrick extends BrickModel implements IBrickCompound
386388
public setBoundingBoxNest(extents: TExtent[]): void {
387389
this._bboxNest = extents;
388390
}
389-
391+
390392
/**
391393
* Recursively update bounding box and connection points to fit nested children.
392394
* Call this after all children are attached, before rendering.
393395
*/
394-
public updateLayoutWithChildren(nestedChildren: BrickModel[]): void {
395-
// If there are nested children, calculate the total bounding box
396+
public updateLayoutWithChildren(nestedChildren: BrickModel[], allNodes: Map<string, ExtendedTowerNode>): void {
396397
if (nestedChildren && nestedChildren.length > 0) {
397-
// Calculate the bounding box that fits all nested children
398-
let _minX = 0,
399-
_minY = 0,
400-
maxX = 0,
401-
maxY = 0;
402-
nestedChildren.forEach((child) => {
403-
const bbox = child.boundingBox;
404-
// For simplicity, assume children are stacked vertically for now
405-
maxY += bbox.h;
406-
maxX = Math.max(maxX, bbox.w);
407-
});
408-
// Expand this brick's bboxNest to fit the children
409-
this._bboxNest = [{ w: maxX, h: maxY }];
398+
let totalHeight = 0;
399+
let maxWidth = 0;
400+
401+
// Recursively calculate the total height and max width of all descendants
402+
const calculateSubtreeDimensions = (children: BrickModel[]): { h: number; w: number } => {
403+
let height = 0;
404+
let width = 0;
405+
children.forEach(child => {
406+
const childNode = Array.from(allNodes.values()).find(n => n.brick.uuid === child.uuid);
407+
if (childNode) {
408+
const { w, h } = calculateCompleteSubtreeDimensions(childNode.brick.uuid, allNodes, new Map<string, { w: number; h: number }>());
409+
height += h;
410+
width = Math.max(width, w);
411+
}
412+
});
413+
return { h: height, w: width };
414+
};
415+
416+
const { h, w } = calculateSubtreeDimensions(nestedChildren);
417+
totalHeight = h;
418+
maxWidth = w;
419+
420+
// Update bboxNest to reflect the full subtree dimensions
421+
this._bboxNest = [{ w: maxWidth, h: totalHeight }];
410422
} else {
411423
this._bboxNest = [];
412424
}
413-
// Update geometry with new bboxNest
414425
this.updateGeometry();
415426
}
416427

428+
// Helper method to get nested children based on the tower structure
429+
public getNestedChildren(allNodes: Map<string, ExtendedTowerNode>): BrickModel[] {
430+
const children: BrickModel[] = [];
431+
allNodes.forEach(node => {
432+
if (node.parent?.brick.uuid === this.uuid && node.isNested) {
433+
children.push(node.brick as BrickModel); // Cast IBrick to BrickModel
434+
}
435+
});
436+
return children;
437+
}
438+
417439
public override get renderProps(): TBrickRenderPropsCompound {
418440
return {
419441
...this.getCommonRenderProps(),

modules/masonry/src/brick/utils/path.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ function getBoundingBox(config: TInputUnion): TBBox {
426426
const baseWidth = CORNER_RADIUS + OFFSET_NOTCH_TOP + WIDTH_NOTCH_TOP + variableTopWidth;
427427

428428
const width = hasArgs
429-
? baseWidth + OFFSET_NOTCH_RIGHT + CORNER_RADIUS + strokeWidth / 2
429+
? baseWidth + OFFSET_NOTCH_RIGHT + CORNER_RADIUS + strokeWidth / 2 + (type === 'type2' ? 7 : 0) // Add 7 more for left notch
430430
: baseWidth + CORNER_RADIUS + strokeWidth / 2;
431431

432432
// Get rightVertical from _generateRight
@@ -437,7 +437,7 @@ function getBoundingBox(config: TInputUnion): TBBox {
437437
bBoxArgs: bBoxArgs || [],
438438
});
439439

440-
let height = rightVertical + CORNER_RADIUS + (type !== 'type3' ? strokeWidth / 2 : 0);
440+
let height = rightVertical + CORNER_RADIUS + (type === 'type2' ? strokeWidth / 2 - 1 : strokeWidth / 2);
441441

442442
if (type === 'type3') {
443443
const { bBoxNesting, secondaryLabel } = config as TInputType3;
@@ -474,7 +474,7 @@ function getBoundingBox(config: TInputUnion): TBBox {
474474

475475
// functions to calculate coordinates of the connection points
476476

477-
type TCentroid = { x: number; y: number };
477+
export type TCentroid = { x: number; y: number };
478478

479479
// Centroid calculation for Top Notch
480480
function getTopCentroid(config: TInputUnion): TCentroid | undefined {

0 commit comments

Comments
 (0)