| 
									
										
										
										
											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.
 | 
					
						
							| 
									
										
										
										
											2019-01-06 17:10:35 +01:00
										 |  |  | #ifndef FTXUI_DOM_NODE_HPP
 | 
					
						
							|  |  |  | #define FTXUI_DOM_NODE_HPP
 | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-01 20:40:35 +02:00
										 |  |  | #include <memory>  // for shared_ptr
 | 
					
						
							|  |  |  | #include <vector>  // for vector
 | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-01 20:40:35 +02:00
										 |  |  | #include "ftxui/dom/requirement.hpp"  // for Requirement
 | 
					
						
							| 
									
										
										
										
											2024-12-27 15:45:13 +07:00
										 |  |  | #include "ftxui/dom/selection.hpp"    // for Selection
 | 
					
						
							| 
									
										
										
										
											2021-05-01 20:40:35 +02:00
										 |  |  | #include "ftxui/screen/box.hpp"       // for Box
 | 
					
						
							| 
									
										
										
										
											2019-01-06 17:10:35 +01:00
										 |  |  | #include "ftxui/screen/screen.hpp"
 | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-12 15:00:08 +01:00
										 |  |  | namespace ftxui { | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-25 01:34:13 +02:00
										 |  |  | class Node; | 
					
						
							| 
									
										
										
										
											2021-05-01 20:40:35 +02:00
										 |  |  | class Screen; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-20 20:36:47 +02:00
										 |  |  | using Element = std::shared_ptr<Node>; | 
					
						
							| 
									
										
										
										
											2021-07-17 15:32:08 +05:30
										 |  |  | using Elements = std::vector<Element>; | 
					
						
							| 
									
										
										
										
											2020-05-20 20:36:47 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-06-05 11:35:14 +02:00
										 |  |  | /// @brief Node is the base class for all elements in the DOM tree.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// It represents a single node in the document object model (DOM) and provides
 | 
					
						
							|  |  |  | /// the basic structure for layout and rendering.
 | 
					
						
							|  |  |  | /// It contains methods for computing layout requirements, setting the box
 | 
					
						
							|  |  |  | /// dimensions, selecting content, rendering to the screen, and checking the
 | 
					
						
							|  |  |  | /// layout status.
 | 
					
						
							|  |  |  | /// It typically contains child elements, which are also instances of Node.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// Users are expected to derive from this class to create custom elements.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// A list of builtin elements can be found in the `elements.hpp` file.
 | 
					
						
							|  |  |  | ///
 | 
					
						
							|  |  |  | /// @ingroup dom
 | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  | class Node { | 
					
						
							|  |  |  |  public: | 
					
						
							|  |  |  |   Node(); | 
					
						
							| 
									
										
										
										
											2024-05-01 11:40:49 +02:00
										 |  |  |   explicit Node(Elements children); | 
					
						
							| 
									
										
										
										
											2022-03-31 02:17:43 +02:00
										 |  |  |   Node(const Node&) = delete; | 
					
						
							|  |  |  |   Node(const Node&&) = delete; | 
					
						
							|  |  |  |   Node& operator=(const Node&) = delete; | 
					
						
							|  |  |  |   Node& operator=(const Node&&) = delete; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  |   virtual ~Node(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-02 22:33:59 +01:00
										 |  |  |   // Step 1: Compute layout requirement. Tell parent what dimensions this
 | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  |   //         element wants to be.
 | 
					
						
							| 
									
										
										
										
											2019-01-02 22:33:59 +01:00
										 |  |  |   //         Propagated from Children to Parents.
 | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  |   virtual void ComputeRequirement(); | 
					
						
							|  |  |  |   Requirement requirement() { return requirement_; } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-02 22:33:59 +01:00
										 |  |  |   // Step 2: Assign this element its final dimensions.
 | 
					
						
							|  |  |  |   //         Propagated from Parents to Children.
 | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  |   virtual void SetBox(Box box); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-27 15:45:13 +07:00
										 |  |  |   // Step 3: (optional) Selection
 | 
					
						
							|  |  |  |   //         Propagated from Parents to Children.
 | 
					
						
							|  |  |  |   virtual void Select(Selection& selection); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Step 4: Draw this element.
 | 
					
						
							| 
									
										
										
										
											2019-01-12 15:00:08 +01:00
										 |  |  |   virtual void Render(Screen& screen); | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-27 15:45:13 +07:00
										 |  |  |   virtual std::string GetSelectedContent(Selection& selection); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-12-11 17:58:25 +01:00
										 |  |  |   // Layout may not resolve within a single iteration for some elements. This
 | 
					
						
							|  |  |  |   // allows them to request additionnal iterations. This signal must be
 | 
					
						
							|  |  |  |   // forwarded to children at least once.
 | 
					
						
							|  |  |  |   struct Status { | 
					
						
							|  |  |  |     int iteration = 0; | 
					
						
							|  |  |  |     bool need_iteration = false; | 
					
						
							|  |  |  |   }; | 
					
						
							|  |  |  |   virtual void Check(Status* status); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-19 20:03:05 +05:30
										 |  |  |   friend void Render(Screen& screen, Node* node, Selection& selection); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  |  protected: | 
					
						
							| 
									
										
										
										
											2021-07-20 13:29:47 +05:30
										 |  |  |   Elements children_; | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  |   Requirement requirement_; | 
					
						
							|  |  |  |   Box box_; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 02:17:43 +02:00
										 |  |  | void Render(Screen& screen, const Element& element); | 
					
						
							| 
									
										
										
										
											2019-01-12 15:00:08 +01:00
										 |  |  | void Render(Screen& screen, Node* node); | 
					
						
							| 
									
										
										
										
											2024-12-27 15:45:13 +07:00
										 |  |  | void Render(Screen& screen, Node* node, Selection& selection); | 
					
						
							|  |  |  | std::string GetNodeSelectedContent(Screen& screen, | 
					
						
							|  |  |  |                                    Node* node, | 
					
						
							|  |  |  |                                    Selection& selection); | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-11 21:44:55 +01:00
										 |  |  | }  // namespace ftxui
 | 
					
						
							| 
									
										
										
										
											2018-09-18 08:48:40 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-31 02:17:43 +02:00
										 |  |  | #endif  // FTXUI_DOM_NODE_HPP
 |