Skip to content

Commit

Permalink
Apply clamp_to_embedder on parent resize and popup.
Browse files Browse the repository at this point in the history
Fixes #75084.

The clamp_to_embedder setting was added in 8be16e0,
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.
  • Loading branch information
rcorre committed Mar 27, 2023
1 parent 92bee43 commit 894ce41
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 18 deletions.
3 changes: 2 additions & 1 deletion editor/create_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -805,4 +805,5 @@ CreateDialog::CreateDialog() {

register_text_enter(search_box);
set_hide_on_ok(false);
set_clamp_to_embedder(true);
}
1 change: 1 addition & 0 deletions editor/editor_help_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"));

Expand Down
1 change: 1 addition & 0 deletions editor/editor_settings_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions editor/export/project_export.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
1 change: 1 addition & 0 deletions editor/project_settings_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
29 changes: 12 additions & 17 deletions scene/main/viewport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down
27 changes: 27 additions & 0 deletions scene/main/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
1 change: 1 addition & 0 deletions scene/main/window.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ class Window : public Viewport {
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;

Expand Down

0 comments on commit 894ce41

Please sign in to comment.