Restore focus to re-enabled elements

This commit is contained in:
Luke Street
2026-08-23 00:09:34 -06:00
parent 705e7f058b
commit 48b5c2e8c1
2 changed files with 12 additions and 0 deletions
+11
View File
@@ -49,13 +49,24 @@ void Component::set_disabled(bool value) {
if (Component::disabled() == value) {
return;
}
auto* context = mRoot->GetContext();
if (value) {
if (context != nullptr && context->GetFocusElement() == mRoot) {
// RmlUi moves focus to the parent when a focused control is disabled.
mDisabledFocusFallback = mRoot->GetParentNode();
}
mRoot->SetAttribute("disabled", "");
mRoot->SetPseudoClass("disabled", true);
mRoot->Blur();
} else {
const bool restoreFocus = mDisabledFocusFallback != nullptr && context != nullptr &&
context->GetFocusElement() == mDisabledFocusFallback;
mRoot->RemoveAttribute("disabled");
mRoot->SetPseudoClass("disabled", false);
mDisabledFocusFallback = nullptr;
if (restoreFocus) {
focus();
}
}
}
+1
View File
@@ -54,6 +54,7 @@ protected:
void clear_children();
Rml::Element* mRoot = nullptr;
Rml::Element* mDisabledFocusFallback = nullptr;
std::vector<std::unique_ptr<Component>> mChildren;
std::vector<std::unique_ptr<ScopedEventListener>> mListeners;
};