1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 06:07:44 +00:00

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
11 changed files with 99 additions and 94 deletions

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