From 4aa8592d7dffc91fcef83e60acf46faadae443e7 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Mon, 30 Sep 2024 23:15:07 +0200 Subject: [PATCH] Use Index() --- CHANGELOG.md | 4 ++++ include/ftxui/component/component_base.hpp | 2 +- src/ftxui/component/button.cpp | 8 ++------ src/ftxui/component/component.cpp | 12 ++++++++---- src/ftxui/component/menu.cpp | 14 +++----------- src/ftxui/component/menu_test.cpp | 6 ++---- 6 files changed, 20 insertions(+), 26 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a0afeb2..611187d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -35,6 +35,10 @@ current (development) - Bugfix: Fix cursor position in when in the last column. See #831. - Bugfix: Fix `ResizeableSplit` keyboard navigation. Fixed by #842. - Bugfix: Fix `Menu` focus. See #841 +- Feature: Add `ComponentBase::Index()`. This allows to get the index of a + component in its parent. See #932 +- Feature: Add `EntryState::index`. This allows to get the index of a menu entry. + See #932 ### Dom - Feature: Add `hscroll_indicator`. It display an horizontal indicator diff --git a/include/ftxui/component/component_base.hpp b/include/ftxui/component/component_base.hpp index 97f0585..ef1b751 100644 --- a/include/ftxui/component/component_base.hpp +++ b/include/ftxui/component/component_base.hpp @@ -44,7 +44,7 @@ class ComponentBase { ComponentBase* Parent() const; Component& ChildAt(size_t i); size_t ChildCount() const; - int IndexOf(ComponentBase* child) const; + int Index() const; void Add(Component children); void Detach(); void DetachAllChildren(); diff --git a/src/ftxui/component/button.cpp b/src/ftxui/component/button.cpp index 37ef884..844dd39 100644 --- a/src/ftxui/component/button.cpp +++ b/src/ftxui/component/button.cpp @@ -48,12 +48,8 @@ class ButtonBase : public ComponentBase, public ButtonOption { } auto focus_management = focused ? focus : active ? select : nothing; - const EntryState state = { - *label, - false, - active, - focused_or_hover, - -1, + const EntryState state{ + *label, false, active, focused_or_hover, Index(), }; auto element = (transform ? transform : DefaultTransform) // diff --git a/src/ftxui/component/component.cpp b/src/ftxui/component/component.cpp index 0b373a0..327426e 100644 --- a/src/ftxui/component/component.cpp +++ b/src/ftxui/component/component.cpp @@ -53,14 +53,18 @@ size_t ComponentBase::ChildCount() const { /// @brief Return index of child or -1 if not found. /// @ingroup component -int ComponentBase::IndexOf(ComponentBase* child) const { +int ComponentBase::Index() const { + if (parent_ == nullptr) { + return -1; + } int index = 0; - for (Component c : children_) { - if (&(*c) == child) + for (const Component& child : parent_->children_) { + if (child.get() == this) { return index; + } index++; } - return -1; + return -1; // Not reached. } /// @brief Add a child. diff --git a/src/ftxui/component/menu.cpp b/src/ftxui/component/menu.cpp index 667d999..0f8b0fc 100644 --- a/src/ftxui/component/menu.cpp +++ b/src/ftxui/component/menu.cpp @@ -123,11 +123,7 @@ class MenuBase : public ComponentBase, public MenuOption { const bool is_selected = (selected() == i); const EntryState state = { - entries[i], - false, - is_selected, - is_focused, - i, + entries[i], false, is_selected, is_focused, i, }; auto focus_management = (selected_focus_ != i) ? nothing @@ -626,12 +622,8 @@ Component MenuEntry(MenuEntryOption option) { const bool focused = Focused(); UpdateAnimationTarget(); - const EntryState state = { - label(), - false, - hovered_, - focused, - Parent()->IndexOf(this), + const EntryState state{ + label(), false, hovered_, focused, Index(), }; const Element element = diff --git a/src/ftxui/component/menu_test.cpp b/src/ftxui/component/menu_test.cpp index c78a293..ca09946 100644 --- a/src/ftxui/component/menu_test.cpp +++ b/src/ftxui/component/menu_test.cpp @@ -266,10 +266,8 @@ TEST(MenuTest, MenuEntryIndex) { menu->OnEvent(Event::ArrowDown); menu->OnEvent(Event::ArrowDown); menu->OnEvent(Event::Return); - for(int idx = 0; idx < menu->ChildCount(); idx++) - { - auto child = menu->ChildAt(idx); - EXPECT_EQ(menu->IndexOf(&(*child)), idx); + for (int index = 0; index < menu->ChildCount(); index++) { + EXPECT_EQ(menu->ChildAt(index)->Index(), index); } }