Loading...
Searching...
No Matches
component_base.hpp
Go to the documentation of this file.
// Copyright 2020 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#ifndef FTXUI_COMPONENT_BASE_HPP
#define FTXUI_COMPONENT_BASE_HPP
#include <memory> // for unique_ptr
#include <vector> // for vector
#include "ftxui/component/captured_mouse.hpp" // for CaptureMouse
#include "ftxui/dom/elements.hpp" // for Element
namespace ftxui {
class Delegate;
class Focus;
struct Event;
namespace animation {
class Params;
} // namespace animation
class ComponentBase;
using Component = std::shared_ptr<ComponentBase>;
using Components = std::vector<Component>;
/// @brief It implement rendering itself as ftxui::Element. It implement
/// keyboard navigation by responding to ftxui::Event.
/// @ingroup component
class ComponentBase {
public:
explicit ComponentBase(Components children)
: children_(std::move(children)) {}
virtual ~ComponentBase();
ComponentBase() = default;
// A component is not copyable/movable.
ComponentBase(const ComponentBase&) = delete;
ComponentBase(ComponentBase&&) = delete;
ComponentBase& operator=(const ComponentBase&) = delete;
ComponentBase& operator=(ComponentBase&&) = delete;
// Component hierarchy:
ComponentBase* Parent() const;
Component& ChildAt(size_t i);
size_t ChildCount() const;
int Index() const;
void Add(Component children);
void Detach();
void DetachAllChildren();
// Renders the component.
Element Render();
// Override this function modify how `Render` works.
virtual Element OnRender();
// Handles an event.
// By default, reduce on children with a lazy OR.
//
// Returns whether the event was handled or not.
virtual bool OnEvent(Event);
// Handle an animation step.
virtual void OnAnimation(animation::Params& params);
// Focus management ----------------------------------------------------------
//
// If this component contains children, this indicates which one is active,
// nullptr if none is active.
//
// We say an element has the focus if the chain of ActiveChild() from the
// root component contains this object.
virtual Component ActiveChild();
// Return true when the component contains focusable elements.
// The non focusable Component will be skipped when navigating using the
// keyboard.
virtual bool Focusable() const;
// Whether this is the active child of its parent.
bool Active() const;
// Whether all the ancestors are active.
bool Focused() const;
// Make the |child| to be the "active" one.
virtual void SetActiveChild(ComponentBase* child);
void SetActiveChild(Component child);
// Configure all the ancestors to give focus to this component.
void TakeFocus();
protected:
CapturedMouse CaptureMouse(const Event& event);
Components children_;
private:
ComponentBase* parent_ = nullptr;
bool in_render = false;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_BASE_HPP */
captured_mouse.hpp
ftxui::ComponentBase
It implement rendering itself as ftxui::Element. It implement keyboard navigation by responding to ft...
Definition component_base.hpp:30
ftxui::ComponentBase::Focusable
virtual bool Focusable() const
Return true when the component contains focusable elements. The non focusable Components will be skip...
Definition component.cpp:196
ftxui::ComponentBase::Focused
bool Focused() const
Returns if the elements if focused by the user. True when the ComponentBase is focused by the user....
Definition component.cpp:216
ftxui::ComponentBase::CaptureMouse
CapturedMouse CaptureMouse(const Event &event)
Take the CapturedMouse if available. There is only one component of them. It represents a component t...
Definition component.cpp:250
ftxui::ComponentBase::Add
void Add(Component children)
Add a child. @param child The child to be attached.
Definition component.cpp:75
ftxui::ComponentBase::children_
Components children_
Definition component_base.hpp:96
ftxui::ComponentBase::Render
Element Render()
Draw the component. Build a ftxui::Element to be drawn on the ftxui::Screen representing this ftxui::...
Definition component.cpp:111
ftxui::ComponentBase::TakeFocus
void TakeFocus()
Configure all the ancestors to give focus to this component.
Definition component.cpp:238
ftxui::ComponentBase::Active
bool Active() const
Returns if the element if the currently active child of its parent.
Definition component.cpp:207
ftxui::ComponentBase::ActiveChild
virtual Component ActiveChild()
Return the currently Active child.
Definition component.cpp:183
ftxui::ComponentBase::DetachAllChildren
void DetachAllChildren()
Remove all children.
Definition component.cpp:101
ftxui::ComponentBase::SetActiveChild
virtual void SetActiveChild(ComponentBase *child)
Make the |child| to be the "active" one.
Definition component.cpp:227
ftxui::ComponentBase::Index
int Index() const
Return index of the component in its parent. -1 if no parent.
Definition component.cpp:58
ftxui::ComponentBase::ChildCount
size_t ChildCount() const
Returns the number of children.
Definition component.cpp:52
ftxui::ComponentBase::ComponentBase
ComponentBase(ComponentBase &&)=delete
ftxui::ComponentBase::operator=
ComponentBase & operator=(ComponentBase &&)=delete
ftxui::ComponentBase::Parent
ComponentBase * Parent() const
Return the parent ComponentBase, or nul if any.
Definition component.cpp:39
ftxui::ComponentBase::OnRender
virtual Element OnRender()
Draw the component. Build a ftxui::Element to be drawn on the ftxi::Screen representing this ftxui::C...
Definition component.cpp:147
ftxui::ComponentBase::OnEvent
virtual bool OnEvent(Event)
Called in response to an event.
Definition component.cpp:161
ftxui::ComponentBase::Detach
void Detach()
Detach this child from its parent.
Definition component.cpp:85
ftxui::ComponentBase::ComponentBase
ComponentBase(const ComponentBase &)=delete
ftxui::ComponentBase::operator=
ComponentBase & operator=(const ComponentBase &)=delete
ftxui::ComponentBase::ComponentBase
ComponentBase()=default
ftxui::ComponentBase::ChildAt
Component & ChildAt(size_t i)
Access the child at index i.
Definition component.cpp:45
ftxui::ComponentBase::ComponentBase
ComponentBase(Components children)
Definition component_base.hpp:32
ftxui::ComponentBase::~ComponentBase
virtual ~ComponentBase()
Definition component.cpp:31
ftxui::ComponentBase::OnAnimation
virtual void OnAnimation(animation::Params &params)
Called in response to an animation event.
Definition component.cpp:174
ftxui::animation::Params
Definition animation.hpp:23
elements.hpp
ftxui
Definition animation.hpp:10
ftxui::CapturedMouse
std::unique_ptr< CapturedMouseInterface > CapturedMouse
Definition captured_mouse.hpp:19
ftxui::Element
std::shared_ptr< Node > Element
Definition elements.hpp:22
ftxui::Make
std::shared_ptr< T > Make(Args &&... args)
Definition component.hpp:26
ftxui::Component
std::shared_ptr< ComponentBase > Component
Definition component_base.hpp:24
ftxui::Components
std::vector< Component > Components
Definition component_base.hpp:25
ftxui::Event
Represent an event. It can be key press event, a terminal resize, or more ...
Definition event.hpp:27