Repository: godotengine/godot
File: editor/plugins/sprite_2d_editor_plugin.cpp (C++)

Code before:
	// Top HBoxContainer definition
	top_hb = memnew(HBoxContainer);

	CanvasItemEditor::get_singleton()->add_control_to_menu_panel(top_hb);

	// Options definition
	options = memnew(MenuButton);

	top_hb->add_child(options);

	options->set_text(TTR("Sprite2D"));
	options->set_flat(false);
	options->set_theme_type_variation("FlatMenuButton");

	options->get_popup()->add_item(TTR("Convert to MeshInstance2D"), MENU_OPTION_CONVERT_TO_MESH_2D);
	options->get_popup()->add_item(TTR("Convert to Polygon2D"), MENU_OPTION_CONVERT_TO_POLYGON_2D);
	options->get_popup()->add_item(TTR("Create CollisionPolygon2D Sibling"), MENU_OPTION_CREATE_COLLISION_POLY_2D);
	options->get_popup()->add_item(TTR("Create LightOccluder2D Sibling"), MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D);
	options->set_switch_on_hover(true);

	options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Sprite2DEditor::_menu_option));

	// Resize region rect definition
	resize_region_rect = memnew(Button);

	top_hb->add_child(resize_region_rect);

	resize_region_rect->set_theme_type_variation("FlatMenuButton");
	resize_region_rect->set_toggle_mode(true);

	Ref<Shortcut> shortcut = ED_SHORTCUT("canvas_item_editor/resize_region_rect", "Dragging to Resize Region Rect", Key::R, true);
	Ref<InputEventKey> key = shortcut->get_events()[0];

	if (key.is_valid()) {
		key->set_ctrl_pressed(true);
	}

	resize_region_rect->set_shortcut(shortcut);

	resize_region_rect->connect(SceneStringName(pressed), callable_mp(this, &Sprite2DEditor::_sync_sprite_resize_mode));

	// Other elements definition
	err_dialog = memnew(AcceptDialog);
	add_child(err_dialog);

	debug_uv_dialog = memnew(ConfirmationDialog);
	debug_uv_dialog->set_size(Size2(960, 540) * EDSCALE);
	VBoxContainer *vb = memnew(VBoxContainer);
	debug_uv_dialog->add_child(vb);
	debug_uv = memnew(Panel);
	debug_uv->connect(SceneStringName(gui_input), callable_mp(this, &Sprite2DEditor::_debug_uv_input));

Review comment (suggestion):
Add child _after_ doing setup. This avoids unnecessary notifications and callbacks when node is being setup while in tree.

Code after:

	options->set_text(TTR("Sprite2D"));
	options->set_flat(false);
	options->set_theme_type_variation("FlatMenuButton");

	options->get_popup()->add_item(TTR("Convert to MeshInstance2D"), MENU_OPTION_CONVERT_TO_MESH_2D);
	options->get_popup()->add_item(TTR("Convert to Polygon2D"), MENU_OPTION_CONVERT_TO_POLYGON_2D);
	options->get_popup()->add_item(TTR("Create CollisionPolygon2D Sibling"), MENU_OPTION_CREATE_COLLISION_POLY_2D);
	options->get_popup()->add_item(TTR("Create LightOccluder2D Sibling"), MENU_OPTION_CREATE_LIGHT_OCCLUDER_2D);
	options->set_switch_on_hover(true);

	options->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &Sprite2DEditor::_menu_option));

	// Resize region rect definition
	resize_region_rect = memnew(Button);

	resize_region_rect->set_theme_type_variation("FlatMenuButton");
	resize_region_rect->set_toggle_mode(true);
	resize_region_rect->set_shortcut(ED_SHORTCUT("canvas_item_editor/resize_region_rect", TTRC("Drag to Resize Region Rect"), KeyModifierMask::CMD_OR_CTRL | Key::R));

	resize_region_rect->connect(SceneStringName(pressed), callable_mp(this, &Sprite2DEditor::_sync_sprite_resize_mode));

	top_hb->add_child(resize_region_rect);

	// Other elements definition
	err_dialog = memnew(AcceptDialog);
	add_child(err_dialog);

	debug_uv_dialog = memnew(ConfirmationDialog);
	debug_uv_dialog->set_size(Size2(960, 540) * EDSCALE);
	VBoxContainer *vb = memnew(VBoxContainer);
	debug_uv_dialog->add_child(vb);
	debug_uv = memnew(Panel);
	debug_uv->connect(SceneStringName(gui_input), callable_mp(this, &Sprite2DEditor::_debug_uv_input));
	debug_uv->connect(SceneStringName(draw), callable_mp(this, &Sprite2DEditor::_debug_uv_draw));
	debug_uv->set_clip_contents(true);
	vb->add_margin_child(TTR("Preview:"), debug_uv, true);

	panner.instantiate();
	panner->set_callbacks(callable_mp(this, &Sprite2DEditor::_pan_callback), callable_mp(this, &Sprite2DEditor::_zoom_callback));

	zoom_widget = memnew(EditorZoomWidget);
	debug_uv->add_child(zoom_widget);
	zoom_widget->set_anchors_and_offsets_preset(Control::PRESET_TOP_LEFT, Control::PRESET_MODE_MINSIZE, 2 * EDSCALE);
	zoom_widget->connect("zoom_changed", callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(true));
	zoom_widget->set_shortcut_context(nullptr);

	v_scroll = memnew(VScrollBar);
	debug_uv->add_child(v_scroll);
	v_scroll->connect(SceneStringName(value_changed), callable_mp(this, &Sprite2DEditor::_update_zoom_and_pan).unbind(1).bind(false));
	h_scroll = memnew(HScrollBar);