Unify Modern dropdown menus with Gold layout and safe keyboard selection (by Codex)
This commit is contained in:
1 parent
e81ecf95f2
commit
fff2cc2f0b
18 files changed
+1361
-17
No files matched your search
+66
-1
@@ -5,6 +5,7 @@
|
||||
#include "launch/rml_path.hpp"
|
||||
#include "launch/rml_video.hpp"
|
||||
#include "modern/ui/controls.hpp"
|
||||
#include "modern/ui/select_menu.hpp"
|
||||
#include "modern/ui/demo_calendar.hpp"
|
||||
#include "modern/ui/demo_finance.hpp"
|
||||
#include "modern/ui/demo_environment.hpp"
|
||||
@@ -346,6 +347,16 @@ class App final : public Rml::EventListener {
|
||||
std::shared_ptr<const Catalog> world_;
|
||||
std::unique_ptr<DemoSource> source_;
|
||||
std::unique_ptr<TicketPopover> ticket_;
|
||||
std::unique_ptr<SelectMenu> select_menu_;
|
||||
bool select_text_input_owned_ = false;
|
||||
struct SelectCapture final : Rml::EventListener {
|
||||
std::function<void(Rml::Event &)> handler;
|
||||
void ProcessEvent(Rml::Event &event) override {
|
||||
if (handler) handler(event);
|
||||
}
|
||||
} select_capture_;
|
||||
static constexpr std::array<const char *, 9> select_capture_events_ = {
|
||||
"mousedown", "mouseup", "click", "keydown", "textinput", "blur", "scroll", "mousemove", "contextmenu"};
|
||||
bool focus_ticket_ = false;
|
||||
std::set<std::string> pending_selects_;
|
||||
std::string focus_after_render_;
|
||||
@@ -758,7 +769,7 @@ class App final : public Rml::EventListener {
|
||||
void entry_sync_club();
|
||||
void entry_confirm();
|
||||
bool entry_action(const std::string &, Rml::Element *);
|
||||
bool entry_change(Rml::Element *);
|
||||
bool entry_change(Rml::Element *, bool user_reselected = false);
|
||||
bool entry_input(Rml::Element *);
|
||||
bool entry_event(Rml::Event &);
|
||||
Json entry_snapshot() const;
|
||||
@@ -828,7 +839,23 @@ class App final : public Rml::EventListener {
|
||||
if (statistics_trophy_.contains("asset"))
|
||||
needs_render_ = true;
|
||||
}
|
||||
void select_sync_keyboard() {
|
||||
if (select_menu_ && select_menu_->is_open()) {
|
||||
if (!SDL_IsTextInputActive()) {
|
||||
SDL_StartTextInput();
|
||||
select_text_input_owned_ = true;
|
||||
}
|
||||
} else if (select_text_input_owned_) {
|
||||
auto *focus = context_ ? context_->GetFocusElement() : nullptr;
|
||||
auto *input = dynamic_cast<Rml::ElementFormControlInput *>(focus);
|
||||
const auto type = input ? input->GetAttribute<Rml::String>("type", "text") : "";
|
||||
const bool editable = input && type != "range" && type != "checkbox" && type != "radio" && type != "button";
|
||||
if (!editable && (!focus || focus->GetTagName() != "textarea")) SDL_StopTextInput();
|
||||
select_text_input_owned_ = false;
|
||||
}
|
||||
}
|
||||
void render() {
|
||||
if (select_menu_) select_menu_->before_render();
|
||||
if (scene_ == "desktop" && !files_capturing_ && opening_pending()) {
|
||||
scene_ = "opening";
|
||||
menu_.clear();
|
||||
@@ -961,6 +988,7 @@ class App final : public Rml::EventListener {
|
||||
}
|
||||
}
|
||||
void render_popup() {
|
||||
if (!popup_.empty() && select_menu_) select_menu_->before_render();
|
||||
if (popup_.empty()) {
|
||||
element("popup")->SetClass("visible", false);
|
||||
rendered_popup_.clear();
|
||||
@@ -1043,6 +1071,11 @@ class App final : public Rml::EventListener {
|
||||
context_->Update();
|
||||
if (center_selects(*doc_))
|
||||
context_->Update();
|
||||
if (select_menu_) {
|
||||
select_menu_->after_layout();
|
||||
select_sync_keyboard();
|
||||
context_->Update();
|
||||
}
|
||||
if (amount_select_all_) {
|
||||
if (auto *input = dynamic_cast<Rml::ElementFormControlInput *>(element("calculator-value"))) input->Select();
|
||||
amount_select_all_ = false;
|
||||
@@ -1221,6 +1254,32 @@ public:
|
||||
doc_ = context_->LoadDocument(rml::asset_path(assets_ / "modern.rml"));
|
||||
if (!doc_)
|
||||
throw std::runtime_error("Modern UI layout missing");
|
||||
select_menu_ = std::make_unique<SelectMenu>(
|
||||
*context_, *doc_, [this](float size) { return glyph("check", size); },
|
||||
[this](const std::string &id, int, bool reselected) {
|
||||
if (!reselected) return;
|
||||
if (auto *control = element(id)) {
|
||||
if ((id == "manager-country" || id == "manager-league") && scene_ == "manager")
|
||||
entry_change(control, true);
|
||||
else {
|
||||
Rml::Dictionary parameters;
|
||||
parameters["ui_select_reselected"] = true;
|
||||
control->DispatchEvent("change", parameters);
|
||||
}
|
||||
}
|
||||
});
|
||||
select_capture_.handler = [this](Rml::Event &event) {
|
||||
if (event.GetType() == "keydown") keyboard_navigation_ = true;
|
||||
else if (event.GetType() == "mousedown") keyboard_navigation_ = false;
|
||||
const bool was_open = select_menu_->is_open();
|
||||
if (select_menu_->process_event(event)) {
|
||||
event.StopImmediatePropagation();
|
||||
dirty_ = true;
|
||||
} else if (was_open != select_menu_->is_open()) dirty_ = true;
|
||||
select_sync_keyboard();
|
||||
};
|
||||
for (const char *event : select_capture_events_)
|
||||
doc_->AddEventListener(event, &select_capture_, true);
|
||||
for (const char *event :
|
||||
{"click", "dblclick", "change", "input", "keydown", "blur", "mousedown",
|
||||
"mouseup", "mousemove", "contextmenu", "focus"})
|
||||
@@ -1277,6 +1336,12 @@ public:
|
||||
}
|
||||
}
|
||||
void shutdown() {
|
||||
select_capture_.handler = {};
|
||||
if (doc_)
|
||||
for (const char *event : select_capture_events_)
|
||||
doc_->RemoveEventListener(event, &select_capture_, true);
|
||||
select_menu_.reset();
|
||||
select_sync_keyboard();
|
||||
ticket_.reset();
|
||||
if (doc_) {
|
||||
doc_->Close();
|
||||
|
||||
Reference in new issue
Block a user