From 087bd7f767d706af2ac9357410b70bcb6db88b6d Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 30 Aug 2021 22:18:20 +0200 Subject: [PATCH] Userland: Use Rect::centered_within() where useful --- .../Applications/DisplaySettings/MonitorWidget.cpp | 3 +-- .../MouseSettings/DoubleClickArrowWidget.cpp | 4 +--- Userland/Applications/PixelPaint/PaletteWidget.cpp | 3 +-- Userland/Applications/ThemeEditor/PreviewWidget.cpp | 12 ++++-------- Userland/Libraries/LibGUI/FileSystemModel.cpp | 3 +-- Userland/Libraries/LibGUI/IconView.cpp | 3 +-- Userland/Libraries/LibGUI/Window.cpp | 8 ++------ Userland/Services/Taskbar/TaskbarWindow.cpp | 3 +-- Userland/Services/WindowServer/Overlays.cpp | 10 +++------- 9 files changed, 15 insertions(+), 34 deletions(-) diff --git a/Userland/Applications/DisplaySettings/MonitorWidget.cpp b/Userland/Applications/DisplaySettings/MonitorWidget.cpp index 506caf0b6c..5a853ccf79 100644 --- a/Userland/Applications/DisplaySettings/MonitorWidget.cpp +++ b/Userland/Applications/DisplaySettings/MonitorWidget.cpp @@ -126,8 +126,7 @@ void MonitorWidget::redraw_desktop_if_needed() auto scaled_bitmap = m_wallpaper_bitmap->scaled(sw, sh); if (m_desktop_wallpaper_mode == "center") { - Gfx::IntRect centered_rect { {}, scaled_size }; - centered_rect.center_within(m_desktop_bitmap->rect()); + auto centered_rect = Gfx::IntRect({}, scaled_size).centered_within(m_desktop_bitmap->rect()); painter.blit(centered_rect.location(), *scaled_bitmap, scaled_bitmap->rect()); } else if (m_desktop_wallpaper_mode == "tile") { painter.draw_tiled_bitmap(m_desktop_bitmap->rect(), *scaled_bitmap); diff --git a/Userland/Applications/MouseSettings/DoubleClickArrowWidget.cpp b/Userland/Applications/MouseSettings/DoubleClickArrowWidget.cpp index a71b774ea8..deccac60fd 100644 --- a/Userland/Applications/MouseSettings/DoubleClickArrowWidget.cpp +++ b/Userland/Applications/MouseSettings/DoubleClickArrowWidget.cpp @@ -35,9 +35,7 @@ void DoubleClickArrowWidget::paint_event(GUI::PaintEvent& event) GUI::Painter painter(*this); painter.add_clip_rect(event.rect()); - auto bottom_arrow_rect = m_arrow_bitmap->rect(); - bottom_arrow_rect.center_within(rect()); - bottom_arrow_rect.translate_by(0, m_arrow_bitmap->height() / 2); + auto bottom_arrow_rect = m_arrow_bitmap->rect().centered_within(rect()).translated(0, m_arrow_bitmap->height() / 2); painter.blit_filtered(bottom_arrow_rect.location(), *m_arrow_bitmap, m_arrow_bitmap->rect(), [&](Color color) { return m_inverted ? color.inverted() : color; diff --git a/Userland/Applications/PixelPaint/PaletteWidget.cpp b/Userland/Applications/PixelPaint/PaletteWidget.cpp index b06adfd9a0..ad6a64c7bc 100644 --- a/Userland/Applications/PixelPaint/PaletteWidget.cpp +++ b/Userland/Applications/PixelPaint/PaletteWidget.cpp @@ -75,8 +75,7 @@ PaletteWidget::PaletteWidget() m_secondary_color_widget->set_fill_with_background_color(true); m_primary_color_widget = add(); - Gfx::IntRect rect { 0, 0, 38, 15 }; - rect.center_within(m_secondary_color_widget->relative_rect()); + auto rect = Gfx::IntRect(0, 0, 38, 15).centered_within(m_secondary_color_widget->relative_rect()); m_primary_color_widget->set_relative_rect(rect); m_primary_color_widget->set_fill_with_background_color(true); diff --git a/Userland/Applications/ThemeEditor/PreviewWidget.cpp b/Userland/Applications/ThemeEditor/PreviewWidget.cpp index 2825895848..0c2fe956b6 100644 --- a/Userland/Applications/ThemeEditor/PreviewWidget.cpp +++ b/Userland/Applications/ThemeEditor/PreviewWidget.cpp @@ -137,15 +137,13 @@ void PreviewWidget::paint_event(GUI::PaintEvent& event) for (auto& button : buttons) { Gfx::StylePainter::paint_button(painter, button.rect, m_preview_palette, Gfx::ButtonStyle::Normal, false); - auto bitmap_rect = button.bitmap->rect(); - bitmap_rect.center_within(button.rect); + auto bitmap_rect = button.bitmap->rect().centered_within(button.rect); painter.blit(bitmap_rect.location(), *button.bitmap, button.bitmap->rect()); } }; - Gfx::IntRect active_rect { 0, 0, 320, 240 }; - active_rect.center_within(frame_inner_rect()); - Gfx::IntRect inactive_rect = active_rect.translated(-20, -20); + auto active_rect = Gfx::IntRect(0, 0, 320, 240).centered_within(frame_inner_rect()); + auto inactive_rect = active_rect.translated(-20, -20); paint_window("Inactive window", inactive_rect, Gfx::WindowTheme::WindowState::Inactive, *m_active_window_icon); paint_window("Active window", active_rect, Gfx::WindowTheme::WindowState::Active, *m_inactive_window_icon); @@ -153,9 +151,7 @@ void PreviewWidget::paint_event(GUI::PaintEvent& event) void PreviewWidget::resize_event(GUI::ResizeEvent&) { - Gfx::IntRect gallery_rect { 0, 0, 320, 240 }; - gallery_rect.center_within(rect()); - m_gallery->set_relative_rect(gallery_rect); + m_gallery->set_relative_rect(Gfx::IntRect(0, 0, 320, 240).centered_within(rect())); } } diff --git a/Userland/Libraries/LibGUI/FileSystemModel.cpp b/Userland/Libraries/LibGUI/FileSystemModel.cpp index 144fd6ff63..861280583c 100644 --- a/Userland/Libraries/LibGUI/FileSystemModel.cpp +++ b/Userland/Libraries/LibGUI/FileSystemModel.cpp @@ -613,8 +613,7 @@ static RefPtr render_thumbnail(const StringView& path) double scale = min(32 / (double)png_bitmap->width(), 32 / (double)png_bitmap->height()); auto thumbnail = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { 32, 32 }); - Gfx::IntRect destination = Gfx::IntRect(0, 0, (int)(png_bitmap->width() * scale), (int)(png_bitmap->height() * scale)); - destination.center_within(thumbnail->rect()); + auto destination = Gfx::IntRect(0, 0, (int)(png_bitmap->width() * scale), (int)(png_bitmap->height() * scale)).centered_within(thumbnail->rect()); Painter painter(*thumbnail); painter.draw_scaled_bitmap(destination, *png_bitmap, png_bitmap->rect()); diff --git a/Userland/Libraries/LibGUI/IconView.cpp b/Userland/Libraries/LibGUI/IconView.cpp index f4333b2c36..0718515be4 100644 --- a/Userland/Libraries/LibGUI/IconView.cpp +++ b/Userland/Libraries/LibGUI/IconView.cpp @@ -433,8 +433,7 @@ void IconView::did_change_cursor_index(const ModelIndex& old_index, const ModelI void IconView::get_item_rects(int item_index, ItemData& item_data, const Gfx::Font& font) const { auto item_rect = this->item_rect(item_index); - item_data.icon_rect = { 0, 0, 32, 32 }; - item_data.icon_rect.center_within(item_rect); + item_data.icon_rect = Gfx::IntRect(0, 0, 32, 32).centered_within(item_rect); item_data.icon_offset_y = -font.glyph_height() - 6; item_data.icon_rect.translate_by(0, item_data.icon_offset_y); diff --git a/Userland/Libraries/LibGUI/Window.cpp b/Userland/Libraries/LibGUI/Window.cpp index 92d6dd570a..3156e0f04f 100644 --- a/Userland/Libraries/LibGUI/Window.cpp +++ b/Userland/Libraries/LibGUI/Window.cpp @@ -286,18 +286,14 @@ void Window::set_minimum_size(const Gfx::IntSize& size) void Window::center_on_screen() { - auto window_rect = rect(); - window_rect.center_within(Desktop::the().rect()); - set_rect(window_rect); + set_rect(rect().centered_within(Desktop::the().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); + set_rect(rect().centered_within(other.rect())); } void Window::set_window_type(WindowType window_type) diff --git a/Userland/Services/Taskbar/TaskbarWindow.cpp b/Userland/Services/Taskbar/TaskbarWindow.cpp index 431de783e4..38a0417f3a 100644 --- a/Userland/Services/Taskbar/TaskbarWindow.cpp +++ b/Userland/Services/Taskbar/TaskbarWindow.cpp @@ -173,8 +173,7 @@ void TaskbarWindow::update_applet_area() if (!main_widget()) return; main_widget()->do_layout(); - Gfx::IntRect new_rect { {}, m_applet_area_size }; - new_rect.center_within(m_applet_area_container->screen_relative_rect()); + auto new_rect = Gfx::IntRect({}, m_applet_area_size).centered_within(m_applet_area_container->screen_relative_rect()); GUI::WindowManagerServerConnection::the().async_set_applet_area_position(new_rect.location()); } diff --git a/Userland/Services/WindowServer/Overlays.cpp b/Userland/Services/WindowServer/Overlays.cpp index 0450e76f3b..592ea7a167 100644 --- a/Userland/Services/WindowServer/Overlays.cpp +++ b/Userland/Services/WindowServer/Overlays.cpp @@ -237,8 +237,7 @@ void WindowGeometryOverlay::update_rect() } m_label_rect = Gfx::IntRect { 0, 0, wm.font().width(m_label) + 16, wm.font().glyph_height() + 10 }; - auto rect = calculate_frame_rect(m_label_rect); - rect.center_within(window->frame().rect()); + auto rect = calculate_frame_rect(m_label_rect).centered_within(window->frame().rect()); auto desktop_rect = wm.desktop_rect(ScreenInput::the().cursor_location_screen()); if (rect.left() < desktop_rect.left()) rect.set_left(desktop_rect.left()); @@ -314,8 +313,7 @@ RefPtr DndOverlay::create_bitmap(int scale_factor) void WindowStackSwitchOverlay::render_overlay_bitmap(Gfx::Painter& painter) { // We should come up with a more elegant way to get the content rectangle - Gfx::IntRect content_rect({}, m_content_size); - content_rect.center_within({ {}, rect().size() }); + auto content_rect = Gfx::IntRect({}, m_content_size).centered_within({ {}, rect().size() }); auto active_color = WindowManager::the().palette().active_window_border1(); auto inactive_color = WindowManager::the().palette().inactive_window_border1(); for (int y = 0; y < m_rows; y++) { @@ -342,9 +340,7 @@ WindowStackSwitchOverlay::WindowStackSwitchOverlay(Screen& screen, WindowStack& m_columns * (default_screen_rect_width + default_screen_rect_padding) - default_screen_rect_padding, m_rows * (default_screen_rect_height + default_screen_rect_padding) - default_screen_rect_padding, }; - auto rect = calculate_frame_rect(Gfx::IntRect({}, m_content_size).inflated(2 * default_screen_rect_margin, 2 * default_screen_rect_margin)); - rect.center_within(screen.rect()); - set_rect(rect); + set_rect(calculate_frame_rect(Gfx::IntRect({}, m_content_size).inflated(2 * default_screen_rect_margin, 2 * default_screen_rect_margin)).centered_within(screen.rect())); } }