diff options
author | Linus Groh <mail@linusgroh.de> | 2020-08-15 16:32:11 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-08-15 17:38:19 +0200 |
commit | 0cab3bca2fb743f6bdc1e138b9f084cfcb42c7e4 (patch) | |
tree | a2907227d8887b61157aee98914b62eb4302110e /Libraries | |
parent | 5f724b6ca1aae3a5a8c7189069649e8a9347cca2 (diff) | |
download | serenity-0cab3bca2fb743f6bdc1e138b9f084cfcb42c7e4.zip |
LibGUI: Add and use Window::center_on_screen()
Various applications were using the same slightly verbose code to center
themselves on the screen/desktop:
Gfx::IntRect window_rect { 0, 0, width, height };
window_rect.center_within(GUI::Desktop::the().rect());
window->set_rect(window_rect);
Which now becomes:
window->resize(width, height);
window->center_on_screen();
Diffstat (limited to 'Libraries')
-rw-r--r-- | Libraries/LibGUI/ProcessChooser.cpp | 6 | ||||
-rw-r--r-- | Libraries/LibGUI/Window.cpp | 8 | ||||
-rw-r--r-- | Libraries/LibGUI/Window.h | 2 |
3 files changed, 12 insertions, 4 deletions
diff --git a/Libraries/LibGUI/ProcessChooser.cpp b/Libraries/LibGUI/ProcessChooser.cpp index 413b3b9888..ad47f4b293 100644 --- a/Libraries/LibGUI/ProcessChooser.cpp +++ b/Libraries/LibGUI/ProcessChooser.cpp @@ -26,7 +26,6 @@ #include <LibGUI/BoxLayout.h> #include <LibGUI/Button.h> -#include <LibGUI/Desktop.h> #include <LibGUI/MessageBox.h> #include <LibGUI/ProcessChooser.h> #include <LibGUI/RunningProcessesModel.h> @@ -48,9 +47,8 @@ ProcessChooser::ProcessChooser(const StringView& window_title, const StringView& else if (parent_window) set_icon(parent_window->icon()); - Gfx::IntRect window_rect { 0, 0, 300, 340 }; - window_rect.center_within(GUI::Desktop::the().rect()); - set_rect(window_rect); + resize(300, 340); + center_on_screen(); auto& widget = set_main_widget<GUI::Widget>(); widget.set_fill_with_background_color(true); diff --git a/Libraries/LibGUI/Window.cpp b/Libraries/LibGUI/Window.cpp index a58b04d898..de1206b918 100644 --- a/Libraries/LibGUI/Window.cpp +++ b/Libraries/LibGUI/Window.cpp @@ -32,6 +32,7 @@ #include <LibCore/MimeData.h> #include <LibGUI/Action.h> #include <LibGUI/Application.h> +#include <LibGUI/Desktop.h> #include <LibGUI/Event.h> #include <LibGUI/Painter.h> #include <LibGUI/Widget.h> @@ -212,6 +213,13 @@ void Window::set_rect(const Gfx::IntRect& a_rect) m_main_widget->resize(window_rect.size()); } +void Window::center_on_screen() +{ + auto window_rect = rect(); + window_rect.center_within(Desktop::the().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 3284a15644..4d8f96e4ce 100644 --- a/Libraries/LibGUI/Window.h +++ b/Libraries/LibGUI/Window.h @@ -123,6 +123,8 @@ public: void resize(int width, int height) { resize({ width, height }); } void resize(const Gfx::IntSize& size) { set_rect({ position(), size }); } + void center_on_screen(); + virtual void event(Core::Event&) override; bool is_visible() const; |