mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-10-31 02:28:11 +08:00 
			
		
		
		
	Add colored border. (#595)
This resolves: https://github.com/ArthurSonzogni/FTXUI/issues/564
This commit is contained in:
		 Arthur Sonzogni
					Arthur Sonzogni
				
			
				
					committed by
					
						 ArthurSonzogni
						ArthurSonzogni
					
				
			
			
				
	
			
			
			 ArthurSonzogni
						ArthurSonzogni
					
				
			
						parent
						
							7bdca3ee68
						
					
				
				
					commit
					399549d180
				
			| @@ -10,6 +10,7 @@ current (development) | |||||||
|  |  | ||||||
| ### Dom | ### Dom | ||||||
| - Feature: Add the dashed style for border and separator. | - Feature: Add the dashed style for border and separator. | ||||||
|  | - Feature: Add colored borders. | ||||||
|  |  | ||||||
| ### | ### | ||||||
| - Breaking: Direction enum is renamed WidthOrHeight | - Breaking: Direction enum is renamed WidthOrHeight | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| set(DIRECTORY_LIB dom) | set(DIRECTORY_LIB dom) | ||||||
|  |  | ||||||
| example(border) | example(border) | ||||||
|  | example(border_colored) | ||||||
| example(border_style) | example(border_style) | ||||||
| example(color_gallery) | example(color_gallery) | ||||||
| example(color_info_palette256) | example(color_info_palette256) | ||||||
|   | |||||||
							
								
								
									
										40
									
								
								examples/dom/border_colored.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										40
									
								
								examples/dom/border_colored.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,40 @@ | |||||||
|  | #include <ftxui/dom/elements.hpp>  // for operator|, text, Element, Fit, borderDouble, borderHeavy, borderLight, borderRounded, vbox | ||||||
|  | #include <ftxui/screen/screen.hpp>  // for Screen | ||||||
|  | #include <iostream>                 // for endl, cout, ostream | ||||||
|  | #include <memory>                   // for allocator | ||||||
|  |  | ||||||
|  | #include "ftxui/dom/node.hpp"      // for Render | ||||||
|  | #include "ftxui/screen/color.hpp"  // for ftxui | ||||||
|  |  | ||||||
|  | int main(int argc, const char* argv[]) { | ||||||
|  |   using namespace ftxui; | ||||||
|  |  | ||||||
|  |   auto make_boxed = [] { | ||||||
|  |     return vbox({ | ||||||
|  |         text("borderLight") | borderStyled(LIGHT, Color::Red), | ||||||
|  |         text("borderDashed") | borderStyled(DASHED, Color::Green), | ||||||
|  |         text("borderHeavy") | borderStyled(HEAVY, Color::Blue), | ||||||
|  |         text("borderDouble") | borderStyled(DOUBLE, Color::Yellow), | ||||||
|  |         text("borderRounded") | borderStyled(ROUNDED, Color::Cyan), | ||||||
|  |     }); | ||||||
|  |   }; | ||||||
|  |  | ||||||
|  |   auto document = hbox({ | ||||||
|  |                       make_boxed(), | ||||||
|  |                       separator() | color(Color::Red), | ||||||
|  |                       make_boxed(), | ||||||
|  |                       separator() | color(Color::Red), | ||||||
|  |                       make_boxed(), | ||||||
|  |                   }) | | ||||||
|  |                   borderStyled(ROUNDED, Color::Red); | ||||||
|  |  | ||||||
|  |   auto screen = | ||||||
|  |       Screen::Create(Dimension::Fit(document), Dimension::Fit(document)); | ||||||
|  |   Render(screen, document); | ||||||
|  |   screen.Print(); | ||||||
|  |   std::cout << std::endl; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // 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. | ||||||
| @@ -73,6 +73,8 @@ Element borderDouble(Element); | |||||||
| Element borderRounded(Element); | Element borderRounded(Element); | ||||||
| Element borderEmpty(Element); | Element borderEmpty(Element); | ||||||
| Decorator borderStyled(BorderStyle); | Decorator borderStyled(BorderStyle); | ||||||
|  | Decorator borderStyled(BorderStyle, Color); | ||||||
|  | Decorator borderStyled(Color); | ||||||
| Decorator borderWith(const Pixel&); | Decorator borderWith(const Pixel&); | ||||||
| Element window(Element title, Element content); | Element window(Element title, Element content); | ||||||
| Element spinner(int charset_index, size_t image_index); | Element spinner(int charset_index, size_t image_index); | ||||||
|   | |||||||
| @@ -1,6 +1,7 @@ | |||||||
| #include <algorithm>  // for max | #include <algorithm>  // for max | ||||||
| #include <array>      // for array | #include <array>      // for array | ||||||
| #include <memory>     // for allocator, make_shared, __shared_ptr_access | #include <memory>     // for allocator, make_shared, __shared_ptr_access | ||||||
|  | #include <optional> | ||||||
| #include <string>   // for basic_string, string | #include <string>   // for basic_string, string | ||||||
| #include <utility>  // for move | #include <utility>  // for move | ||||||
| #include <vector>   // for __alloc_traits<>::value_type | #include <vector>   // for __alloc_traits<>::value_type | ||||||
| @@ -28,11 +29,15 @@ static Charsets simple_border_charset = { | |||||||
| // For reference, here is the charset for normal border: | // For reference, here is the charset for normal border: | ||||||
| class Border : public Node { | class Border : public Node { | ||||||
|  public: |  public: | ||||||
|   Border(Elements children, BorderStyle style) |   Border(Elements children, | ||||||
|  |          BorderStyle style, | ||||||
|  |          std::optional<Color> foreground_color = std::nullopt) | ||||||
|       : Node(std::move(children)), |       : Node(std::move(children)), | ||||||
|         charset_(simple_border_charset[style]) {}  // NOLINT |         charset_(simple_border_charset[style]), | ||||||
|  |         foreground_color_(foreground_color) {}  // NOLINT | ||||||
|  |  | ||||||
|   const Charset& charset_;  // NOLINT |   const Charset& charset_;  // NOLINT | ||||||
|  |   std::optional<Color> foreground_color_; | ||||||
|  |  | ||||||
|   void ComputeRequirement() override { |   void ComputeRequirement() override { | ||||||
|     Node::ComputeRequirement(); |     Node::ComputeRequirement(); | ||||||
| @@ -101,6 +106,18 @@ class Border : public Node { | |||||||
|     if (children_.size() == 2) { |     if (children_.size() == 2) { | ||||||
|       children_[1]->Render(screen); |       children_[1]->Render(screen); | ||||||
|     } |     } | ||||||
|  |  | ||||||
|  |     // Draw the border color. | ||||||
|  |     if (foreground_color_) { | ||||||
|  |       for (int x = box_.x_min; x <= box_.x_max; ++x) { | ||||||
|  |         screen.PixelAt(x, box_.y_min).foreground_color = *foreground_color_; | ||||||
|  |         screen.PixelAt(x, box_.y_max).foreground_color = *foreground_color_; | ||||||
|  |       } | ||||||
|  |       for (int y = box_.y_min; y <= box_.y_max; ++y) { | ||||||
|  |         screen.PixelAt(box_.x_min, y).foreground_color = *foreground_color_; | ||||||
|  |         screen.PixelAt(box_.x_max, y).foreground_color = *foreground_color_; | ||||||
|  |       } | ||||||
|  |     } | ||||||
|   } |   } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| @@ -179,6 +196,8 @@ class BorderPixel : public Node { | |||||||
| /// @see borderHeavy | /// @see borderHeavy | ||||||
| /// @see borderEmpty | /// @see borderEmpty | ||||||
| /// @see borderRounded | /// @see borderRounded | ||||||
|  | /// @see borderStyled | ||||||
|  | /// @see borderWith | ||||||
| /// | /// | ||||||
| /// Add a border around an element | /// Add a border around an element | ||||||
| /// | /// | ||||||
| @@ -221,6 +240,26 @@ Decorator borderStyled(BorderStyle style) { | |||||||
|   }; |   }; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | /// @brief Same as border but with a foreground color. | ||||||
|  | /// @ingroup dom | ||||||
|  | /// @see border | ||||||
|  | Decorator borderStyled(Color foreground_color) { | ||||||
|  |   return [foreground_color](Element child) { | ||||||
|  |     return std::make_shared<Border>(unpack(std::move(child)), ROUNDED, | ||||||
|  |                                     foreground_color); | ||||||
|  |   }; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | /// @brief Same as border but with a foreground color and a different style | ||||||
|  | /// @ingroup dom | ||||||
|  | /// @see border | ||||||
|  | Decorator borderStyled(BorderStyle style, Color foreground_color) { | ||||||
|  |   return [style, foreground_color](Element child) { | ||||||
|  |     return std::make_shared<Border>(unpack(std::move(child)), style, | ||||||
|  |                                     foreground_color); | ||||||
|  |   }; | ||||||
|  | } | ||||||
|  |  | ||||||
| /// @brief Draw a light border around the element. | /// @brief Draw a light border around the element. | ||||||
| /// @ingroup dom | /// @ingroup dom | ||||||
| /// @see border | /// @see border | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user