summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI
diff options
context:
space:
mode:
authorthankyouverycool <66646555+thankyouverycool@users.noreply.github.com>2023-05-13 05:06:46 -0400
committerAndreas Kling <kling@serenityos.org>2023-05-15 12:15:39 +0200
commitdfe06096c70a11cf23963c69373ffed636a6dd6e (patch)
tree3033e54ec267cf3f8f4a4f580b900add5f0f5c71 /Userland/Libraries/LibGUI
parent2d64147f027c3a512f37da9bd92010f2edb00d3f (diff)
downloadserenity-dfe06096c70a11cf23963c69373ffed636a6dd6e.zip
LibGUI: Add Window::constrain_to_desktop() helper
And a center_within(IntRect const&) overload
Diffstat (limited to 'Userland/Libraries/LibGUI')
-rw-r--r--Userland/Libraries/LibGUI/Window.cpp22
-rw-r--r--Userland/Libraries/LibGUI/Window.h3
2 files changed, 25 insertions, 0 deletions
diff --git a/Userland/Libraries/LibGUI/Window.cpp b/Userland/Libraries/LibGUI/Window.cpp
index d28d92db95..3ac4057450 100644
--- a/Userland/Libraries/LibGUI/Window.cpp
+++ b/Userland/Libraries/LibGUI/Window.cpp
@@ -24,6 +24,7 @@
#include <LibGUI/Widget.h>
#include <LibGUI/Window.h>
#include <LibGfx/Bitmap.h>
+#include <LibGfx/Palette.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
@@ -319,6 +320,27 @@ void Window::center_within(Window const& other)
set_rect(rect().centered_within(other.rect()));
}
+void Window::center_within(Gfx::IntRect const& other)
+{
+ set_rect(rect().centered_within(other));
+}
+
+void Window::constrain_to_desktop()
+{
+ auto desktop_rect = Desktop::the().rect().shrunken(0, 0, Desktop::the().taskbar_height(), 0);
+ auto titlebar = Application::the()->palette().window_title_height();
+ auto border = Application::the()->palette().window_border_thickness();
+ auto constexpr margin = 1;
+
+ auto framed_rect = rect().inflated(border + titlebar + margin, border, border, border);
+ if (desktop_rect.contains(framed_rect))
+ return;
+
+ auto constrained = framed_rect.constrained_to(desktop_rect);
+ constrained.shrink(border + titlebar + margin, border, border, border);
+ set_rect(constrained.x(), constrained.y(), rect().width(), rect().height());
+}
+
void Window::set_window_type(WindowType window_type)
{
m_window_type = window_type;
diff --git a/Userland/Libraries/LibGUI/Window.h b/Userland/Libraries/LibGUI/Window.h
index 57f75b2e10..45d771594a 100644
--- a/Userland/Libraries/LibGUI/Window.h
+++ b/Userland/Libraries/LibGUI/Window.h
@@ -129,7 +129,10 @@ public:
void resize(Gfx::IntSize size) { set_rect({ position(), size }); }
void center_on_screen();
+ void constrain_to_desktop();
+
void center_within(Window const&);
+ void center_within(Gfx::IntRect const&);
virtual void event(Core::Event&) override;