1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:47:45 +00:00

DisplaySettings: Lazily load wallpapers

Load the wallpaper in a background action instead of on the main thread.

This reduces the time to first paint, and makes the UI feel more
responsive when clicking on wallpaper thumbnails.

The behavior of the method is changed slightly to return true if it
succesfully "loads" the empty path. This makes the API a little more
consistent, where "true" means "I made changes" and "false" means "I did
not make changes". No call sites currently use the return value, so no
changes are needed to those.
This commit is contained in:
Andrew January 2021-08-17 13:02:29 +01:00 committed by Andreas Kling
parent ac055554f6
commit 1474a537b6
2 changed files with 31 additions and 13 deletions

View file

@ -10,6 +10,7 @@
#include <LibGUI/Painter.h> #include <LibGUI/Painter.h>
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibGfx/Font.h> #include <LibGfx/Font.h>
#include <LibThreading/BackgroundAction.h>
REGISTER_WIDGET(DisplaySettings, MonitorWidget) REGISTER_WIDGET(DisplaySettings, MonitorWidget)
@ -26,23 +27,35 @@ MonitorWidget::MonitorWidget()
bool MonitorWidget::set_wallpaper(String path) bool MonitorWidget::set_wallpaper(String path)
{ {
if (path == m_desktop_wallpaper_path) if (!is_different_to_current_wallpaper_path(path))
return false; return false;
if (path.is_empty()) { Threading::BackgroundAction<RefPtr<Gfx::Bitmap>>::create(
m_wallpaper_bitmap = nullptr; [path](auto&) {
m_desktop_wallpaper_path = nullptr; RefPtr<Gfx::Bitmap> bmp;
m_desktop_dirty = true; if (!path.is_empty())
update(); bmp = Gfx::Bitmap::try_load_from_file(path);
return false; return bmp;
} },
auto bitmap = Gfx::Bitmap::try_load_from_file(path); [this, path](RefPtr<Gfx::Bitmap> bitmap) {
if (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); m_wallpaper_bitmap = move(bitmap);
m_desktop_wallpaper_path = move(path); else
m_wallpaper_bitmap = nullptr;
m_desktop_dirty = true; m_desktop_dirty = true;
update(); update();
});
if (path.is_empty())
m_desktop_wallpaper_path = nullptr;
else
m_desktop_wallpaper_path = move(path);
return true; return true;
} }

View file

@ -47,6 +47,11 @@ private:
Gfx::IntSize m_desktop_resolution; Gfx::IntSize m_desktop_resolution;
int m_desktop_scale_factor { 1 }; int m_desktop_scale_factor { 1 };
Gfx::Color m_desktop_color; 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);
}
}; };
} }