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

View file

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

View file

@ -121,7 +121,7 @@ RefPtr<GUI::Menu> DebugInfoWidget::get_context_menu_for_variable(const GUI::Mode
auto* variable = static_cast<Debug::DebugInfo::VariableInfo const*>(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<VariablesModel&>(*m_variables_view->model());
model.set_variable_value(index, value, window());

View file

@ -522,12 +522,12 @@ ErrorOr<NonnullRefPtr<GUI::Action>> 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<NonnullRefPtr<GUI::Action>> 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<NonnullRefPtr<GUI::Action>> 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));

View file

@ -75,9 +75,9 @@ ErrorOr<int> 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<int> 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;

View file

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

View file

@ -11,116 +11,119 @@
#include <LibGUI/InputBox.h>
#include <LibGUI/Label.h>
#include <LibGUI/TextBox.h>
#include <LibGfx/Font/Font.h>
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<NonnullRefPtr<InputBox>> 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<Dialog::ExecResult> 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<void> InputBox::build()
{
auto widget = set_main_widget<Widget>().release_value_but_fixme_should_propagate_errors();
auto main_widget = TRY(set_main_widget<Widget>());
TRY(main_widget->try_set_layout<VerticalBoxLayout>(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<Widget>());
input_container->set_layout<HorizontalBoxLayout>();
widget->set_layout<VerticalBoxLayout>(6, 6);
widget->set_fill_with_background_color(true);
widget->set_preferred_height(SpecialDimension::Fit);
m_prompt_label = TRY(input_container->try_add<Label>());
m_prompt_label->set_autosize(true);
m_prompt_label->set_text(move(m_prompt).to_deprecated_string());
auto& label_editor_container = widget->add<Widget>();
label_editor_container.set_layout<HorizontalBoxLayout>();
label_editor_container.set_preferred_height(SpecialDimension::Fit);
auto& label = label_editor_container.add<Label>(m_prompt);
label.set_preferred_width(text_width);
TRY(input_container->add_spacer());
switch (m_input_type) {
case InputType::Text:
case InputType::NonemptyText:
m_text_editor = label_editor_container.add<TextBox>();
m_text_editor = TRY(input_container->try_add<TextBox>());
break;
case InputType::Password:
m_text_editor = label_editor_container.add<PasswordBox>();
m_text_editor = TRY(input_container->try_add<PasswordBox>());
break;
}
m_text_editor->set_text(m_text_value);
auto button_container = TRY(main_widget->try_add<Widget>());
TRY(button_container->try_set_layout<HorizontalBoxLayout>(0, 6));
TRY(button_container->add_spacer());
if (!m_placeholder.is_null())
m_text_editor->set_placeholder(m_placeholder);
auto& button_container_outer = widget->add<Widget>();
button_container_outer.set_preferred_height(SpecialDimension::Fit);
button_container_outer.set_layout<VerticalBoxLayout>();
auto& button_container_inner = button_container_outer.add<Widget>();
button_container_inner.set_layout<HorizontalBoxLayout>(GUI::Margins {}, 6);
button_container_inner.set_preferred_height(SpecialDimension::Fit);
button_container_inner.add_spacer().release_value_but_fixme_should_propagate_errors();
m_ok_button = button_container_inner.add<DialogButton>();
m_ok_button->set_text("OK"_short_string);
m_ok_button->on_click = [this](auto) {
dbgln("GUI::InputBox: OK button clicked");
done(ExecResult::OK);
};
m_ok_button = TRY(button_container->try_add<DialogButton>("OK"_short_string));
m_ok_button->on_click = [this](auto) { done(ExecResult::OK); };
m_ok_button->set_default(true);
m_cancel_button = button_container_inner.add<DialogButton>();
m_cancel_button->set_text("Cancel"_short_string);
m_cancel_button->on_click = [this](auto) {
dbgln("GUI::InputBox: Cancel button clicked");
done(ExecResult::Cancel);
};
m_cancel_button = TRY(button_container->try_add<DialogButton>("Cancel"_short_string));
m_cancel_button->on_click = [this](auto) { done(ExecResult::Cancel); };
m_text_editor->on_escape_pressed = [this] {
m_cancel_button->click();
auto resize_editor = [this, button_container] {
auto width = button_container->effective_min_size().width().as_int();
m_text_editor->set_min_width(width);
};
m_text_editor->set_focus(true);
resize_editor();
on_font_change = [resize_editor] { resize_editor(); };
m_text_editor->set_text(m_text_value);
if (m_input_type == InputType::NonemptyText) {
m_text_editor->on_change = [this] {
@ -129,7 +132,10 @@ void InputBox::build()
m_text_editor->on_change();
}
set_rect(x(), y(), max_width + 140, widget->effective_preferred_size().height().as_int());
auto size = main_widget->effective_min_size();
resize(TRY(size.width().shrink_value()), TRY(size.height().shrink_value()));
return {};
}
}

View file

@ -19,29 +19,33 @@ enum class InputType {
};
class InputBox : public Dialog {
C_OBJECT(InputBox)
C_OBJECT_ABSTRACT(InputBox)
public:
virtual ~InputBox() override = default;
static ExecResult show(Window* parent_window, DeprecatedString& text_value, StringView prompt, StringView title, InputType input_type = InputType::Text, StringView placeholder = {});
static ExecResult show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type = InputType::Text, StringView placeholder = {});
static ErrorOr<ExecResult> try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type = InputType::Text, StringView placeholder = {});
static ErrorOr<NonnullRefPtr<InputBox>> create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type);
DeprecatedString const& text_value() const { return m_text_value; }
void set_text_value(DeprecatedString text_value);
String const& text_value() const { return m_text_value; }
void set_text_value(String);
void set_placeholder(StringView);
private:
explicit InputBox(Window* parent_window, DeprecatedString text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder);
InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type);
virtual void on_done(ExecResult) override;
void build();
ErrorOr<void> build();
DeprecatedString m_text_value;
DeprecatedString m_prompt;
String m_text_value;
String m_prompt;
InputType m_input_type;
DeprecatedString m_placeholder;
RefPtr<Button> m_ok_button;
RefPtr<Button> m_cancel_button;
RefPtr<TextEditor> m_text_editor;
RefPtr<Label> m_prompt_label;
};
}

View file

@ -97,9 +97,9 @@ void TextEditor::create_actions()
if (is_multi_line()) {
m_go_to_line_action = Action::create(
"Go to line...", { Mod_Ctrl, Key_L }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-to.png"sv).release_value_but_fixme_should_propagate_errors(), [this](auto&) {
DeprecatedString value;
String value;
if (InputBox::show(window(), value, "Line:"sv, "Go to line"sv) == InputBox::ExecResult::OK) {
auto line_target = value.to_uint();
auto line_target = AK::StringUtils::convert_to_uint(value.bytes_as_string_view());
if (line_target.has_value()) {
set_cursor_and_focus_line(line_target.value() - 1, 0);
}

View file

@ -371,12 +371,13 @@ void OutOfProcessWebView::notify_server_did_request_confirm(Badge<WebContentClie
void OutOfProcessWebView::notify_server_did_request_prompt(Badge<WebContentClient>, String const& message, String const& default_)
{
m_dialog = GUI::InputBox::construct(window(), default_.to_deprecated_string(), message, "Prompt"sv, GUI::InputType::Text, StringView {});
String mutable_value = default_;
m_dialog = GUI::InputBox::create(window(), mutable_value, message, "Prompt"sv, GUI::InputType::Text).release_value_but_fixme_should_propagate_errors();
m_dialog->set_icon(window()->icon());
if (m_dialog->exec() == GUI::InputBox::ExecResult::OK) {
auto const& dialog = static_cast<GUI::InputBox const&>(*m_dialog);
auto response = String::from_deprecated_string(dialog.text_value()).release_value_but_fixme_should_propagate_errors();
auto response = dialog.text_value();
client().async_prompt_closed(move(response));
} else {
@ -389,7 +390,7 @@ void OutOfProcessWebView::notify_server_did_request_prompt(Badge<WebContentClien
void OutOfProcessWebView::notify_server_did_request_set_prompt_text(Badge<WebContentClient>, String const& message)
{
if (m_dialog && is<GUI::InputBox>(*m_dialog))
static_cast<GUI::InputBox&>(*m_dialog).set_text_value(message.to_deprecated_string());
static_cast<GUI::InputBox&>(*m_dialog).set_text_value(message);
}
void OutOfProcessWebView::notify_server_did_request_accept_dialog(Badge<WebContentClient>)