| 
									
										
										
										
											2023-08-19 13:56:36 +02: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.
 | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02:00
										 |  |  | #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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 18:51:46 +01:00
										 |  |  | namespace animation { | 
					
						
							|  |  |  | class Params; | 
					
						
							|  |  |  | }  // namespace animation
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02:00
										 |  |  | 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: | 
					
						
							| 
									
										
										
										
											2024-05-01 11:40:49 +02:00
										 |  |  |   explicit ComponentBase(Components children) | 
					
						
							|  |  |  |       : children_(std::move(children)) {} | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02:00
										 |  |  |   virtual ~ComponentBase(); | 
					
						
							| 
									
										
										
										
											2023-05-16 11:33:41 -05:00
										 |  |  |   ComponentBase() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-01 11:40:49 +02:00
										 |  |  |   // A component is not copyable/movable.
 | 
					
						
							| 
									
										
										
										
											2023-05-16 11:33:41 -05:00
										 |  |  |   ComponentBase(const ComponentBase&) = delete; | 
					
						
							| 
									
										
										
										
											2024-05-01 11:40:49 +02:00
										 |  |  |   ComponentBase(ComponentBase&&) = delete; | 
					
						
							|  |  |  |   ComponentBase& operator=(const ComponentBase&) = delete; | 
					
						
							|  |  |  |   ComponentBase& operator=(ComponentBase&&) = delete; | 
					
						
							| 
									
										
										
										
											2023-05-16 11:33:41 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-15 15:29:33 +02:00
										 |  |  |   // Component hierarchy:
 | 
					
						
							| 
									
										
										
										
											2021-07-21 04:07:44 +05:30
										 |  |  |   ComponentBase* Parent() const; | 
					
						
							| 
									
										
										
										
											2021-07-15 15:29:33 +02:00
										 |  |  |   Component& ChildAt(size_t i); | 
					
						
							|  |  |  |   size_t ChildCount() const; | 
					
						
							| 
									
										
										
										
											2024-09-30 23:18:59 +02:00
										 |  |  |   int Index() const; | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02:00
										 |  |  |   void Add(Component children); | 
					
						
							| 
									
										
										
										
											2021-07-15 15:29:33 +02:00
										 |  |  |   void Detach(); | 
					
						
							|  |  |  |   void DetachAllChildren(); | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // Renders the component.
 | 
					
						
							| 
									
										
										
										
											2025-03-19 20:03:05 +05:30
										 |  |  |   Element Render(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Override this function modify how `Render` works.
 | 
					
						
							|  |  |  |   virtual Element OnRender(); | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02: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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-13 18:51:46 +01:00
										 |  |  |   // Handle an animation step.
 | 
					
						
							|  |  |  |   virtual void OnAnimation(animation::Params& params); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02:00
										 |  |  |   // 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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-05 22:40:40 +02:00
										 |  |  |   // Return true when the component contains focusable elements.
 | 
					
						
							|  |  |  |   // The non focusable Component will be skipped when navigating using the
 | 
					
						
							|  |  |  |   // keyboard.
 | 
					
						
							|  |  |  |   virtual bool Focusable() const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02:00
										 |  |  |   // Whether this is the active child of its parent.
 | 
					
						
							| 
									
										
										
										
											2021-07-21 04:07:44 +05:30
										 |  |  |   bool Active() const; | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02:00
										 |  |  |   // Whether all the ancestors are active.
 | 
					
						
							| 
									
										
										
										
											2021-07-21 04:07:44 +05:30
										 |  |  |   bool Focused() const; | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |   // 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); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-07-21 04:07:44 +05:30
										 |  |  |   Components children_; | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |  private: | 
					
						
							|  |  |  |   ComponentBase* parent_ = nullptr; | 
					
						
							| 
									
										
										
										
											2025-03-19 20:03:05 +05:30
										 |  |  |   bool in_render = false; | 
					
						
							| 
									
										
										
										
											2021-05-09 20:32:27 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }  // namespace ftxui
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif /* end of include guard: FTXUI_COMPONENT_BASE_HPP */
 |