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

WindowServer+Userland: Pass wallpapers as Gfx::Bitmap instead of path

The WindowServer _really_ does not need to know the filesystem path to
it's wallpaper, and allows setting arbitrary wallpapers (those outside
of `/res/wallpapers`).

The GUI::Desktop will keep track of the path to the wallpaper (if any),
and save it to config if desired (to be persisted).

This avoids the need to `unveil` paths to the wallpaper, fixing #11158
This commit is contained in:
James Puleo 2022-02-13 13:00:57 -05:00 committed by Ali Mohammad Pur
parent f538545987
commit a0e7a4b9a8
13 changed files with 61 additions and 58 deletions

View file

@ -140,9 +140,7 @@ void BackgroundSettingsWidget::load_current_settings()
void BackgroundSettingsWidget::apply_settings()
{
if (GUI::Desktop::the().set_wallpaper(m_monitor_widget->wallpaper()))
Config::write_string("WindowManager", "Background", "Wallpaper", m_monitor_widget->wallpaper());
else
if (!GUI::Desktop::the().set_wallpaper(m_monitor_widget->wallpaper_bitmap(), m_monitor_widget->wallpaper()))
GUI::MessageBox::show_error(window(), String::formatted("Unable to load file {} as wallpaper", m_monitor_widget->wallpaper()));
GUI::Desktop::the().set_background_color(m_color_input->text());

View file

@ -58,7 +58,7 @@ bool MonitorWidget::set_wallpaper(String path)
return true;
}
String MonitorWidget::wallpaper()
StringView MonitorWidget::wallpaper() const
{
return m_desktop_wallpaper_path;
}
@ -72,7 +72,7 @@ void MonitorWidget::set_wallpaper_mode(String mode)
update();
}
String MonitorWidget::wallpaper_mode()
StringView MonitorWidget::wallpaper_mode() const
{
return m_desktop_wallpaper_mode;
}

View file

@ -15,10 +15,12 @@ class MonitorWidget final : public GUI::Widget {
public:
bool set_wallpaper(String path);
String wallpaper();
StringView wallpaper() const;
void set_wallpaper_mode(String mode);
String wallpaper_mode();
StringView wallpaper_mode() const;
RefPtr<Gfx::Bitmap> wallpaper_bitmap() const { return m_wallpaper_bitmap; }
void set_desktop_resolution(Gfx::IntSize resolution);
Gfx::IntSize desktop_resolution();

View file

@ -506,14 +506,20 @@ ErrorOr<int> run_in_desktop_mode()
struct BackgroundWallpaperListener : Config::Listener {
virtual void config_string_did_change(String const& domain, String const& group, String const& key, String const& value) override
{
if (domain == "WindowManager" && group == "Background" && key == "Wallpaper")
GUI::Desktop::the().set_wallpaper(value, false);
if (domain == "WindowManager" && group == "Background" && key == "Wallpaper") {
auto wallpaper_bitmap_or_error = Gfx::Bitmap::try_load_from_file(value);
if (wallpaper_bitmap_or_error.is_error())
dbgln("Failed to load wallpaper bitmap from path: {}", wallpaper_bitmap_or_error.error());
else
GUI::Desktop::the().set_wallpaper(wallpaper_bitmap_or_error.release_value(), {});
}
}
} wallpaper_listener;
auto selected_wallpaper = Config::read_string("WindowManager", "Background", "Wallpaper", "");
if (!selected_wallpaper.is_empty()) {
GUI::Desktop::the().set_wallpaper(selected_wallpaper, false);
auto wallpaper_bitmap = TRY(Gfx::Bitmap::try_load_from_file(selected_wallpaper));
GUI::Desktop::the().set_wallpaper(wallpaper_bitmap, {});
}
window->show();

View file

@ -169,8 +169,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
auto desktop_wallpaper_action = GUI::Action::create("Set as Desktop &Wallpaper", TRY(Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-display-settings.png")),
[&](auto&) {
auto could_set_wallpaper = GUI::Desktop::the().set_wallpaper(widget->path());
if (!could_set_wallpaper) {
if (!GUI::Desktop::the().set_wallpaper(widget->bitmap(), widget->path())) {
GUI::MessageBox::show(window,
String::formatted("set_wallpaper({}) failed", widget->path()),
"Could not set wallpaper",