1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 17:17:42 +00:00

LibGUI: Allow InputBox to show placeholder

If passed as an argument, showing an InputBox now supports to
set a placeholder text which will be shown in its TextBox.
This commit is contained in:
networkException 2021-07-06 16:47:44 +02:00 committed by Andreas Kling
parent f8798c154b
commit 72776b2f61
2 changed files with 12 additions and 5 deletions

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -14,10 +15,11 @@
namespace GUI { namespace GUI {
InputBox::InputBox(Window* parent_window, String& text_value, const StringView& prompt, const StringView& title) InputBox::InputBox(Window* parent_window, String& text_value, const StringView& prompt, const StringView& title, const StringView& placeholder)
: Dialog(parent_window) : Dialog(parent_window)
, m_text_value(text_value) , m_text_value(text_value)
, m_prompt(prompt) , m_prompt(prompt)
, m_placeholder(placeholder)
{ {
set_title(title); set_title(title);
build(); build();
@ -27,9 +29,9 @@ InputBox::~InputBox()
{ {
} }
int InputBox::show(Window* parent_window, String& text_value, const StringView& prompt, const StringView& title) int InputBox::show(Window* parent_window, String& text_value, const StringView& prompt, const StringView& title, const StringView& placeholder)
{ {
auto box = InputBox::construct(parent_window, text_value, prompt, title); auto box = InputBox::construct(parent_window, text_value, prompt, title, placeholder);
box->set_resizable(false); box->set_resizable(false);
if (parent_window) if (parent_window)
box->set_icon(parent_window->icon()); box->set_icon(parent_window->icon());
@ -64,6 +66,9 @@ void InputBox::build()
m_text_editor->set_fixed_height(19); m_text_editor->set_fixed_height(19);
m_text_editor->set_text(m_text_value); m_text_editor->set_text(m_text_value);
if (!m_placeholder.is_null())
m_text_editor->set_placeholder(m_placeholder);
auto& button_container_outer = widget.add<Widget>(); auto& button_container_outer = widget.add<Widget>();
button_container_outer.set_fixed_height(20); button_container_outer.set_fixed_height(20);
button_container_outer.set_layout<VerticalBoxLayout>(); button_container_outer.set_layout<VerticalBoxLayout>();

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Jakob-Niklas See <git@nwex.de>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -15,16 +16,17 @@ class InputBox : public Dialog {
public: public:
virtual ~InputBox() override; virtual ~InputBox() override;
static int show(Window* parent_window, String& text_value, const StringView& prompt, const StringView& title); static int show(Window* parent_window, String& text_value, const StringView& prompt, const StringView& title, const StringView& placeholder = {});
private: private:
explicit InputBox(Window* parent_window, String& text_value, const StringView& prompt, const StringView& title); explicit InputBox(Window* parent_window, String& text_value, const StringView& prompt, const StringView& title, const StringView& placeholder);
String text_value() const { return m_text_value; } String text_value() const { return m_text_value; }
void build(); void build();
String m_text_value; String m_text_value;
String m_prompt; String m_prompt;
String m_placeholder;
RefPtr<Button> m_ok_button; RefPtr<Button> m_ok_button;
RefPtr<Button> m_cancel_button; RefPtr<Button> m_cancel_button;