BackgroundSettingsWidget.cpp 4.9 KB

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