Repository: godotengine/godot
File: editor/scene/texture/texture_region_editor_plugin.cpp (C++)

Code before:
						undo_redo->add_undo_method(node_ninepatch, "set_region_rect", rect_prev);
					} else if (res_stylebox.is_valid()) {
						undo_redo->add_do_method(res_stylebox.ptr(), "set_region_rect", res_stylebox->get_region_rect());
						undo_redo->add_undo_method(res_stylebox.ptr(), "set_region_rect", rect_prev);
					} else if (res_atlas_texture.is_valid()) {
						undo_redo->add_do_method(res_atlas_texture.ptr(), "set_region", res_atlas_texture->get_region());
						undo_redo->add_undo_method(res_atlas_texture.ptr(), "set_region", rect_prev);
					}
					drag_index = -1;
				}
				undo_redo->add_do_method(this, "_update_rect");
				undo_redo->add_undo_method(this, "_update_rect");
				undo_redo->add_do_method(texture_overlay, "queue_redraw");
				undo_redo->add_undo_method(texture_overlay, "queue_redraw");
				undo_redo->commit_action();
				drag = false;
				creating = false;
			}

		} else if (drag && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
			cancel_drag = true;
		}
	}

	Ref<InputEventKey> k = p_input;
	if (k.is_valid() && k->is_pressed() && k->get_keycode() == Key::ESCAPE) {
		if (drag) {
			cancel_drag = true;
		} else {
			hide();
		}
	}

	if (cancel_drag) {
		drag = false;
		if (edited_margin >= 0) {
			static Side side[4] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
			if (node_ninepatch) {
				node_ninepatch->set_patch_margin(side[edited_margin], prev_margin);
			}
			if (res_stylebox.is_valid()) {
				res_stylebox->set_texture_margin(side[edited_margin], prev_margin);
			}
			edited_margin = -1;
		} else {
			_apply_rect(rect_prev);
			rect = rect_prev;
			texture_preview->queue_redraw();
			texture_overlay->queue_redraw();
			drag_index = -1;
		}

Review comment (security):
Note that `close_on_escape` uses `ui_cancel`, instead of hard-coding ESCAPE.

Code after:
									undo_redo->add_undo_method(res_atlas_texture.ptr(), "set_region", res_atlas_texture->get_region());
								}

								undo_redo->add_do_method(this, "_update_rect");
								undo_redo->add_undo_method(this, "_update_rect");
								undo_redo->add_do_method(texture_overlay, "queue_redraw");
								undo_redo->add_undo_method(texture_overlay, "queue_redraw");
								undo_redo->commit_action();
								break;
							}
						}
					} else if (edited_margin < 0) {
						// We didn't hit anything and it's not autoslice, which means we try to create a new region.

						if (drag_index == -1) {
							creating = true;
							rect = Rect2(drag_from, Size2());
						}
					}
				}

			} else if (!mb->is_pressed() && drag) {
				_commit_drag();
			}
		} else if (drag && mb->get_button_index() == MouseButton::RIGHT && mb->is_pressed()) {
			cancel_drag = true;
		}
	}

	if (drag && p_input.is_valid() && p_input->is_action_pressed(SNAME("ui_cancel"), false, true)) {
		cancel_drag = true;
	}

	if (cancel_drag) {
		drag = false;
		if (edited_margin >= 0) {
			static Side side[4] = { SIDE_TOP, SIDE_BOTTOM, SIDE_LEFT, SIDE_RIGHT };
			if (node_ninepatch) {
				node_ninepatch->set_patch_margin(side[edited_margin], prev_margin);
			}
			if (res_stylebox.is_valid()) {
				res_stylebox->set_texture_margin(side[edited_margin], prev_margin);
			}
			edited_margin = -1;
		} else {
			_apply_rect(rect_prev);
			rect = rect_prev;
			texture_preview->queue_redraw();
			texture_overlay->queue_redraw();
			drag_index = -1;
		}