From 04e3bcfa75deaa0dc6e55241b8d8142440af5e10 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Wed, 30 Dec 2020 13:41:53 +0100 Subject: [PATCH] LibGUI: Add Window::center_within(Window) This allows you to easily center one window within another window. --- Libraries/LibGUI/Window.cpp | 9 +++++++++ Libraries/LibGUI/Window.h | 1 + 2 files changed, 10 insertions(+) diff --git a/Libraries/LibGUI/Window.cpp b/Libraries/LibGUI/Window.cpp index 351a68a8eb..7fcd0ee14b 100644 --- a/Libraries/LibGUI/Window.cpp +++ b/Libraries/LibGUI/Window.cpp @@ -239,6 +239,15 @@ void Window::center_on_screen() set_rect(window_rect); } +void Window::center_within(const Window& other) +{ + if (this == &other) + return; + auto window_rect = rect(); + window_rect.center_within(other.rect()); + set_rect(window_rect); +} + void Window::set_window_type(WindowType window_type) { m_window_type = window_type; diff --git a/Libraries/LibGUI/Window.h b/Libraries/LibGUI/Window.h index f4298c6986..b13933c268 100644 --- a/Libraries/LibGUI/Window.h +++ b/Libraries/LibGUI/Window.h @@ -108,6 +108,7 @@ public: void resize(const Gfx::IntSize& size) { set_rect({ position(), size }); } void center_on_screen(); + void center_within(const Window&); virtual void event(Core::Event&) override;