1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 22:28:12 +00:00

Userland: Use Rect::centered_within() where useful

This commit is contained in:
Andreas Kling 2021-08-30 22:18:20 +02:00
parent 43220568b0
commit 087bd7f767
9 changed files with 15 additions and 34 deletions

View file

@ -126,8 +126,7 @@ void MonitorWidget::redraw_desktop_if_needed()
auto scaled_bitmap = m_wallpaper_bitmap->scaled(sw, sh); auto scaled_bitmap = m_wallpaper_bitmap->scaled(sw, sh);
if (m_desktop_wallpaper_mode == "center") { if (m_desktop_wallpaper_mode == "center") {
Gfx::IntRect centered_rect { {}, scaled_size }; auto centered_rect = Gfx::IntRect({}, scaled_size).centered_within(m_desktop_bitmap->rect());
centered_rect.center_within(m_desktop_bitmap->rect());
painter.blit(centered_rect.location(), *scaled_bitmap, scaled_bitmap->rect()); painter.blit(centered_rect.location(), *scaled_bitmap, scaled_bitmap->rect());
} else if (m_desktop_wallpaper_mode == "tile") { } else if (m_desktop_wallpaper_mode == "tile") {
painter.draw_tiled_bitmap(m_desktop_bitmap->rect(), *scaled_bitmap); painter.draw_tiled_bitmap(m_desktop_bitmap->rect(), *scaled_bitmap);

View file

@ -35,9 +35,7 @@ void DoubleClickArrowWidget::paint_event(GUI::PaintEvent& event)
GUI::Painter painter(*this); GUI::Painter painter(*this);
painter.add_clip_rect(event.rect()); painter.add_clip_rect(event.rect());
auto bottom_arrow_rect = m_arrow_bitmap->rect(); auto bottom_arrow_rect = m_arrow_bitmap->rect().centered_within(rect()).translated(0, m_arrow_bitmap->height() / 2);
bottom_arrow_rect.center_within(rect());
bottom_arrow_rect.translate_by(0, m_arrow_bitmap->height() / 2);
painter.blit_filtered(bottom_arrow_rect.location(), *m_arrow_bitmap, m_arrow_bitmap->rect(), [&](Color color) { painter.blit_filtered(bottom_arrow_rect.location(), *m_arrow_bitmap, m_arrow_bitmap->rect(), [&](Color color) {
return m_inverted ? color.inverted() : color; return m_inverted ? color.inverted() : color;

View file

@ -75,8 +75,7 @@ PaletteWidget::PaletteWidget()
m_secondary_color_widget->set_fill_with_background_color(true); m_secondary_color_widget->set_fill_with_background_color(true);
m_primary_color_widget = add<GUI::Frame>(); m_primary_color_widget = add<GUI::Frame>();
Gfx::IntRect rect { 0, 0, 38, 15 }; auto rect = Gfx::IntRect(0, 0, 38, 15).centered_within(m_secondary_color_widget->relative_rect());
rect.center_within(m_secondary_color_widget->relative_rect());
m_primary_color_widget->set_relative_rect(rect); m_primary_color_widget->set_relative_rect(rect);
m_primary_color_widget->set_fill_with_background_color(true); m_primary_color_widget->set_fill_with_background_color(true);

View file

@ -137,15 +137,13 @@ void PreviewWidget::paint_event(GUI::PaintEvent& event)
for (auto& button : buttons) { for (auto& button : buttons) {
Gfx::StylePainter::paint_button(painter, button.rect, m_preview_palette, Gfx::ButtonStyle::Normal, false); Gfx::StylePainter::paint_button(painter, button.rect, m_preview_palette, Gfx::ButtonStyle::Normal, false);
auto bitmap_rect = button.bitmap->rect(); auto bitmap_rect = button.bitmap->rect().centered_within(button.rect);
bitmap_rect.center_within(button.rect);
painter.blit(bitmap_rect.location(), *button.bitmap, button.bitmap->rect()); painter.blit(bitmap_rect.location(), *button.bitmap, button.bitmap->rect());
} }
}; };
Gfx::IntRect active_rect { 0, 0, 320, 240 }; auto active_rect = Gfx::IntRect(0, 0, 320, 240).centered_within(frame_inner_rect());
active_rect.center_within(frame_inner_rect()); auto inactive_rect = active_rect.translated(-20, -20);
Gfx::IntRect inactive_rect = active_rect.translated(-20, -20);
paint_window("Inactive window", inactive_rect, Gfx::WindowTheme::WindowState::Inactive, *m_active_window_icon); 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); 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&) void PreviewWidget::resize_event(GUI::ResizeEvent&)
{ {
Gfx::IntRect gallery_rect { 0, 0, 320, 240 }; m_gallery->set_relative_rect(Gfx::IntRect(0, 0, 320, 240).centered_within(rect()));
gallery_rect.center_within(rect());
m_gallery->set_relative_rect(gallery_rect);
} }
} }

View file

@ -613,8 +613,7 @@ static RefPtr<Gfx::Bitmap> render_thumbnail(const StringView& path)
double scale = min(32 / (double)png_bitmap->width(), 32 / (double)png_bitmap->height()); double scale = min(32 / (double)png_bitmap->width(), 32 / (double)png_bitmap->height());
auto thumbnail = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, { 32, 32 }); 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)); auto destination = Gfx::IntRect(0, 0, (int)(png_bitmap->width() * scale), (int)(png_bitmap->height() * scale)).centered_within(thumbnail->rect());
destination.center_within(thumbnail->rect());
Painter painter(*thumbnail); Painter painter(*thumbnail);
painter.draw_scaled_bitmap(destination, *png_bitmap, png_bitmap->rect()); painter.draw_scaled_bitmap(destination, *png_bitmap, png_bitmap->rect());

View file

@ -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 void IconView::get_item_rects(int item_index, ItemData& item_data, const Gfx::Font& font) const
{ {
auto item_rect = this->item_rect(item_index); auto item_rect = this->item_rect(item_index);
item_data.icon_rect = { 0, 0, 32, 32 }; item_data.icon_rect = Gfx::IntRect(0, 0, 32, 32).centered_within(item_rect);
item_data.icon_rect.center_within(item_rect);
item_data.icon_offset_y = -font.glyph_height() - 6; item_data.icon_offset_y = -font.glyph_height() - 6;
item_data.icon_rect.translate_by(0, item_data.icon_offset_y); item_data.icon_rect.translate_by(0, item_data.icon_offset_y);

View file

@ -286,18 +286,14 @@ void Window::set_minimum_size(const Gfx::IntSize& size)
void Window::center_on_screen() void Window::center_on_screen()
{ {
auto window_rect = rect(); set_rect(rect().centered_within(Desktop::the().rect()));
window_rect.center_within(Desktop::the().rect());
set_rect(window_rect);
} }
void Window::center_within(const Window& other) void Window::center_within(const Window& other)
{ {
if (this == &other) if (this == &other)
return; return;
auto window_rect = rect(); set_rect(rect().centered_within(other.rect()));
window_rect.center_within(other.rect());
set_rect(window_rect);
} }
void Window::set_window_type(WindowType window_type) void Window::set_window_type(WindowType window_type)

View file

@ -173,8 +173,7 @@ void TaskbarWindow::update_applet_area()
if (!main_widget()) if (!main_widget())
return; return;
main_widget()->do_layout(); main_widget()->do_layout();
Gfx::IntRect new_rect { {}, m_applet_area_size }; auto new_rect = Gfx::IntRect({}, m_applet_area_size).centered_within(m_applet_area_container->screen_relative_rect());
new_rect.center_within(m_applet_area_container->screen_relative_rect());
GUI::WindowManagerServerConnection::the().async_set_applet_area_position(new_rect.location()); GUI::WindowManagerServerConnection::the().async_set_applet_area_position(new_rect.location());
} }

View file

@ -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 }; 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); auto rect = calculate_frame_rect(m_label_rect).centered_within(window->frame().rect());
rect.center_within(window->frame().rect());
auto desktop_rect = wm.desktop_rect(ScreenInput::the().cursor_location_screen()); auto desktop_rect = wm.desktop_rect(ScreenInput::the().cursor_location_screen());
if (rect.left() < desktop_rect.left()) if (rect.left() < desktop_rect.left())
rect.set_left(desktop_rect.left()); rect.set_left(desktop_rect.left());
@ -314,8 +313,7 @@ RefPtr<Gfx::Bitmap> DndOverlay::create_bitmap(int scale_factor)
void WindowStackSwitchOverlay::render_overlay_bitmap(Gfx::Painter& painter) void WindowStackSwitchOverlay::render_overlay_bitmap(Gfx::Painter& painter)
{ {
// We should come up with a more elegant way to get the content rectangle // We should come up with a more elegant way to get the content rectangle
Gfx::IntRect content_rect({}, m_content_size); auto content_rect = Gfx::IntRect({}, m_content_size).centered_within({ {}, rect().size() });
content_rect.center_within({ {}, rect().size() });
auto active_color = WindowManager::the().palette().active_window_border1(); auto active_color = WindowManager::the().palette().active_window_border1();
auto inactive_color = WindowManager::the().palette().inactive_window_border1(); auto inactive_color = WindowManager::the().palette().inactive_window_border1();
for (int y = 0; y < m_rows; y++) { 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_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, 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)); 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()));
rect.center_within(screen.rect());
set_rect(rect);
} }
} }