Files
PyUIBuilder/src/frameworks/tkinter/widgets/frame.js

194 lines
6.4 KiB
JavaScript
Raw Normal View History

import { Layouts } from "../../../canvas/constants/layouts"
2025-03-26 15:08:25 +05:30
import Tools from "../../../canvas/constants/tools"
2024-09-22 22:24:35 +05:30
import Widget from "../../../canvas/widgets/base"
import { convertObjectToKeyValueString } from "../../../utils/common"
2024-09-27 16:04:03 +05:30
import {TkinterBase} from "./base"
2024-09-22 22:24:35 +05:30
2024-09-25 17:27:12 +05:30
class Frame extends TkinterBase{
2024-09-22 22:24:35 +05:30
static widgetType = "frame"
2025-03-11 07:23:17 +05:30
static displayName = "Frame"
2024-09-22 22:24:35 +05:30
constructor(props) {
super(props)
this.droppableTags = {
2024-09-26 11:59:24 +05:30
exclude: ["image", "video", "media", "toplevel", "main_window"]
2024-09-22 22:24:35 +05:30
}
2024-09-27 16:04:03 +05:30
this.state = {
...this.state,
fitContent: {width: true, height: true},
2025-03-26 15:08:25 +05:30
widgetName: "Frame",
attrs: {
...this.state.attrs,
padding: {
label: "padding",
padX: {
label: "Pad X",
tool: Tools.NUMBER_INPUT,
toolProps: {min: 0, max: 140},
value: null,
onChange: (value) => {
// this.setWidgetInnerStyle("paddingLeft", `${value}px`)
// this.setWidgetInnerStyle("paddingRight", `${value}px`)
// const widgetStyle = {
// }
this.setState((prevState) => ({
widgetInnerStyling: {
...prevState.widgetInnerStyling,
paddingLeft: `${value}px`,
paddingRight: `${value}px`
}
}))
this.setAttrValue("padding.padX", value)
}
},
padY: {
label: "Pad Y",
tool: Tools.NUMBER_INPUT,
toolProps: {min: 0, max: 140},
value: null,
onChange: (value) => {
this.setState((prevState) => ({
widgetInnerStyling: {
...prevState.widgetInnerStyling,
paddingTop: `${value}px`,
paddingBottom: `${value}px`
}
}))
// this.setState({
// widgetInnerStyling: widgetStyle
// })
this.setAttrValue("padding.padY", value)
2025-03-26 15:08:25 +05:30
}
},
},
margin: {
label: "Margin",
marginX: {
label: "Margin X",
tool: Tools.NUMBER_INPUT,
toolProps: {min: 0, max: 140},
value: null,
onChange: (value) => {
this.updateState((prev) => ({
widgetOuterStyling: {
...prev.widgetOuterStyling,
marginLeft: `${value}px`,
marginRight: `${value}px`
},
}))
this.setAttrValue("margin.marginX", value)
}
},
marginY: {
label: "Margin Y",
tool: Tools.NUMBER_INPUT,
toolProps: {min: 0, max: 140},
value: null,
onChange: (value) => {
this.updateState((prev) => ({
widgetOuterStyling: {
...prev.widgetOuterStyling,
marginTop: `${value}px`,
marginBottom: `${value}px`
},
}))
this.setAttrValue("margin.marginY", value)
}
},
},
}
2024-09-27 16:04:03 +05:30
}
}
componentDidMount(){
super.componentDidMount()
this.setAttrValue("styling.backgroundColor", "#EDECEC")
2024-09-26 23:16:43 +05:30
}
2024-09-22 22:24:35 +05:30
getConfigCode(){
const bg = this.getAttrValue("styling.backgroundColor")
const fitWidth = this.state.fitContent.width
const fitHeight = this.state.fitContent.height
const {width, height} = this.getSize()
const {layout} = this.getParentLayout()
const config = {
bg: `"${bg}"`
}
if (layout !== Layouts.PLACE){
if (!fitWidth){
config['width'] = width
}
if (!fitHeight){
config['height'] = height
}
}
return config
}
2024-09-26 23:16:43 +05:30
generateCode(variableName, parent){
2024-09-22 22:24:35 +05:30
const config = convertObjectToKeyValueString(this.getConfigCode())
2024-09-26 23:16:43 +05:30
return [
`${variableName} = tk.Frame(master=${parent})`,
`${variableName}.config(${config})`,
2025-03-24 15:50:46 +05:30
`${variableName}.${this.getLayoutCode()}`,
...this.getGridLayoutConfigurationCode(variableName)
2024-09-26 23:16:43 +05:30
]
2024-09-22 22:24:35 +05:30
}
2025-03-30 05:23:18 +05:30
getToolbarAttrs(){
const {layout, ...toolBarAttrs} = super.getToolbarAttrs()
// places layout at the end
return ({
id: this.__id,
...toolBarAttrs,
padding: this.state.attrs.padding,
margin: this.state.attrs.margin,
layout
})
}
2024-09-22 22:24:35 +05:30
renderContent(){
// console.log("bounding rect: ", this.getBoundingRect())
2024-09-25 17:27:12 +05:30
// console.log("widget styling: ", this.state.widgetInnerStyling)
2024-09-22 22:24:35 +05:30
return (
<div className="tw-w-flex tw-flex-col tw-w-full tw-h-full tw-relative tw-rounded-md tw-overflow-hidden">
2025-03-22 11:11:19 +05:30
<div className="tw-p-2 tw-w-full tw-h-full tw-content-start"
ref={this.styleAreaRef}
style={this.getInnerRenderStyling()}>
{this.renderTkinterLayout()} {/*This is required for pack layouts, so if your widget accepts child widgets, ensure to add this */}
2024-09-22 22:24:35 +05:30
</div>
</div>
)
}
}
export default Frame