Fixes a bug where if a nested child was focused, then the container lost

focus, then the user tabbed back to the nested layout it would not focus
the first/last subview correctly.
This commit is contained in:
casey langen 2020-10-24 17:20:29 -07:00
parent b7ba7eb6bc
commit b821f6a293
2 changed files with 20 additions and 22 deletions

View File

@ -91,32 +91,29 @@ IWindowPtr LayoutBase::EnsureValidFocus() {
return newFocus; return newFocus;
} }
IWindowPtr LayoutBase::EnsureValidFocusFromNext() { LayoutBase* LayoutBase::GetLayoutAtFocusIndex() {
auto newFocus = this->GetFocus(); if (this->focused >= 0 && (int) this->focusable.size() > this->focused) {
if (newFocus && this->IsVisible()) { return dynamic_cast<LayoutBase*>(this->focusable.at(this->focused).get());
LayoutBase* layout = dynamic_cast<LayoutBase*>(newFocus.get());
if (layout) {
layout->FocusFirst();
}
else {
newFocus->Focus();
}
} }
return newFocus; return nullptr;
}
IWindowPtr LayoutBase::EnsureValidFocusFromNext() {
/* if the item we just focused is a layout, then we ask it to focus its
first child. otherwise we call through to our default logic to make sure
the regular view at the focus index is focused. */
IWindowPtr result;
const auto layoutAtFocus = GetLayoutAtFocusIndex();
if (layoutAtFocus) { result = layoutAtFocus->FocusFirst(); }
return result ? result : this->EnsureValidFocus();
} }
IWindowPtr LayoutBase::EnsureValidFocusFromPrev() { IWindowPtr LayoutBase::EnsureValidFocusFromPrev() {
auto newFocus = this->GetFocus(); /* see comment in EnsureValidFocusFromNext() */
if (newFocus && this->IsVisible()) { IWindowPtr result;
LayoutBase* layout = dynamic_cast<LayoutBase*>(newFocus.get()); const auto layoutAtFocus = GetLayoutAtFocusIndex();
if (layout) { if (layoutAtFocus) { result = layoutAtFocus->FocusLast(); }
layout->FocusLast(); return result ? result : this->EnsureValidFocus();
}
else {
newFocus->Focus();
}
}
return newFocus;
} }
void LayoutBase::OnVisibilityChanged(bool visible) { void LayoutBase::OnVisibilityChanged(bool visible) {

View File

@ -115,6 +115,7 @@ namespace cursespp {
void RemoveFocusable(IWindowPtr window); void RemoveFocusable(IWindowPtr window);
void SortFocusables(); void SortFocusables();
void IndexFocusables(); void IndexFocusables();
LayoutBase* GetLayoutAtFocusIndex();
std::vector<IWindowPtr> children; std::vector<IWindowPtr> children;
std::vector<IWindowPtr> focusable; std::vector<IWindowPtr> focusable;