| 163 | } |
| 164 | |
| 165 | ScriptObject* add(const std::string& type, const std::string& id) { |
| 166 | auto dialog = this->dialog(); |
| 167 | if (!dialog) |
| 168 | return nullptr; |
| 169 | |
| 170 | if (type.empty() || get(id)) |
| 171 | return nullptr; |
| 172 | |
| 173 | auto cleanType = base::string_to_lower(type); // "lAbEl" -> "label" |
| 174 | auto unprefixedType = cleanType; |
| 175 | cleanType[0] = toupper(cleanType[0]); // "label" -> "Label" |
| 176 | cleanType += "WidgetScriptObject"; // "Label" -> "LabelWidgetScriptObject" |
| 177 | |
| 178 | auto sobj = getEngine()->create(cleanType); |
| 179 | if (!sobj) { |
| 180 | return nullptr; |
| 181 | } |
| 182 | |
| 183 | auto widget = sobj->handle<ui::Widget>(); |
| 184 | if (!widget) |
| 185 | return nullptr; |
| 186 | |
| 187 | dialog->add(widget); |
| 188 | |
| 189 | auto cleanId = !id.empty() ? id : unprefixedType + std::to_string(m_nextWidgetId++); |
| 190 | sobj->set("id", cleanId); |
| 191 | |
| 192 | return sobj; |
| 193 | } |
| 194 | |
| 195 | uint32_t m_nextWidgetId = 0; |
| 196 | }; |