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();
This commit is contained in:
Linus Groh 2020-08-15 16:32:11 +02:00 committed by Andreas Kling
parent 5f724b6ca1
commit 0cab3bca2f
Notes: sideshowbarker 2024-07-19 03:36:36 +09:00
6 changed files with 18 additions and 16 deletions

View file

@ -35,7 +35,6 @@
#include <LibGUI/Application.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/Desktop.h>
#include <LibGUI/ImageWidget.h>
#include <LibGUI/Label.h>
#include <LibGUI/MessageBox.h>
@ -156,10 +155,8 @@ int main(int argc, char** argv)
auto window = GUI::Window::construct();
window->set_title("Welcome");
Gfx::IntRect window_rect { 0, 0, 640, 360 };
window_rect.center_within(GUI::Desktop::the().rect());
window->set_resizable(true);
window->set_rect(window_rect);
window->resize(640, 360);
window->center_on_screen();
auto& background = window->set_main_widget<BackgroundWidget>();
background.set_fill_with_background_color(false);

View file

@ -137,9 +137,8 @@ static bool prompt_to_stop_profiling()
auto window = GUI::Window::construct();
window->set_title("Profiling");
window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-profiler.png"));
Gfx::IntRect window_rect { 0, 0, 320, 200 };
window_rect.center_within(GUI::Desktop::the().rect());
window->set_rect(window_rect);
window->resize(320, 200);
window->center_on_screen();
auto& widget = window->set_main_widget<GUI::Widget>();
widget.set_fill_with_background_color(true);
widget.set_layout<GUI::VerticalBoxLayout>();

View file

@ -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);

View file

@ -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;

View file

@ -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;

View file

@ -29,7 +29,6 @@
#include <AK/Vector.h>
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Button.h>
#include <LibGUI/Desktop.h>
#include <LibGUI/Label.h>
#include <LibGUI/RadioButton.h>
#include <LibGUI/Widget.h>
@ -62,9 +61,8 @@ Vector<char const*> ShutdownDialog::show()
ShutdownDialog::ShutdownDialog()
: Dialog(nullptr)
{
Gfx::IntRect rect({ 0, 0, 180, 180 + ((static_cast<int>(options.size()) - 3) * 16) });
rect.center_within(GUI::Desktop::the().rect());
set_rect(rect);
resize(180, 180 + ((static_cast<int>(options.size()) - 3) * 16));
center_on_screen();
set_resizable(false);
set_title("SerenityOS");
set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png"));