diff --git a/src/App.js b/src/App.js index 0a9809a..c040107 100644 --- a/src/App.js +++ b/src/App.js @@ -85,15 +85,49 @@ function App() { useEffect(() => { + + if (!canvasRef) + return + + const canvasBoundingBox = canvasRef.current.getCanvasContainerBoundingRect() + const canvasCenterX = (canvasBoundingBox.width - canvasBoundingBox.left) / 2 + const canvasCenterY = (canvasBoundingBox.height - canvasBoundingBox.top) / 2 + if (UIFramework === FrameWorks.TKINTER){ - canvasRef?.current?.createWidget(TkMainWindow) + canvasRef?.current?.createWidget(TkMainWindow, ({id, widgetRef}) => { + + // center the widget when adding to canvas + if (!widgetRef.current){ + return + } + + const widgetBoundingBox = widgetRef.current?.getBoundingRect() + const widgetCenterX = (widgetBoundingBox.width - widgetBoundingBox.left) / 2 + const widgetCenterY = (widgetBoundingBox.height - widgetBoundingBox.top) / 2 + + + widgetRef.current?.setPos(canvasCenterX-widgetCenterX, canvasCenterY-widgetCenterY) + }) }else if (UIFramework === FrameWorks.CUSTOMTK){ - canvasRef?.current?.createWidget(CTkMainWindow) + canvasRef?.current?.createWidget(CTkMainWindow, ({id, widgetRef}) => { + + // center the widget when adding to canvas + if (!widgetRef.current){ + return + } + + const widgetBoundingBox = widgetRef.current?.getBoundingRect() + const widgetCenterX = (widgetBoundingBox.width - widgetBoundingBox.left) / 2 + const widgetCenterY = (widgetBoundingBox.height - widgetBoundingBox.top) / 2 + + + widgetRef.current?.setPos(canvasCenterX-widgetCenterX, canvasCenterY-widgetCenterY) + }) } - }, [UIFramework]) + }, [UIFramework, canvasRef]) // const handleDragStart = (event) => { // console.log("Drag start: ", event) @@ -169,7 +203,6 @@ function App() { const handleWidgetAddedToCanvas = (widgets) => { setCanvasWidgets(widgets) - console.log("widget added: ", widgets) } const handleCodeGen = () => { @@ -223,7 +256,7 @@ function App() { {/* */} + /> {/* */} {/* dragOverlay (dnd-kit) helps move items from one container to another */} diff --git a/src/canvas/canvas.js b/src/canvas/canvas.js index 4ac96f5..504bf2f 100644 --- a/src/canvas/canvas.js +++ b/src/canvas/canvas.js @@ -50,7 +50,7 @@ class Canvas extends React.Component { constructor(props) { super(props) - const { canvasWidgets, onWidgetListUpdated } = props + // const { canvasWidgets, onWidgetListUpdated } = props this.canvasRef = React.createRef() this.canvasContainerRef = React.createRef() @@ -90,7 +90,7 @@ class Canvas extends React.Component { toolbarAttrs: null } - this._onWidgetListUpdated = onWidgetListUpdated // a function callback when the widget is added to the canvas + // this._onWidgetListUpdated = onWidgetListUpdated // a function callback when the widget is added to the canvas this.resetTransforms = this.resetTransforms.bind(this) this.renderWidget = this.renderWidget.bind(this) @@ -1068,8 +1068,8 @@ class Canvas extends React.Component { if (callback) callback({ id, widgetRef }) - if (this._onWidgetListUpdated) - this._onWidgetListUpdated(widgets) // inform the parent container + // if (this._onWidgetListUpdated) + // this._onWidgetListUpdated(widgets) // inform the parent container }, 1) // Update the state to include the new widget's type and ID @@ -1128,8 +1128,8 @@ class Canvas extends React.Component { // widgets: [] // }) - if (this._onWidgetListUpdated) - this._onWidgetListUpdated([]) + // if (this._onWidgetListUpdated) + // this._onWidgetListUpdated([]) } getWidgetByIdFromWidgetList = (widgetId) => { @@ -1165,8 +1165,8 @@ class Canvas extends React.Component { // }) this.setWidgets(widgets) - if (this._onWidgetListUpdated) - this._onWidgetListUpdated(widgets) + // if (this._onWidgetListUpdated) + // this._onWidgetListUpdated(widgets) } onActiveWidgetUpdate(widgetId) { @@ -1268,7 +1268,7 @@ class Canvas extends React.Component { return (
+ tw-top-4 tw-place-items-center tw-left-4 tw-shadow-md tw-rounded-md tw-flex">