Use Index()

This commit is contained in:
ArthurSonzogni 2024-09-30 23:15:07 +02:00
parent d16359cf8b
commit 4aa8592d7d
No known key found for this signature in database
GPG Key ID: 41D98248C074CD6C
6 changed files with 20 additions and 26 deletions

View File

@ -35,6 +35,10 @@ current (development)
- Bugfix: Fix cursor position in when in the last column. See #831. - Bugfix: Fix cursor position in when in the last column. See #831.
- Bugfix: Fix `ResizeableSplit` keyboard navigation. Fixed by #842. - Bugfix: Fix `ResizeableSplit` keyboard navigation. Fixed by #842.
- Bugfix: Fix `Menu` focus. See #841 - 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 ### Dom
- Feature: Add `hscroll_indicator`. It display an horizontal indicator - Feature: Add `hscroll_indicator`. It display an horizontal indicator

View File

@ -44,7 +44,7 @@ class ComponentBase {
ComponentBase* Parent() const; ComponentBase* Parent() const;
Component& ChildAt(size_t i); Component& ChildAt(size_t i);
size_t ChildCount() const; size_t ChildCount() const;
int IndexOf(ComponentBase* child) const; int Index() const;
void Add(Component children); void Add(Component children);
void Detach(); void Detach();
void DetachAllChildren(); void DetachAllChildren();

View File

@ -48,12 +48,8 @@ class ButtonBase : public ComponentBase, public ButtonOption {
} }
auto focus_management = focused ? focus : active ? select : nothing; auto focus_management = focused ? focus : active ? select : nothing;
const EntryState state = { const EntryState state{
*label, *label, false, active, focused_or_hover, Index(),
false,
active,
focused_or_hover,
-1,
}; };
auto element = (transform ? transform : DefaultTransform) // auto element = (transform ? transform : DefaultTransform) //

View File

@ -53,14 +53,18 @@ size_t ComponentBase::ChildCount() const {
/// @brief Return index of child or -1 if not found. /// @brief Return index of child or -1 if not found.
/// @ingroup component /// @ingroup component
int ComponentBase::IndexOf(ComponentBase* child) const { int ComponentBase::Index() const {
if (parent_ == nullptr) {
return -1;
}
int index = 0; int index = 0;
for (Component c : children_) { for (const Component& child : parent_->children_) {
if (&(*c) == child) if (child.get() == this) {
return index; return index;
}
index++; index++;
} }
return -1; return -1; // Not reached.
} }
/// @brief Add a child. /// @brief Add a child.

View File

@ -123,11 +123,7 @@ class MenuBase : public ComponentBase, public MenuOption {
const bool is_selected = (selected() == i); const bool is_selected = (selected() == i);
const EntryState state = { const EntryState state = {
entries[i], entries[i], false, is_selected, is_focused, i,
false,
is_selected,
is_focused,
i,
}; };
auto focus_management = (selected_focus_ != i) ? nothing auto focus_management = (selected_focus_ != i) ? nothing
@ -626,12 +622,8 @@ Component MenuEntry(MenuEntryOption option) {
const bool focused = Focused(); const bool focused = Focused();
UpdateAnimationTarget(); UpdateAnimationTarget();
const EntryState state = { const EntryState state{
label(), label(), false, hovered_, focused, Index(),
false,
hovered_,
focused,
Parent()->IndexOf(this),
}; };
const Element element = const Element element =

View File

@ -266,10 +266,8 @@ TEST(MenuTest, MenuEntryIndex) {
menu->OnEvent(Event::ArrowDown); menu->OnEvent(Event::ArrowDown);
menu->OnEvent(Event::ArrowDown); menu->OnEvent(Event::ArrowDown);
menu->OnEvent(Event::Return); menu->OnEvent(Event::Return);
for(int idx = 0; idx < menu->ChildCount(); idx++) for (int index = 0; index < menu->ChildCount(); index++) {
{ EXPECT_EQ(menu->ChildAt(index)->Index(), index);
auto child = menu->ChildAt(idx);
EXPECT_EQ(menu->IndexOf(&(*child)), idx);
} }
} }