1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 03:27:34 +00:00

LibGUI+Userland: Improve error and font handling for InputBox

Adds fallible factories, ports DeprecatedString, and rebuilds the
layout to accomodate system font changes.
This commit is contained in:
thankyouverycool 2023-04-16 16:02:07 -04:00 committed by Andreas Kling
parent 9c2bcffe83
commit 02a9e5d3f6
19 changed files with 135 additions and 124 deletions

View file

@ -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<void> 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);

View file

@ -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);
}
};

View file

@ -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())

View file

@ -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;

View file

@ -223,7 +223,7 @@ void do_create_link(Vector<DeprecatedString> const& selected_file_paths, GUI::Wi
void do_create_archive(Vector<DeprecatedString> 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<DeprecatedString> 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();

View file

@ -242,7 +242,7 @@ ErrorOr<void> 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())

View file

@ -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()));

View file

@ -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);
}

View file

@ -373,7 +373,7 @@ PDF::PDFErrorOr<void> 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

View file

@ -100,16 +100,16 @@ SpreadsheetWidget::SpreadsheetWidget(GUI::Window& parent_window, Vector<NonnullR
auto* sheet_ptr = m_tab_context_menu_sheet_view->sheet_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<GUI::Widget&>(*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<GUI::Widget&>(*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<NonnullRefPtr<Sheet>> new_sheets;
new_sheets.append(m_workbook->add_sheet(name));