Repository: SerenityOS/serenity
File: Userland/Applications/DisplaySettings/MonitorWidget.cpp (C++)

Code before:
    painter.fill_rect(m_desktop_bitmap->rect(), m_desktop_color);

    if (!m_wallpaper_bitmap)
        return;

    float sw = (float)m_desktop_bitmap->width() / (float)m_desktop_resolution.width();
    float sh = (float)m_desktop_bitmap->height() / (float)m_desktop_resolution.height();

    auto scaled_size = m_wallpaper_bitmap->size().to_type<float>().scaled(sw, sh).to_type<int>();
    auto scaled_bitmap_or_error = m_wallpaper_bitmap->scaled(sw, sh);
    if (scaled_bitmap_or_error.is_error()) {
        GUI::MessageBox::show_error(window(), "There was an error updating the desktop preview"sv);
        return;
    }
    auto scaled_bitmap = scaled_bitmap_or_error.release_value();

    if (m_desktop_wallpaper_mode == "Center"sv) {
        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"sv) {
        painter.draw_tiled_bitmap(m_desktop_bitmap->rect(), *scaled_bitmap);
    } else if (m_desktop_wallpaper_mode == "Stretch"sv) {
        painter.draw_scaled_bitmap(m_desktop_bitmap->rect(), *m_wallpaper_bitmap, m_wallpaper_bitmap->rect(), 1.f, Gfx::ScalingMode::BilinearBlend);
    } else if (m_desktop_wallpaper_mode == "Fill"sv) {
        auto wallpaper_rect = m_wallpaper_bitmap->rect();
        auto scale_x = static_cast<float>(m_desktop_bitmap->width()) / wallpaper_rect.width();
        auto scale_y = static_cast<float>(m_desktop_bitmap->height()) / wallpaper_rect.height();
        auto scale = max(scale_x, scale_y);
        auto new_width = static_cast<int>(wallpaper_rect.width() * scale);
        auto new_height = static_cast<int>(wallpaper_rect.height() * scale);
        Gfx::IntRect stretched_rect { (m_desktop_bitmap->width() - new_width) / 2, (m_desktop_bitmap->height() - new_height) / 2, new_width, new_height };
        painter.draw_scaled_bitmap(stretched_rect, *m_wallpaper_bitmap, wallpaper_rect, 1.0f, Gfx::ScalingMode::BilinearBlend);
    } else {
        VERIFY_NOT_REACHED();
    }
}

void MonitorWidget::paint_event(GUI::PaintEvent& event)
{
    redraw_desktop_if_needed();

    GUI::Painter painter(*this);
    painter.add_clip_rect(event.rect());

    painter.blit({ 0, 0 }, *m_monitor_bitmap, m_monitor_bitmap->rect());
    painter.blit(m_monitor_rect.location(), *m_desktop_bitmap, m_desktop_bitmap->rect());
}

}


Review comment (suggestion):
This is the same as `sw` which already exists, right?

Maybe this does the same in a bit shorter:

```
auto new_size = (wallpaper_rect.size().to_type<float>() * max(sw, sh)).to_type<int>();
auto stretched_rect = Gfx::IntRect({}, new_size).centered_within(m_desktop_bitmap->rect());
```

? (not tested)


Code after:
    painter.fill_rect(m_desktop_bitmap->rect(), m_desktop_color);

    if (!m_wallpaper_bitmap)
        return;

    float sw = (float)m_desktop_bitmap->width() / (float)m_desktop_resolution.width();
    float sh = (float)m_desktop_bitmap->height() / (float)m_desktop_resolution.height();

    auto scaled_size = m_wallpaper_bitmap->size().to_type<float>().scaled(sw, sh).to_type<int>();
    auto scaled_bitmap_or_error = m_wallpaper_bitmap->scaled(sw, sh);
    if (scaled_bitmap_or_error.is_error()) {
        GUI::MessageBox::show_error(window(), "There was an error updating the desktop preview"sv);
        return;
    }
    auto scaled_bitmap = scaled_bitmap_or_error.release_value();

    if (m_desktop_wallpaper_mode == "Center"sv) {
        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"sv) {
        painter.draw_tiled_bitmap(m_desktop_bitmap->rect(), *scaled_bitmap);
    } else if (m_desktop_wallpaper_mode == "Stretch"sv) {
        painter.draw_scaled_bitmap(m_desktop_bitmap->rect(), *m_wallpaper_bitmap, m_wallpaper_bitmap->rect(), 1.f, Gfx::ScalingMode::BilinearBlend);
    } else if (m_desktop_wallpaper_mode == "Fill"sv) {
        auto wallpaper_rect = m_wallpaper_bitmap->rect();
        auto scale_x = static_cast<float>(m_desktop_bitmap->width()) / wallpaper_rect.width();
        auto scale_y = static_cast<float>(m_desktop_bitmap->height()) / wallpaper_rect.height();
        auto new_size = (wallpaper_rect.size().to_type<float>() * max(scale_x, scale_y)).to_type<int>();
        auto stretched_rect = Gfx::IntRect({}, new_size).centered_within(m_desktop_bitmap->rect());
        painter.draw_scaled_bitmap(stretched_rect, *m_wallpaper_bitmap, wallpaper_rect, 1.0f, Gfx::ScalingMode::BilinearBlend);
    } else {
        VERIFY_NOT_REACHED();
    }
}

void MonitorWidget::paint_event(GUI::PaintEvent& event)
{
    redraw_desktop_if_needed();

    GUI::Painter painter(*this);
    painter.add_clip_rect(event.rect());

    painter.blit({ 0, 0 }, *m_monitor_bitmap, m_monitor_bitmap->rect());
    painter.blit(m_monitor_rect.location(), *m_desktop_bitmap, m_desktop_bitmap->rect());
}

}