diff --git a/Userland/Applications/DisplaySettings/MonitorWidget.cpp b/Userland/Applications/DisplaySettings/MonitorWidget.cpp index db25078f95..506caf0b6c 100644 --- a/Userland/Applications/DisplaySettings/MonitorWidget.cpp +++ b/Userland/Applications/DisplaySettings/MonitorWidget.cpp @@ -10,6 +10,7 @@ #include #include #include +#include REGISTER_WIDGET(DisplaySettings, MonitorWidget) @@ -26,23 +27,35 @@ MonitorWidget::MonitorWidget() bool MonitorWidget::set_wallpaper(String path) { - if (path == m_desktop_wallpaper_path) + if (!is_different_to_current_wallpaper_path(path)) return false; - if (path.is_empty()) { - m_wallpaper_bitmap = nullptr; + Threading::BackgroundAction>::create( + [path](auto&) { + RefPtr bmp; + if (!path.is_empty()) + bmp = Gfx::Bitmap::try_load_from_file(path); + return bmp; + }, + + [this, path](RefPtr bitmap) { + // If we've been requested to change while we were loading the bitmap, don't bother spending the cost to + // move and render the now stale bitmap. + if (is_different_to_current_wallpaper_path(path)) + return; + if (!bitmap.is_null()) + m_wallpaper_bitmap = move(bitmap); + else + m_wallpaper_bitmap = nullptr; + m_desktop_dirty = true; + update(); + }); + + if (path.is_empty()) m_desktop_wallpaper_path = nullptr; - m_desktop_dirty = true; - update(); - return false; - } + else + m_desktop_wallpaper_path = move(path); - auto bitmap = Gfx::Bitmap::try_load_from_file(path); - if (bitmap) - m_wallpaper_bitmap = move(bitmap); - m_desktop_wallpaper_path = move(path); - m_desktop_dirty = true; - update(); return true; } diff --git a/Userland/Applications/DisplaySettings/MonitorWidget.h b/Userland/Applications/DisplaySettings/MonitorWidget.h index 272eaa5156..154e87d8b6 100644 --- a/Userland/Applications/DisplaySettings/MonitorWidget.h +++ b/Userland/Applications/DisplaySettings/MonitorWidget.h @@ -47,6 +47,11 @@ private: Gfx::IntSize m_desktop_resolution; int m_desktop_scale_factor { 1 }; Gfx::Color m_desktop_color; + + bool is_different_to_current_wallpaper_path(String const& path) + { + return (!path.is_empty() && path != m_desktop_wallpaper_path) || (path.is_empty() && m_desktop_wallpaper_path != nullptr); + } }; }