mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 15:12:45 +00:00 
			
		
		
		
	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:
		
							parent
							
								
									5f724b6ca1
								
							
						
					
					
						commit
						0cab3bca2f
					
				
					 6 changed files with 18 additions and 16 deletions
				
			
		|  | @ -35,7 +35,6 @@ | ||||||
| #include <LibGUI/Application.h> | #include <LibGUI/Application.h> | ||||||
| #include <LibGUI/BoxLayout.h> | #include <LibGUI/BoxLayout.h> | ||||||
| #include <LibGUI/Button.h> | #include <LibGUI/Button.h> | ||||||
| #include <LibGUI/Desktop.h> |  | ||||||
| #include <LibGUI/ImageWidget.h> | #include <LibGUI/ImageWidget.h> | ||||||
| #include <LibGUI/Label.h> | #include <LibGUI/Label.h> | ||||||
| #include <LibGUI/MessageBox.h> | #include <LibGUI/MessageBox.h> | ||||||
|  | @ -156,10 +155,8 @@ int main(int argc, char** argv) | ||||||
| 
 | 
 | ||||||
|     auto window = GUI::Window::construct(); |     auto window = GUI::Window::construct(); | ||||||
|     window->set_title("Welcome"); |     window->set_title("Welcome"); | ||||||
|     Gfx::IntRect window_rect { 0, 0, 640, 360 }; |     window->resize(640, 360); | ||||||
|     window_rect.center_within(GUI::Desktop::the().rect()); |     window->center_on_screen(); | ||||||
|     window->set_resizable(true); |  | ||||||
|     window->set_rect(window_rect); |  | ||||||
| 
 | 
 | ||||||
|     auto& background = window->set_main_widget<BackgroundWidget>(); |     auto& background = window->set_main_widget<BackgroundWidget>(); | ||||||
|     background.set_fill_with_background_color(false); |     background.set_fill_with_background_color(false); | ||||||
|  |  | ||||||
|  | @ -137,9 +137,8 @@ static bool prompt_to_stop_profiling() | ||||||
|     auto window = GUI::Window::construct(); |     auto window = GUI::Window::construct(); | ||||||
|     window->set_title("Profiling"); |     window->set_title("Profiling"); | ||||||
|     window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-profiler.png")); |     window->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-profiler.png")); | ||||||
|     Gfx::IntRect window_rect { 0, 0, 320, 200 }; |     window->resize(320, 200); | ||||||
|     window_rect.center_within(GUI::Desktop::the().rect()); |     window->center_on_screen(); | ||||||
|     window->set_rect(window_rect); |  | ||||||
|     auto& widget = window->set_main_widget<GUI::Widget>(); |     auto& widget = window->set_main_widget<GUI::Widget>(); | ||||||
|     widget.set_fill_with_background_color(true); |     widget.set_fill_with_background_color(true); | ||||||
|     widget.set_layout<GUI::VerticalBoxLayout>(); |     widget.set_layout<GUI::VerticalBoxLayout>(); | ||||||
|  |  | ||||||
|  | @ -26,7 +26,6 @@ | ||||||
| 
 | 
 | ||||||
| #include <LibGUI/BoxLayout.h> | #include <LibGUI/BoxLayout.h> | ||||||
| #include <LibGUI/Button.h> | #include <LibGUI/Button.h> | ||||||
| #include <LibGUI/Desktop.h> |  | ||||||
| #include <LibGUI/MessageBox.h> | #include <LibGUI/MessageBox.h> | ||||||
| #include <LibGUI/ProcessChooser.h> | #include <LibGUI/ProcessChooser.h> | ||||||
| #include <LibGUI/RunningProcessesModel.h> | #include <LibGUI/RunningProcessesModel.h> | ||||||
|  | @ -48,9 +47,8 @@ ProcessChooser::ProcessChooser(const StringView& window_title, const StringView& | ||||||
|     else if (parent_window) |     else if (parent_window) | ||||||
|         set_icon(parent_window->icon()); |         set_icon(parent_window->icon()); | ||||||
| 
 | 
 | ||||||
|     Gfx::IntRect window_rect { 0, 0, 300, 340 }; |     resize(300, 340); | ||||||
|     window_rect.center_within(GUI::Desktop::the().rect()); |     center_on_screen(); | ||||||
|     set_rect(window_rect); |  | ||||||
| 
 | 
 | ||||||
|     auto& widget = set_main_widget<GUI::Widget>(); |     auto& widget = set_main_widget<GUI::Widget>(); | ||||||
|     widget.set_fill_with_background_color(true); |     widget.set_fill_with_background_color(true); | ||||||
|  |  | ||||||
|  | @ -32,6 +32,7 @@ | ||||||
| #include <LibCore/MimeData.h> | #include <LibCore/MimeData.h> | ||||||
| #include <LibGUI/Action.h> | #include <LibGUI/Action.h> | ||||||
| #include <LibGUI/Application.h> | #include <LibGUI/Application.h> | ||||||
|  | #include <LibGUI/Desktop.h> | ||||||
| #include <LibGUI/Event.h> | #include <LibGUI/Event.h> | ||||||
| #include <LibGUI/Painter.h> | #include <LibGUI/Painter.h> | ||||||
| #include <LibGUI/Widget.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()); |         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) | void Window::set_window_type(WindowType window_type) | ||||||
| { | { | ||||||
|     m_window_type = window_type; |     m_window_type = window_type; | ||||||
|  |  | ||||||
|  | @ -123,6 +123,8 @@ public: | ||||||
|     void resize(int width, int height) { resize({ width, height }); } |     void resize(int width, int height) { resize({ width, height }); } | ||||||
|     void resize(const Gfx::IntSize& size) { set_rect({ position(), size }); } |     void resize(const Gfx::IntSize& size) { set_rect({ position(), size }); } | ||||||
| 
 | 
 | ||||||
|  |     void center_on_screen(); | ||||||
|  | 
 | ||||||
|     virtual void event(Core::Event&) override; |     virtual void event(Core::Event&) override; | ||||||
| 
 | 
 | ||||||
|     bool is_visible() const; |     bool is_visible() const; | ||||||
|  |  | ||||||
|  | @ -29,7 +29,6 @@ | ||||||
| #include <AK/Vector.h> | #include <AK/Vector.h> | ||||||
| #include <LibGUI/BoxLayout.h> | #include <LibGUI/BoxLayout.h> | ||||||
| #include <LibGUI/Button.h> | #include <LibGUI/Button.h> | ||||||
| #include <LibGUI/Desktop.h> |  | ||||||
| #include <LibGUI/Label.h> | #include <LibGUI/Label.h> | ||||||
| #include <LibGUI/RadioButton.h> | #include <LibGUI/RadioButton.h> | ||||||
| #include <LibGUI/Widget.h> | #include <LibGUI/Widget.h> | ||||||
|  | @ -62,9 +61,8 @@ Vector<char const*> ShutdownDialog::show() | ||||||
| ShutdownDialog::ShutdownDialog() | ShutdownDialog::ShutdownDialog() | ||||||
|     : Dialog(nullptr) |     : Dialog(nullptr) | ||||||
| { | { | ||||||
|     Gfx::IntRect rect({ 0, 0, 180, 180 + ((static_cast<int>(options.size()) - 3) * 16) }); |     resize(180, 180 + ((static_cast<int>(options.size()) - 3) * 16)); | ||||||
|     rect.center_within(GUI::Desktop::the().rect()); |     center_on_screen(); | ||||||
|     set_rect(rect); |  | ||||||
|     set_resizable(false); |     set_resizable(false); | ||||||
|     set_title("SerenityOS"); |     set_title("SerenityOS"); | ||||||
|     set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png")); |     set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/power.png")); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Linus Groh
						Linus Groh