diff options
author | implicitfield <114500360+implicitfield@users.noreply.github.com> | 2023-03-16 23:11:53 +0400 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-03-21 10:06:41 +0100 |
commit | 44b67db7ab1efb0bd27ec0e94ff36d97276f311e (patch) | |
tree | c30dfda6590e1e21bb4bb0d0315132cb37ec2833 /Userland/Applications/DisplaySettings | |
parent | c9ad252e3320e57913684047916b876be5af00b0 (diff) | |
download | serenity-44b67db7ab1efb0bd27ec0e94ff36d97276f311e.zip |
DisplaySettings: Stop using DeprecatedString
This also further improves error propagation in cases where String
demands it.
Diffstat (limited to 'Userland/Applications/DisplaySettings')
13 files changed, 217 insertions, 116 deletions
diff --git a/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.cpp b/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.cpp index cd48d92011..1ceada02c9 100644 --- a/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.cpp +++ b/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.cpp @@ -34,9 +34,9 @@ ErrorOr<NonnullRefPtr<BackgroundSettingsWidget>> BackgroundSettingsWidget::try_c { auto background_settings_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) BackgroundSettingsWidget(background_settings_changed))); - TRY(background_settings_widget->m_modes.try_append("Tile")); - TRY(background_settings_widget->m_modes.try_append("Center")); - TRY(background_settings_widget->m_modes.try_append("Stretch")); + TRY(background_settings_widget->m_modes.try_append(TRY(String::from_utf8("Tile"sv)))); + TRY(background_settings_widget->m_modes.try_append(TRY(String::from_utf8("Center"sv)))); + TRY(background_settings_widget->m_modes.try_append(TRY(String::from_utf8("Stretch"sv)))); TRY(background_settings_widget->create_frame()); TRY(background_settings_widget->load_current_settings()); @@ -59,12 +59,15 @@ ErrorOr<void> BackgroundSettingsWidget::create_frame() m_wallpaper_view->set_model(GUI::FileSystemModel::create("/res/wallpapers")); m_wallpaper_view->set_model_column(GUI::FileSystemModel::Column::Name); m_wallpaper_view->on_selection_change = [this] { - DeprecatedString path; - if (m_wallpaper_view->selection().is_empty()) { - path = ""; - } else { + String path = String::from_utf8_short_string(""sv); + if (!m_wallpaper_view->selection().is_empty()) { auto index = m_wallpaper_view->selection().first(); - path = static_cast<GUI::FileSystemModel*>(m_wallpaper_view->model())->full_path(index); + auto path_or_error = String::from_deprecated_string(static_cast<GUI::FileSystemModel*>(m_wallpaper_view->model())->full_path(index)); + if (path_or_error.is_error()) { + GUI::MessageBox::show_error(window(), "Unable to load wallpaper"sv); + return; + } + path = path_or_error.release_value(); } m_monitor_widget->set_wallpaper(path); @@ -74,7 +77,7 @@ ErrorOr<void> BackgroundSettingsWidget::create_frame() m_context_menu = GUI::Menu::construct(); auto const file_manager_icon = TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/app-file-manager.png"sv)); m_show_in_file_manager_action = GUI::Action::create("Show in File Manager", file_manager_icon, [this](GUI::Action const&) { - LexicalPath path { m_monitor_widget->wallpaper() }; + LexicalPath path { m_monitor_widget->wallpaper().value().to_deprecated_string() }; Desktop::Launcher::open(URL::create_with_file_scheme(path.dirname(), path.basename())); }); m_context_menu->add_action(*m_show_in_file_manager_action); @@ -82,8 +85,9 @@ ErrorOr<void> BackgroundSettingsWidget::create_frame() TRY(m_context_menu->try_add_separator()); m_copy_action = GUI::CommonActions::make_copy_action( [this](auto&) { - auto url = URL::create_with_file_scheme(m_monitor_widget->wallpaper()).to_deprecated_string(); - GUI::Clipboard::the().set_data(url.bytes(), "text/uri-list"); + auto wallpaper = m_monitor_widget->wallpaper(); + if (wallpaper.has_value()) + GUI::Clipboard::the().set_data(URL::create_with_file_scheme(wallpaper.value().to_deprecated_string()).to_deprecated_string().bytes(), "text/uri-list"); }, this); m_context_menu->add_action(*m_copy_action); @@ -96,18 +100,23 @@ ErrorOr<void> BackgroundSettingsWidget::create_frame() auto& button = *find_descendant_of_type_named<GUI::Button>("wallpaper_open_button"); button.on_click = [this](auto) { - auto path = GUI::FilePicker::get_open_filepath(window(), "Select wallpaper from file system", "/res/wallpapers"sv, false, GUI::Dialog::ScreenPosition::CenterWithinParent, { { GUI::FileTypeFilter::image_files(), GUI::FileTypeFilter::all_files() } }); - if (!path.has_value()) + auto path_or_empty = GUI::FilePicker::get_open_filepath(window(), "Select wallpaper from file system", "/res/wallpapers"sv, false, GUI::Dialog::ScreenPosition::CenterWithinParent, { { GUI::FileTypeFilter::image_files(), GUI::FileTypeFilter::all_files() } }); + if (!path_or_empty.has_value()) + return; + auto path = String::from_deprecated_string(path_or_empty.value()); + if (path.is_error()) { + GUI::MessageBox::show_error(window(), "Unable to set wallpaper"sv); return; + } m_wallpaper_view->selection().clear(); - m_monitor_widget->set_wallpaper(path.value()); + m_monitor_widget->set_wallpaper(path.release_value()); m_background_settings_changed = true; set_modified(true); }; m_mode_combo = *find_descendant_of_type_named<GUI::ComboBox>("mode_combo"); m_mode_combo->set_only_allow_values_from_model(true); - m_mode_combo->set_model(*GUI::ItemListModel<DeprecatedString>::create(m_modes)); + m_mode_combo->set_model(*GUI::ItemListModel<String>::create(m_modes)); bool first_mode_change = true; m_mode_combo->on_change = [this, first_mode_change](auto&, const GUI::ModelIndex& index) mutable { m_monitor_widget->set_wallpaper_mode(m_modes.at(index.row())); @@ -134,17 +143,17 @@ ErrorOr<void> BackgroundSettingsWidget::load_current_settings() { auto ws_config = TRY(Core::ConfigFile::open("/etc/WindowServer.ini")); - auto selected_wallpaper = Config::read_string("WindowManager"sv, "Background"sv, "Wallpaper"sv, ""sv); + auto selected_wallpaper = TRY(String::from_deprecated_string(Config::read_string("WindowManager"sv, "Background"sv, "Wallpaper"sv, ""sv))); if (!selected_wallpaper.is_empty()) { - auto index = static_cast<GUI::FileSystemModel*>(m_wallpaper_view->model())->index(selected_wallpaper, m_wallpaper_view->model_column()); + auto index = static_cast<GUI::FileSystemModel*>(m_wallpaper_view->model())->index(selected_wallpaper.to_deprecated_string(), m_wallpaper_view->model_column()); m_wallpaper_view->set_cursor(index, GUI::AbstractView::SelectionUpdate::Set); m_monitor_widget->set_wallpaper(selected_wallpaper); } - auto mode = ws_config->read_entry("Background", "Mode", "Center"); + auto mode = TRY(String::from_deprecated_string(ws_config->read_entry("Background", "Mode", "Center"))); if (!m_modes.contains_slow(mode)) { warnln("Invalid background mode '{}' in WindowServer config, falling back to 'Center'", mode); - mode = "Center"; + mode = TRY(String::from_utf8("Center"sv)); } m_monitor_widget->set_wallpaper_mode(mode); m_mode_combo->set_selected_index(m_modes.find_first_index(mode).value_or(0), GUI::AllowCallback::No); @@ -167,8 +176,19 @@ ErrorOr<void> BackgroundSettingsWidget::load_current_settings() void BackgroundSettingsWidget::apply_settings() { - if (!GUI::Desktop::the().set_wallpaper(m_monitor_widget->wallpaper_bitmap(), m_monitor_widget->wallpaper())) - GUI::MessageBox::show_error(window(), DeprecatedString::formatted("Unable to load file {} as wallpaper", m_monitor_widget->wallpaper())); + auto wallpaper_path_or_empty = m_monitor_widget->wallpaper(); + + if (!GUI::Desktop::the().set_wallpaper(m_monitor_widget->wallpaper_bitmap(), wallpaper_path_or_empty)) { + if (!wallpaper_path_or_empty.has_value()) { + GUI::MessageBox::show_error(window(), "Unable to load wallpaper"sv); + } else { + auto detailed_error_message = String::formatted("Unable to load file {} as wallpaper", wallpaper_path_or_empty.value()); + if (!detailed_error_message.is_error()) + GUI::MessageBox::show_error(window(), detailed_error_message.release_value()); + else + GUI::MessageBox::show_error(window(), "Unable to load wallpaper"sv); + } + } GUI::Desktop::the().set_background_color(m_color_input->text()); GUI::Desktop::the().set_wallpaper_mode(m_monitor_widget->wallpaper_mode()); diff --git a/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.h b/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.h index fcb92ebd54..6de09c15f2 100644 --- a/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.h +++ b/Userland/Applications/DisplaySettings/BackgroundSettingsWidget.h @@ -9,6 +9,7 @@ #pragma once #include "MonitorWidget.h" +#include <AK/String.h> #include <LibCore/Timer.h> #include <LibGUI/ColorInput.h> #include <LibGUI/ComboBox.h> @@ -33,7 +34,7 @@ private: ErrorOr<void> create_frame(); ErrorOr<void> load_current_settings(); - Vector<DeprecatedString> m_modes; + Vector<String> m_modes; bool& m_background_settings_changed; diff --git a/Userland/Applications/DisplaySettings/DesktopSettingsWidget.cpp b/Userland/Applications/DisplaySettings/DesktopSettingsWidget.cpp index d7a4da600f..e832a5d35f 100644 --- a/Userland/Applications/DisplaySettings/DesktopSettingsWidget.cpp +++ b/Userland/Applications/DisplaySettings/DesktopSettingsWidget.cpp @@ -15,7 +15,8 @@ namespace DisplaySettings { -ErrorOr<NonnullRefPtr<DesktopSettingsWidget>> DesktopSettingsWidget::try_create() { +ErrorOr<NonnullRefPtr<DesktopSettingsWidget>> DesktopSettingsWidget::try_create() +{ auto desktop_settings_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) DesktopSettingsWidget())); TRY(desktop_settings_widget->create_frame()); desktop_settings_widget->load_current_settings(); @@ -55,8 +56,7 @@ void DesktopSettingsWidget::apply_settings() auto& desktop = GUI::Desktop::the(); if (workspace_rows != desktop.workspace_rows() || workspace_columns != desktop.workspace_columns()) { if (!GUI::ConnectionToWindowServer::the().apply_workspace_settings(workspace_rows, workspace_columns, true)) { - GUI::MessageBox::show(window(), DeprecatedString::formatted("Error applying workspace settings"), - "Workspace settings"sv, GUI::MessageBox::Type::Error); + GUI::MessageBox::show_error(window(), "Error applying workspace settings"sv); } } } diff --git a/Userland/Applications/DisplaySettings/EffectsSettingsWidget.cpp b/Userland/Applications/DisplaySettings/EffectsSettingsWidget.cpp index 1c7cfad9ac..aea56f8565 100644 --- a/Userland/Applications/DisplaySettings/EffectsSettingsWidget.cpp +++ b/Userland/Applications/DisplaySettings/EffectsSettingsWidget.cpp @@ -128,8 +128,8 @@ ErrorOr<void> EffectsSettingsWidget::load_settings() "Never"sv }; for (size_t i = 0; i < list.size(); ++i) - TRY(m_geometry_list.try_append(list[i])); - m_geometry_combobox->set_model(ItemListModel<DeprecatedString>::create(m_geometry_list)); + TRY(m_geometry_list.try_append(TRY(String::from_utf8(list[i])))); + m_geometry_combobox->set_model(ItemListModel<String>::create(m_geometry_list)); m_geometry_combobox->set_selected_index(m_system_effects.geometry()); return {}; diff --git a/Userland/Applications/DisplaySettings/EffectsSettingsWidget.h b/Userland/Applications/DisplaySettings/EffectsSettingsWidget.h index d68ccaf49c..9abd977aa0 100644 --- a/Userland/Applications/DisplaySettings/EffectsSettingsWidget.h +++ b/Userland/Applications/DisplaySettings/EffectsSettingsWidget.h @@ -6,6 +6,7 @@ #pragma once +#include <AK/String.h> #include <LibGUI/SettingsWindow.h> #include <LibGUI/SystemEffects.h> @@ -30,7 +31,7 @@ private: ErrorOr<void> load_settings(); SystemEffects m_system_effects; - Vector<DeprecatedString> m_geometry_list; + Vector<String> m_geometry_list; RefPtr<ComboBox> m_geometry_combobox; }; diff --git a/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp b/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp index 2702c7be8d..20d2a655fb 100644 --- a/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp +++ b/Userland/Applications/DisplaySettings/MonitorSettingsWidget.cpp @@ -67,7 +67,7 @@ ErrorOr<void> MonitorSettingsWidget::create_resolution_list() i32 aspect_width = resolution.width() / gcf; i32 aspect_height = resolution.height() / gcf; - TRY(m_resolution_strings.try_append(DeprecatedString::formatted("{}x{} ({}:{})", resolution.width(), resolution.height(), aspect_width, aspect_height))); + TRY(m_resolution_strings.try_append(TRY(String::formatted("{}x{} ({}:{})", resolution.width(), resolution.height(), aspect_width, aspect_height)))); } return {}; @@ -81,21 +81,27 @@ ErrorOr<void> MonitorSettingsWidget::create_frame() m_screen_combo = *find_descendant_of_type_named<GUI::ComboBox>("screen_combo"); m_screen_combo->set_only_allow_values_from_model(true); - m_screen_combo->set_model(*GUI::ItemListModel<DeprecatedString>::create(m_screens)); + m_screen_combo->set_model(*GUI::ItemListModel<String>::create(m_screens)); m_screen_combo->on_change = [this](auto&, const GUI::ModelIndex& index) { m_selected_screen_index = index.row(); - selected_screen_index_or_resolution_changed(); + auto result = selected_screen_index_or_resolution_changed(); + if (result.is_error()) + GUI::MessageBox::show_error(window(), "Screen info could not be updated"sv); }; m_resolution_combo = *find_descendant_of_type_named<GUI::ComboBox>("resolution_combo"); m_resolution_combo->set_only_allow_values_from_model(true); - m_resolution_combo->set_model(*GUI::ItemListModel<DeprecatedString>::create(m_resolution_strings)); + m_resolution_combo->set_model(*GUI::ItemListModel<String>::create(m_resolution_strings)); m_resolution_combo->on_change = [this](auto&, const GUI::ModelIndex& index) { auto& selected_screen = m_screen_layout.screens[m_selected_screen_index]; selected_screen.resolution = m_resolutions.at(index.row()); // Try to auto re-arrange things if there are overlaps or disconnected screens m_screen_layout.normalize(); - selected_screen_index_or_resolution_changed(); + auto result = selected_screen_index_or_resolution_changed(); + if (result.is_error()) { + GUI::MessageBox::show_error(window(), "Screen info could not be updated"sv); + return; + } set_modified(true); }; @@ -129,20 +135,20 @@ ErrorOr<void> MonitorSettingsWidget::create_frame() return {}; } -static DeprecatedString display_name_from_edid(EDID::Parser const& edid) +static ErrorOr<String> display_name_from_edid(EDID::Parser const& edid) { auto manufacturer_name = edid.manufacturer_name(); auto product_name = edid.display_product_name(); - auto build_manufacturer_product_name = [&]() { + auto build_manufacturer_product_name = [&]() -> ErrorOr<String> { if (product_name.is_null() || product_name.is_empty()) - return manufacturer_name; - return DeprecatedString::formatted("{} {}", manufacturer_name, product_name); + return TRY(String::from_deprecated_string(manufacturer_name)); + return String::formatted("{} {}", manufacturer_name, product_name); }; if (auto screen_size = edid.screen_size(); screen_size.has_value()) { auto diagonal_inch = hypot(screen_size.value().horizontal_cm(), screen_size.value().vertical_cm()) / 2.54; - return DeprecatedString::formatted("{} {}\"", build_manufacturer_product_name(), roundf(diagonal_inch)); + return String::formatted("{} {}\"", TRY(build_manufacturer_product_name()), roundf(diagonal_inch)); } return build_manufacturer_product_name(); @@ -157,34 +163,34 @@ ErrorOr<void> MonitorSettingsWidget::load_current_settings() size_t virtual_screen_count = 0; for (size_t i = 0; i < m_screen_layout.screens.size(); i++) { - DeprecatedString screen_display_name; + String screen_display_name; if (m_screen_layout.screens[i].mode == WindowServer::ScreenLayout::Screen::Mode::Device) { if (auto edid = EDID::Parser::from_display_connector_device(m_screen_layout.screens[i].device.value()); !edid.is_error()) { // TODO: multihead - screen_display_name = display_name_from_edid(edid.value()); + screen_display_name = TRY(display_name_from_edid(edid.value())); TRY(m_screen_edids.try_append(edid.release_value())); } else { dbgln("Error getting EDID from device {}: {}", m_screen_layout.screens[i].device.value(), edid.error()); - screen_display_name = m_screen_layout.screens[i].device.value(); + screen_display_name = TRY(String::from_deprecated_string(m_screen_layout.screens[i].device.value())); TRY(m_screen_edids.try_append({})); } } else { dbgln("Frame buffer {} is virtual.", i); - screen_display_name = DeprecatedString::formatted("Virtual screen {}", virtual_screen_count++); + screen_display_name = TRY(String::formatted("Virtual screen {}", virtual_screen_count++)); TRY(m_screen_edids.try_append({})); } if (i == m_screen_layout.main_screen_index) - TRY(m_screens.try_append(DeprecatedString::formatted("{}: {} (main screen)", i + 1, screen_display_name))); + TRY(m_screens.try_append(TRY(String::formatted("{}: {} (main screen)", i + 1, screen_display_name)))); else - TRY(m_screens.try_append(DeprecatedString::formatted("{}: {}", i + 1, screen_display_name))); + TRY(m_screens.try_append(TRY(String::formatted("{}: {}", i + 1, screen_display_name)))); } m_selected_screen_index = m_screen_layout.main_screen_index; m_screen_combo->set_selected_index(m_selected_screen_index); - selected_screen_index_or_resolution_changed(); + TRY(selected_screen_index_or_resolution_changed()); return {}; } -void MonitorSettingsWidget::selected_screen_index_or_resolution_changed() +ErrorOr<void> MonitorSettingsWidget::selected_screen_index_or_resolution_changed() { auto& screen = m_screen_layout.screens[m_selected_screen_index]; @@ -193,7 +199,7 @@ void MonitorSettingsWidget::selected_screen_index_or_resolution_changed() Gfx::IntSize current_resolution = m_resolutions.at(index); Optional<unsigned> screen_dpi; - DeprecatedString screen_dpi_tooltip; + String screen_dpi_tooltip; if (m_screen_edids[m_selected_screen_index].has_value()) { auto& edid = m_screen_edids[m_selected_screen_index]; if (auto screen_size = edid.value().screen_size(); screen_size.has_value()) { @@ -203,14 +209,15 @@ void MonitorSettingsWidget::selected_screen_index_or_resolution_changed() auto diagonal_pixels = hypot(current_resolution.width(), current_resolution.height()); if (diagonal_pixels != 0.0) { screen_dpi = diagonal_pixels / diagonal_inch; - screen_dpi_tooltip = DeprecatedString::formatted("{} inch display ({}cm x {}cm)", roundf(diagonal_inch), x_cm, y_cm); + screen_dpi_tooltip = TRY(String::formatted("{} inch display ({}cm x {}cm)", roundf(diagonal_inch), x_cm, y_cm)); } } } - if (screen_dpi.has_value()) { - m_dpi_label->set_tooltip(screen_dpi_tooltip); - m_dpi_label->set_text(DeprecatedString::formatted("{} dpi", screen_dpi.value())); + auto dpi_label_value = String::formatted("{} dpi", screen_dpi.value()); + if (screen_dpi.has_value() && !dpi_label_value.is_error()) { + m_dpi_label->set_tooltip(screen_dpi_tooltip.to_deprecated_string()); + m_dpi_label->set_text(dpi_label_value.release_value().to_deprecated_string()); m_dpi_label->set_visible(true); } else { m_dpi_label->set_visible(false); @@ -228,6 +235,8 @@ void MonitorSettingsWidget::selected_screen_index_or_resolution_changed() m_resolution_combo->set_selected_index(index, GUI::AllowCallback::No); m_monitor_widget->update(); + + return {}; } void MonitorSettingsWidget::apply_settings() @@ -241,42 +250,73 @@ void MonitorSettingsWidget::apply_settings() if (result.success() && !load_current_settings().is_error()) { auto seconds_until_revert = 10; - auto box_text = [&seconds_until_revert] { - return DeprecatedString::formatted("Do you want to keep the new settings? They will be reverted after {} {}.", + auto box_text = [this, &seconds_until_revert]() -> ErrorOr<String> { + auto output = String::formatted("Do you want to keep the new settings? They will be reverted after {} {}.", seconds_until_revert, seconds_until_revert == 1 ? "second" : "seconds"); + if (output.is_error()) { + GUI::MessageBox::show_error(window(), "Unable to apply changes"sv); + return Error::from_string_literal("Unable to create a formatted string"); + } + return output.release_value(); }; - auto box = GUI::MessageBox::construct(window(), box_text(), "Apply new screen layout"sv, + auto current_box_text_or_error = box_text(); + if (current_box_text_or_error.is_error()) + return; + auto current_box_text = current_box_text_or_error.release_value(); + + auto box = GUI::MessageBox::construct(window(), current_box_text, "Apply new screen layout"sv, GUI::MessageBox::Type::Question, GUI::MessageBox::InputType::YesNo); box->set_icon(window()->icon()); // If after 10 seconds the user doesn't close the message box, just close it. - auto revert_timer = Core::Timer::create_repeating(1000, [&] { + auto revert_timer_or_error = Core::Timer::create_repeating(1000, [&] { seconds_until_revert -= 1; - box->set_text(box_text()); + current_box_text_or_error = box_text(); + if (current_box_text_or_error.is_error()) { + seconds_until_revert = 0; + box->close(); + return; + } + auto current_box_text = current_box_text_or_error.release_value(); + box->set_text(current_box_text.to_deprecated_string()); if (seconds_until_revert <= 0) { box->close(); } - }).release_value_but_fixme_should_propagate_errors(); + }); + if (revert_timer_or_error.is_error()) { + GUI::MessageBox::show_error(window(), "Unable to apply changes"sv); + return; + } + auto revert_timer = revert_timer_or_error.release_value(); revert_timer->start(); // If the user selects "No", closes the window or the window gets closed by the 10 seconds timer, revert the changes. if (box->exec() == GUI::MessageBox::ExecResult::Yes) { auto save_result = GUI::ConnectionToWindowServer::the().save_screen_layout(); if (!save_result.success()) { - GUI::MessageBox::show(window(), DeprecatedString::formatted("Error saving settings: {}", save_result.error_msg()), - "Unable to save setting"sv, GUI::MessageBox::Type::Error); + auto detailed_error_message = String::formatted("Error saving settings: {}", save_result.error_msg()); + if (!detailed_error_message.is_error()) + GUI::MessageBox::show_error(window(), detailed_error_message.release_value()); + else + GUI::MessageBox::show_error(window(), "Unable to save settings"sv); } } else { auto restore_result = GUI::ConnectionToWindowServer::the().set_screen_layout(current_layout, false); if (!restore_result.success() || load_current_settings().is_error()) { - GUI::MessageBox::show(window(), DeprecatedString::formatted("Error restoring settings: {}", restore_result.error_msg()), - "Unable to restore setting"sv, GUI::MessageBox::Type::Error); + auto detailed_error_message = String::formatted("Error restoring settings: {}", restore_result.error_msg()); + if (!detailed_error_message.is_error()) + GUI::MessageBox::show_error(window(), detailed_error_message.release_value()); + else + GUI::MessageBox::show_error(window(), "Unable to restore settings"sv); } } } else { - GUI::MessageBox::show(window(), DeprecatedString::formatted("Error setting screen layout: {}", result.error_msg()), - "Unable to apply changes"sv, GUI::MessageBox::Type::Error); + auto detailed_error_message = String::formatted("Error setting screen layout: {}", result.error_msg()); + if (!detailed_error_message.is_error()) + GUI::MessageBox::show_error(window(), detailed_error_message.release_value()); + else + GUI::MessageBox::show_error(window(), "Unable to set screen layout"sv); } } } diff --git a/Userland/Applications/DisplaySettings/MonitorSettingsWidget.h b/Userland/Applications/DisplaySettings/MonitorSettingsWidget.h index a7e45a6399..1c689d4892 100644 --- a/Userland/Applications/DisplaySettings/MonitorSettingsWidget.h +++ b/Userland/Applications/DisplaySettings/MonitorSettingsWidget.h @@ -7,6 +7,7 @@ #pragma once #include "MonitorWidget.h" +#include <AK/String.h> #include <LibCore/Timer.h> #include <LibEDID/EDID.h> #include <LibGUI/ColorInput.h> @@ -41,15 +42,15 @@ private: ErrorOr<void> create_frame(); ErrorOr<void> create_resolution_list(); ErrorOr<void> load_current_settings(); - void selected_screen_index_or_resolution_changed(); + ErrorOr<void> selected_screen_index_or_resolution_changed(); size_t m_selected_screen_index { 0 }; WindowServer::ScreenLayout m_screen_layout; - Vector<DeprecatedString> m_screens; + Vector<String> m_screens; Vector<Optional<EDID::Parser>> m_screen_edids; Vector<Gfx::IntSize> m_resolutions; - Vector<DeprecatedString> m_resolution_strings; + Vector<String> m_resolution_strings; RefPtr<DisplaySettings::MonitorWidget> m_monitor_widget; RefPtr<GUI::ComboBox> m_screen_combo; diff --git a/Userland/Applications/DisplaySettings/MonitorWidget.cpp b/Userland/Applications/DisplaySettings/MonitorWidget.cpp index 9a9fff7e8a..7dd8c005a6 100644 --- a/Userland/Applications/DisplaySettings/MonitorWidget.cpp +++ b/Userland/Applications/DisplaySettings/MonitorWidget.cpp @@ -28,7 +28,7 @@ ErrorOr<NonnullRefPtr<MonitorWidget>> MonitorWidget::try_create() return monitor_widget; } -bool MonitorWidget::set_wallpaper(DeprecatedString path) +bool MonitorWidget::set_wallpaper(String path) { if (!is_different_to_current_wallpaper_path(path)) return false; @@ -55,19 +55,21 @@ bool MonitorWidget::set_wallpaper(DeprecatedString path) }); if (path.is_empty()) - m_desktop_wallpaper_path = nullptr; + m_desktop_wallpaper_path = OptionalNone(); else - m_desktop_wallpaper_path = move(path); + m_desktop_wallpaper_path = path; return true; } -StringView MonitorWidget::wallpaper() const +Optional<StringView> MonitorWidget::wallpaper() const { - return m_desktop_wallpaper_path; + if (m_desktop_wallpaper_path.has_value()) + return m_desktop_wallpaper_path->bytes_as_string_view(); + return OptionalNone(); } -void MonitorWidget::set_wallpaper_mode(DeprecatedString mode) +void MonitorWidget::set_wallpaper_mode(String mode) { if (m_desktop_wallpaper_mode == mode) return; @@ -133,12 +135,12 @@ void MonitorWidget::redraw_desktop_if_needed() } auto scaled_bitmap = scaled_bitmap_or_error.release_value(); - if (m_desktop_wallpaper_mode == "Center") { + if (m_desktop_wallpaper_mode == "Center"sv) { auto centered_rect = Gfx::IntRect({}, scaled_size).centered_within(m_desktop_bitmap->rect()); painter.blit(centered_rect.location(), *scaled_bitmap, scaled_bitmap->rect()); - } else if (m_desktop_wallpaper_mode == "Tile") { + } else if (m_desktop_wallpaper_mode == "Tile"sv) { painter.draw_tiled_bitmap(m_desktop_bitmap->rect(), *scaled_bitmap); - } else if (m_desktop_wallpaper_mode == "Stretch") { + } else if (m_desktop_wallpaper_mode == "Stretch"sv) { painter.draw_scaled_bitmap(m_desktop_bitmap->rect(), *m_wallpaper_bitmap, m_wallpaper_bitmap->rect()); } else { VERIFY_NOT_REACHED(); diff --git a/Userland/Applications/DisplaySettings/MonitorWidget.h b/Userland/Applications/DisplaySettings/MonitorWidget.h index 412b4b42d5..3586314429 100644 --- a/Userland/Applications/DisplaySettings/MonitorWidget.h +++ b/Userland/Applications/DisplaySettings/MonitorWidget.h @@ -6,6 +6,8 @@ #pragma once +#include <AK/Optional.h> +#include <AK/String.h> #include <LibGUI/Widget.h> namespace DisplaySettings { @@ -15,10 +17,10 @@ class MonitorWidget final : public GUI::Widget { public: static ErrorOr<NonnullRefPtr<MonitorWidget>> try_create(); - bool set_wallpaper(DeprecatedString path); - StringView wallpaper() const; + bool set_wallpaper(String path); + Optional<StringView> wallpaper() const; - void set_wallpaper_mode(DeprecatedString mode); + void set_wallpaper_mode(String mode); StringView wallpaper_mode() const; RefPtr<Gfx::Bitmap> wallpaper_bitmap() const { return m_wallpaper_bitmap; } @@ -44,16 +46,23 @@ private: RefPtr<Gfx::Bitmap> m_desktop_bitmap; bool m_desktop_dirty { true }; - DeprecatedString m_desktop_wallpaper_path; + Optional<String> m_desktop_wallpaper_path; RefPtr<Gfx::Bitmap> m_wallpaper_bitmap; - DeprecatedString m_desktop_wallpaper_mode; + String m_desktop_wallpaper_mode; Gfx::IntSize m_desktop_resolution; int m_desktop_scale_factor { 1 }; Gfx::Color m_desktop_color; - bool is_different_to_current_wallpaper_path(DeprecatedString const& path) + bool is_different_to_current_wallpaper_path(String const& path) { - return (!path.is_empty() && path != m_desktop_wallpaper_path) || (path.is_empty() && m_desktop_wallpaper_path != nullptr); + if (!m_desktop_wallpaper_path.has_value()) { + if (path.is_empty()) + return false; + return true; + } + if (m_desktop_wallpaper_path.value() == path) + return false; + return true; } }; diff --git a/Userland/Applications/DisplaySettings/ThemePreviewWidget.cpp b/Userland/Applications/DisplaySettings/ThemePreviewWidget.cpp index 7969cd7554..4adc8d0631 100644 --- a/Userland/Applications/DisplaySettings/ThemePreviewWidget.cpp +++ b/Userland/Applications/DisplaySettings/ThemePreviewWidget.cpp @@ -18,10 +18,10 @@ ThemePreviewWidget::ThemePreviewWidget(Gfx::Palette const& palette) set_fixed_size(304, 201); } -ErrorOr<void> ThemePreviewWidget::set_theme(DeprecatedString path) +ErrorOr<void> ThemePreviewWidget::set_theme(String path) { - auto config_file = TRY(Core::File::open(path, Core::File::OpenMode::Read)); - TRY(set_theme_from_file(path, move(config_file))); + auto config_file = TRY(Core::File::open(path.to_deprecated_string(), Core::File::OpenMode::Read)); + TRY(set_theme_from_file(path.to_deprecated_string(), move(config_file))); return {}; } diff --git a/Userland/Applications/DisplaySettings/ThemePreviewWidget.h b/Userland/Applications/DisplaySettings/ThemePreviewWidget.h index 8c5207a31e..0a90940175 100644 --- a/Userland/Applications/DisplaySettings/ThemePreviewWidget.h +++ b/Userland/Applications/DisplaySettings/ThemePreviewWidget.h @@ -6,6 +6,7 @@ #pragma once +#include <AK/String.h> #include <LibGUI/AbstractThemePreview.h> #include <LibGUI/Widget.h> #include <LibGfx/Bitmap.h> @@ -18,7 +19,7 @@ class ThemePreviewWidget final : public GUI::AbstractThemePreview { C_OBJECT(ThemePreviewWidget); public: - ErrorOr<void> set_theme(DeprecatedString path); + ErrorOr<void> set_theme(String path); private: explicit ThemePreviewWidget(Gfx::Palette const& palette); diff --git a/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp b/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp index 4ca86602b7..d0e28f7053 100644 --- a/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp +++ b/Userland/Applications/DisplaySettings/ThemesSettingsWidget.cpp @@ -19,12 +19,13 @@ namespace DisplaySettings { -static DeprecatedString get_color_scheme_name_from_pathname(DeprecatedString const& color_scheme_path) +static ErrorOr<String> get_color_scheme_name_from_pathname(StringView color_scheme_path) { - return color_scheme_path.replace("/res/color-schemes/"sv, ""sv, ReplaceMode::FirstOnly).replace(".ini"sv, ""sv, ReplaceMode::FirstOnly); + return TRY(String::from_deprecated_string(color_scheme_path.replace("/res/color-schemes/"sv, ""sv, ReplaceMode::FirstOnly).replace(".ini"sv, ""sv, ReplaceMode::FirstOnly))); }; -ErrorOr<NonnullRefPtr<ThemesSettingsWidget>> ThemesSettingsWidget::try_create(bool& background_settings_changed) { +ErrorOr<NonnullRefPtr<ThemesSettingsWidget>> ThemesSettingsWidget::try_create(bool& background_settings_changed) +{ auto theme_settings_widget = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) ThemesSettingsWidget(background_settings_changed))); TRY(theme_settings_widget->load_from_gml(themes_settings_gml)); TRY(theme_settings_widget->setup_interface()); @@ -32,14 +33,15 @@ ErrorOr<NonnullRefPtr<ThemesSettingsWidget>> ThemesSettingsWidget::try_create(bo return theme_settings_widget; } -ErrorOr<void> ThemesSettingsWidget::setup_interface() { +ErrorOr<void> ThemesSettingsWidget::setup_interface() +{ m_themes = TRY(Gfx::list_installed_system_themes()); size_t current_theme_index; auto current_theme_name = GUI::ConnectionToWindowServer::the().get_system_theme(); TRY(m_theme_names.try_ensure_capacity(m_themes.size())); for (auto& theme_meta : m_themes) { - m_theme_names.append(theme_meta.name); + TRY(m_theme_names.try_append(TRY(String::from_deprecated_string(theme_meta.name)))); if (current_theme_name == theme_meta.name) { m_selected_theme = &theme_meta; current_theme_index = m_theme_names.size() - 1; @@ -49,12 +51,20 @@ ErrorOr<void> ThemesSettingsWidget::setup_interface() { m_theme_preview = find_descendant_of_type_named<GUI::Frame>("preview_frame")->add<ThemePreviewWidget>(palette()); m_themes_combo = *find_descendant_of_type_named<GUI::ComboBox>("themes_combo"); m_themes_combo->set_only_allow_values_from_model(true); - m_themes_combo->set_model(*GUI::ItemListModel<DeprecatedString>::create(m_theme_names)); + m_themes_combo->set_model(*GUI::ItemListModel<String>::create(m_theme_names)); m_themes_combo->on_change = [this](auto&, const GUI::ModelIndex& index) { m_selected_theme = &m_themes.at(index.row()); - auto set_theme_result = m_theme_preview->set_theme(m_selected_theme->path); + auto selected_theme_path = String::from_deprecated_string(m_selected_theme->path); + ErrorOr<void> set_theme_result; + if (!selected_theme_path.is_error()) + set_theme_result = m_theme_preview->set_theme(selected_theme_path.release_value()); if (set_theme_result.is_error()) { - GUI::MessageBox::show_error(window(), DeprecatedString::formatted("There was an error generating the theme preview: {}", set_theme_result.error())); + auto detailed_error_message = String::formatted("There was an error generating the theme preview: {}", set_theme_result.error()); + if (!detailed_error_message.is_error()) + GUI::MessageBox::show_error(window(), detailed_error_message.release_value()); + else + GUI::MessageBox::show_error(window(), "There was an error generating the theme preview"sv); + return; } set_modified(true); }; @@ -66,13 +76,13 @@ ErrorOr<void> ThemesSettingsWidget::setup_interface() { Core::DirIterator iterator("/res/color-schemes", Core::DirIterator::SkipParentAndBaseDir); while (iterator.has_next()) { auto path = iterator.next_path(); - m_color_scheme_names.append(path.replace(".ini"sv, ""sv, ReplaceMode::FirstOnly)); + TRY(m_color_scheme_names.try_append(TRY(String::from_deprecated_string(path.replace(".ini"sv, ""sv, ReplaceMode::FirstOnly))))); } quick_sort(m_color_scheme_names); auto& color_scheme_combo = *find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo"); color_scheme_combo.set_only_allow_values_from_model(true); - color_scheme_combo.set_model(*GUI::ItemListModel<DeprecatedString>::create(m_color_scheme_names)); - m_selected_color_scheme_name = get_color_scheme_name_from_pathname(GUI::Widget::palette().color_scheme_path()); + color_scheme_combo.set_model(*GUI::ItemListModel<String>::create(m_color_scheme_names)); + m_selected_color_scheme_name = TRY(get_color_scheme_name_from_pathname(GUI::Widget::palette().color_scheme_path())); auto selected_color_scheme_index = m_color_scheme_names.find_first_index(m_selected_color_scheme_name); if (selected_color_scheme_index.has_value()) color_scheme_combo.set_selected_index(selected_color_scheme_index.value()); @@ -85,7 +95,10 @@ ErrorOr<void> ThemesSettingsWidget::setup_interface() { } } color_scheme_combo.on_change = [this](auto&, const GUI::ModelIndex& index) { - m_selected_color_scheme_name = index.data().as_string(); + auto result = String::from_deprecated_string(index.data().as_string()); + if (result.is_error()) + return; + m_selected_color_scheme_name = result.release_value(); m_color_scheme_is_file_based = true; set_modified(true); }; @@ -123,9 +136,16 @@ ErrorOr<void> ThemesSettingsWidget::setup_interface() { if (current_theme_name == theme_meta.name) { m_themes_combo->set_selected_index(index, GUI::AllowCallback::No); m_selected_theme = &m_themes.at(index); - auto set_theme_result = m_theme_preview->set_theme(m_selected_theme->path); + auto selected_theme_path = String::from_deprecated_string(m_selected_theme->path); + ErrorOr<void> set_theme_result; + if (!selected_theme_path.is_error()) + set_theme_result = m_theme_preview->set_theme(selected_theme_path.release_value()); if (set_theme_result.is_error()) { - GUI::MessageBox::show_error(window(), DeprecatedString::formatted("There was an error setting the new theme: {}", set_theme_result.error())); + auto detailed_error_message = String::formatted("There was an error generating the theme preview: {}", set_theme_result.error()); + if (!detailed_error_message.is_error()) + GUI::MessageBox::show_error(window(), detailed_error_message.release_value()); + else + GUI::MessageBox::show_error(window(), "There was an error generating the theme preview"sv); } } ++index; @@ -136,36 +156,42 @@ ErrorOr<void> ThemesSettingsWidget::setup_interface() { } ThemesSettingsWidget::ThemesSettingsWidget(bool& background_settings_changed) - : m_background_settings_changed {background_settings_changed } + : m_background_settings_changed { background_settings_changed } { } void ThemesSettingsWidget::apply_settings() { m_background_settings_changed = false; - Optional<DeprecatedString> color_scheme_path = DeprecatedString::formatted("/res/color-schemes/{}.ini", m_selected_color_scheme_name); + auto color_scheme_path_or_error = String::formatted("/res/color-schemes/{}.ini", m_selected_color_scheme_name); + if (color_scheme_path_or_error.is_error()) { + GUI::MessageBox::show_error(window(), "Unable to apply changes"sv); + return; + } + auto color_scheme_path = color_scheme_path_or_error.release_value(); if (!m_color_scheme_is_file_based && find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) { auto set_theme_result = GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, "Custom"sv); - if(!set_theme_result) + if (!set_theme_result) GUI::MessageBox::show_error(window(), "Failed to apply theme settings"sv); } else if (find_descendant_of_type_named<GUI::CheckBox>("custom_color_scheme_checkbox")->is_checked()) { - auto set_theme_result = GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, color_scheme_path); - if(!set_theme_result) + auto set_theme_result = GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, color_scheme_path.to_deprecated_string()); + if (!set_theme_result) GUI::MessageBox::show_error(window(), "Failed to apply theme settings"sv); } else { - auto set_theme_result = GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, OptionalNone()); - if (!set_theme_result) { - GUI::MessageBox::show_error(window(), "Failed to apply theme settings"sv); - return; - } - // Update the color scheme combobox to match the new theme. auto theme_config = Core::ConfigFile::open(m_selected_theme->path); if (theme_config.is_error()) { GUI::MessageBox::show_error(window(), "Failed to open theme config file"sv); return; } - auto const color_scheme_index = m_color_scheme_names.find_first_index(get_color_scheme_name_from_pathname(theme_config.release_value()->read_entry("Paths", "ColorScheme"))); + auto preferred_color_scheme_path = get_color_scheme_name_from_pathname(theme_config.release_value()->read_entry("Paths", "ColorScheme")); + auto set_theme_result = GUI::ConnectionToWindowServer::the().set_system_theme(m_selected_theme->path, m_selected_theme->name, m_background_settings_changed, OptionalNone()); + if (!set_theme_result || preferred_color_scheme_path.is_error()) { + GUI::MessageBox::show_error(window(), "Failed to apply theme settings"sv); + return; + } + // Update the color scheme combobox to match the new theme. + auto const color_scheme_index = m_color_scheme_names.find_first_index(preferred_color_scheme_path.value()); if (color_scheme_index.has_value()) find_descendant_of_type_named<GUI::ComboBox>("color_scheme_combo")->set_selected_index(color_scheme_index.value()); } diff --git a/Userland/Applications/DisplaySettings/ThemesSettingsWidget.h b/Userland/Applications/DisplaySettings/ThemesSettingsWidget.h index 8617aff73e..ebba4df6ff 100644 --- a/Userland/Applications/DisplaySettings/ThemesSettingsWidget.h +++ b/Userland/Applications/DisplaySettings/ThemesSettingsWidget.h @@ -6,7 +6,7 @@ #pragma once -#include <AK/DeprecatedString.h> +#include <AK/String.h> #include <AK/Vector.h> #include <LibGUI/ComboBox.h> #include <LibGUI/SettingsWindow.h> @@ -26,13 +26,13 @@ public: private: ErrorOr<void> setup_interface(); Vector<Gfx::SystemThemeMetaData> m_themes; - Vector<DeprecatedString> m_theme_names; - Vector<DeprecatedString> m_color_scheme_names; + Vector<String> m_theme_names; + Vector<String> m_color_scheme_names; RefPtr<GUI::ComboBox> m_themes_combo; RefPtr<ThemePreviewWidget> m_theme_preview; Gfx::SystemThemeMetaData const* m_selected_theme { nullptr }; - DeprecatedString m_selected_color_scheme_name = ""; + String m_selected_color_scheme_name {}; RefPtr<GUI::Button> m_cursor_themes_button; |