Skip to content

Commit

Permalink
LibGUI: Add and use Window::center_on_screen()
Browse files Browse the repository at this point in the history
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();
  • Loading branch information
linusg authored and awesomekling committed Aug 15, 2020
1 parent 5f724b6 commit 0cab3bc
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 16 deletions.
7 changes: 2 additions & 5 deletions Applications/Welcome/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 2 additions & 3 deletions DevTools/Profiler/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>();
Expand Down
6 changes: 2 additions & 4 deletions Libraries/LibGUI/ProcessChooser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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);
Expand Down
8 changes: 8 additions & 0 deletions Libraries/LibGUI/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions Libraries/LibGUI/Window.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ class Window : public Core::Object {
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;
Expand Down
6 changes: 2 additions & 4 deletions Services/SystemMenu/ShutdownDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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"));
Expand Down

0 comments on commit 0cab3bc

Please sign in to comment.