summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/Dialog.cpp
diff options
context:
space:
mode:
authorMike Akers <mike.akers@gmail.com>2022-06-19 21:50:36 -0400
committerSam Atkins <atkinssj@gmail.com>2022-06-20 16:50:25 +0100
commit27f93a30522b332602474acd652800c35b76015a (patch)
treee5c5e13cc947ffdb63385bee7ca255b134d6e20b /Userland/Libraries/LibGUI/Dialog.cpp
parent9a908748e0369eb20f480cbe809a2e5872fcf736 (diff)
downloadserenity-27f93a30522b332602474acd652800c35b76015a.zip
LibGUI: Prevent CenterWithinParent Dialogs from appearing offscreen
When a dialog is created the position is checked against the Desktop's rect and repositioned to be entirely visible. If the dialog is larger than the desktop's rect it is just centered.
Diffstat (limited to 'Userland/Libraries/LibGUI/Dialog.cpp')
-rw-r--r--Userland/Libraries/LibGUI/Dialog.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/Dialog.cpp b/Userland/Libraries/LibGUI/Dialog.cpp
index f7ebd63b5f..dc99915274 100644
--- a/Userland/Libraries/LibGUI/Dialog.cpp
+++ b/Userland/Libraries/LibGUI/Dialog.cpp
@@ -9,6 +9,7 @@
#include <LibGUI/Desktop.h>
#include <LibGUI/Dialog.h>
#include <LibGUI/Event.h>
+#include <LibGfx/Palette.h>
namespace GUI {
@@ -39,7 +40,27 @@ Dialog::ExecResult Dialog::exec()
if (parent() && is<Window>(parent())) {
auto& parent_window = *static_cast<Window*>(parent());
if (parent_window.is_visible()) {
+ // Check the dialog's positiom against the Desktop's rect and reposition it to be entirely visible.
+ // If the dialog is larger than the desktop's rect just center it.
window_rect.center_within(parent_window.rect());
+ if (window_rect.size().width() < desktop_rect.size().width() && window_rect.size().height() < desktop_rect.size().height()) {
+ auto taskbar_top_y = desktop_rect.bottom() - Desktop::the().taskbar_height();
+ auto palette = GUI::Application::the()->palette();
+ auto border_thickness = palette.window_border_thickness();
+ auto top_border_title_thickness = border_thickness + palette.window_title_height();
+ if (window_rect.top() < top_border_title_thickness) {
+ window_rect.set_y(top_border_title_thickness);
+ }
+ if (window_rect.right() + border_thickness > desktop_rect.right()) {
+ window_rect.translate_by((window_rect.right() + border_thickness - desktop_rect.right()) * -1, 0);
+ }
+ if (window_rect.bottom() + border_thickness > taskbar_top_y) {
+ window_rect.translate_by(0, (window_rect.bottom() + border_thickness - taskbar_top_y) * -1);
+ }
+ if (window_rect.left() - border_thickness < 0) {
+ window_rect.set_x(0 + border_thickness);
+ }
+ }
break;
}
}