LibGUI: Allow more programmatic control over GUI::InputBox

This will be needed for WebDriver, which will require constructing and
controlling dialogs manually. Currently, InputBox will only set its text
value when the OK button is pressed. This changes InputBox to update its
text when done(ExecResult::OK) is invoked in any way.

This also makes the text_value() method public, allows for setting the
text value, and allows for moving-in the initial text value.
This commit is contained in:
Timothy Flynn 2022-11-16 07:35:02 -05:00 committed by Linus Groh
parent db4fa36b58
commit 5b31a3dbc7
Notes: sideshowbarker 2024-07-17 07:48:42 +09:00
4 changed files with 23 additions and 6 deletions

View file

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

View file

@ -52,6 +52,8 @@ public:
protected:
explicit Dialog(Window* parent_window, ScreenPosition = ScreenPosition::CenterWithinParent);
virtual void on_done(ExecResult) { }
private:
OwnPtr<Core::EventLoop> m_event_loop;
ExecResult m_result { ExecResult::Aborted };

View file

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

View file

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