mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 09:30:24 +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:
parent
62335c5f0c
commit
a17fbd98e7
Notes:
sideshowbarker
2024-07-19 13:13:55 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/a17fbd98e73
8 changed files with 63 additions and 26 deletions
|
@ -97,7 +97,7 @@ int main(int argc, char** argv)
|
|||
input_box.text_value().characters()));
|
||||
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, window);
|
||||
GMessageBox::show(String::format("mkdir(\"%s\") failed: %s", new_dir_path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window);
|
||||
} else {
|
||||
directory_view->refresh();
|
||||
}
|
||||
|
|
|
@ -50,7 +50,7 @@ int main(int argc, char** argv)
|
|||
auto filename = picker.selected_file().string();
|
||||
auto bitmap = load_png(filename);
|
||||
if (!bitmap) {
|
||||
GMessageBox msgbox(String::format("Failed to load '%s'", filename.characters()), "Open failed", GMessageBox::Type::Error, window);
|
||||
GMessageBox msgbox(String::format("Failed to load '%s'", filename.characters()), "Open failed", GMessageBox::Type::Error, GMessageBox::InputType::OK, window);
|
||||
msgbox.exec();
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ int run_shutdown_dialog(int argc, char** argv)
|
|||
GApplication app(argc, argv);
|
||||
|
||||
{
|
||||
GMessageBox box("Shut down Serenity?", "Confirm Shutdown", GMessageBox::Type::Warning, GMessageBox::InputType::OkCancel);
|
||||
GMessageBox box("Shut down Serenity?", "Confirm Shutdown", GMessageBox::Type::Warning, GMessageBox::InputType::OKCancel);
|
||||
auto result = box.exec();
|
||||
|
||||
if (result == GMessageBox::ExecOK) {
|
||||
|
|
|
@ -46,7 +46,7 @@ TextEditorWidget::TextEditorWidget()
|
|||
auto save_action = GAction::create("Save document", { Mod_Ctrl, Key_S }, GraphicsBitmap::load_from_file("/res/icons/16x16/save.png"), [this](const GAction&) {
|
||||
if (!m_path.is_empty()) {
|
||||
if (!m_editor->write_to_file(m_path))
|
||||
GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, window());
|
||||
GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||
|
||||
return;
|
||||
}
|
||||
|
@ -56,7 +56,7 @@ TextEditorWidget::TextEditorWidget()
|
|||
return;
|
||||
|
||||
if (!m_editor->write_to_file(save_name.value())) {
|
||||
GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, window());
|
||||
GMessageBox::show("Unable to save file.\n", "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -135,7 +135,7 @@ void TextEditorWidget::open_sesame(const String& path)
|
|||
CFile file(path);
|
||||
|
||||
if (!file.open(CIODevice::ReadOnly)) {
|
||||
GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, window());
|
||||
GMessageBox::show(String::format("Opening \"%s\" failed: %s", path.characters(), strerror(errno)), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||
}
|
||||
|
||||
window()->set_title(String::format("Text Editor: %s", path.characters()));
|
||||
|
|
|
@ -294,7 +294,7 @@ void VBForm::load_from_file(const String& path)
|
|||
{
|
||||
CFile file(path);
|
||||
if (!file.open(CIODevice::ReadOnly)) {
|
||||
GMessageBox::show(String::format("Could not open '%s' for reading", path.characters()), "Error", GMessageBox::Type::Error, window());
|
||||
GMessageBox::show(String::format("Could not open '%s' for reading", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -302,7 +302,7 @@ void VBForm::load_from_file(const String& path)
|
|||
auto form_json = JsonValue::from_string(file_contents);
|
||||
|
||||
if (!form_json.is_object()) {
|
||||
GMessageBox::show(String::format("Could not parse '%s'", path.characters()), "Error", GMessageBox::Type::Error, window());
|
||||
GMessageBox::show(String::format("Could not parse '%s'", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -329,7 +329,7 @@ void VBForm::write_to_file(const String& path)
|
|||
{
|
||||
CFile file(path);
|
||||
if (!file.open(CIODevice::WriteOnly)) {
|
||||
GMessageBox::show(String::format("Could not open '%s' for writing", path.characters()), "Error", GMessageBox::Type::Error, window());
|
||||
GMessageBox::show(String::format("Could not open '%s' for writing", path.characters()), "Error", GMessageBox::Type::Error, GMessageBox::InputType::OK, window());
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 };
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue