MCPcopy Create free account
hub / github.com/PaulleDemon/PyUIBuilder / generateCustomTkCodeList

Function generateCustomTkCodeList

src/frameworks/customtk/engine/code.js:10–83  ·  view source on GitHub ↗
(widgetList = [], widgetRefs = [], parentVariable = null, mainVariable = "", usedVariableNames = new Set())

Source from the content-addressed store, hash-verified

8// FIXME: if the toplevel comes first, before the MainWindow in widgetlist the root may become null
9// Recursive function to generate the code list, imports, requirements, and track mainVariable
10function generateCustomTkCodeList(widgetList = [], widgetRefs = [], parentVariable = null, mainVariable = "", usedVariableNames = new Set()) {
11 let variableMapping = new Map() // Map widget to variable { widgetId: variableName }
12 let imports = new Set([])
13 let requirements = new Set([])
14 let code = []
15
16 for (let widget of widgetList) {
17 const widgetRef = widgetRefs[widget.id].current
18 let varName = widgetRef.getVariableName()
19
20 // Add imports and requirements to sets
21 widgetRef.getImports().forEach(importItem => imports.add(importItem))
22 widgetRef.getRequirements().forEach(requirementItem => requirements.add(requirementItem))
23
24 // Set main variable if the widget is MainWindow
25 if (widget.widgetType === MainWindow) {
26 mainVariable = varName
27 }
28
29 // Ensure unique variable names across recursion
30 let originalVarName = varName
31 let count = 1;
32
33 // Check for uniqueness and update varName
34 while (usedVariableNames.has(varName)) {
35 varName = `${originalVarName}${count}`
36 count++
37 }
38
39 usedVariableNames.add(varName)
40 variableMapping.set(widget.id, varName) // Store the variable name by widget ID
41
42 // Determine the current parent variable from variableNames or fallback to parentVariable
43 let currentParentVariable = parentVariable || (variableMapping.get(widget.id) || null)
44
45 if (widget.widgetType === TopLevel){
46 // for top level set it to the main variable
47 // TODO: the toplevels parent should be determined other ways, suppose the top level has another toplevel
48 currentParentVariable = mainVariable
49 }
50
51 let widgetCode = widgetRef.generateCode(varName, currentParentVariable)
52
53 if (!(widgetCode instanceof Array)) {
54 throw new Error("generateCode() function should return array, each new line should be a new item")
55 }
56
57 // Add \n after every line
58 widgetCode = widgetCode.flatMap((item, index) => index < widgetCode.length - 1 ? [item, "\n"] : [item])
59
60 code.push(...widgetCode)
61 code.push("\n\n")
62
63 // Recursively handle child widgets
64 if (widget.children && widget.children.length > 0) {
65 // Pass down the unique names for children to prevent duplication
66 const childResult = generateCustomTkCodeList(widget.children, widgetRefs, varName, mainVariable, usedVariableNames)
67

Callers 1

generateCustomTkCodeFunction · 0.85

Calls 3

getImportsMethod · 0.45
getRequirementsMethod · 0.45
generateCodeMethod · 0.45

Tested by

no test coverage detected