From 02a9e5d3f681bbf3c6157d368e95ad3183b200be Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Sun, 16 Apr 2023 16:02:07 -0400 Subject: [PATCH] LibGUI+Userland: Improve error and font handling for InputBox Adds fallible factories, ports DeprecatedString, and rebuilds the layout to accomodate system font changes. --- .../Applications/Browser/BrowserWindow.cpp | 18 +-- .../ContentFilterSettingsWidget.cpp | 4 +- .../CharacterMap/CharacterMapWidget.cpp | 2 +- .../FileManager/DirectoryView.cpp | 4 +- Userland/Applications/FileManager/main.cpp | 4 +- .../Applications/FontEditor/MainWidget.cpp | 2 +- .../HexEditor/HexEditorWidget.cpp | 8 +- .../KeyboardMapper/KeyboardMapperWidget.cpp | 8 +- .../PDFViewer/PDFViewerWidget.cpp | 2 +- .../Spreadsheet/SpreadsheetWidget.cpp | 6 +- .../Demos/WidgetGallery/GalleryWidget.cpp | 2 +- .../HackStudio/Debugger/DebugInfoWidget.cpp | 2 +- .../DevTools/HackStudio/HackStudioWidget.cpp | 10 +- Userland/Games/MasterWord/main.cpp | 8 +- Userland/Libraries/LibGUI/FilePicker.cpp | 2 +- Userland/Libraries/LibGUI/InputBox.cpp | 144 +++++++++--------- Userland/Libraries/LibGUI/InputBox.h | 22 +-- Userland/Libraries/LibGUI/TextEditor.cpp | 4 +- .../LibWebView/OutOfProcessWebView.cpp | 7 +- 19 files changed, 135 insertions(+), 124 deletions(-) diff --git a/Userland/Applications/Browser/BrowserWindow.cpp b/Userland/Applications/Browser/BrowserWindow.cpp index 65f4335549..5495256728 100644 --- a/Userland/Applications/Browser/BrowserWindow.cpp +++ b/Userland/Applications/Browser/BrowserWindow.cpp @@ -308,11 +308,11 @@ void BrowserWindow::build_menus() m_change_homepage_action = GUI::Action::create( "Set Homepage URL...", g_icon_bag.go_home, [this](auto&) { - auto homepage_url = Config::read_string("Browser"sv, "Preferences"sv, "Home"sv, "about:blank"sv); + String homepage_url = String::from_deprecated_string(Config::read_string("Browser"sv, "Preferences"sv, "Home"sv, "about:blank"sv)).release_value_but_fixme_should_propagate_errors(); if (GUI::InputBox::show(this, homepage_url, "Enter URL"sv, "Change homepage URL"sv) == GUI::InputBox::ExecResult::OK) { if (URL(homepage_url).is_valid()) { Config::write_string("Browser"sv, "Preferences"sv, "Home"sv, homepage_url); - Browser::g_home_url = homepage_url; + Browser::g_home_url = homepage_url.to_deprecated_string(); } else { GUI::MessageBox::show_error(this, "The URL you have entered is not valid"sv); } @@ -439,13 +439,13 @@ void BrowserWindow::build_menus() add_user_agent("Safari iOS Mobile", "Mozilla/5.0 (iPhone; CPU iPhone OS 14_4_2 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0 Mobile/15E148 Safari/604.1"); auto custom_user_agent = GUI::Action::create_checkable("Custom...", [this](auto& action) { - DeprecatedString user_agent; + String user_agent; if (GUI::InputBox::show(this, user_agent, "Enter User Agent:"sv, "Custom User Agent"sv, GUI::InputType::NonemptyText) != GUI::InputBox::ExecResult::OK) { m_disable_user_agent_spoofing->activate(); return; } - active_tab().view().debug_request("spoof-user-agent", user_agent); - action.set_status_tip(user_agent); + active_tab().view().debug_request("spoof-user-agent", user_agent.to_deprecated_string()); + action.set_status_tip(user_agent.to_deprecated_string()); }); spoof_user_agent_menu.add_action(custom_user_agent); m_user_agent_spoof_actions.add_action(custom_user_agent); @@ -530,22 +530,22 @@ ErrorOr BrowserWindow::load_search_engines(GUI::Menu& settings_menu) } auto custom_search_engine_action = GUI::Action::create_checkable("Custom...", [&](auto& action) { - DeprecatedString search_engine; + String search_engine; if (GUI::InputBox::show(this, search_engine, "Enter URL template:"sv, "Custom Search Engine"sv, GUI::InputType::NonemptyText, "https://host/search?q={}"sv) != GUI::InputBox::ExecResult::OK) { m_disable_search_engine_action->activate(); return; } - auto argument_count = search_engine.count("{}"sv); + auto argument_count = AK::StringUtils::count(search_engine, "{}"sv); if (argument_count != 1) { GUI::MessageBox::show(this, "Invalid format, must contain '{}' once!"sv, "Error"sv, GUI::MessageBox::Type::Error); m_disable_search_engine_action->activate(); return; } - g_search_engine = search_engine; + g_search_engine = search_engine.to_deprecated_string(); Config::write_string("Browser"sv, "Preferences"sv, "SearchEngine"sv, g_search_engine); - action.set_status_tip(search_engine); + action.set_status_tip(search_engine.to_deprecated_string()); }); search_engine_menu.add_action(custom_search_engine_action); m_search_engine_actions.add_action(custom_search_engine_action); diff --git a/Userland/Applications/BrowserSettings/ContentFilterSettingsWidget.cpp b/Userland/Applications/BrowserSettings/ContentFilterSettingsWidget.cpp index 435db8e5cf..6ef9631db6 100644 --- a/Userland/Applications/BrowserSettings/ContentFilterSettingsWidget.cpp +++ b/Userland/Applications/BrowserSettings/ContentFilterSettingsWidget.cpp @@ -116,10 +116,10 @@ ContentFilterSettingsWidget::ContentFilterSettingsWidget() m_enable_content_filtering_checkbox->on_checked = [&](auto) { set_modified(true); }; m_add_new_domain_button->on_click = [&](unsigned) { - DeprecatedString text; + String text; if (GUI::InputBox::show(window(), text, "Enter domain name"sv, "Add domain to Content Filter"sv, GUI::InputType::NonemptyText) == GUI::Dialog::ExecResult::OK) { - m_domain_list_model->add_domain(move(text)); + m_domain_list_model->add_domain(move(text).to_deprecated_string()); set_modified(true); } }; diff --git a/Userland/Applications/CharacterMap/CharacterMapWidget.cpp b/Userland/Applications/CharacterMap/CharacterMapWidget.cpp index fa889e6b43..59c80781ee 100644 --- a/Userland/Applications/CharacterMap/CharacterMapWidget.cpp +++ b/Userland/Applications/CharacterMap/CharacterMapWidget.cpp @@ -69,7 +69,7 @@ CharacterMapWidget::CharacterMapWidget() m_next_glyph_action->set_status_tip("Seek the next visible glyph"); m_go_to_glyph_action = GUI::Action::create("Go to glyph...", { Mod_Ctrl, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-to.png"sv).release_value_but_fixme_should_propagate_errors(), [&](auto&) { - DeprecatedString input; + String input; if (GUI::InputBox::show(window(), input, "Hexadecimal:"sv, "Go to glyph"sv, GUI::InputType::NonemptyText) == GUI::InputBox::ExecResult::OK) { auto maybe_code_point = AK::StringUtils::convert_to_uint_from_hex(input); if (!maybe_code_point.has_value()) diff --git a/Userland/Applications/FileManager/DirectoryView.cpp b/Userland/Applications/FileManager/DirectoryView.cpp index ddfba640f6..3866f3f84c 100644 --- a/Userland/Applications/FileManager/DirectoryView.cpp +++ b/Userland/Applications/FileManager/DirectoryView.cpp @@ -568,7 +568,7 @@ void DirectoryView::handle_selection_change() void DirectoryView::setup_actions() { m_mkdir_action = GUI::Action::create("&New Directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"sv).release_value_but_fixme_should_propagate_errors(), [&](GUI::Action const&) { - DeprecatedString value; + String value; if (GUI::InputBox::show(window(), value, "Enter name:"sv, "New directory"sv, GUI::InputType::NonemptyText) == GUI::InputBox::ExecResult::OK) { auto new_dir_path = LexicalPath::canonicalized_path(DeprecatedString::formatted("{}/{}", path(), value)); int rc = mkdir(new_dir_path.characters(), 0777); @@ -580,7 +580,7 @@ void DirectoryView::setup_actions() }); m_touch_action = GUI::Action::create("New &File...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"sv).release_value_but_fixme_should_propagate_errors(), [&](GUI::Action const&) { - DeprecatedString value; + String value; if (GUI::InputBox::show(window(), value, "Enter name:"sv, "New file"sv, GUI::InputType::NonemptyText) == GUI::InputBox::ExecResult::OK) { auto new_file_path = LexicalPath::canonicalized_path(DeprecatedString::formatted("{}/{}", path(), value)); struct stat st; diff --git a/Userland/Applications/FileManager/main.cpp b/Userland/Applications/FileManager/main.cpp index 3724bf9d82..d878e6847f 100644 --- a/Userland/Applications/FileManager/main.cpp +++ b/Userland/Applications/FileManager/main.cpp @@ -223,7 +223,7 @@ void do_create_link(Vector const& selected_file_paths, GUI::Wi void do_create_archive(Vector const& selected_file_paths, GUI::Window* window) { - DeprecatedString archive_name; + String archive_name; if (GUI::InputBox::show(window, archive_name, "Enter name:"sv, "Create Archive"sv) != GUI::InputBox::ExecResult::OK) return; @@ -237,7 +237,7 @@ void do_create_archive(Vector const& selected_file_paths, GUI: path_builder.append(".zip"sv); } else { path_builder.append(archive_name); - if (!archive_name.ends_with(".zip"sv)) + if (!AK::StringUtils::ends_with(archive_name, ".zip"sv, CaseSensitivity::CaseSensitive)) path_builder.append(".zip"sv); } auto output_path = path_builder.to_deprecated_string(); diff --git a/Userland/Applications/FontEditor/MainWidget.cpp b/Userland/Applications/FontEditor/MainWidget.cpp index 9c2ef6fe17..407c7dd294 100644 --- a/Userland/Applications/FontEditor/MainWidget.cpp +++ b/Userland/Applications/FontEditor/MainWidget.cpp @@ -242,7 +242,7 @@ ErrorOr MainWidget::create_actions() m_show_system_emoji_action->set_status_tip("Show or hide system emoji"); m_go_to_glyph_action = GUI::Action::create("&Go to Glyph...", { Mod_Ctrl, Key_G }, TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/go-to.png"sv)), [&](auto&) { - DeprecatedString input; + String input; if (GUI::InputBox::show(window(), input, "Hexadecimal:"sv, "Go to glyph"sv, GUI::InputType::NonemptyText) == GUI::InputBox::ExecResult::OK) { auto maybe_code_point = AK::StringUtils::convert_to_uint_from_hex(input); if (!maybe_code_point.has_value()) diff --git a/Userland/Applications/HexEditor/HexEditorWidget.cpp b/Userland/Applications/HexEditor/HexEditorWidget.cpp index 653a355c42..712db290a5 100644 --- a/Userland/Applications/HexEditor/HexEditorWidget.cpp +++ b/Userland/Applications/HexEditor/HexEditorWidget.cpp @@ -99,9 +99,9 @@ HexEditorWidget::HexEditorWidget() }; m_new_action = GUI::Action::create("New", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"sv).release_value_but_fixme_should_propagate_errors(), [this](const GUI::Action&) { - DeprecatedString value; + String value; if (request_close() && GUI::InputBox::show(window(), value, "Enter new file size:"sv, "New file size"sv, GUI::InputType::NonemptyText) == GUI::InputBox::ExecResult::OK) { - auto file_size = value.to_uint(); + auto file_size = AK::StringUtils::convert_to_uint(value); if (!file_size.has_value()) { GUI::MessageBox::show(window(), "Invalid file size entered."sv, "Error"sv, GUI::MessageBox::Type::Error); return; @@ -242,9 +242,9 @@ HexEditorWidget::HexEditorWidget() m_copy_as_c_code_action->set_enabled(false); m_fill_selection_action = GUI::Action::create("Fill &Selection...", { Mod_Ctrl, Key_B }, [&](const GUI::Action&) { - DeprecatedString value; + String value; if (GUI::InputBox::show(window(), value, "Fill byte (hex):"sv, "Fill Selection"sv, GUI::InputType::NonemptyText) == GUI::InputBox::ExecResult::OK) { - auto fill_byte = strtol(value.characters(), nullptr, 16); + auto fill_byte = strtol(value.bytes_as_string_view().characters_without_null_termination(), nullptr, 16); auto result = m_editor->fill_selection(fill_byte); if (result.is_error()) GUI::MessageBox::show_error(window(), DeprecatedString::formatted("{}", result.error())); diff --git a/Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp b/Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp index 849494b414..a830081c26 100644 --- a/Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp +++ b/Userland/Applications/KeyboardMapper/KeyboardMapperWidget.cpp @@ -55,7 +55,7 @@ void KeyboardMapperWidget::create_frame() tmp_button.set_enabled(keys[i].enabled); tmp_button.on_click = [this, &tmp_button]() { - DeprecatedString value; + String value; if (GUI::InputBox::show(window(), value, "New Character:"sv, "Select Character"sv) == GUI::InputBox::ExecResult::OK) { int i = m_keys.find_first_index(&tmp_button).value_or(0); VERIFY(i > 0); @@ -63,13 +63,13 @@ void KeyboardMapperWidget::create_frame() auto index = keys[i].map_index; VERIFY(index > 0); - tmp_button.set_text(String::from_deprecated_string(value).release_value_but_fixme_should_propagate_errors()); + tmp_button.set_text(value); u32* map = map_from_name(m_current_map_name); - if (value.length() == 0) + if (value.is_empty()) map[index] = '\0'; // Empty string else - map[index] = value[0]; + map[index] = value.bytes().at(0); window()->set_modified(true); } diff --git a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp index 3a11e3c32a..b5a316121e 100644 --- a/Userland/Applications/PDFViewer/PDFViewerWidget.cpp +++ b/Userland/Applications/PDFViewer/PDFViewerWidget.cpp @@ -373,7 +373,7 @@ PDF::PDFErrorOr PDFViewerWidget::try_open_file(StringView path, NonnullOwn auto document = TRY(PDF::Document::create(m_buffer)); if (auto sh = document->security_handler(); sh && !sh->has_user_password()) { - DeprecatedString password; + String password; while (true) { auto result = GUI::InputBox::show(window(), password, "Password"sv, "Password required"sv, GUI::InputType::Password); if (result == GUI::Dialog::ExecResult::OK diff --git a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp index 897ccb2037..c8ca139905 100644 --- a/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp +++ b/Userland/Applications/Spreadsheet/SpreadsheetWidget.cpp @@ -100,16 +100,16 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, Vectorsheet_if_available(); VERIFY(sheet_ptr); // How did we get here without a sheet? auto& sheet = *sheet_ptr; - DeprecatedString new_name; + String new_name; if (GUI::InputBox::show(window(), new_name, DeprecatedString::formatted("New name for '{}'", sheet.name()), "Rename sheet"sv) == GUI::Dialog::ExecResult::OK) { sheet.set_name(new_name); sheet.update(); - m_tab_widget->set_tab_title(static_cast(*m_tab_context_menu_sheet_view), String::from_deprecated_string(new_name).release_value_but_fixme_should_propagate_errors()); + m_tab_widget->set_tab_title(static_cast(*m_tab_context_menu_sheet_view), new_name); } }); m_tab_context_menu->add_action(*m_rename_action); m_tab_context_menu->add_action(GUI::Action::create("Add new sheet...", Gfx::Bitmap::load_from_file("/res/icons/16x16/new-tab.png"sv).release_value_but_fixme_should_propagate_errors(), [this](auto&) { - DeprecatedString name; + String name; if (GUI::InputBox::show(window(), name, "Name for new sheet"sv, "Create sheet"sv) == GUI::Dialog::ExecResult::OK) { Vector> new_sheets; new_sheets.append(m_workbook->add_sheet(name)); diff --git a/Userland/Demos/WidgetGallery/GalleryWidget.cpp b/Userland/Demos/WidgetGallery/GalleryWidget.cpp index 9109950d63..4107b9db52 100644 --- a/Userland/Demos/WidgetGallery/GalleryWidget.cpp +++ b/Userland/Demos/WidgetGallery/GalleryWidget.cpp @@ -118,7 +118,7 @@ GalleryWidget::GalleryWidget() m_input_button->set_icon(Gfx::Bitmap::load_from_file("/res/icons/16x16/properties.png"sv).release_value_but_fixme_should_propagate_errors()); m_input_button->on_click = [&](auto) { - DeprecatedString value; + String value; if (GUI::InputBox::show(window(), value, "Enter input:"sv, "Input"sv, GUI::InputType::NonemptyText) == GUI::InputBox::ExecResult::OK) m_text_editor->set_text(value); }; diff --git a/Userland/DevTools/HackStudio/Debugger/DebugInfoWidget.cpp b/Userland/DevTools/HackStudio/Debugger/DebugInfoWidget.cpp index 7c615635d0..c4ff1ebe16 100644 --- a/Userland/DevTools/HackStudio/Debugger/DebugInfoWidget.cpp +++ b/Userland/DevTools/HackStudio/Debugger/DebugInfoWidget.cpp @@ -121,7 +121,7 @@ RefPtr DebugInfoWidget::get_context_menu_for_variable(const GUI::Mode auto* variable = static_cast(index.internal_data()); if (does_variable_support_writing(variable)) { context_menu->add_action(GUI::Action::create("Change value", [&](auto&) { - DeprecatedString value; + String value; if (GUI::InputBox::show(window(), value, "Enter new value:"sv, "Set variable value"sv) == GUI::InputBox::ExecResult::OK) { auto& model = static_cast(*m_variables_view->model()); model.set_variable_value(index, value, window()); diff --git a/Userland/DevTools/HackStudio/HackStudioWidget.cpp b/Userland/DevTools/HackStudio/HackStudioWidget.cpp index a46350dbe4..6d6cceaa25 100644 --- a/Userland/DevTools/HackStudio/HackStudioWidget.cpp +++ b/Userland/DevTools/HackStudio/HackStudioWidget.cpp @@ -522,12 +522,12 @@ ErrorOr> HackStudioWidget::create_new_file_action(Dep { auto icon_no_shadow = TRY(Gfx::Bitmap::load_from_file(icon)); return GUI::Action::create(label, icon_no_shadow, [this, extension](const GUI::Action&) { - DeprecatedString filename; + String filename; if (GUI::InputBox::show(window(), filename, "Enter name of new file:"sv, "Add new file to project"sv) != GUI::InputBox::ExecResult::OK) return; - if (!extension.is_empty() && !filename.ends_with(DeprecatedString::formatted(".{}", extension))) { - filename = DeprecatedString::formatted("{}.{}", filename, extension); + if (!extension.is_empty() && !AK::StringUtils::ends_with(filename, DeprecatedString::formatted(".{}", extension), CaseSensitivity::CaseSensitive)) { + filename = String::formatted("{}.{}", filename, extension).release_value_but_fixme_should_propagate_errors(); } auto path_to_selected = selected_file_paths(); @@ -564,7 +564,7 @@ ErrorOr> HackStudioWidget::create_new_directory_actio { auto icon = TRY(Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"sv)); return GUI::Action::create("&Directory...", { Mod_Ctrl | Mod_Shift, Key_N }, icon, [this](const GUI::Action&) { - DeprecatedString directory_name; + String directory_name; if (GUI::InputBox::show(window(), directory_name, "Enter name of new directory:"sv, "Add new folder to project"sv) != GUI::InputBox::ExecResult::OK) return; @@ -580,7 +580,7 @@ ErrorOr> HackStudioWidget::create_new_directory_actio else dir_path = selected.dirname(); - directory_name = DeprecatedString::formatted("{}/{}", dir_path, directory_name); + directory_name = String::formatted("{}/{}", dir_path, directory_name).release_value_but_fixme_should_propagate_errors(); } auto formatted_dir_name = LexicalPath::canonicalized_path(DeprecatedString::formatted("{}/{}", m_project->model().root_path(), directory_name)); diff --git a/Userland/Games/MasterWord/main.cpp b/Userland/Games/MasterWord/main.cpp index c5c9dde320..ef1515267b 100644 --- a/Userland/Games/MasterWord/main.cpp +++ b/Userland/Games/MasterWord/main.cpp @@ -75,9 +75,9 @@ ErrorOr serenity_main(Main::Arguments arguments) TRY(settings_menu->try_add_action(GUI::Action::create("Set &Word Length", [&](auto&) { auto word_length = Config::read_i32("MasterWord"sv, ""sv, "word_length"sv, 5); - auto word_length_string = DeprecatedString::number(word_length); + auto word_length_string = String::number(word_length).release_value_but_fixme_should_propagate_errors(); if (GUI::InputBox::show(window, word_length_string, "Word length:"sv, "MasterWord"sv, GUI::InputType::NonemptyText) == GUI::InputBox::ExecResult::OK) { - auto maybe_word_length = word_length_string.template to_uint(); + auto maybe_word_length = AK::StringUtils::convert_to_uint(word_length_string); if (!maybe_word_length.has_value() || maybe_word_length.value() < shortest_word || maybe_word_length.value() > longest_word) { GUI::MessageBox::show(window, DeprecatedString::formatted("Please enter a number between {} and {}.", shortest_word, longest_word), "MasterWord"sv); return; @@ -90,9 +90,9 @@ ErrorOr serenity_main(Main::Arguments arguments) }))); TRY(settings_menu->try_add_action(GUI::Action::create("Set &Number Of Guesses", [&](auto&) { auto max_guesses = Config::read_i32("MasterWord"sv, ""sv, "max_guesses"sv, 5); - auto max_guesses_string = DeprecatedString::number(max_guesses); + auto max_guesses_string = String::number(max_guesses).release_value_but_fixme_should_propagate_errors(); if (GUI::InputBox::show(window, max_guesses_string, "Maximum number of guesses:"sv, "MasterWord"sv, GUI::InputType::NonemptyText) == GUI::InputBox::ExecResult::OK) { - auto maybe_max_guesses = max_guesses_string.template to_uint(); + auto maybe_max_guesses = AK::StringUtils::convert_to_uint(max_guesses_string); if (!maybe_max_guesses.has_value() || maybe_max_guesses.value() < 1 || maybe_max_guesses.value() > 20) { GUI::MessageBox::show(window, "Please enter a number between 1 and 20."sv, "MasterWord"sv); return; diff --git a/Userland/Libraries/LibGUI/FilePicker.cpp b/Userland/Libraries/LibGUI/FilePicker.cpp index dced842f09..fbef90de65 100644 --- a/Userland/Libraries/LibGUI/FilePicker.cpp +++ b/Userland/Libraries/LibGUI/FilePicker.cpp @@ -163,7 +163,7 @@ FilePicker::FilePicker(Window* parent_window, Mode mode, StringView filename, St auto mkdir_action = Action::create( "New directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"sv).release_value_but_fixme_should_propagate_errors(), [this](Action const&) { - DeprecatedString value; + String value; if (InputBox::show(this, value, "Enter name:"sv, "New directory"sv, GUI::InputType::NonemptyText) == InputBox::ExecResult::OK) { auto new_dir_path = LexicalPath::canonicalized_path(DeprecatedString::formatted("{}/{}", m_model->root_path(), value)); int rc = mkdir(new_dir_path.characters(), 0777); diff --git a/Userland/Libraries/LibGUI/InputBox.cpp b/Userland/Libraries/LibGUI/InputBox.cpp index 5c590d821a..85e5e93353 100644 --- a/Userland/Libraries/LibGUI/InputBox.cpp +++ b/Userland/Libraries/LibGUI/InputBox.cpp @@ -11,116 +11,119 @@ #include #include #include -#include namespace GUI { -InputBox::InputBox(Window* parent_window, DeprecatedString text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder) - : Dialog(parent_window) - , m_text_value(move(text_value)) - , m_prompt(prompt) - , m_input_type(input_type) - , m_placeholder(placeholder) +ErrorOr> InputBox::create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type) { - set_title(title); - build(); + auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) InputBox(parent_window, text_value, TRY(String::from_utf8(title)), TRY(String::from_utf8(prompt)), input_type))); + TRY(box->build()); + return box; } -Dialog::ExecResult InputBox::show(Window* parent_window, DeprecatedString& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder) +InputBox::InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type) + : Dialog(parent_window) + , m_text_value(move(text_value)) + , m_prompt(move(prompt)) + , m_input_type(input_type) { - auto box = InputBox::construct(parent_window, text_value, prompt, title, input_type, placeholder); - box->set_resizable(false); + set_title(move(title).to_deprecated_string()); + set_resizable(false); + set_auto_shrink(true); +} + +Dialog::ExecResult InputBox::show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder) +{ + return MUST(try_show(parent_window, text_value, prompt, title, input_type, placeholder)); +} + +ErrorOr InputBox::try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder) +{ + auto box = TRY(InputBox::create(parent_window, text_value, prompt, title, input_type)); if (parent_window) box->set_icon(parent_window->icon()); + box->set_placeholder(placeholder); auto result = box->exec(); text_value = box->text_value(); return result; } -void InputBox::set_text_value(DeprecatedString text_value) +void InputBox::set_placeholder(StringView view) { - m_text_editor->set_text(move(text_value)); + m_text_editor->set_placeholder(view); +} + +void InputBox::set_text_value(String value) +{ + if (m_text_value == value) + return; + m_text_value = move(value); + m_text_editor->set_text(m_text_value); } void InputBox::on_done(ExecResult result) { - if (result == ExecResult::OK) { - m_text_value = m_text_editor->text(); + if (result != ExecResult::OK) + return; - switch (m_input_type) { - case InputType::Text: - case InputType::Password: - break; + if (auto value = String::from_deprecated_string(m_text_editor->text()); !value.is_error()) + m_text_value = value.release_value(); - case InputType::NonemptyText: - VERIFY(!m_text_value.is_empty()); - break; - } + switch (m_input_type) { + case InputType::Text: + case InputType::Password: + break; + + case InputType::NonemptyText: + VERIFY(!m_text_value.is_empty()); + break; } } -void InputBox::build() +ErrorOr InputBox::build() { - auto widget = set_main_widget().release_value_but_fixme_should_propagate_errors(); + auto main_widget = TRY(set_main_widget()); + TRY(main_widget->try_set_layout(4, 6)); + main_widget->set_fill_with_background_color(true); - int text_width = widget->font().width(m_prompt); - int title_width = widget->font().width(title()) + 24 /* icon, plus a little padding -- not perfect */; - int max_width = max(text_width, title_width); + auto input_container = TRY(main_widget->try_add()); + input_container->set_layout(); - widget->set_layout(6, 6); - widget->set_fill_with_background_color(true); - widget->set_preferred_height(SpecialDimension::Fit); + m_prompt_label = TRY(input_container->try_add