Desktop.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, the SerenityOS developers.
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Badge.h>
  8. #include <AK/TemporaryChange.h>
  9. #include <LibConfig/Client.h>
  10. #include <LibGUI/ConnectionToWindowServer.h>
  11. #include <LibGUI/Desktop.h>
  12. #include <string.h>
  13. namespace GUI {
  14. Desktop& Desktop::the()
  15. {
  16. static Desktop s_the;
  17. return s_the;
  18. }
  19. void Desktop::did_receive_screen_rects(Badge<ConnectionToWindowServer>, Vector<Gfx::IntRect, 4> const& rects, size_t main_screen_index, unsigned workspace_rows, unsigned workspace_columns)
  20. {
  21. m_main_screen_index = main_screen_index;
  22. m_rects = rects;
  23. if (!m_rects.is_empty()) {
  24. m_bounding_rect = m_rects[0];
  25. for (size_t i = 1; i < m_rects.size(); i++)
  26. m_bounding_rect = m_bounding_rect.united(m_rects[i]);
  27. } else {
  28. m_bounding_rect = {};
  29. }
  30. m_workspace_rows = workspace_rows;
  31. m_workspace_columns = workspace_columns;
  32. for (auto& callback : m_receive_rects_callbacks)
  33. callback(*this);
  34. }
  35. void Desktop::set_background_color(StringView background_color)
  36. {
  37. ConnectionToWindowServer::the().async_set_background_color(background_color);
  38. }
  39. void Desktop::set_wallpaper_mode(StringView mode)
  40. {
  41. ConnectionToWindowServer::the().async_set_wallpaper_mode(mode);
  42. }
  43. DeprecatedString Desktop::wallpaper_path() const
  44. {
  45. return Config::read_string("WindowManager"sv, "Background"sv, "Wallpaper"sv);
  46. }
  47. RefPtr<Gfx::Bitmap> Desktop::wallpaper_bitmap() const
  48. {
  49. return ConnectionToWindowServer::the().get_wallpaper().bitmap();
  50. }
  51. bool Desktop::set_wallpaper(RefPtr<Gfx::Bitmap const> wallpaper_bitmap, Optional<StringView> path)
  52. {
  53. if (m_is_setting_desktop_wallpaper)
  54. return false;
  55. TemporaryChange is_setting_desktop_wallpaper_change(m_is_setting_desktop_wallpaper, true);
  56. auto result = ConnectionToWindowServer::the().set_wallpaper(wallpaper_bitmap ? wallpaper_bitmap->to_shareable_bitmap() : Gfx::ShareableBitmap {});
  57. if (result && path.has_value()) {
  58. dbgln("Saving wallpaper path '{}' to ConfigServer", *path);
  59. Config::write_string("WindowManager"sv, "Background"sv, "Wallpaper"sv, *path);
  60. }
  61. return result;
  62. }
  63. }