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