LibGUI: Add InputBox::show with required parent window argument

Similar to MessageBox::show, this encourages passing in a window.
This commit is contained in:
Tom 2020-07-16 07:54:42 -06:00 committed by Andreas Kling
parent 27bd2eab22
commit 65a11fb5f9
Notes: sideshowbarker 2024-07-19 04:46:07 +09:00
11 changed files with 99 additions and 94 deletions

View File

@ -164,12 +164,12 @@ int run_in_desktop_mode(RefPtr<Core::ConfigFile> config, String initial_location
auto desktop_view_context_menu = GUI::Menu::construct("Directory View");
auto mkdir_action = GUI::Action::create("New directory...", {}, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GUI::Action&) {
auto input_box = GUI::InputBox::construct("Enter name:", "New directory", window);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
String value;
if (GUI::InputBox::show(value, window, "Enter name:", "New directory") == GUI::InputBox::ExecOK && !value.is_empty()) {
auto new_dir_path = LexicalPath::canonicalized_path(
String::format("%s/%s",
model->root_path().characters(),
input_box->text_value().characters()));
value.characters()));
int rc = mkdir(new_dir_path.characters(), 0777);
if (rc < 0) {
GUI::MessageBox::show(window, String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
@ -178,12 +178,12 @@ int run_in_desktop_mode(RefPtr<Core::ConfigFile> config, String initial_location
});
auto touch_action = GUI::Action::create("New file...", {}, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
auto input_box = GUI::InputBox::construct("Enter name:", "New file", window);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
String value;
if (GUI::InputBox::show(value, window, "Enter name:", "New file") == GUI::InputBox::ExecOK && !value.is_empty()) {
auto new_file_path = LexicalPath::canonicalized_path(
String::format("%s/%s",
model->root_path().characters(),
input_box->text_value().characters()));
value.characters()));
struct stat st;
int rc = stat(new_file_path.characters(), &st);
if ((rc < 0 && errno != ENOENT)) {
@ -322,12 +322,12 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
});
auto mkdir_action = GUI::Action::create("New directory...", { Mod_Ctrl | Mod_Shift, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [&](const GUI::Action&) {
auto input_box = GUI::InputBox::construct("Enter name:", "New directory", window);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
String value;
if (GUI::InputBox::show(value, window, "Enter name:", "New directory") == GUI::InputBox::ExecOK && !value.is_empty()) {
auto new_dir_path = LexicalPath::canonicalized_path(
String::format("%s/%s",
directory_view.path().characters(),
input_box->text_value().characters()));
value.characters()));
int rc = mkdir(new_dir_path.characters(), 0777);
if (rc < 0) {
GUI::MessageBox::show(window, String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GUI::MessageBox::Type::Error);
@ -338,12 +338,12 @@ int run_in_windowed_mode(RefPtr<Core::ConfigFile> config, String initial_locatio
});
auto touch_action = GUI::Action::create("New file...", { Mod_Ctrl | Mod_Shift, Key_F }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
auto input_box = GUI::InputBox::construct("Enter name:", "New file", window);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
String value;
if (GUI::InputBox::show(value, window, "Enter name:", "New file") == GUI::InputBox::ExecOK && !value.is_empty()) {
auto new_file_path = LexicalPath::canonicalized_path(
String::format("%s/%s",
directory_view.path().characters(),
input_box->text_value().characters()));
value.characters()));
struct stat st;
int rc = stat(new_file_path.characters(), &st);
if ((rc < 0 && errno != ENOENT)) {

View File

@ -77,9 +77,9 @@ HexEditorWidget::HexEditorWidget()
m_save_action->activate();
}
auto input_box = GUI::InputBox::construct("Enter new file size:", "New file size", window());
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
auto file_size = input_box->text_value().to_int();
String value;
if (GUI::InputBox::show(value, window(), "Enter new file size:", "New file size") == GUI::InputBox::ExecOK && !value.is_empty()) {
auto file_size = value.to_int();
if (file_size.has_value() && file_size.value() > 0) {
m_document_dirty = false;
m_editor->set_buffer(ByteBuffer::create_zeroed(file_size.value()));
@ -143,27 +143,27 @@ HexEditorWidget::HexEditorWidget()
}));
m_goto_decimal_offset_action = GUI::Action::create("Go To Offset (Decimal)...", { Mod_Ctrl | Mod_Shift, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GUI::Action&) {
auto input_box = GUI::InputBox::construct("Enter Decimal offset:", "Go To", window());
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
auto new_offset = input_box->text_value().to_int();
String value;
if (GUI::InputBox::show(value, window(), "Enter Decimal offset:", "Go To") == GUI::InputBox::ExecOK && !value.is_empty()) {
auto new_offset = value.to_int();
if (new_offset.has_value())
m_editor->set_position(new_offset.value());
}
});
m_goto_hex_offset_action = GUI::Action::create("Go To Offset (Hex)...", { Mod_Ctrl, Key_G }, Gfx::Bitmap::load_from_file("/res/icons/16x16/go-forward.png"), [this](const GUI::Action&) {
auto input_box = GUI::InputBox::construct("Enter Hex offset:", "Go To", window());
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
auto new_offset = strtol(input_box->text_value().characters(), nullptr, 16);
String value;
if (GUI::InputBox::show(value, window(), "Enter Hex offset:", "Go To") == GUI::InputBox::ExecOK && !value.is_empty()) {
auto new_offset = strtol(value.characters(), nullptr, 16);
m_editor->set_position(new_offset);
}
});
auto& edit_menu = menubar->add_menu("Edit");
edit_menu.add_action(GUI::Action::create("Fill selection...", { Mod_Ctrl, Key_B }, [&](const GUI::Action&) {
auto input_box = GUI::InputBox::construct("Fill byte (hex):", "Fill Selection", window());
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty()) {
auto fill_byte = strtol(input_box->text_value().characters(), nullptr, 16);
String value;
if (GUI::InputBox::show(value, window(), "Fill byte (hex):", "Fill Selection") == GUI::InputBox::ExecOK && !value.is_empty()) {
auto fill_byte = strtol(value.characters(), nullptr, 16);
m_editor->fill_selection(fill_byte);
}
}));

View File

@ -94,12 +94,11 @@ void IRCAppWindow::setup_client()
};
if (m_client->hostname().is_empty()) {
auto input_box = GUI::InputBox::construct("Enter server:", "Connect to server", this);
auto result = input_box->exec();
if (result == GUI::InputBox::ExecCancel)
String value;
if (GUI::InputBox::show(value, this, "Enter server:", "Connect to server") == GUI::InputBox::ExecCancel)
::exit(0);
m_client->set_server(input_box->text_value(), 6667);
m_client->set_server(value, 6667);
}
update_title();
bool success = m_client->connect();
@ -109,9 +108,9 @@ void IRCAppWindow::setup_client()
void IRCAppWindow::setup_actions()
{
m_join_action = GUI::Action::create("Join channel", { Mod_Ctrl, Key_J }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-join.png"), [&](auto&) {
auto input_box = GUI::InputBox::construct("Enter channel name:", "Join channel", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_join_action(input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter channel name:", "Join channel") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_join_action(value);
});
m_list_channels_action = GUI::Action::create("List channels", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-list.png"), [&](auto&) {
@ -127,15 +126,15 @@ void IRCAppWindow::setup_actions()
});
m_whois_action = GUI::Action::create("Whois user", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-whois.png"), [&](auto&) {
auto input_box = GUI::InputBox::construct("Enter nickname:", "IRC WHOIS lookup", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_whois_action(input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter nickname:", "IRC WHOIS lookup") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_whois_action(value);
});
m_open_query_action = GUI::Action::create("Open query", { Mod_Ctrl, Key_O }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-open-query.png"), [&](auto&) {
auto input_box = GUI::InputBox::construct("Enter nickname:", "Open IRC query with...", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_open_query_action(input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter nickname:", "Open IRC query with...") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_open_query_action(value);
});
m_close_query_action = GUI::Action::create("Close query", { Mod_Ctrl, Key_D }, Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-close-query.png"), [](auto&) {
@ -143,9 +142,9 @@ void IRCAppWindow::setup_actions()
});
m_change_nick_action = GUI::Action::create("Change nickname", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-nick.png"), [this](auto&) {
auto input_box = GUI::InputBox::construct("Enter nickname:", "Change nickname", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_change_nick_action(input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter nickname:", "Change nickname") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_change_nick_action(value);
});
m_change_topic_action = GUI::Action::create("Change topic", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-topic.png"), [this](auto&) {
@ -153,9 +152,9 @@ void IRCAppWindow::setup_actions()
if (!window || window->type() != IRCWindow::Type::Channel) {
return;
}
auto input_box = GUI::InputBox::construct("Enter topic:", "Change topic", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_change_topic_action(window->channel().name(), input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter topic:", "Change topic") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_change_topic_action(window->channel().name(), value);
});
m_invite_user_action = GUI::Action::create("Invite user", Gfx::Bitmap::load_from_file("/res/icons/16x16/irc-invite.png"), [this](auto&) {
@ -163,9 +162,9 @@ void IRCAppWindow::setup_actions()
if (!window || window->type() != IRCWindow::Type::Channel) {
return;
}
auto input_box = GUI::InputBox::construct("Enter nick:", "Invite user", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_invite_user_action(window->channel().name(), input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter nick:", "Invite user") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_invite_user_action(window->channel().name(), value);
});
m_banlist_action = GUI::Action::create("Ban list", [this](auto&) {
@ -181,9 +180,9 @@ void IRCAppWindow::setup_actions()
if (!window || window->type() != IRCWindow::Type::Channel) {
return;
}
auto input_box = GUI::InputBox::construct("Enter nick:", "Voice user", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_voice_user_action(window->channel().name(), input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter nick:", "Voice user") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_voice_user_action(window->channel().name(), value);
});
m_devoice_user_action = GUI::Action::create("DeVoice user", [this](auto&) {
@ -191,9 +190,9 @@ void IRCAppWindow::setup_actions()
if (!window || window->type() != IRCWindow::Type::Channel) {
return;
}
auto input_box = GUI::InputBox::construct("Enter nick:", "DeVoice user", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_devoice_user_action(window->channel().name(), input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter nick:", "DeVoice user") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_devoice_user_action(window->channel().name(), value);
});
m_hop_user_action = GUI::Action::create("Hop user", [this](auto&) {
@ -201,9 +200,9 @@ void IRCAppWindow::setup_actions()
if (!window || window->type() != IRCWindow::Type::Channel) {
return;
}
auto input_box = GUI::InputBox::construct("Enter nick:", "Hop user", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_hop_user_action(window->channel().name(), input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter nick:", "Hop user") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_hop_user_action(window->channel().name(), value);
});
m_dehop_user_action = GUI::Action::create("DeHop user", [this](auto&) {
@ -211,9 +210,9 @@ void IRCAppWindow::setup_actions()
if (!window || window->type() != IRCWindow::Type::Channel) {
return;
}
auto input_box = GUI::InputBox::construct("Enter nick:", "DeHop user", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_dehop_user_action(window->channel().name(), input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter nick:", "DeHop user") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_dehop_user_action(window->channel().name(), value);
});
m_op_user_action = GUI::Action::create("Op user", [this](auto&) {
@ -221,9 +220,9 @@ void IRCAppWindow::setup_actions()
if (!window || window->type() != IRCWindow::Type::Channel) {
return;
}
auto input_box = GUI::InputBox::construct("Enter nick:", "Op user", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_op_user_action(window->channel().name(), input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter nick:", "Op user") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_op_user_action(window->channel().name(), value);
});
m_deop_user_action = GUI::Action::create("DeOp user", [this](auto&) {
@ -231,9 +230,9 @@ void IRCAppWindow::setup_actions()
if (!window || window->type() != IRCWindow::Type::Channel) {
return;
}
auto input_box = GUI::InputBox::construct("Enter nick:", "DeOp user", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
m_client->handle_deop_user_action(window->channel().name(), input_box->text_value());
String value;
if (GUI::InputBox::show(value, this, "Enter nick:", "DeOp user") == GUI::InputBox::ExecOK && !value.is_empty())
m_client->handle_deop_user_action(window->channel().name(), value);
});
m_kick_user_action = GUI::Action::create("Kick user", [this](auto&) {
@ -241,11 +240,12 @@ void IRCAppWindow::setup_actions()
if (!window || window->type() != IRCWindow::Type::Channel) {
return;
}
auto input_box = GUI::InputBox::construct("Enter nick:", "Kick user", this);
auto reason_box = GUI::InputBox::construct("Enter reason:", "Reason", this);
if (input_box->exec() == GUI::InputBox::ExecOK && !input_box->text_value().is_empty())
if (reason_box->exec() == GUI::InputBox::ExecOK)
m_client->handle_kick_user_action(window->channel().name(), input_box->text_value(), reason_box->text_value().characters());
String nick_value;
if (GUI::InputBox::show(nick_value, this, "Enter nick:", "Kick user") != GUI::InputBox::ExecOK || nick_value.is_empty())
return;
String reason_value;
if (GUI::InputBox::show(reason_value, this, "Enter reason:", "Reason") == GUI::InputBox::ExecOK)
m_client->handle_kick_user_action(window->channel().name(), nick_value, reason_value.characters());
});
m_cycle_channel_action = GUI::Action::create("Cycle channel", [this](auto&) {

View File

@ -96,7 +96,6 @@ IRCWindow::IRCWindow(IRCClient& client, void* owner, Type type, const String& na
auto nick = channel().member_model()->nick_at(member_view.selection().first());
if (nick.is_empty())
return;
auto input_box = GUI::InputBox::construct("Enter reason:", "Reason");
m_client.handle_voice_user_action(m_name.characters(), m_client.nick_without_prefix(nick.characters()));
}));
@ -143,9 +142,9 @@ IRCWindow::IRCWindow(IRCClient& client, void* owner, Type type, const String& na
return;
if (IRCClient::is_nick_prefix(nick[0]))
nick = nick.substring(1, nick.length() - 1);
auto input_box = GUI::InputBox::construct("Enter reason:", "Reason");
if (input_box->exec() == GUI::InputBox::ExecOK)
m_client.handle_kick_user_action(m_name.characters(), m_client.nick_without_prefix(nick.characters()), input_box->text_value());
String value;
if (GUI::InputBox::show(value, window(), "Enter reason:", "Reason") == GUI::InputBox::ExecOK)
m_client.handle_kick_user_action(m_name.characters(), m_client.nick_without_prefix(nick.characters()), value);
}));
auto& context_ctcp_menu = m_context_menu->add_submenu("CTCP");

View File

@ -66,10 +66,8 @@ void KeyboardMapperWidget::create_frame()
tmp_button.set_enabled(keys[i].enabled);
tmp_button.on_click = [&]() {
auto input_box = GUI::InputBox::construct("New Character:", "Select Character", window());
if (input_box->exec() == GUI::InputBox::ExecOK) {
auto value = input_box->text_value();
String value;
if (GUI::InputBox::show(value, window(), "New Character:", "Select Character") == GUI::InputBox::ExecOK) {
int i = m_keys.find_first_index(&tmp_button).value_or(0);
ASSERT(i > 0);

View File

@ -84,11 +84,10 @@ DebugInfoWidget::DebugInfoWidget()
if (!is_valid_index(index))
return;
auto input = GUI::InputBox::construct("Enter new value:", "Set variable value", window());
if (input->exec() == GUI::InputBox::ExecOK) {
String value;
if (GUI::InputBox::show(value, window(), "Enter new value:", "Set variable value") == GUI::InputBox::ExecOK) {
auto& model = static_cast<VariablesModel&>(*m_variables_view->model());
model.set_variable_value(index, input->text_value(), window());
model.set_variable_value(index, value, window());
}
};
}

View File

@ -203,10 +203,9 @@ int main(int argc, char** argv)
};
auto new_action = GUI::Action::create("Add new file to project...", { Mod_Ctrl, Key_N }, Gfx::Bitmap::load_from_file("/res/icons/16x16/new.png"), [&](const GUI::Action&) {
auto input_box = GUI::InputBox::construct("Enter name of new file:", "Add new file to project", g_window);
if (input_box->exec() == GUI::InputBox::ExecCancel)
String filename;
if (GUI::InputBox::show(filename, g_window, "Enter name of new file:", "Add new file to project") != GUI::InputBox::ExecOK)
return;
auto filename = input_box->text_value();
auto file = Core::File::construct(filename);
if (!file->open((Core::IODevice::OpenMode)(Core::IODevice::WriteOnly | Core::IODevice::MustBeNew))) {
GUI::MessageBox::show(g_window, String::format("Failed to create '%s'", filename.characters()), "Error", GUI::MessageBox::Type::Error);

View File

@ -152,11 +152,11 @@ FilePicker::FilePicker(Window* parent_window, Mode mode, Options options, const
toolbar.add_separator();
auto mkdir_action = Action::create("New directory...", Gfx::Bitmap::load_from_file("/res/icons/16x16/mkdir.png"), [this](const Action&) {
auto& input_box = add<InputBox>("Enter name:", "New directory");
if (input_box.exec() == InputBox::ExecOK && !input_box.text_value().is_empty()) {
String value;
if (InputBox::show(value, this, "Enter name:", "New directory") == InputBox::ExecOK && !value.is_empty()) {
auto new_dir_path = LexicalPath(String::format("%s/%s",
m_model->root_path().characters(),
input_box.text_value().characters()))
value.characters()))
.string();
int rc = mkdir(new_dir_path.characters(), 0777);
if (rc < 0) {

View File

@ -34,7 +34,7 @@
namespace GUI {
InputBox::InputBox(const StringView& prompt, const StringView& title, GUI::Window* parent_window)
InputBox::InputBox(Window* parent_window, const StringView& prompt, const StringView& title)
: Dialog(parent_window)
, m_prompt(prompt)
{
@ -46,6 +46,14 @@ InputBox::~InputBox()
{
}
int InputBox::show(String& text_value, Window* parent_window, const StringView& prompt, const StringView& title)
{
auto box = InputBox::construct(parent_window, prompt, title);
auto result = box->exec();
text_value = box->text_value();
return result;
}
void InputBox::build()
{
auto& widget = set_main_widget<Widget>();

View File

@ -33,12 +33,15 @@ namespace GUI {
class InputBox : public Dialog {
C_OBJECT(InputBox)
public:
explicit InputBox(const StringView& prompt, const StringView& title, Window* parent_window = nullptr);
virtual ~InputBox() override;
static int show(String& text_value, Window* parent_window, const StringView& prompt, const StringView& title);
private:
explicit InputBox(Window* parent_window, const StringView& prompt, const StringView& title);
String text_value() const { return m_text_value; }
private:
void build();
String m_prompt;
String m_text_value;

View File

@ -87,10 +87,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-forward.png"), [this](auto&) {
auto input_box = InputBox::construct("Line:", "Go to line", window());
auto result = input_box->exec();
if (result == InputBox::ExecOK) {
auto line_number = input_box->text_value().to_uint();
String value;
if (InputBox::show(value, window(), "Line:", "Go to line") == InputBox::ExecOK) {
auto line_number = value.to_uint();
if (line_number.has_value())
set_cursor(line_number.value() - 1, 0);
}