#include "mologram/ui/DialogKit.hpp"
#include <algorithm>
namespace mologram::ui {
using namespace ttgui;
std::shared_ptr<bool> DialogKit::tokenOf(Window* w) const {
auto it = tokens_.find(w);
return it == tokens_.end() ? nullptr : it->second;
}
Rect DialogKit::place(int w, int h) const {
Rect wa = app_.wm.workArea;
int ww = std::max(10, std::min(w, wa.w));
int hh = std::max(4, std::min(h, wa.h));
return {wa.x + (wa.w - ww) / 2, wa.y + (wa.h - hh) / 2, ww, hh};
}
DialogKit::Form DialogKit::open(const std::string& title, const std::string& appName, int w, int h, bool modal) {
Form f;
f.win = app_.wm.createWindow(title, "lightblue", place(w, h));
f.win->appName = appName;
f.win->modal = modal;
f.win->popFrames = 0;
f.box = new VBox();
auto token = std::make_shared<bool>(true);
tokens_[f.win] = token;
Window* key = f.win;
f.win->onClose.connect([this, token, key]() { *token = false; tokens_.erase(key); });
return f;
}
Label* DialogKit::label(Form& f, const std::string& text) {
auto* l = new Label(text);
l->cfg = &app_.cfg;
l->windowClass = "lightblue";
f.box->addChild(std::unique_ptr<Widget>(l));
return l;
}
EditBox* DialogKit::edit(Form& f, const std::string& placeholder, const std::string& text, bool password) {
auto* e = new EditBox(text);
e->cfg = &app_.cfg;
e->placeholder = placeholder;
e->password = password;
f.box->addChild(std::unique_ptr<Widget>(e));
return e;
}
PushButton* DialogKit::button(Form& f, const std::string& label, std::function<void()> fn) {
auto* b = new PushButton(label);
b->cfg = &app_.cfg;
if (fn) b->onPressed.connect(std::move(fn));
f.box->addChild(std::unique_ptr<Widget>(b));
return b;
}
CheckBox* DialogKit::check(Form& f, const std::string& label, bool value, std::function<void(bool)> fn) {
auto* c = new CheckBox(label, value);
c->cfg = &app_.cfg;
if (fn) c->onChanged.connect(std::move(fn));
f.box->addChild(std::unique_ptr<Widget>(c));
return c;
}
ListBox* DialogKit::list(Form& f, std::vector<std::string> items, int flex) {
auto* l = new ListBox();
l->cfg = &app_.cfg;
l->items = std::move(items);
l->selected = l->items.empty() ? -1 : 0;
l->flex = flex;
f.box->addChild(std::unique_ptr<Widget>(l));
return l;
}
Table* DialogKit::table(Form& f, std::vector<std::string> headers, std::vector<std::vector<std::string>> rows, std::vector<int> colWidths, int flex) {
auto* t = new Table();
t->cfg = &app_.cfg;
t->headers = std::move(headers);
t->rows = std::move(rows);
t->colWidths = std::move(colWidths);
t->flex = flex;
f.box->addChild(std::unique_ptr<Widget>(t));
return t;
}
ComboBox* DialogKit::combo(Form& f, std::vector<std::string> items, int selected) {
auto* c = new ComboBox();
c->cfg = &app_.cfg;
c->items = std::move(items);
c->selected = selected;
f.box->addChild(std::unique_ptr<Widget>(c));
return c;
}
TextWidget* DialogKit::text(Form& f, const std::string& body, int flex) {
auto* t = new TextWidget();
t->cfg = &app_.cfg;
t->text = body;
t->flex = flex;
f.box->addChild(std::unique_ptr<Widget>(t));
return t;
}
HBox* DialogKit::row(Form& f) {
auto* r = new HBox();
r->prefSize = {10, 1};
f.box->addChild(std::unique_ptr<Widget>(r));
return r;
}
PushButton* DialogKit::rowButton(HBox* r, const std::string& label, std::function<void()> fn) {
auto* b = new PushButton(label);
b->cfg = &app_.cfg;
if (fn) b->onPressed.connect(std::move(fn));
r->addChild(std::unique_ptr<Widget>(b));
return b;
}
void DialogKit::show(Form& f, Widget* focus) {
f.win->setContent(std::unique_ptr<Widget>(f.box));
f.box->bounds = f.win->contentRect();
f.box->layout();
if (focus) app_.setFocused(focus);
}
void DialogKit::close(Window* w) {
if (w) app_.wm.closeWindow(w);
}
} // namespace mologram::ui