BackgroundSettingsWidget.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2019-2020, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "BackgroundSettingsWidget.h"
  8. #include <AK/StringBuilder.h>
  9. #include <Applications/DisplaySettings/BackgroundSettingsGML.h>
  10. #include <LibCore/ConfigFile.h>
  11. #include <LibGUI/Application.h>
  12. #include <LibGUI/BoxLayout.h>
  13. #include <LibGUI/Button.h>
  14. #include <LibGUI/ComboBox.h>
  15. #include <LibGUI/Desktop.h>
  16. #include <LibGUI/FilePicker.h>
  17. #include <LibGUI/FileSystemModel.h>
  18. #include <LibGUI/IconView.h>
  19. #include <LibGUI/ItemListModel.h>
  20. #include <LibGUI/WindowServerConnection.h>
  21. #include <LibGfx/Palette.h>
  22. #include <LibGfx/SystemTheme.h>
  23. namespace DisplaySettings {
  24. BackgroundSettingsWidget::BackgroundSettingsWidget()
  25. {
  26. m_modes.append("simple");
  27. m_modes.append("tile");
  28. m_modes.append("center");
  29. m_modes.append("stretch");
  30. create_frame();
  31. load_current_settings();
  32. }
  33. BackgroundSettingsWidget::~BackgroundSettingsWidget()
  34. {
  35. }
  36. void BackgroundSettingsWidget::create_frame()
  37. {
  38. load_from_gml(background_settings_gml);
  39. m_monitor_widget = *find_descendant_of_type_named<DisplaySettings::MonitorWidget>("monitor_widget");
  40. m_wallpaper_view = *find_descendant_of_type_named<GUI::IconView>("wallpaper_view");
  41. m_wallpaper_view->set_model(GUI::FileSystemModel::create("/res/wallpapers"));
  42. m_wallpaper_view->set_model_column(GUI::FileSystemModel::Column::Name);
  43. m_wallpaper_view->on_selection_change = [this] {
  44. String path;
  45. if (m_wallpaper_view->selection().is_empty()) {
  46. path = "";
  47. } else {
  48. auto index = m_wallpaper_view->selection().first();
  49. path = static_cast<GUI::FileSystemModel*>(m_wallpaper_view->model())->full_path(index);
  50. }
  51. m_monitor_widget->set_wallpaper(path);
  52. };
  53. auto& button = *find_descendant_of_type_named<GUI::Button>("wallpaper_open_button");
  54. button.set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/open.png"));
  55. button.on_click = [this](auto) {
  56. auto path = GUI::FilePicker::get_open_filepath(nullptr, "Select wallpaper from file system.");
  57. if (!path.has_value())
  58. return;
  59. m_wallpaper_view->selection().clear();
  60. m_monitor_widget->set_wallpaper(path.value());
  61. };
  62. m_mode_combo = *find_descendant_of_type_named<GUI::ComboBox>("mode_combo");
  63. m_mode_combo->set_only_allow_values_from_model(true);
  64. m_mode_combo->set_model(*GUI::ItemListModel<String>::create(m_modes));
  65. m_mode_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
  66. m_monitor_widget->set_wallpaper_mode(m_modes.at(index.row()));
  67. };
  68. m_color_input = *find_descendant_of_type_named<GUI::ColorInput>("color_input");
  69. m_color_input->set_color_has_alpha_channel(false);
  70. m_color_input->set_color_picker_title("Select color for desktop");
  71. m_color_input->on_change = [this] {
  72. m_monitor_widget->set_background_color(m_color_input->color());
  73. };
  74. }
  75. void BackgroundSettingsWidget::load_current_settings()
  76. {
  77. auto ws_config = Core::ConfigFile::open("/etc/WindowServer.ini");
  78. auto wm_config = Core::ConfigFile::get_for_app("WindowManager");
  79. auto selected_wallpaper = wm_config->read_entry("Background", "Wallpaper", "");
  80. if (!selected_wallpaper.is_empty()) {
  81. auto index = static_cast<GUI::FileSystemModel*>(m_wallpaper_view->model())->index(selected_wallpaper, m_wallpaper_view->model_column());
  82. m_wallpaper_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
  83. m_monitor_widget->set_wallpaper(selected_wallpaper);
  84. }
  85. auto mode = ws_config->read_entry("Background", "Mode", "simple");
  86. if (!m_modes.contains_slow(mode)) {
  87. warnln("Invalid background mode '{}' in WindowServer config, falling back to 'simple'", mode);
  88. mode = "simple";
  89. }
  90. m_monitor_widget->set_wallpaper_mode(mode);
  91. m_mode_combo->set_selected_index(m_modes.find_first_index(mode).value_or(0));
  92. auto palette_desktop_color = palette().desktop_background();
  93. auto background_color = ws_config->read_entry("Background", "Color", "");
  94. if (!background_color.is_empty()) {
  95. auto opt_color = Color::from_string(background_color);
  96. if (opt_color.has_value())
  97. palette_desktop_color = opt_color.value();
  98. }
  99. m_color_input->set_color(palette_desktop_color);
  100. m_monitor_widget->set_background_color(palette_desktop_color);
  101. }
  102. void BackgroundSettingsWidget::apply_settings()
  103. {
  104. auto wm_config = Core::ConfigFile::get_for_app("WindowManager");
  105. wm_config->write_entry("Background", "Wallpaper", m_monitor_widget->wallpaper());
  106. if (!m_monitor_widget->wallpaper().is_empty()) {
  107. GUI::Desktop::the().set_wallpaper(m_monitor_widget->wallpaper());
  108. } else {
  109. GUI::Desktop::the().set_wallpaper("");
  110. GUI::Desktop::the().set_background_color(m_color_input->text());
  111. }
  112. GUI::Desktop::the().set_wallpaper_mode(m_monitor_widget->wallpaper_mode());
  113. }
  114. }