diff --git a/gui/libui/CMakeLists.txt b/gui/libui/CMakeLists.txt index fb325b54..b40db454 100644 --- a/gui/libui/CMakeLists.txt +++ b/gui/libui/CMakeLists.txt @@ -19,6 +19,7 @@ set(SOURCES src/Label.cpp src/InputField.cpp src/TextInput.cpp + src/Dialog.cpp ) add_library(ui ${SOURCES}) diff --git a/gui/libui/include/ui/Dialog.h b/gui/libui/include/ui/Dialog.h new file mode 100644 index 00000000..eb541dff --- /dev/null +++ b/gui/libui/include/ui/Dialog.h @@ -0,0 +1,22 @@ +/** + * @file Window.h + * @author apio (cloudapio.eu) + * @brief UI window dialogs. + * + * @copyright Copyright (c) 2024, the Luna authors. + * + */ + +#pragma once +#include +#include + +namespace ui +{ + namespace Dialog + { + Result show_message(StringView title, StringView message); + + Result show_input_dialog(StringView title, StringView message, os::Function callback); + } +} diff --git a/gui/libui/src/Dialog.cpp b/gui/libui/src/Dialog.cpp new file mode 100644 index 00000000..f3fc9af0 --- /dev/null +++ b/gui/libui/src/Dialog.cpp @@ -0,0 +1,82 @@ +/** + * @file Dialog.cpp + * @author apio (cloudapio.eu) + * @brief UI window dialogs. + * + * @copyright Copyright (c) 2024, the Luna authors. + * + */ + +#include +#include +#include +#include +#include +#include + +namespace ui::Dialog +{ + Result show_message(StringView title, StringView message) + { + auto rect = ui::App::the().main_window()->canvas().rect(); + int text_length = (int)message.length() * ui::Font::default_font()->width(); + int text_height = ui::Font::default_font()->height(); + + ui::Rect dialog_rect = { 0, 0, text_length + 20, text_height + 20 }; + + auto* dialog = TRY(ui::Window::create( + ui::align(rect, dialog_rect, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center))); + + dialog->set_background(ui::GRAY); + dialog->set_title(title); + + ui::Label* text = TRY(make(message)); + text->set_color(ui::BLACK); + dialog->set_main_widget(*text); + + dialog->on_close([text] { delete text; }); + + dialog->draw(); + + return {}; + } + + Result show_input_dialog(StringView title, StringView message, os::Function callback) + { + auto rect = ui::App::the().main_window()->canvas().rect(); + int text_length = (int)message.length() * ui::Font::default_font()->width(); + int text_height = ui::Font::default_font()->height(); + + ui::Rect dialog_rect = { 0, 0, max(text_length + 20, 300), text_height * 2 + 30 }; + + auto* dialog = TRY(ui::Window::create( + ui::align(rect, dialog_rect, ui::VerticalAlignment::Center, ui::HorizontalAlignment::Center))); + + dialog->set_background(ui::GRAY); + dialog->set_title(title); + + ui::VerticalLayout* layout = TRY(make()); + dialog->set_main_widget(*layout); + + ui::Label* text = TRY(make((message))); + text->set_color(ui::BLACK); + layout->add_widget(*text); + + ui::InputField* input = TRY(make(ui::Font::default_font())); + input->on_submit([dialog, callback](StringView s) { + callback(s); + dialog->close(); + }); + layout->add_widget(*input); + + dialog->on_close([layout, text, input] { + delete text; + delete input; + delete layout; + }); + + dialog->draw(); + + return {}; + } +}