2018-10-10 01:06:03 +08:00
|
|
|
#ifndef FTXUI_COMPONENT_COMPONENT_HPP
|
|
|
|
#define FTXUI_COMPONENT_COMPONENT_HPP
|
|
|
|
|
2021-05-02 02:40:35 +08:00
|
|
|
#include <memory> // for unique_ptr
|
|
|
|
#include <vector> // for vector
|
|
|
|
|
|
|
|
#include "ftxui/component/captured_mouse.hpp" // for CaptureMouse
|
|
|
|
#include "ftxui/dom/elements.hpp" // for Element
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2018-10-10 01:06:03 +08:00
|
|
|
|
|
|
|
class Delegate;
|
|
|
|
class Focus;
|
2021-05-02 02:40:35 +08:00
|
|
|
struct Event;
|
2018-10-10 01:06:03 +08:00
|
|
|
|
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
|
2018-10-10 01:06:03 +08:00
|
|
|
class Component {
|
|
|
|
public:
|
|
|
|
// Constructor/Destructor.
|
2019-01-13 01:24:46 +08:00
|
|
|
Component() = default;
|
2018-10-10 01:06:03 +08:00
|
|
|
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);
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2019-01-13 01:24:46 +08:00
|
|
|
// Renders the component.
|
|
|
|
virtual Element Render();
|
2018-10-10 01:06:03 +08:00
|
|
|
|
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
|
2018-10-10 01:06:03 +08:00
|
|
|
// root component contains this object.
|
2019-01-13 01:24:46 +08:00
|
|
|
virtual Component* ActiveChild();
|
2020-08-26 20:57:42 +08:00
|
|
|
|
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();
|
2018-10-10 01:06:03 +08:00
|
|
|
|
2020-08-26 20:57:42 +08:00
|
|
|
// 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();
|
|
|
|
|
2021-05-02 00:13:56 +08:00
|
|
|
protected:
|
2021-05-02 02:40:35 +08:00
|
|
|
CapturedMouse CaptureMouse(const Event& event);
|
|
|
|
|
2021-05-02 00:13:56 +08:00
|
|
|
std::vector<Component*> children_;
|
|
|
|
|
2018-10-10 01:06:03 +08:00
|
|
|
private:
|
2019-01-13 01:24:46 +08:00
|
|
|
Component* parent_ = nullptr;
|
|
|
|
void Detach();
|
|
|
|
void Attach(Component* parent);
|
2018-10-10 01:06:03 +08:00
|
|
|
};
|
|
|
|
|
2021-04-29 06:18:58 +08:00
|
|
|
using ComponentPtr = std::unique_ptr<Component>;
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
} // namespace ftxui
|
2018-10-10 01:06:03 +08:00
|
|
|
|
|
|
|
#endif /* end of include guard: FTXUI_COMPONENT_COMPONENT_HPP */
|
2020-08-16 06:24:18 +08:00
|
|
|
|
|
|
|
// 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.
|