mirror of
https://github.com/TwilitRealm/dusklight
synced 2026-08-17 12:39:27 -04:00
Window animations & tags instead of classes
This commit is contained in:
@@ -7,17 +7,16 @@
|
||||
namespace dusk::ui {
|
||||
namespace {
|
||||
|
||||
Rml::Element* createRoot(Rml::Element* parent, const Rml::String& className) {
|
||||
Rml::Element* createRoot(Rml::Element* parent, const Rml::String& tagName) {
|
||||
auto* doc = parent->GetOwnerDocument();
|
||||
auto elem = doc->CreateElement("button");
|
||||
elem->SetClass(className, true);
|
||||
auto elem = doc->CreateElement(tagName);
|
||||
return parent->AppendChild(std::move(elem));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Button::Button(Rml::Element* parent, ButtonProps props, const Rml::String& className)
|
||||
: Component(createRoot(parent, className)) {
|
||||
Button::Button(Rml::Element* parent, ButtonProps props, const Rml::String& tagName)
|
||||
: Component(createRoot(parent, tagName)) {
|
||||
update_props(std::move(props));
|
||||
listen(mRoot, Rml::EventId::Click, [this](Rml::Event& event) {
|
||||
if (mProps.onPressed) {
|
||||
|
||||
@@ -14,7 +14,7 @@ class Button : public Component {
|
||||
public:
|
||||
using Props = ButtonProps;
|
||||
|
||||
Button(Rml::Element* parent, ButtonProps props, const Rml::String& className = "button");
|
||||
Button(Rml::Element* parent, ButtonProps props, const Rml::String& tagName = "button");
|
||||
|
||||
void set_text(const Rml::String& text);
|
||||
void set_selected(bool selected);
|
||||
|
||||
@@ -6,17 +6,17 @@
|
||||
namespace dusk::ui {
|
||||
namespace {
|
||||
|
||||
Rml::ElementDocument* load_document(const Rml::String& path) {
|
||||
Rml::ElementDocument* load_document(const Rml::String& source) {
|
||||
auto* context = aurora::rmlui::get_context();
|
||||
if (context == nullptr) {
|
||||
return nullptr;
|
||||
}
|
||||
return context->LoadDocument(path);
|
||||
return context->LoadDocumentFromMemory(source);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Document::Document(const Rml::String& path) : mDocument(load_document(path)) {
|
||||
Document::Document(const Rml::String& source) : mDocument(load_document(source)) {
|
||||
listen(Rml::EventId::Keydown, [this](Rml::Event& event) {
|
||||
const auto cmd = map_nav_event(event);
|
||||
if (cmd != NavCommand::None && handle_nav_command(event, cmd)) {
|
||||
|
||||
@@ -7,7 +7,7 @@ namespace dusk::ui {
|
||||
|
||||
class Document {
|
||||
public:
|
||||
Document(const Rml::String& path);
|
||||
Document(const Rml::String& source);
|
||||
virtual ~Document();
|
||||
|
||||
Document(const Document&) = delete;
|
||||
|
||||
@@ -5,17 +5,16 @@
|
||||
namespace dusk::ui {
|
||||
namespace {
|
||||
|
||||
Rml::Element* createRoot(Rml::Element* parent, const Rml::String& className) {
|
||||
Rml::Element* createRoot(Rml::Element* parent) {
|
||||
auto* doc = parent->GetOwnerDocument();
|
||||
auto elem = doc->CreateElement("div");
|
||||
elem->SetClass(className, true);
|
||||
auto elem = doc->CreateElement("pane");
|
||||
return parent->AppendChild(std::move(elem));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Pane::Pane(Rml::Element* parent, Direction direction, const Rml::String& className)
|
||||
: Component(createRoot(parent, className)), mDirection(direction) {
|
||||
Pane::Pane(Rml::Element* parent, Direction direction)
|
||||
: Component(createRoot(parent)), mDirection(direction) {
|
||||
listen(mRoot, Rml::EventId::Keydown, [this](Rml::Event& event) {
|
||||
const auto cmd = map_nav_event(event);
|
||||
int direction = 0;
|
||||
@@ -95,8 +94,7 @@ void Pane::finalize() {
|
||||
// padding-bottom or margin-bottom on a scrollable flex container, so
|
||||
// we need to create a fake spacer with an actual layout height to get
|
||||
// padding at the bottom of a scrollable container.
|
||||
auto* elem = append(mRoot, "div");
|
||||
elem->SetClass("spacer", true);
|
||||
append(mRoot, "spacer");
|
||||
}
|
||||
|
||||
void Pane::clear() {
|
||||
|
||||
@@ -13,7 +13,7 @@ public:
|
||||
Horizontal,
|
||||
};
|
||||
|
||||
explicit Pane(Rml::Element* parent, Direction direction, const Rml::String& className = "pane");
|
||||
explicit Pane(Rml::Element* parent, Direction direction);
|
||||
|
||||
bool focus() override;
|
||||
void update() override;
|
||||
|
||||
+17
-3
@@ -11,8 +11,22 @@
|
||||
#include <chrono>
|
||||
|
||||
namespace dusk::ui {
|
||||
namespace {
|
||||
|
||||
Popup::Popup() : Document("res/rml/popup.rml"), mRoot(mDocument->GetElementById("popup")) {
|
||||
const Rml::String kDocumentSource = R"RML(
|
||||
<rml>
|
||||
<head>
|
||||
<link type="text/rcss" href="res/rml/popup.rcss" />
|
||||
</head>
|
||||
<body>
|
||||
<popup id="popup"></div>
|
||||
</body>
|
||||
</rml>
|
||||
)RML";
|
||||
|
||||
}
|
||||
|
||||
Popup::Popup() : Document(kDocumentSource), mRoot(mDocument->GetElementById("popup")) {
|
||||
mTabBar = std::make_unique<TabBar>(mRoot, TabBar::Props{.autoSelect = false});
|
||||
mTabBar->add_tab("Settings", [] { push_document(std::make_unique<SettingsWindow>()); });
|
||||
mTabBar->add_tab("Warp", [] {
|
||||
@@ -48,7 +62,7 @@ void Popup::show() {
|
||||
}
|
||||
|
||||
Document::show();
|
||||
mRoot->SetClass("popup-hidden", false);
|
||||
mRoot->SetAttribute("open", "");
|
||||
mTabBar->set_active_tab(-1);
|
||||
mVisible = true;
|
||||
}
|
||||
@@ -62,7 +76,7 @@ void Popup::hide() {
|
||||
return;
|
||||
}
|
||||
|
||||
mRoot->SetClass("popup-hidden", true);
|
||||
mRoot->RemoveAttribute("open");
|
||||
mVisible = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -9,18 +9,15 @@ namespace {
|
||||
|
||||
Rml::Element* createRoot(Rml::Element* parent) {
|
||||
auto* doc = parent->GetOwnerDocument();
|
||||
auto elem = doc->CreateElement("button");
|
||||
elem->SetClass("select-button", true);
|
||||
auto elem = doc->CreateElement("select-button");
|
||||
return parent->AppendChild(std::move(elem));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
SelectButton::SelectButton(Rml::Element* parent, Props props) : Component(createRoot(parent)) {
|
||||
mKeyElem = append(mRoot, "div");
|
||||
mKeyElem->SetClass("key", true);
|
||||
mValueElem = append(mRoot, "div");
|
||||
mValueElem->SetClass("value", true);
|
||||
mKeyElem = append(mRoot, "key");
|
||||
mValueElem = append(mRoot, "value");
|
||||
update_props(std::move(props));
|
||||
listen(mRoot, Rml::EventId::Click, [this](Rml::Event& event) {
|
||||
if (mProps.disabled) {
|
||||
|
||||
@@ -5,8 +5,7 @@ namespace {
|
||||
|
||||
Rml::Element* createRoot(Rml::Element* parent) {
|
||||
auto* doc = parent->GetOwnerDocument();
|
||||
auto elem = doc->CreateElement("div");
|
||||
elem->SetClass("tab-bar", true);
|
||||
auto elem = doc->CreateElement("tab-bar");
|
||||
return parent->AppendChild(std::move(elem));
|
||||
}
|
||||
|
||||
|
||||
+2
-2
@@ -87,8 +87,8 @@ Document* top_document() noexcept {
|
||||
}
|
||||
|
||||
void update() noexcept {
|
||||
for (size_t i = 0; i < sDocuments.size(); ++i) {
|
||||
sDocuments[i].doc->update();
|
||||
for (const auto& doc : sDocuments) {
|
||||
doc.doc->update();
|
||||
}
|
||||
sDocuments.erase(
|
||||
std::remove_if(sDocuments.begin(), sDocuments.end(),
|
||||
|
||||
+47
-4
@@ -23,17 +23,27 @@ float base_body_padding(Rml::Context* context) noexcept {
|
||||
return 64.0f * dpRatio;
|
||||
}
|
||||
|
||||
const Rml::String kDocumentSource = R"RML(
|
||||
<rml>
|
||||
<head>
|
||||
<link type="text/rcss" href="res/rml/window.rcss" />
|
||||
</head>
|
||||
<body>
|
||||
<window id="window"></window>
|
||||
</body>
|
||||
</rml>
|
||||
)RML";
|
||||
|
||||
} // namespace
|
||||
|
||||
Window::Window() : Document("res/rml/window.rml"), mRoot(mDocument->GetElementById("window")) {
|
||||
Window::Window() : Document(kDocumentSource), mRoot(mDocument->GetElementById("window")) {
|
||||
mTabBar = std::make_unique<TabBar>(mRoot, TabBar::Props{
|
||||
.selectedTabIndex = 0,
|
||||
.autoSelect = true,
|
||||
});
|
||||
|
||||
auto elem = mDocument->CreateElement("div");
|
||||
auto elem = mDocument->CreateElement("content");
|
||||
elem->SetAttribute("id", "content");
|
||||
elem->SetClass("content", true);
|
||||
mContentRoot = mRoot->AppendChild(std::move(elem));
|
||||
|
||||
listen(Rml::EventId::Keydown, [this](Rml::Event& event) {
|
||||
@@ -49,6 +59,39 @@ Window::Window() : Document("res/rml/window.rml"), mRoot(mDocument->GetElementBy
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// Hide document after transition completion
|
||||
listen(mRoot, Rml::EventId::Transitionend, [this](Rml::Event& event) {
|
||||
if (event.GetTargetElement() == mRoot &&
|
||||
*mRoot->GetProperty(Rml::PropertyId::Visibility) == Rml::Style::Visibility::Visible &&
|
||||
!mVisible)
|
||||
{
|
||||
Document::hide();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
void Window::show() {
|
||||
if (mVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
Document::show();
|
||||
mRoot->SetAttribute("open", "");
|
||||
mVisible = true;
|
||||
}
|
||||
|
||||
void Window::hide() {
|
||||
if (mDocument == nullptr) {
|
||||
mVisible = false;
|
||||
return;
|
||||
}
|
||||
if (!mVisible) {
|
||||
return;
|
||||
}
|
||||
|
||||
mRoot->RemoveAttribute("open");
|
||||
mVisible = false;
|
||||
}
|
||||
|
||||
void Window::update() {
|
||||
@@ -114,7 +157,7 @@ bool Window::focus() {
|
||||
|
||||
bool Window::handle_nav_command(Rml::Event& event, NavCommand cmd) {
|
||||
auto* target = event.GetTargetElement();
|
||||
if (cmd != NavCommand::Next && cmd != NavCommand::Previous && target->Closest(".content")) {
|
||||
if (cmd != NavCommand::Next && cmd != NavCommand::Previous && target->Closest("content")) {
|
||||
if (handle_content_nav(event, cmd)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -23,6 +23,8 @@ public:
|
||||
Window(const Window&) = delete;
|
||||
Window& operator=(const Window&) = delete;
|
||||
|
||||
void show() override;
|
||||
void hide() override;
|
||||
void update() override;
|
||||
bool focus() override;
|
||||
bool set_active_tab(int index);
|
||||
@@ -47,6 +49,7 @@ protected:
|
||||
std::unique_ptr<TabBar> mTabBar;
|
||||
std::vector<std::unique_ptr<Component> > mContentComponents;
|
||||
Insets mBodyPadding;
|
||||
bool mVisible = false;
|
||||
};
|
||||
|
||||
} // namespace dusk::ui
|
||||
|
||||
Reference in New Issue
Block a user