libui: Add Dialog
This commit is contained in:
parent
38fcd8e3e1
commit
fd2fe16538
@ -19,6 +19,7 @@ set(SOURCES
|
|||||||
src/Label.cpp
|
src/Label.cpp
|
||||||
src/InputField.cpp
|
src/InputField.cpp
|
||||||
src/TextInput.cpp
|
src/TextInput.cpp
|
||||||
|
src/Dialog.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
add_library(ui ${SOURCES})
|
add_library(ui ${SOURCES})
|
||||||
|
22
gui/libui/include/ui/Dialog.h
Normal file
22
gui/libui/include/ui/Dialog.h
Normal file
@ -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 <os/Action.h>
|
||||||
|
#include <ui/Window.h>
|
||||||
|
|
||||||
|
namespace ui
|
||||||
|
{
|
||||||
|
namespace Dialog
|
||||||
|
{
|
||||||
|
Result<void> show_message(StringView title, StringView message);
|
||||||
|
|
||||||
|
Result<void> show_input_dialog(StringView title, StringView message, os::Function<StringView> callback);
|
||||||
|
}
|
||||||
|
}
|
82
gui/libui/src/Dialog.cpp
Normal file
82
gui/libui/src/Dialog.cpp
Normal file
@ -0,0 +1,82 @@
|
|||||||
|
/**
|
||||||
|
* @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 {};
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user