2024-09-23 18:25:40 +05:30
import Widget from "../../../canvas/widgets/base"
import Tools from "../../../canvas/constants/tools"
2024-09-27 19:22:33 +05:30
import { convertObjectToKeyValueString , removeKeyFromObject } from "../../../utils/common"
import { TkinterBase , TkinterWidgetBase } from "./base"
2024-09-23 18:25:40 +05:30
2024-09-27 19:22:33 +05:30
class Slider extends TkinterWidgetBase {
2024-09-23 18:25:40 +05:30
static widgetType = "scale"
2025-03-11 07:23:17 +05:30
static displayName = "Scale"
2024-09-30 15:30:46 +05:30
// FIXME: You provided a `value` prop to a form field without an `onChange` handler. This will render a read-only field. If the field should be mutable use
2024-09-23 18:25:40 +05:30
constructor ( props ) {
super ( props )
this . state = {
... this . state ,
2024-09-27 16:04:03 +05:30
widgetName : "Scale" ,
2024-09-30 22:18:08 +05:30
size : { width : 120 , height : 10 } ,
fitContent : { width : true , height : true } ,
2024-09-23 18:25:40 +05:30
attrs : {
2024-09-27 19:22:33 +05:30
... this . state . attrs ,
2024-09-23 18:25:40 +05:30
styling : {
2024-09-27 19:22:33 +05:30
... this . state . attrs . styling ,
// TODO: trough color
troughColor : {
label : "Trough Color" ,
tool : Tools . COLOR _PICKER ,
value : "#fff" ,
2024-09-23 18:25:40 +05:30
onChange : ( value ) => {
2024-09-27 19:22:33 +05:30
// this.setWidgetInnerStyle("color", value)
this . setAttrValue ( "styling.troughColor" , value )
2024-09-23 18:25:40 +05:30
}
}
} ,
scale : {
label : "Scale" ,
display : "horizontal" ,
min : {
label : "Min" ,
tool : Tools . NUMBER _INPUT , // the tool to display, can be either HTML ELement or a constant string
toolProps : { placeholder : "min" } ,
value : 0 ,
onChange : ( value ) => this . setAttrValue ( "scale.min" , value )
} ,
max : {
label : "Max" ,
tool : Tools . NUMBER _INPUT ,
toolProps : { placeholder : "max" } ,
value : 100 ,
onChange : ( value ) => this . setAttrValue ( "scale.max" , value )
} ,
step : {
label : "Step" ,
tool : Tools . NUMBER _INPUT ,
toolProps : { placeholder : "max" , stringMode : true , step : "0.1" } ,
value : 1 ,
onChange : ( value ) => this . setAttrValue ( "scale.step" , value )
2024-09-23 19:31:30 +05:30
} ,
default : {
label : "Default" ,
tool : Tools . NUMBER _INPUT ,
toolProps : { placeholder : "max" , stringMode : true , step : "0.1" } ,
value : 0 ,
onChange : ( value ) => this . setAttrValue ( "scale.default" , value )
2024-09-23 18:25:40 +05:30
}
} ,
2024-09-27 23:27:07 +05:30
orientation : {
label : "Orientation" ,
tool : Tools . SELECT _DROPDOWN ,
toolProps : { placeholder : "select orientation" } ,
value : "" ,
options : [ { value : "horizontal" , label : "horizontal" } , { value : "vertical" , label : "vertical" } ] ,
onChange : ( value ) => {
// const widgetStyling = {
// transformOrigin: "0 0",
// transform: value === "horizontal" ? "rotate(0deg)" : "rotate(90deg)"
// }
// this.setState((prev) => ({
// widgetOuterStyling: {...prev, ...widgetStyling}
// }))
// this.setWidgetOuterStyle("transform-origin", "0 0")
// this.setWidgetOuterStyle("transform", value === "horizontal" ? "rotate(0deg)" : "rotate(90deg)")
this . setAttrValue ( "orientation" , value )
}
} ,
2024-09-23 18:25:40 +05:30
}
}
}
componentDidMount ( ) {
super . componentDidMount ( )
this . setAttrValue ( "styling.backgroundColor" , "#fff" )
}
2024-09-27 19:22:33 +05:30
generateCode ( variableName , parent ) {
// TODO: add orientation
const config = this . getConfigCode ( )
2024-09-30 15:30:46 +05:30
config [ "from_" ] = this . getAttrValue ( "scale.min" )
2024-09-27 19:22:33 +05:30
config [ "to" ] = this . getAttrValue ( "scale.max" )
config [ "resolution" ] = this . getAttrValue ( "scale.step" )
2024-09-27 23:27:07 +05:30
if ( this . getAttrValue ( "orientation" ) ) {
config [ "orientation" ] = this . getAttrValue ( "orientation" )
}
2024-09-30 15:30:46 +05:30
const defaultValue = this . getAttrValue ( "scale.default" )
2024-09-27 19:22:33 +05:30
return [
2024-09-30 15:30:46 +05:30
` ${ variableName } _var = tk.DoubleVar(value= ${ defaultValue } ) ` ,
2024-09-27 19:22:33 +05:30
` ${ variableName } = tk.Scale(master= ${ parent } , variable= ${ variableName } _var) ` ,
2024-09-30 15:30:46 +05:30
` ${ variableName } .config( ${ convertObjectToKeyValueString ( config ) } ) ` ,
2024-09-27 19:22:33 +05:30
` ${ variableName } . ${ this . getLayoutCode ( ) } `
]
}
2024-09-23 18:25:40 +05:30
getToolbarAttrs ( ) {
const toolBarAttrs = super . getToolbarAttrs ( )
return ( {
id : this . _ _id ,
widgetName : toolBarAttrs . widgetName ,
placeHolder : this . state . attrs . placeHolder ,
size : toolBarAttrs . size ,
... this . state . attrs ,
} )
}
renderContent ( ) {
return (
< div className = "tw-w-flex tw-flex-col tw-w-full tw-h-full tw-rounded-md tw-overflow-hidden" >
2024-09-27 23:27:07 +05:30
< div className = " flex flex - col items - center justify - center h - screen
2025-03-22 11:11:19 +05:30
bg - gray - 100 "
ref = { this . styleAreaRef }
style = { this . getInnerRenderStyling ( ) } >
2024-09-23 19:31:30 +05:30
< div className = "w-full max-w-md" >
< input
type = "range"
min = { this . getAttrValue ( "scale.min" ) }
max = { this . getAttrValue ( "scale.max" ) }
step = { this . getAttrValue ( "scale.step" ) }
value = { this . getAttrValue ( "scale.default" ) }
2024-09-27 23:27:07 +05:30
style = { { backgroundColor : this . getAttrValue ( "styling.troughColor" ) } }
2024-09-23 19:31:30 +05:30
className = "tw-pointer-events-none w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer accent-blue-500"
/ >
2024-09-23 18:25:40 +05:30
< / d i v >
< / d i v >
< / d i v >
)
}
}
export default Slider