83 lines
2.4 KiB
C++
83 lines
2.4 KiB
C++
/**
|
|
* @file Dialog.cpp
|
|
* @author apio (cloudapio.eu)
|
|
* @brief UI window dialogs.
|
|
*
|
|
* @copyright Copyright (c) 2024, the Luna authors.
|
|
*
|
|
*/
|
|
|
|
#include <luna/Alloc.h>
|
|
#include <ui/App.h>
|
|
#include <ui/Dialog.h>
|
|
#include <ui/InputField.h>
|
|
#include <ui/Label.h>
|
|
#include <ui/Layout.h>
|
|
|
|
namespace ui::Dialog
|
|
{
|
|
Result<void> 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<ui::Label>(message));
|
|
text->set_color(ui::BLACK);
|
|
dialog->set_main_widget(*text);
|
|
|
|
dialog->on_close([text] { delete text; });
|
|
|
|
dialog->draw();
|
|
|
|
return {};
|
|
}
|
|
|
|
Result<void> show_input_dialog(StringView title, StringView message, os::Function<StringView> 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<ui::VerticalLayout>());
|
|
dialog->set_main_widget(*layout);
|
|
|
|
ui::Label* text = TRY(make<ui::Label>((message)));
|
|
text->set_color(ui::BLACK);
|
|
layout->add_widget(*text);
|
|
|
|
ui::InputField* input = TRY(make<ui::InputField>(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 {};
|
|
}
|
|
}
|