FTXUI/include/ftxui/component/component.hpp

70 lines
1.8 KiB
C++
Raw Normal View History

#ifndef FTXUI_COMPONENT_COMPONENT_HPP
#define FTXUI_COMPONENT_COMPONENT_HPP
#include "ftxui/component/event.hpp"
2018-10-19 04:58:38 +08:00
#include "ftxui/dom/elements.hpp"
namespace ftxui {
class Delegate;
class Focus;
2020-08-16 08:24:50 +08:00
/// @brief It implement rendering itself as ftxui::Element. It implement
/// keyboard navigation by responding to ftxui::Event.
/// @ingroup component
class Component {
public:
// Constructor/Destructor.
2019-01-13 01:24:46 +08:00
Component() = default;
virtual ~Component();
2019-01-13 01:24:46 +08:00
// Component hierarchy.
2020-08-16 08:24:50 +08:00
Component* Parent();
2019-01-13 01:24:46 +08:00
void Add(Component* children);
2019-01-13 01:24:46 +08:00
// Renders the component.
virtual Element Render();
2019-01-13 01:24:46 +08:00
// Handles an event.
// By default, reduce on children with a lazy OR.
//
// Returns whether the event was handled or not.
virtual bool OnEvent(Event);
// 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.
2019-01-13 01:24:46 +08:00
virtual Component* ActiveChild();
2019-01-13 01:24:46 +08:00
// Whether this is the active child of its parent.
bool Active();
// Whether all the ancestors are active.
bool Focused();
// Make the |child| to be the "active" one.
virtual void SetActiveChild(Component* child);
// Configure all the ancestors to give focus to this component.
void TakeFocus();
private:
2019-01-13 01:24:46 +08:00
Component* parent_ = nullptr;
void Detach();
void Attach(Component* parent);
protected:
std::vector<Component*> children_;
};
} // namespace ftxui
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_HPP */
// 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.