mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 03:37:43 +00:00
LibGUI: Let InputBox display an ImageWidget
InputBox can now be given a bitmap to display alongside its prompt and editor. Prompts are now optional to allow for compact dialogs.
This commit is contained in:
parent
02a9e5d3f6
commit
7c314f3855
2 changed files with 38 additions and 16 deletions
|
@ -8,38 +8,40 @@
|
||||||
|
|
||||||
#include <LibGUI/BoxLayout.h>
|
#include <LibGUI/BoxLayout.h>
|
||||||
#include <LibGUI/Button.h>
|
#include <LibGUI/Button.h>
|
||||||
|
#include <LibGUI/ImageWidget.h>
|
||||||
#include <LibGUI/InputBox.h>
|
#include <LibGUI/InputBox.h>
|
||||||
#include <LibGUI/Label.h>
|
#include <LibGUI/Label.h>
|
||||||
#include <LibGUI/TextBox.h>
|
#include <LibGUI/TextBox.h>
|
||||||
|
|
||||||
namespace GUI {
|
namespace GUI {
|
||||||
|
|
||||||
ErrorOr<NonnullRefPtr<InputBox>> InputBox::create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type)
|
ErrorOr<NonnullRefPtr<InputBox>> InputBox::create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type, RefPtr<Gfx::Bitmap const> icon)
|
||||||
{
|
{
|
||||||
auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) InputBox(parent_window, text_value, TRY(String::from_utf8(title)), TRY(String::from_utf8(prompt)), input_type)));
|
auto box = TRY(adopt_nonnull_ref_or_enomem(new (nothrow) InputBox(parent_window, text_value, TRY(String::from_utf8(title)), TRY(String::from_utf8(prompt)), input_type, move(icon))));
|
||||||
TRY(box->build());
|
TRY(box->build());
|
||||||
return box;
|
return box;
|
||||||
}
|
}
|
||||||
|
|
||||||
InputBox::InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type)
|
InputBox::InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type, RefPtr<Gfx::Bitmap const> icon)
|
||||||
: Dialog(parent_window)
|
: Dialog(parent_window)
|
||||||
, m_text_value(move(text_value))
|
, m_text_value(move(text_value))
|
||||||
, m_prompt(move(prompt))
|
, m_prompt(move(prompt))
|
||||||
, m_input_type(input_type)
|
, m_input_type(input_type)
|
||||||
|
, m_icon(move(icon))
|
||||||
{
|
{
|
||||||
set_title(move(title).to_deprecated_string());
|
set_title(move(title).to_deprecated_string());
|
||||||
set_resizable(false);
|
set_resizable(false);
|
||||||
set_auto_shrink(true);
|
set_auto_shrink(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
Dialog::ExecResult InputBox::show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder)
|
Dialog::ExecResult InputBox::show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder, RefPtr<Gfx::Bitmap const> icon)
|
||||||
{
|
{
|
||||||
return MUST(try_show(parent_window, text_value, prompt, title, input_type, placeholder));
|
return MUST(try_show(parent_window, text_value, prompt, title, input_type, placeholder, move(icon)));
|
||||||
}
|
}
|
||||||
|
|
||||||
ErrorOr<Dialog::ExecResult> InputBox::try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder)
|
ErrorOr<Dialog::ExecResult> InputBox::try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type, StringView placeholder, RefPtr<Gfx::Bitmap const> icon)
|
||||||
{
|
{
|
||||||
auto box = TRY(InputBox::create(parent_window, text_value, prompt, title, input_type));
|
auto box = TRY(InputBox::create(parent_window, text_value, prompt, title, input_type, move(icon)));
|
||||||
if (parent_window)
|
if (parent_window)
|
||||||
box->set_icon(parent_window->icon());
|
box->set_icon(parent_window->icon());
|
||||||
box->set_placeholder(placeholder);
|
box->set_placeholder(placeholder);
|
||||||
|
@ -86,15 +88,29 @@ ErrorOr<void> InputBox::build()
|
||||||
TRY(main_widget->try_set_layout<VerticalBoxLayout>(4, 6));
|
TRY(main_widget->try_set_layout<VerticalBoxLayout>(4, 6));
|
||||||
main_widget->set_fill_with_background_color(true);
|
main_widget->set_fill_with_background_color(true);
|
||||||
|
|
||||||
auto input_container = TRY(main_widget->try_add<Widget>());
|
auto top_container = TRY(main_widget->try_add<Widget>());
|
||||||
input_container->set_layout<HorizontalBoxLayout>();
|
TRY(top_container->try_set_layout<HorizontalBoxLayout>(0, 8));
|
||||||
|
|
||||||
m_prompt_label = TRY(input_container->try_add<Label>());
|
if (m_icon) {
|
||||||
m_prompt_label->set_autosize(true);
|
auto image_widget = TRY(top_container->try_add<ImageWidget>());
|
||||||
m_prompt_label->set_text(move(m_prompt).to_deprecated_string());
|
image_widget->set_bitmap(m_icon);
|
||||||
|
}
|
||||||
|
|
||||||
|
auto input_container = TRY(top_container->try_add<Widget>());
|
||||||
|
auto orientation = m_icon ? Gfx::Orientation::Vertical : Gfx::Orientation::Horizontal;
|
||||||
|
TRY(input_container->try_set_layout<BoxLayout>(orientation));
|
||||||
TRY(input_container->add_spacer());
|
TRY(input_container->add_spacer());
|
||||||
|
|
||||||
|
if (!m_prompt.is_empty()) {
|
||||||
|
m_label_container = TRY(input_container->try_add<Widget>());
|
||||||
|
TRY(m_label_container->try_set_layout<HorizontalBoxLayout>());
|
||||||
|
m_prompt_label = TRY(m_label_container->try_add<Label>());
|
||||||
|
m_prompt_label->set_autosize(true);
|
||||||
|
m_prompt_label->set_text_wrapping(Gfx::TextWrapping::DontWrap);
|
||||||
|
m_prompt_label->set_text_alignment(Gfx::TextAlignment::CenterLeft);
|
||||||
|
m_prompt_label->set_text(move(m_prompt).to_deprecated_string());
|
||||||
|
}
|
||||||
|
|
||||||
switch (m_input_type) {
|
switch (m_input_type) {
|
||||||
case InputType::Text:
|
case InputType::Text:
|
||||||
case InputType::NonemptyText:
|
case InputType::NonemptyText:
|
||||||
|
@ -105,6 +121,8 @@ ErrorOr<void> InputBox::build()
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TRY(input_container->add_spacer());
|
||||||
|
|
||||||
auto button_container = TRY(main_widget->try_add<Widget>());
|
auto button_container = TRY(main_widget->try_add<Widget>());
|
||||||
TRY(button_container->try_set_layout<HorizontalBoxLayout>(0, 6));
|
TRY(button_container->try_set_layout<HorizontalBoxLayout>(0, 6));
|
||||||
TRY(button_container->add_spacer());
|
TRY(button_container->add_spacer());
|
||||||
|
@ -119,6 +137,8 @@ ErrorOr<void> InputBox::build()
|
||||||
auto resize_editor = [this, button_container] {
|
auto resize_editor = [this, button_container] {
|
||||||
auto width = button_container->effective_min_size().width().as_int();
|
auto width = button_container->effective_min_size().width().as_int();
|
||||||
m_text_editor->set_min_width(width);
|
m_text_editor->set_min_width(width);
|
||||||
|
if (!m_icon && m_label_container)
|
||||||
|
m_label_container->set_fixed_width(m_prompt_label->max_width());
|
||||||
};
|
};
|
||||||
resize_editor();
|
resize_editor();
|
||||||
on_font_change = [resize_editor] { resize_editor(); };
|
on_font_change = [resize_editor] { resize_editor(); };
|
||||||
|
|
|
@ -23,9 +23,9 @@ class InputBox : public Dialog {
|
||||||
public:
|
public:
|
||||||
virtual ~InputBox() override = default;
|
virtual ~InputBox() override = default;
|
||||||
|
|
||||||
static ExecResult show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type = InputType::Text, StringView placeholder = {});
|
static ExecResult show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type = InputType::Text, StringView placeholder = {}, RefPtr<Gfx::Bitmap const> icon = nullptr);
|
||||||
static ErrorOr<ExecResult> try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type = InputType::Text, StringView placeholder = {});
|
static ErrorOr<ExecResult> try_show(Window* parent_window, String& text_value, StringView prompt, StringView title, InputType input_type = InputType::Text, StringView placeholder = {}, RefPtr<Gfx::Bitmap const> icon = nullptr);
|
||||||
static ErrorOr<NonnullRefPtr<InputBox>> create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type);
|
static ErrorOr<NonnullRefPtr<InputBox>> create(Window* parent_window, String text_value, StringView prompt, StringView title, InputType input_type, RefPtr<Gfx::Bitmap const> icon = nullptr);
|
||||||
|
|
||||||
String const& text_value() const { return m_text_value; }
|
String const& text_value() const { return m_text_value; }
|
||||||
void set_text_value(String);
|
void set_text_value(String);
|
||||||
|
@ -33,7 +33,7 @@ public:
|
||||||
void set_placeholder(StringView);
|
void set_placeholder(StringView);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type);
|
InputBox(Window* parent_window, String text_value, String title, String prompt, InputType input_type, RefPtr<Gfx::Bitmap const> icon);
|
||||||
|
|
||||||
virtual void on_done(ExecResult) override;
|
virtual void on_done(ExecResult) override;
|
||||||
ErrorOr<void> build();
|
ErrorOr<void> build();
|
||||||
|
@ -46,6 +46,8 @@ private:
|
||||||
RefPtr<Button> m_cancel_button;
|
RefPtr<Button> m_cancel_button;
|
||||||
RefPtr<TextEditor> m_text_editor;
|
RefPtr<TextEditor> m_text_editor;
|
||||||
RefPtr<Label> m_prompt_label;
|
RefPtr<Label> m_prompt_label;
|
||||||
|
RefPtr<Widget> m_label_container;
|
||||||
|
RefPtr<Gfx::Bitmap const> m_icon;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue