Skip to content

Commit

Permalink
Add childrenChanged() callback to avoid reacting on every child add…
Browse files Browse the repository at this point in the history
…ed to a component
  • Loading branch information
ImJimmi committed Nov 12, 2023
1 parent e932678 commit d432605
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
13 changes: 5 additions & 8 deletions jive_layouts/layout/gui-items/jive_CommonGuiItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,14 +187,6 @@ namespace jive
enabled = component->isEnabled();
}

void CommonGuiItem::componentChildrenChanged(juce::Component& componentThatsChildrenChanged)
{
if (&componentThatsChildrenChanged != component.get())
return;

getTopLevelDecorator().layOutChildren();
}

[[nodiscard]] static auto hasWidgetRole(const juce::Component& component)
{
if (auto* handler = const_cast<juce::Component*>(&component)->getAccessibilityHandler())
Expand Down Expand Up @@ -264,6 +256,11 @@ namespace jive
juce::roundToInt(boxModel.getHeight()));
getTopLevelDecorator().layOutChildren();
}

void CommonGuiItem::childrenChanged()
{
getTopLevelDecorator().layOutChildren();
}
} // namespace jive

#if JIVE_UNIT_TESTS
Expand Down
3 changes: 2 additions & 1 deletion jive_layouts/layout/gui-items/jive_CommonGuiItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,11 @@ namespace jive
void componentVisibilityChanged(juce::Component& component) override;
void componentNameChanged(juce::Component& component) override;
void componentEnablementChanged(juce::Component& component) override;
void componentChildrenChanged(juce::Component& component) override;
void componentParentHierarchyChanged(juce::Component& component) override;
void boxModelChanged(BoxModel& boxModel) override;

void childrenChanged() override;

private:
Property<juce::String> name;
Property<juce::String> title;
Expand Down
12 changes: 11 additions & 1 deletion jive_layouts/layout/gui-items/jive_GuiItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ namespace jive
}

void GuiItem::insertChild(std::unique_ptr<GuiItem> child, int index)
{
insertChild(std::move(child), index, true);
}

void GuiItem::insertChild(std::unique_ptr<GuiItem> child, int index, bool invokeCallback)
{
if (child == nullptr)
{
Expand All @@ -71,14 +76,19 @@ namespace jive

auto* newlyAddedChild = children.insert(index, std::move(child));
component->addChildComponent(*newlyAddedChild->getComponent());

if (invokeCallback)
childrenChanged();
}

void GuiItem::setChildren(std::vector<std::unique_ptr<GuiItem>>&& newChildren)
{
children.clearQuick(true);

for (auto& child : newChildren)
insertChild(std::move(child), children.size());
insertChild(std::move(child), children.size(), false);

childrenChanged();
}

void GuiItem::removeChild(GuiItem& childToRemove)
Expand Down
4 changes: 4 additions & 0 deletions jive_layouts/layout/gui-items/jive_GuiItem.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ namespace jive
juce::ValueTree state;

protected:
virtual void childrenChanged() {}

const std::shared_ptr<juce::Component> component;
GuiItem* const parent;

Expand Down Expand Up @@ -67,6 +69,8 @@ namespace jive
const StyleSheet::ReferenceCountedPointer styleSheet;
#endif

void insertChild(std::unique_ptr<GuiItem> child, int index, bool invokeCallback);

std::unique_ptr<Remover> remover;

JUCE_LEAK_DETECTOR(GuiItem)
Expand Down
2 changes: 2 additions & 0 deletions jive_layouts/layout/gui-items/jive_GuiItemDecorator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@ namespace jive
void GuiItemDecorator::insertChild(std::unique_ptr<GuiItem> child, int index)
{
item->insertChild(std::move(child), index);
childrenChanged();
}

void GuiItemDecorator::setChildren(std::vector<std::unique_ptr<GuiItem>>&& newChildren)
{
item->setChildren(std::move(newChildren));
childrenChanged();
}

void GuiItemDecorator::removeChild(GuiItem& child)
Expand Down

0 comments on commit d432605

Please sign in to comment.