Skip to content

Commit

Permalink
Fixed dialog construction.
Browse files Browse the repository at this point in the history
  • Loading branch information
5cript committed Sep 15, 2023
1 parent 30874df commit 0256495
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 28 deletions.
17 changes: 11 additions & 6 deletions nui/include/nui/frontend/components/dialog.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ namespace Nui::Components

struct ConstructionArgs
{
Observed<std::optional<std::string>> className{std::nullopt};
Observed<std::string> title{""};
Observed<std::string> body{""};
Observed<std::string> buttonClassName{""};
Observed<ButtonConfiguration> buttonConfiguration{ButtonConfiguration::Ok};
std::optional<std::string> className{std::nullopt};
std::string title{""};
std::string body{""};
std::string buttonClassName{""};
ButtonConfiguration buttonConfiguration{ButtonConfiguration::Ok};
std::function<void(Button)> onButtonClicked = [](Button) {};
};

Expand Down Expand Up @@ -107,6 +107,11 @@ namespace Nui::Components
private:
bool isOpen_;
std::weak_ptr<Dom::Element> element_;
ConstructionArgs args_;
Observed<std::optional<std::string>> className_;
Observed<std::string> title_;
Observed<std::string> body_;
Observed<std::string> buttonClassName_;
Observed<ButtonConfiguration> buttonConfiguration_;
std::function<void(Button)> onButtonClicked_;
};
}
49 changes: 27 additions & 22 deletions nui/src/nui/frontend/components/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ namespace Nui::Components
DialogController::DialogController(ConstructionArgs&& args)
: isOpen_{false}
, element_{}
, args_{std::move(args)}
, className_{std::move(args.className)}
, title_{std::move(args.title)}
, body_{std::move(args.body)}
, buttonClassName_{std::move(args.buttonClassName)}
, buttonConfiguration_{std::move(args.buttonConfiguration)}
, onButtonClicked_{std::move(args.onButtonClicked)}
{}
//---------------------------------------------------------------------------------------------------------------------
void DialogController::showModal()
Expand Down Expand Up @@ -54,32 +59,32 @@ namespace Nui::Components
//---------------------------------------------------------------------------------------------------------------------
void DialogController::setClassName(std::string const& className)
{
args_.className = className;
className_ = className;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogController::setButtonClassName(std::string const& className)
{
args_.buttonClassName = className;
buttonClassName_ = className;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogController::setTitle(std::string const& title)
{
args_.title = title;
title_ = title;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogController::setBody(std::string const& body)
{
args_.body = body;
body_ = body;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogController::setButtonConfiguration(ButtonConfiguration buttons)
{
args_.buttonConfiguration = buttons;
buttonConfiguration_ = buttons;
}
//---------------------------------------------------------------------------------------------------------------------
void DialogController::setOnButtonClicked(std::function<void(Button)> const& onButtonClicked)
{
args_.onButtonClicked = onButtonClicked;
onButtonClicked_ = onButtonClicked;
}
//---------------------------------------------------------------------------------------------------------------------
Nui::ElementRenderer Dialog(DialogController& controller)
Expand All @@ -89,7 +94,7 @@ namespace Nui::Components

// clang-format off
return dialog{
class_ = controller.args_.className,
class_ = controller.className_,
reference = [&controller](auto&& element){
controller.element_ = std::static_pointer_cast<Dom::Element>(element.lock());
}
Expand All @@ -98,43 +103,43 @@ namespace Nui::Components
method = "dialog"
}(
h1{}(
controller.args_.title
controller.title_
),
p{}(
controller.args_.body
controller.body_
),
menu{}(
observe(controller.args_.buttonConfiguration),
[&controller, &conf = controller.args_.buttonConfiguration]() -> Nui::ElementRenderer {
observe(controller.buttonConfiguration_),
[&controller, &conf = controller.buttonConfiguration_]() -> Nui::ElementRenderer {
switch (conf.value()) {
case(DialogController::ButtonConfiguration::Ok):
{
return button{
class_ = controller.args_.buttonClassName,
class_ = controller.buttonClassName_,
type = "submit",
onClick = [&controller](){
controller.isOpen_ = false;
controller.args_.onButtonClicked(DialogController::Button::Ok);
controller.onButtonClicked_(DialogController::Button::Ok);
}
}("Ok");
}
case(DialogController::ButtonConfiguration::OkCancel):
{
return fragment(
button{
class_ = controller.args_.buttonClassName,
class_ = controller.buttonClassName_,
type = "submit",
onClick = [&controller](){
controller.isOpen_ = false;
controller.args_.onButtonClicked(DialogController::Button::Ok);
controller.onButtonClicked_(DialogController::Button::Ok);
}
}("Ok"),
button{
class_ = controller.args_.buttonClassName,
class_ = controller.buttonClassName_,
type = "cancel",
onClick = [&controller](){
controller.isOpen_ = false;
controller.args_.onButtonClicked(DialogController::Button::Cancel);
controller.onButtonClicked_(DialogController::Button::Cancel);
}
}("Cancel")
);
Expand All @@ -143,19 +148,19 @@ namespace Nui::Components
{
return fragment(
button{
class_ = controller.args_.buttonClassName,
class_ = controller.buttonClassName_,
type = "submit",
onClick = [&controller](){
controller.isOpen_ = false;
controller.args_.onButtonClicked(DialogController::Button::Yes);
controller.onButtonClicked_(DialogController::Button::Yes);
}
}("Yes"),
button{
class_ = controller.args_.buttonClassName,
class_ = controller.buttonClassName_,
type = "cancel",
onClick = [&controller](){
controller.isOpen_ = false;
controller.args_.onButtonClicked(DialogController::Button::No);
controller.onButtonClicked_(DialogController::Button::No);
}
}("No")
);
Expand Down

0 comments on commit 0256495

Please sign in to comment.