1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 19:18:12 +00:00

LibGUI: Add input types to GMessageBox.

Currently the two available input types are:

- GMessageBox::InputType::OK (default)
- GMessageBox::InputType::OKCancel

Based on your choice, GMessageBox::exec() will return ExecOK or ExecCancel.
This commit is contained in:
Andreas Kling 2019-07-16 21:32:10 +02:00
parent 62335c5f0c
commit a17fbd98e7
8 changed files with 63 additions and 26 deletions

View file

@ -40,7 +40,7 @@ Optional<String> GFilePicker::get_save_filepath()
if (GFilePicker::file_exists(file_path)) {
//TODO: Add Yes, No Messagebox to give the user a proper option
GMessageBox::show("File already exists: Overwrite?\n", "Warning", GMessageBox::Type::Warning, &picker);
GMessageBox::show("File already exists: Overwrite?\n", "Warning", GMessageBox::Type::Warning, GMessageBox::InputType::OK, &picker);
return file_path;
}
@ -110,7 +110,7 @@ GFilePicker::GFilePicker(Mode mode, const StringView& path, CObject* parent)
.string();
int rc = mkdir(new_dir_path.characters(), 0777);
if (rc < 0) {
GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, this);
GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, this);
} else {
m_model->update();
}
@ -244,4 +244,4 @@ bool GFilePicker::file_exists(const StringView& path)
return true;
}
return false;
}
}

View file

@ -4,16 +4,17 @@
#include <LibGUI/GMessageBox.h>
#include <stdio.h>
void GMessageBox::show(const StringView& text, const StringView& title, Type type, CObject* parent)
void GMessageBox::show(const StringView& text, const StringView& title, Type type, InputType input_type, CObject* parent)
{
GMessageBox box(text, title, type, parent);
GMessageBox box(text, title, type, input_type, parent);
box.exec();
}
GMessageBox::GMessageBox(const StringView& text, const StringView& title, Type type, CObject* parent)
GMessageBox::GMessageBox(const StringView& text, const StringView& title, Type type, InputType input_type, CObject* parent)
: GDialog(parent)
, m_text(text)
, m_type(type)
, m_input_type(input_type)
{
set_title(title);
build();
@ -37,6 +38,16 @@ RefPtr<GraphicsBitmap> GMessageBox::icon() const
}
}
bool GMessageBox::should_include_ok_button() const
{
return m_input_type == InputType::OK || m_input_type == InputType::OKCancel;
}
bool GMessageBox::should_include_cancel_button() const
{
return m_input_type == InputType::OKCancel;
}
void GMessageBox::build()
{
auto* widget = new GWidget;
@ -69,14 +80,32 @@ void GMessageBox::build()
label->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
label->set_preferred_size({ text_width, 16 });
auto* button = new GButton(widget);
button->set_size_policy(SizePolicy::Fixed, SizePolicy::Fixed);
button->set_preferred_size({ 100, 20 });
button->set_text("OK");
button->on_click = [this](auto&) {
dbgprintf("GMessageBox: OK button clicked\n");
done(0);
};
auto* button_container = new GWidget(widget);
button_container->set_layout(make<GBoxLayout>(Orientation::Horizontal));
button_container->layout()->set_spacing(5);
button_container->layout()->set_margins({ 15, 0, 15, 0 });
if (should_include_ok_button()) {
auto* ok_button = new GButton(button_container);
ok_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
ok_button->set_preferred_size({ 0, 20 });
ok_button->set_text("OK");
ok_button->on_click = [this](auto&) {
dbgprintf("GMessageBox: OK button clicked\n");
done(GDialog::ExecOK);
};
}
if (should_include_cancel_button()) {
auto* cancel_button = new GButton(button_container);
cancel_button->set_size_policy(SizePolicy::Fill, SizePolicy::Fixed);
cancel_button->set_preferred_size({ 0, 20 });
cancel_button->set_text("Cancel");
cancel_button->on_click = [this](auto&) {
dbgprintf("GMessageBox: Cancel button clicked\n");
done(GDialog::ExecCancel);
};
}
set_rect(x(), y(), text_width + icon_width + 80, 100);
set_resizable(false);

View file

@ -11,17 +11,25 @@ public:
Error,
};
explicit GMessageBox(const StringView& text, const StringView& title, Type type = Type::None, CObject* parent = nullptr);
enum class InputType {
OK,
OKCancel,
};
explicit GMessageBox(const StringView& text, const StringView& title, Type type = Type::None, InputType = InputType::OK, CObject* parent = nullptr);
virtual ~GMessageBox() override;
static void show(const StringView& text, const StringView& title, Type type = Type::None, CObject* parent = nullptr);
static void show(const StringView& text, const StringView& title, Type type = Type::None, InputType = InputType::OK, CObject* parent = nullptr);
virtual const char* class_name() const override { return "GMessageBox"; }
private:
bool should_include_ok_button() const;
bool should_include_cancel_button() const;
void build();
RefPtr<GraphicsBitmap> icon() const;
String m_text;
Type m_type { Type::None };
InputType m_input_type { InputType::OK };
};