2024-09-25 17:27:12 +05:30
|
|
|
import { Layouts, PosType } from "../../../canvas/constants/layouts"
|
2024-09-26 11:59:24 +05:30
|
|
|
import Tools from "../../../canvas/constants/tools"
|
|
|
|
|
import Widget from "../../../canvas/widgets/base"
|
2024-09-27 16:04:03 +05:30
|
|
|
import { removeKeyFromObject } from "../../../utils/common"
|
|
|
|
|
import { Tkinter_TO_WEB_CURSOR_MAPPING } from "../constants/cursor"
|
|
|
|
|
import { Tkinter_To_GFonts } from "../constants/fontFamily"
|
2024-09-27 19:22:33 +05:30
|
|
|
import { JUSTIFY, RELIEF } from "../constants/styling"
|
2024-09-25 17:27:12 +05:30
|
|
|
|
2024-09-26 23:16:43 +05:30
|
|
|
// TODO: add full width and full height in base widget
|
|
|
|
|
// TODO: the pack should configure width and height of widgets
|
2024-09-25 17:27:12 +05:30
|
|
|
|
2024-09-27 16:04:03 +05:30
|
|
|
export class TkinterBase extends Widget {
|
2024-09-26 11:59:24 +05:30
|
|
|
|
|
|
|
|
static requiredImports = ['import tkinter as tk']
|
|
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props)
|
|
|
|
|
|
|
|
|
|
this.getLayoutCode = this.getLayoutCode.bind(this)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getLayoutCode(){
|
|
|
|
|
const {layout: parentLayout, direction, gap} = this.getParentLayout()
|
|
|
|
|
|
|
|
|
|
let layoutManager = `pack()`
|
|
|
|
|
|
|
|
|
|
if (parentLayout === Layouts.FLEX){
|
2024-09-26 23:16:43 +05:30
|
|
|
layoutManager = `pack(side=${direction === "row" ? "tk.LEFT" : "tk.TOP"})`
|
2024-09-26 11:59:24 +05:30
|
|
|
}else if (parentLayout === Layouts.GRID){
|
|
|
|
|
const row = this.getAttrValue("gridManager.row")
|
|
|
|
|
const col = this.getAttrValue("gridManager.col")
|
|
|
|
|
layoutManager = `grid(row=${row}, col=${col})`
|
|
|
|
|
}else{
|
|
|
|
|
layoutManager = `place(x=${this.state.pos.x}, y=${this.state.pos.y})`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return layoutManager
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 23:27:07 +05:30
|
|
|
setParentLayout(layout){
|
2024-09-26 11:59:24 +05:30
|
|
|
|
2024-09-27 23:27:07 +05:30
|
|
|
if (!layout){
|
2024-09-27 16:04:03 +05:30
|
|
|
return {}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 23:27:07 +05:30
|
|
|
const {layout: parentLayout, direction, gap} = layout
|
2024-09-26 11:59:24 +05:30
|
|
|
|
2024-09-25 17:27:12 +05:30
|
|
|
// show attributes related to the layout manager
|
|
|
|
|
let updates = {
|
2024-09-27 23:27:07 +05:30
|
|
|
parentLayout: layout,
|
2024-09-25 17:27:12 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.removeAttr("gridManager")
|
2024-09-27 23:27:07 +05:30
|
|
|
if (parentLayout === Layouts.FLEX || parentLayout === Layouts.GRID) {
|
2024-09-25 17:27:12 +05:30
|
|
|
|
|
|
|
|
updates = {
|
|
|
|
|
...updates,
|
|
|
|
|
positionType: PosType.NONE
|
|
|
|
|
}
|
2024-09-25 19:20:05 +05:30
|
|
|
|
2024-09-26 11:59:24 +05:30
|
|
|
if (parentLayout === Layouts.GRID) {
|
2024-09-25 17:27:12 +05:30
|
|
|
// Set attributes related to grid layout manager
|
|
|
|
|
updates = {
|
|
|
|
|
...updates,
|
|
|
|
|
attrs: {
|
|
|
|
|
...this.state.attrs,
|
|
|
|
|
gridManager: {
|
|
|
|
|
label: "Grid manager",
|
|
|
|
|
display: "horizontal",
|
|
|
|
|
row: {
|
|
|
|
|
label: "Row",
|
|
|
|
|
tool: Tools.NUMBER_INPUT,
|
|
|
|
|
toolProps: { placeholder: "width", max: 1000, min: 1 },
|
|
|
|
|
value: 1,
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
|
|
|
|
|
const previousRow = this.getWidgetOuterStyle("gridRow") || "1/1"
|
2024-09-25 19:20:05 +05:30
|
|
|
|
|
|
|
|
let [_row=1, rowSpan=1] = previousRow.replace(/\s+/g, '').split("/").map(Number)
|
|
|
|
|
|
|
|
|
|
if (value > rowSpan){
|
|
|
|
|
// rowSpan should always be greater than or eq to row
|
|
|
|
|
rowSpan = value
|
|
|
|
|
this.setAttrValue("gridManager.rowSpan", rowSpan)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setAttrValue("gridManager.row", value)
|
2024-09-25 17:27:12 +05:30
|
|
|
this.setWidgetOuterStyle("gridRow", `${value+' / '+rowSpan}`)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
rowSpan: {
|
|
|
|
|
label: "Row span",
|
|
|
|
|
tool: Tools.NUMBER_INPUT,
|
|
|
|
|
toolProps: { placeholder: "height", max: 1000, min: 1 },
|
|
|
|
|
value: 1,
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
|
|
|
|
|
const previousRow = this.getWidgetOuterStyle("gridRow") || "1/1"
|
2024-09-25 19:20:05 +05:30
|
|
|
|
2024-09-25 17:27:12 +05:30
|
|
|
const [row=1, _rowSpan=1] = previousRow.replace(/\s+/g, '').split("/").map(Number)
|
2024-09-25 19:20:05 +05:30
|
|
|
|
|
|
|
|
if (value < row){
|
|
|
|
|
value = row + 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setAttrValue("gridManager.rowSpan", value)
|
2024-09-25 17:27:12 +05:30
|
|
|
this.setWidgetOuterStyle("gridRow", `${row + ' / ' +value}`)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
column: {
|
|
|
|
|
label: "Column",
|
|
|
|
|
tool: Tools.NUMBER_INPUT,
|
|
|
|
|
toolProps: { placeholder: "height", max: 1000, min: 1 },
|
|
|
|
|
value: 1,
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
|
|
|
|
|
const previousRow = this.getWidgetOuterStyle("gridColumn") || "1/1"
|
|
|
|
|
|
2024-09-25 19:20:05 +05:30
|
|
|
let [_col=1, colSpan=1] = previousRow.replace(/\s+/g, '').split("/").map(Number)
|
|
|
|
|
|
|
|
|
|
if (value > colSpan){
|
|
|
|
|
// The colSpan has always be equal or greater than col
|
|
|
|
|
colSpan = value
|
|
|
|
|
this.setAttrValue("gridManager.columnSpan", colSpan)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setAttrValue("gridManager.column", value)
|
2024-09-25 17:27:12 +05:30
|
|
|
this.setWidgetOuterStyle("gridColumn", `${value +' / ' + colSpan}`)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
columnSpan: {
|
|
|
|
|
label: "Column span",
|
|
|
|
|
tool: Tools.NUMBER_INPUT,
|
|
|
|
|
toolProps: { placeholder: "height", max: 1000, min: 1 },
|
|
|
|
|
value: 1,
|
|
|
|
|
onChange: (value) => {
|
2024-09-25 19:20:05 +05:30
|
|
|
|
2024-09-25 17:27:12 +05:30
|
|
|
const previousCol = this.getWidgetOuterStyle("gridColumn") || "1/1"
|
|
|
|
|
|
|
|
|
|
const [col=1, _colSpan=1] = previousCol.replace(/\s+/g, '').split("/").map(Number)
|
|
|
|
|
|
2024-09-25 19:20:05 +05:30
|
|
|
if (value < col){
|
|
|
|
|
value = col + 1
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.setAttrValue("gridManager.columnSpan", value)
|
2024-09-25 17:27:12 +05:30
|
|
|
this.setWidgetOuterStyle("gridColumn", `${col + ' / ' + value}`)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-27 23:27:07 +05:30
|
|
|
} else if (parentLayout === Layouts.PLACE) {
|
2024-09-25 17:27:12 +05:30
|
|
|
updates = {
|
|
|
|
|
...updates,
|
|
|
|
|
positionType: PosType.ABSOLUTE
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.updateState(updates)
|
|
|
|
|
|
|
|
|
|
return updates
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* loads the data
|
|
|
|
|
* @param {object} data
|
|
|
|
|
*/
|
2024-09-25 23:29:50 +05:30
|
|
|
load(data, callback=null){
|
2024-09-25 17:27:12 +05:30
|
|
|
|
|
|
|
|
if (Object.keys(data).length === 0) return // no data to load
|
|
|
|
|
|
|
|
|
|
data = {...data} // create a shallow copy
|
|
|
|
|
|
2024-09-27 16:04:03 +05:30
|
|
|
const {attrs, parentLayout=null, ...restData} = data
|
2024-09-25 17:27:12 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
let layoutUpdates = {
|
|
|
|
|
parentLayout: parentLayout
|
|
|
|
|
}
|
2024-09-27 16:04:03 +05:30
|
|
|
|
|
|
|
|
if (parentLayout){
|
|
|
|
|
if (parentLayout.layout === Layouts.FLEX || parentLayout.layout === Layouts.GRID){
|
2024-09-25 17:27:12 +05:30
|
|
|
|
2024-09-27 16:04:03 +05:30
|
|
|
layoutUpdates = {
|
|
|
|
|
...layoutUpdates,
|
|
|
|
|
positionType: PosType.NONE
|
|
|
|
|
}
|
2024-09-25 17:27:12 +05:30
|
|
|
|
2024-09-27 16:04:03 +05:30
|
|
|
}else if (parentLayout.layout === Layouts.PLACE){
|
|
|
|
|
layoutUpdates = {
|
|
|
|
|
...layoutUpdates,
|
|
|
|
|
positionType: PosType.ABSOLUTE
|
|
|
|
|
}
|
2024-09-25 17:27:12 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const newData = {
|
|
|
|
|
...restData,
|
|
|
|
|
...layoutUpdates
|
|
|
|
|
}
|
2024-09-27 16:04:03 +05:30
|
|
|
|
2024-09-25 17:27:12 +05:30
|
|
|
|
|
|
|
|
this.setState(newData, () => {
|
2024-09-27 23:27:07 +05:30
|
|
|
let layoutAttrs = this.setParentLayout(parentLayout).attrs || {}
|
|
|
|
|
|
2024-09-25 17:27:12 +05:30
|
|
|
// UPdates attrs
|
|
|
|
|
let newAttrs = { ...this.state.attrs, ...layoutAttrs }
|
|
|
|
|
|
|
|
|
|
// Iterate over each path in the updates object
|
|
|
|
|
Object.entries(attrs).forEach(([path, value]) => {
|
|
|
|
|
const keys = path.split('.')
|
|
|
|
|
const lastKey = keys.pop()
|
|
|
|
|
|
|
|
|
|
// Traverse the nested object within attrs
|
|
|
|
|
let nestedObject = newAttrs
|
|
|
|
|
|
|
|
|
|
keys.forEach(key => {
|
|
|
|
|
nestedObject[key] = { ...nestedObject[key] } // Ensure immutability for each nested level
|
|
|
|
|
nestedObject = nestedObject[key]
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
// Set the value at the last key
|
|
|
|
|
if (nestedObject[lastKey])
|
|
|
|
|
nestedObject[lastKey].value = value
|
|
|
|
|
})
|
|
|
|
|
|
2024-09-25 23:29:50 +05:30
|
|
|
|
|
|
|
|
if (newAttrs?.styling?.backgroundColor){
|
|
|
|
|
// TODO: find a better way to apply innerStyles
|
|
|
|
|
this.setWidgetInnerStyle("backgroundColor", newAttrs.styling.backgroundColor.value)
|
|
|
|
|
}
|
2024-09-27 16:04:03 +05:30
|
|
|
|
2024-09-25 23:29:50 +05:30
|
|
|
this.updateState({ attrs: newAttrs }, callback)
|
2024-09-25 17:27:12 +05:30
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-09-27 16:04:03 +05:30
|
|
|
// base for widgets that have common base properties such as bg, fg, cursor etc
|
|
|
|
|
export class TkinterWidgetBase extends TkinterBase{
|
|
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props)
|
|
|
|
|
|
|
|
|
|
this.droppableTags = null // disables drops
|
|
|
|
|
|
|
|
|
|
const newAttrs = removeKeyFromObject("layout", this.state.attrs)
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
...this.state,
|
|
|
|
|
attrs: {
|
|
|
|
|
...newAttrs,
|
|
|
|
|
styling: {
|
|
|
|
|
...newAttrs.styling,
|
|
|
|
|
foregroundColor: {
|
|
|
|
|
label: "Foreground Color",
|
|
|
|
|
tool: Tools.COLOR_PICKER,
|
|
|
|
|
value: "#000",
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
this.setWidgetInnerStyle("color", value)
|
|
|
|
|
this.setAttrValue("styling.foregroundColor", value)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
borderWidth: {
|
|
|
|
|
label: "Border thickness",
|
|
|
|
|
tool: Tools.NUMBER_INPUT,
|
|
|
|
|
toolProps: {min: 0, max: 10},
|
|
|
|
|
value: 0,
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
this.setWidgetInnerStyle("border", `${value}px solid black`)
|
|
|
|
|
this.setAttrValue("styling.borderWidth", value)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
relief: {
|
|
|
|
|
label: "Relief",
|
|
|
|
|
tool: Tools.SELECT_DROPDOWN,
|
|
|
|
|
options: RELIEF.map((val) => ({value: val, label: val})),
|
2024-09-27 19:22:33 +05:30
|
|
|
value: "",
|
2024-09-27 16:04:03 +05:30
|
|
|
onChange: (value) => {
|
2024-09-27 19:22:33 +05:30
|
|
|
// this.setWidgetInnerStyle("fontFamily", Tkinter_To_GFonts[value])
|
|
|
|
|
this.setAttrValue("styling.relief", value)
|
2024-09-27 16:04:03 +05:30
|
|
|
}
|
2024-09-27 19:22:33 +05:30
|
|
|
},
|
|
|
|
|
// justify: {
|
|
|
|
|
// label: "Justify",
|
|
|
|
|
// tool: Tools.SELECT_DROPDOWN,
|
|
|
|
|
// options: JUSTIFY.map((val) => ({value: val, label: val})),
|
|
|
|
|
// value: "",
|
|
|
|
|
// onChange: (value) => {
|
|
|
|
|
// this.setWidgetInnerStyle("text-align", value)
|
|
|
|
|
// this.setAttrValue("styling.justify", value)
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
},
|
|
|
|
|
padding: {
|
|
|
|
|
label: "padding",
|
|
|
|
|
padX: {
|
|
|
|
|
label: "Pad X",
|
|
|
|
|
tool: Tools.NUMBER_INPUT,
|
|
|
|
|
toolProps: {min: 1, max: 140},
|
|
|
|
|
value: null,
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
// this.setWidgetInnerStyle("paddingLeft", `${value}px`)
|
|
|
|
|
// this.setWidgetInnerStyle("paddingRight", `${value}px`)
|
|
|
|
|
|
|
|
|
|
const widgetStyle = {
|
|
|
|
|
...this.state.widgetInnerStyling,
|
|
|
|
|
paddingLeft: `${value}px`,
|
|
|
|
|
paddingRight: `${value}px`
|
|
|
|
|
}
|
|
|
|
|
this.setState({
|
|
|
|
|
|
|
|
|
|
widgetInnerStyling: widgetStyle
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
this.setAttrValue("padding.padX", value)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
padY: {
|
|
|
|
|
label: "Pad Y",
|
|
|
|
|
tool: Tools.NUMBER_INPUT,
|
|
|
|
|
toolProps: {min: 1, max: 140},
|
|
|
|
|
value: null,
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
const widgetStyle = {
|
|
|
|
|
...this.state.widgetInnerStyling,
|
|
|
|
|
paddingTop: `${value}px`,
|
|
|
|
|
paddingBottom: `${value}px`
|
|
|
|
|
}
|
|
|
|
|
this.setState({
|
|
|
|
|
|
|
|
|
|
widgetInnerStyling: widgetStyle
|
|
|
|
|
})
|
|
|
|
|
this.setAttrValue("padding.padX", value)
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-09-27 16:04:03 +05:30
|
|
|
},
|
|
|
|
|
font: {
|
|
|
|
|
label: "font",
|
|
|
|
|
fontFamily: {
|
|
|
|
|
label: "font family",
|
|
|
|
|
tool: Tools.SELECT_DROPDOWN,
|
|
|
|
|
options: Object.keys(Tkinter_To_GFonts).map((val) => ({value: val, label: val})),
|
|
|
|
|
value: "Arial",
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
this.setWidgetInnerStyle("fontFamily", Tkinter_To_GFonts[value])
|
|
|
|
|
this.setAttrValue("font.fontFamily", value)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fontSize: {
|
|
|
|
|
label: "font size",
|
|
|
|
|
tool: Tools.NUMBER_INPUT,
|
|
|
|
|
toolProps: {min: 1, max: 140},
|
2024-09-27 19:22:33 +05:30
|
|
|
value: null,
|
2024-09-27 16:04:03 +05:30
|
|
|
onChange: (value) => {
|
|
|
|
|
this.setWidgetInnerStyle("fontSize", `${value}px`)
|
|
|
|
|
this.setAttrValue("font.fontSize", value)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
cursor: {
|
|
|
|
|
label: "Cursor",
|
|
|
|
|
tool: Tools.SELECT_DROPDOWN,
|
|
|
|
|
toolProps: {placeholder: "select cursor"},
|
2024-09-27 19:22:33 +05:30
|
|
|
value: "",
|
2024-09-27 16:04:03 +05:30
|
|
|
options: Object.keys(Tkinter_TO_WEB_CURSOR_MAPPING).map((val) => ({value: val, label: val})),
|
|
|
|
|
onChange: (value) => {
|
|
|
|
|
this.setWidgetInnerStyle("cursor", Tkinter_TO_WEB_CURSOR_MAPPING[value])
|
|
|
|
|
this.setAttrValue("cursor", value)
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-09-27 19:22:33 +05:30
|
|
|
|
|
|
|
|
this.getConfigCode = this.getConfigCode.bind(this)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getConfigCode(){
|
|
|
|
|
|
|
|
|
|
const code = {
|
|
|
|
|
bg: `"${this.getAttrValue("styling.backgroundColor")}"`,
|
|
|
|
|
fg: `"${this.getAttrValue("styling.foregroundColor")}"`,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.getAttrValue("styling.borderWidth"))
|
|
|
|
|
code["bd"] = this.getAttrValue("styling.borderWidth")
|
|
|
|
|
|
|
|
|
|
if (this.getAttrValue("styling.relief"))
|
|
|
|
|
code["relief"] = `"${this.getAttrValue("styling.relief")}"`
|
|
|
|
|
|
|
|
|
|
if (this.getAttrValue("font.fontFamily") || this.getAttrValue("font.fontSize")){
|
|
|
|
|
code["font"] = [`"${this.getAttrValue("font.fontFamily")}"`, this.getAttrValue("font.fontSize"), ]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.getAttrValue("cursor"))
|
|
|
|
|
code["cursor"] = `"${this.getAttrValue("cursor")}"`
|
|
|
|
|
|
|
|
|
|
if (this.getAttrValue("padding.padX")){
|
|
|
|
|
code["padx"] = this.getAttrValue("padding.padX")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (this.getAttrValue("padding.padY")){
|
|
|
|
|
code["pady"] = this.getAttrValue("padding.padY")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return code
|
2024-09-27 16:04:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|