/** * @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 {}; } }