Add TakeFocus and SetActiveChild.

This allows developers to set child children component must be the
currently active/focused one.

This can be used to "control" where the focus is, without user
interactions.
This commit is contained in:
ArthurSonzogni
2020-08-26 14:57:42 +02:00
committed by Arthur Sonzogni
parent 114ab4ae2a
commit 81d79d311d
6 changed files with 247 additions and 1 deletions

View File

@@ -58,9 +58,16 @@ Component* Component::ActiveChild() {
return children_.empty() ? nullptr : children_.front();
}
/// @brief Returns if the element if the currently active child of its parent.
/// @ingroup component
bool Component::Active() {
return !parent_ || parent_->ActiveChild() == this;
}
/// @brief Returns if the elements if focused by the user.
/// True when the Component is focused by the user. An element is Focused when
/// it is with all its ancestors the ActiveChild() of their parents.
/// @ingroup component
bool Component::Focused() {
Component* current = this;
for (;;) {
@@ -73,6 +80,23 @@ bool Component::Focused() {
}
}
/// @brief Make the |child| to be the "active" one.
/// @argument child the child to become active.
/// @ingroup component
void Component::SetActiveChild(Component*) {}
/// @brief Configure all the ancestors to give focus to this component.
/// @ingroup component
void Component::TakeFocus() {
Component* child = this;
Component* parent = parent_;
while (parent) {
parent->SetActiveChild(child);
child = parent;
parent = parent->parent_;
}
}
/// @brief Detach this children from its parent.
/// @see Attach
/// @see Detach