BackgroundSettingsWidget.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 <LibCore/ConfigFile.h>
  12. #include <LibDesktop/Launcher.h>
  13. #include <LibGUI/Application.h>
  14. #include <LibGUI/BoxLayout.h>
  15. #include <LibGUI/Button.h>
  16. #include <LibGUI/Clipboard.h>
  17. #include <LibGUI/ComboBox.h>
  18. #include <LibGUI/ConnectionToWindowServer.h>
  19. #include <LibGUI/Desktop.h>
  20. #include <LibGUI/FilePicker.h>
  21. #include <LibGUI/FileSystemModel.h>
  22. #include <LibGUI/IconView.h>
  23. #include <LibGUI/ItemListModel.h>
  24. #include <LibGUI/MessageBox.h>
  25. #include <LibGfx/Palette.h>
  26. #include <LibGfx/SystemTheme.h>
  27. // Including this after to avoid LibIPC errors
  28. #include <LibConfig/Client.h>
  29. namespace DisplaySettings {
  30. BackgroundSettingsWidget::BackgroundSettingsWidget()
  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. };
  55. m_context_menu = GUI::Menu::construct();
  56. 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").release_value_but_fixme_should_propagate_errors(), [this](GUI::Action const&) {
  57. LexicalPath path { m_monitor_widget->wallpaper() };
  58. Desktop::Launcher::open(URL::create_with_file_protocol(path.dirname(), path.basename()));
  59. });
  60. m_context_menu->add_action(*m_show_in_file_manager_action);
  61. m_context_menu->add_separator();
  62. m_copy_action = GUI::CommonActions::make_copy_action([this](auto&) { GUI::Clipboard::the().set_plain_text(m_monitor_widget->wallpaper()); }, this);
  63. m_context_menu->add_action(*m_copy_action);
  64. m_wallpaper_view->on_context_menu_request = [&](const GUI::ModelIndex& index, const GUI::ContextMenuEvent& event) {
  65. if (index.is_valid()) {
  66. m_context_menu->popup(event.screen_position(), m_show_in_file_manager_action);
  67. }
  68. };
  69. auto& button = *find_descendant_of_type_named<GUI::Button>("wallpaper_open_button");
  70. button.on_click = [this](auto) {
  71. auto path = GUI::FilePicker::get_open_filepath(window(), "Select wallpaper from file system", "/res/wallpapers");
  72. if (!path.has_value())
  73. return;
  74. m_wallpaper_view->selection().clear();
  75. m_monitor_widget->set_wallpaper(path.value());
  76. };
  77. m_mode_combo = *find_descendant_of_type_named<GUI::ComboBox>("mode_combo");
  78. m_mode_combo->set_only_allow_values_from_model(true);
  79. m_mode_combo->set_model(*GUI::ItemListModel<String>::create(m_modes));
  80. m_mode_combo->on_change = [this](auto&, const GUI::ModelIndex& index) {
  81. m_monitor_widget->set_wallpaper_mode(m_modes.at(index.row()));
  82. };
  83. m_color_input = *find_descendant_of_type_named<GUI::ColorInput>("color_input");
  84. m_color_input->set_color_has_alpha_channel(false);
  85. m_color_input->set_color_picker_title("Select color for desktop");
  86. m_color_input->on_change = [this] {
  87. m_monitor_widget->set_background_color(m_color_input->color());
  88. };
  89. }
  90. void BackgroundSettingsWidget::load_current_settings()
  91. {
  92. auto ws_config = Core::ConfigFile::open("/etc/WindowServer.ini").release_value_but_fixme_should_propagate_errors();
  93. auto selected_wallpaper = Config::read_string("WindowManager", "Background", "Wallpaper", "");
  94. if (!selected_wallpaper.is_empty()) {
  95. auto index = static_cast<GUI::FileSystemModel*>(m_wallpaper_view->model())->index(selected_wallpaper, m_wallpaper_view->model_column());
  96. m_wallpaper_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set);
  97. m_monitor_widget->set_wallpaper(selected_wallpaper);
  98. }
  99. auto mode = ws_config->read_entry("Background", "Mode", "center");
  100. if (!m_modes.contains_slow(mode)) {
  101. warnln("Invalid background mode '{}' in WindowServer config, falling back to 'center'", mode);
  102. mode = "center";
  103. }
  104. m_monitor_widget->set_wallpaper_mode(mode);
  105. m_mode_combo->set_selected_index(m_modes.find_first_index(mode).value_or(0));
  106. auto palette_desktop_color = palette().desktop_background();
  107. auto background_color = ws_config->read_entry("Background", "Color", "");
  108. if (!background_color.is_empty()) {
  109. auto opt_color = Color::from_string(background_color);
  110. if (opt_color.has_value())
  111. palette_desktop_color = opt_color.value();
  112. }
  113. m_color_input->set_color(palette_desktop_color);
  114. m_monitor_widget->set_background_color(palette_desktop_color);
  115. }
  116. void BackgroundSettingsWidget::apply_settings()
  117. {
  118. if (!GUI::Desktop::the().set_wallpaper(m_monitor_widget->wallpaper_bitmap(), m_monitor_widget->wallpaper()))
  119. GUI::MessageBox::show_error(window(), String::formatted("Unable to load file {} as wallpaper", m_monitor_widget->wallpaper()));
  120. GUI::Desktop::the().set_background_color(m_color_input->text());
  121. GUI::Desktop::the().set_wallpaper_mode(m_monitor_widget->wallpaper_mode());
  122. }
  123. }