summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorthankyouverycool <66646555+thankyouverycool@users.noreply.github.com>2023-05-13 05:07:09 -0400
committerAndreas Kling <kling@serenityos.org>2023-05-15 12:15:39 +0200
commitf76d24c2ecf97c337b6e08549a5dc2ed541c7625 (patch)
tree13909cb7e4ed2054a26da3274b577d7cb327b523
parentdfe06096c70a11cf23963c69373ffed636a6dd6e (diff)
downloadserenity-f76d24c2ecf97c337b6e08549a5dc2ed541c7625.zip
LibGUI: Simplify Dialog::ScreenPosition and allow Dialogs to opt out
These options were created 2 years ago but the only use cases thus far are for Center and CenterWithinParent, so let's chuck the rest for now. Adds a new DoNotPosition option to opt out of automatic centering and a ScreenPosition setter and getter for configuration before exec()
-rw-r--r--Userland/Libraries/LibGUI/Dialog.cpp79
-rw-r--r--Userland/Libraries/LibGUI/Dialog.h20
2 files changed, 15 insertions, 84 deletions
diff --git a/Userland/Libraries/LibGUI/Dialog.cpp b/Userland/Libraries/LibGUI/Dialog.cpp
index 90147459a7..60a51d3818 100644
--- a/Userland/Libraries/LibGUI/Dialog.cpp
+++ b/Userland/Libraries/LibGUI/Dialog.cpp
@@ -6,7 +6,6 @@
*/
#include <LibCore/EventLoop.h>
-#include <LibGUI/Desktop.h>
#include <LibGUI/Dialog.h>
#include <LibGUI/Event.h>
#include <LibGfx/Palette.h>
@@ -25,83 +24,21 @@ Dialog::ExecResult Dialog::exec()
VERIFY(!m_event_loop);
m_event_loop = make<Core::EventLoop>();
- auto desktop_rect = Desktop::the().rect();
- auto window_rect = rect();
-
- auto top_align = [](Gfx::Rect<int>& rect) { rect.set_y(32); };
- auto bottom_align = [this, desktop_rect](Gfx::Rect<int>& rect) { rect.set_y(desktop_rect.height() - Desktop::the().taskbar_height() - height() - 12); };
-
- auto left_align = [](Gfx::Rect<int>& rect) { rect.set_x(12); };
- auto right_align = [this, desktop_rect](Gfx::Rect<int>& rect) { rect.set_x(desktop_rect.width() - width() - 12); };
-
switch (m_screen_position) {
+ case ScreenPosition::DoNotPosition:
+ break;
case ScreenPosition::CenterWithinParent:
- if (parent() && is<Window>(parent())) {
- auto& parent_window = *static_cast<Window*>(parent());
- if (parent_window.is_visible()) {
- // Check the dialog's position 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;
- }
+ if (auto parent = find_parent_window(); parent && parent->is_visible()) {
+ center_within(*parent);
+ constrain_to_desktop();
+ break;
}
- [[fallthrough]]; // Fall back to `Center` if parent window is invalid or not visible
+ [[fallthrough]];
case ScreenPosition::Center:
- window_rect.center_within(desktop_rect);
- break;
- case ScreenPosition::CenterLeft:
- left_align(window_rect);
- window_rect.center_vertically_within(desktop_rect);
- break;
- case ScreenPosition::CenterRight:
- right_align(window_rect);
- window_rect.center_vertically_within(desktop_rect);
- break;
- case ScreenPosition::TopLeft:
- left_align(window_rect);
- top_align(window_rect);
- break;
- case ScreenPosition::TopCenter:
- window_rect.center_horizontally_within(desktop_rect);
- top_align(window_rect);
- break;
- case ScreenPosition::TopRight:
- right_align(window_rect);
- top_align(window_rect);
- break;
- case ScreenPosition::BottomLeft:
- left_align(window_rect);
- bottom_align(window_rect);
- break;
- case ScreenPosition::BottomCenter:
- window_rect.center_horizontally_within(desktop_rect);
- bottom_align(window_rect);
- break;
- case ScreenPosition::BottomRight:
- right_align(window_rect);
- bottom_align(window_rect);
+ center_on_screen();
break;
}
- set_rect(window_rect);
show();
auto result = m_event_loop->exec();
m_event_loop = nullptr;
diff --git a/Userland/Libraries/LibGUI/Dialog.h b/Userland/Libraries/LibGUI/Dialog.h
index a78b9536a3..396498ceea 100644
--- a/Userland/Libraries/LibGUI/Dialog.h
+++ b/Userland/Libraries/LibGUI/Dialog.h
@@ -22,20 +22,11 @@ public:
Yes = 3,
No = 4,
};
- enum class ScreenPosition {
- CenterWithinParent = 0,
-
- Center = 1,
- CenterLeft = 2,
- CenterRight = 3,
-
- TopLeft = 4,
- TopCenter = 5,
- TopRight = 6,
- BottomLeft = 7,
- BottomCenter = 8,
- BottomRight = 9,
+ enum class ScreenPosition {
+ DoNotPosition,
+ CenterWithinParent,
+ Center,
};
virtual ~Dialog() override = default;
@@ -45,6 +36,9 @@ public:
ExecResult result() const { return m_result; }
void done(ExecResult);
+ ScreenPosition screen_position() const { return m_screen_position; }
+ void set_screen_position(ScreenPosition position) { m_screen_position = position; }
+
virtual void event(Core::Event&) override;
virtual void close() override;