Apply clamp_to_embedder on parent resize and popup.

Fixes #75084.

The clamp_to_embedder setting was added in 8be16e0704,
but was not set on any of the in-editor dialogs.

This patch sets `clamp_to_embedder` on editor dialogs so they cannot be dragged out of the frame.
This also modifies `clamp_to_embedder` so a window is clamped to the bounds of an embedder when
it pops up and when the parent is resized.
This commit is contained in:
Ryan Roden-Corrent 2023-03-19 12:26:22 -04:00
parent 92bee43adb
commit 894ce41180
No known key found for this signature in database
GPG key ID: 4E5072F68872BC04
8 changed files with 46 additions and 18 deletions

View file

@ -457,7 +457,7 @@ void CreateDialog::_notification(int p_what) {
search_box->call_deferred(SNAME("grab_focus")); // still not visible
search_box->select_all();
} else {
EditorSettings::get_singleton()->get_project_metadata("dialog_bounds", "create_new_node", Rect2(get_position(), get_size()));
EditorSettings::get_singleton()->set_project_metadata("dialog_bounds", "create_new_node", Rect2(get_position(), get_size()));
}
} break;
@ -805,4 +805,5 @@ CreateDialog::CreateDialog() {
register_text_enter(search_box);
set_hide_on_ok(false);
set_clamp_to_embedder(true);
}

View file

@ -182,6 +182,7 @@ void EditorHelpSearch::popup_dialog(const String &p_term) {
EditorHelpSearch::EditorHelpSearch() {
set_hide_on_ok(false);
set_clamp_to_embedder(true);
set_title(TTR("Search Help"));

View file

@ -698,6 +698,7 @@ void EditorSettingsDialog::_bind_methods() {
EditorSettingsDialog::EditorSettingsDialog() {
set_title(TTR("Editor Settings"));
set_clamp_to_embedder(true);
tabs = memnew(TabContainer);
tabs->set_theme_type_variation("TabContainerOdd");

View file

@ -1078,6 +1078,7 @@ void ProjectExportDialog::_bind_methods() {
ProjectExportDialog::ProjectExportDialog() {
set_title(TTR("Export"));
set_clamp_to_embedder(true);
VBoxContainer *main_vb = memnew(VBoxContainer);
main_vb->connect("theme_changed", callable_mp(this, &ProjectExportDialog::_theme_changed));

View file

@ -582,6 +582,7 @@ void ProjectSettingsEditor::_bind_methods() {
ProjectSettingsEditor::ProjectSettingsEditor(EditorData *p_data) {
singleton = this;
set_title(TTR("Project Settings (project.godot)"));
set_clamp_to_embedder(true);
ps = ProjectSettings::get_singleton();
data = p_data;

View file

@ -875,6 +875,17 @@ void Viewport::_set_size(const Size2i &p_size, const Size2i &p_size_2d_override,
}
emit_signal(SNAME("size_changed"));
Rect2i limit = get_visible_rect();
for (int i = 0; i < gui.sub_windows.size(); ++i) {
Window *sw = gui.sub_windows[i].window;
Rect2i rect = Rect2i(sw->position, sw->size);
Rect2i new_rect = sw->fit_rect_in_parent(rect, limit);
if (new_rect != rect) {
sw->set_position(new_rect.position);
sw->set_size(new_rect.size);
}
}
}
Size2i Viewport::_get_size() const {
@ -2553,23 +2564,7 @@ bool Viewport::_sub_windows_forward_input(const Ref<InputEvent> &p_event) {
Rect2i new_rect(gui.subwindow_drag_pos + diff, gui.subwindow_focused->get_size());
if (gui.subwindow_focused->is_clamped_to_embedder()) {
Size2i limit = get_visible_rect().size;
if (new_rect.position.x + new_rect.size.x > limit.x) {
new_rect.position.x = limit.x - new_rect.size.x;
}
if (new_rect.position.y + new_rect.size.y > limit.y) {
new_rect.position.y = limit.y - new_rect.size.y;
}
if (new_rect.position.x < 0) {
new_rect.position.x = 0;
}
int title_height = gui.subwindow_focused->get_flag(Window::FLAG_BORDERLESS) ? 0 : gui.subwindow_focused->get_theme_constant(SNAME("title_height"));
if (new_rect.position.y < title_height) {
new_rect.position.y = title_height;
}
new_rect = gui.subwindow_focused->fit_rect_in_parent(new_rect, get_visible_rect());
}
gui.subwindow_focused->_rect_changed_callback(new_rect);

View file

@ -1554,11 +1554,38 @@ void Window::popup(const Rect2i &p_screen_rect) {
ERR_PRINT(vformat("Window %d spawned at invalid position: %s.", get_window_id(), position));
set_position((parent_rect.size - size) / 2);
}
if (parent_rect != Rect2i() && is_clamped_to_embedder()) {
Rect2i new_rect = fit_rect_in_parent(Rect2i(position, size), parent_rect);
set_position(new_rect.position);
set_size(new_rect.size);
}
_post_popup();
notification(NOTIFICATION_POST_POPUP);
}
Rect2i Window::fit_rect_in_parent(Rect2i p_rect, const Rect2i &p_parent_rect) const {
Size2i limit = p_parent_rect.size;
if (p_rect.position.x + p_rect.size.x > limit.x) {
p_rect.position.x = limit.x - p_rect.size.x;
}
if (p_rect.position.y + p_rect.size.y > limit.y) {
p_rect.position.y = limit.y - p_rect.size.y;
}
if (p_rect.position.x < 0) {
p_rect.position.x = 0;
}
int title_height = get_flag(Window::FLAG_BORDERLESS) ? 0 : get_theme_constant(SNAME("title_height"));
if (p_rect.position.y < title_height) {
p_rect.position.y = title_height;
}
return p_rect;
}
Size2 Window::get_contents_minimum_size() const {
return _get_contents_minimum_size();
}

View file

@ -310,6 +310,7 @@ public:
void popup_centered(const Size2i &p_minsize = Size2i());
void popup_centered_clamped(const Size2i &p_size = Size2i(), float p_fallback_ratio = 0.75);
Rect2i fit_rect_in_parent(Rect2i p_rect, const Rect2i &p_parent_rect) const;
Size2 get_contents_minimum_size() const;
Size2 get_clamped_minimum_size() const;