BackgroundSettingsWidget.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2019-2020, Jesse Buhagiar <jooster669@gmail.com>
  3. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  4. * Copyright (c) 2022, the SerenityOS developers.
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "BackgroundSettingsWidget.h"
  9. #include <AK/StringBuilder.h>
  10. #include <Applications/DisplaySettings/BackgroundSettingsGML.h>
  11. #include <LibConfig/Client.h>
  12. #include <LibCore/ConfigFile.h>
  13. #include <LibDesktop/Launcher.h>
  14. #include <LibGUI/Application.h>
  15. #include <LibGUI/BoxLayout.h>
  16. #include <LibGUI/Button.h>
  17. #include <LibGUI/Clipboard.h>
  18. #include <LibGUI/ComboBox.h>
  19. #include <LibGUI/ConnectionToWindowServer.h>
  20. #include <LibGUI/Desktop.h>
  21. #include <LibGUI/FilePicker.h>
  22. #include <LibGUI/FileSystemModel.h>
  23. #include <LibGUI/IconView.h>
  24. #include <LibGUI/ItemListModel.h>
  25. #include <LibGUI/MessageBox.h>
  26. #include <LibGfx/Palette.h>
  27. #include <LibGfx/SystemTheme.h>
  28. namespace DisplaySettings {
  29. BackgroundSettingsWidget::BackgroundSettingsWidget(bool& background_settings_changed)
  30. : m_background_settings_changed { background_settings_changed }
  31. {
  32. m_modes.append("Tile");
  33. m_modes.append("Center");
  34. m_modes.append("Stretch");
  35. create_frame();
  36. load_current_settings();
  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. set_modified(true);
  55. };
  56. m_context_menu = GUI::Menu::construct();
  57. m_show_in_file_manager_action = GUI::Action::create("Show in File Manager", Gfx::Bitmap::try_load_from_file("/res/icons/16x16/app-file-manager.png"sv).release_value_but_fixme_should_propagate_errors(), [this](GUI::Action const&) {
  58. LexicalPath path { m_monitor_widget->wallpaper() };
  59. Desktop::Launcher::open(URL::create_with_file_scheme(path.dirname(), path.basename()));
  60. });
  61. m_context_menu->add_action(*m_show_in_file_manager_action);
  62. m_context_menu->add_separator();
  63. m_copy_action = GUI::CommonActions::make_copy_action(
  64. [this](auto&) {
  65. auto url = URL::create_with_file_scheme(m_monitor_widget->wallpaper()).to_string();
  66. GUI::Clipboard::the().set_data(url.bytes(), "text/uri-list");
  67. },
  68. this);
  69. m_context_menu->add_action(*m_copy_action);
  70. m_wallpaper_view->on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  71. if (index.is_valid()) {
  72. m_context_menu->popup(event.screen_position(), m_show_in_file_manager_action);
  73. }
  74. };
  75. auto& button = *find_descendant_of_type_named<GUI::Button>("wallpaper_open_button");
  76. button.on_click = [this](auto) {
  77. auto path = GUI::FilePicker::get_open_filepath(window(), "Select wallpaper from file system", "/res/wallpapers"sv);
  78. if (!path.has_value())
  79. return;
  80. m_wallpaper_view->selection().clear();
  81. m_monitor_widget->set_wallpaper(path.value());
  82. m_background_settings_changed = true;
  83. set_modified(true);
  84. };
  85. m_mode_combo = *find_descendant_of_type_named<GUI::ComboBox>("mode_combo");
  86. m_mode_combo->set_only_allow_values_from_model(true);
  87. m_mode_combo->set_model(*GUI::ItemListModel<String>::create(m_modes));
  88. bool first_mode_change = true;
  89. m_mode_combo->on_change = [this, first_mode_change](auto&, const GUI::ModelIndex& index) mutable {
  90. m_monitor_widget->set_wallpaper_mode(m_modes.at(index.row()));
  91. m_background_settings_changed = !first_mode_change;
  92. first_mode_change = false;
  93. set_modified(true);
  94. };
  95. m_color_input = *find_descendant_of_type_named<GUI::ColorInput>("color_input");
  96. m_color_input->set_color_has_alpha_channel(false);
  97. m_color_input->set_color_picker_title("Select color for desktop");
  98. bool first_color_change = true;
  99. m_color_input->on_change = [this, first_color_change]() mutable {
  100. m_monitor_widget->set_background_color(m_color_input->color());
  101. m_background_settings_changed = !first_color_change;
  102. first_color_change = false;
  103. set_modified(true);
  104. };
  105. }
  106. void BackgroundSettingsWidget::load_current_settings()
  107. {
  108. auto ws_config = Core::ConfigFile::open("/etc/WindowServer.ini").release_value_but_fixme_should_propagate_errors();
  109. auto selected_wallpaper = Config::read_string("WindowManager"sv, "Background"sv, "Wallpaper"sv, ""sv);
  110. if (!selected_wallpaper.is_empty()) {
  111. auto index = static_cast<GUI::FileSystemModel*>(m_wallpaper_view->model())->index(selected_wallpaper, m_wallpaper_view->model_column());
  112. m_wallpaper_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
  113. m_monitor_widget->set_wallpaper(selected_wallpaper);
  114. }
  115. auto mode = ws_config->read_entry("Background", "Mode", "center");
  116. if (!m_modes.contains_slow(mode)) {
  117. warnln("Invalid background mode '{}' in WindowServer config, falling back to 'center'", mode);
  118. mode = "center";
  119. }
  120. m_monitor_widget->set_wallpaper_mode(mode);
  121. m_mode_combo->set_selected_index(m_modes.find_first_index(mode).value_or(0), GUI::AllowCallback::No);
  122. auto palette_desktop_color = palette().desktop_background();
  123. auto background_color = ws_config->read_entry("Background", "Color", "");
  124. if (!background_color.is_empty()) {
  125. auto opt_color = Color::from_string(background_color);
  126. if (opt_color.has_value())
  127. palette_desktop_color = opt_color.value();
  128. }
  129. m_color_input->set_color(palette_desktop_color, GUI::AllowCallback::No);
  130. m_monitor_widget->set_background_color(palette_desktop_color);
  131. m_background_settings_changed = false;
  132. }
  133. void BackgroundSettingsWidget::apply_settings()
  134. {
  135. if (!GUI::Desktop::the().set_wallpaper(m_monitor_widget->wallpaper_bitmap(), m_monitor_widget->wallpaper()))
  136. GUI::MessageBox::show_error(window(), String::formatted("Unable to load file {} as wallpaper", m_monitor_widget->wallpaper()));
  137. GUI::Desktop::the().set_background_color(m_color_input->text());
  138. GUI::Desktop::the().set_wallpaper_mode(m_monitor_widget->wallpaper_mode());
  139. }
  140. }