diff --git a/Userland/Libraries/LibGUI/Dialog.cpp b/Userland/Libraries/LibGUI/Dialog.cpp index 3d6767010a..199ea47326 100644 --- a/Userland/Libraries/LibGUI/Dialog.cpp +++ b/Userland/Libraries/LibGUI/Dialog.cpp @@ -115,6 +115,8 @@ void Dialog::done(ExecResult result) if (!m_event_loop) return; m_result = result; + on_done(m_result); + dbgln("{}: Quit event loop with result {}", *this, to_underlying(result)); m_event_loop->quit(to_underlying(result)); } diff --git a/Userland/Libraries/LibGUI/Dialog.h b/Userland/Libraries/LibGUI/Dialog.h index 4f7b869932..a78b9536a3 100644 --- a/Userland/Libraries/LibGUI/Dialog.h +++ b/Userland/Libraries/LibGUI/Dialog.h @@ -52,6 +52,8 @@ public: protected: explicit Dialog(Window* parent_window, ScreenPosition = ScreenPosition::CenterWithinParent); + virtual void on_done(ExecResult) { } + private: OwnPtr m_event_loop; ExecResult m_result { ExecResult::Aborted }; diff --git a/Userland/Libraries/LibGUI/InputBox.cpp b/Userland/Libraries/LibGUI/InputBox.cpp index 0fffa1e9dc..1bc8b9478e 100644 --- a/Userland/Libraries/LibGUI/InputBox.cpp +++ b/Userland/Libraries/LibGUI/InputBox.cpp @@ -15,9 +15,9 @@ namespace GUI { -InputBox::InputBox(Window* parent_window, String& text_value, StringView prompt, StringView title, StringView placeholder, InputType input_type) +InputBox::InputBox(Window* parent_window, String text_value, StringView prompt, StringView title, StringView placeholder, InputType input_type) : Dialog(parent_window) - , m_text_value(text_value) + , m_text_value(move(text_value)) , m_prompt(prompt) , m_placeholder(placeholder) { @@ -36,6 +36,17 @@ Dialog::ExecResult InputBox::show(Window* parent_window, String& text_value, Str return result; } +void InputBox::set_text_value(String text_value) +{ + m_text_editor->set_text(move(text_value)); +} + +void InputBox::on_done(ExecResult result) +{ + if (result == ExecResult::OK) + m_text_value = m_text_editor->text(); +} + void InputBox::build(InputType input_type) { auto& widget = set_main_widget(); @@ -86,7 +97,6 @@ void InputBox::build(InputType input_type) m_ok_button->set_text("OK"); m_ok_button->on_click = [this](auto) { dbgln("GUI::InputBox: OK button clicked"); - m_text_value = m_text_editor->text(); done(ExecResult::OK); }; m_ok_button->set_default(true); diff --git a/Userland/Libraries/LibGUI/InputBox.h b/Userland/Libraries/LibGUI/InputBox.h index f34c542035..2c15bda02f 100644 --- a/Userland/Libraries/LibGUI/InputBox.h +++ b/Userland/Libraries/LibGUI/InputBox.h @@ -24,12 +24,15 @@ public: static ExecResult show(Window* parent_window, String& text_value, StringView prompt, StringView title, StringView placeholder = {}, InputType input_type = InputType::Text); + String const& text_value() const { return m_text_value; } + void set_text_value(String text_value); + private: - explicit InputBox(Window* parent_window, String& text_value, StringView prompt, StringView title, StringView placeholder, InputType input_type); - - String text_value() const { return m_text_value; } + explicit InputBox(Window* parent_window, String text_value, StringView prompt, StringView title, StringView placeholder, InputType input_type); + virtual void on_done(ExecResult) override; void build(InputType input_type); + String m_text_value; String m_prompt; String m_placeholder;