mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-10-31 10:38:09 +08:00 
			
		
		
		
	Add the Button component.
This commit is contained in:
		 ArthurSonzogni
					ArthurSonzogni
				
			
				
					committed by
					
						 Arthur Sonzogni
						Arthur Sonzogni
					
				
			
			
				
	
			
			
			 Arthur Sonzogni
						Arthur Sonzogni
					
				
			
						parent
						
							81d79d311d
						
					
				
				
					commit
					5a8ed208da
				
			| @@ -4,9 +4,11 @@ function(example name) | ||||
|   set_property(TARGET ${name} PROPERTY CXX_STANDARD 17) | ||||
| endfunction(example) | ||||
|  | ||||
| example(button) | ||||
| example(checkbox) | ||||
| example(checkbox_in_frame) | ||||
| example(gallery) | ||||
| example(homescreen) | ||||
| example(input) | ||||
| example(menu) | ||||
| example(menu2) | ||||
| @@ -16,4 +18,3 @@ example(radiobox_in_frame) | ||||
| example(tab_horizontal) | ||||
| example(tab_vertical) | ||||
| example(toggle) | ||||
| example(homescreen) | ||||
|   | ||||
							
								
								
									
										48
									
								
								examples/component/button.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								examples/component/button.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,48 @@ | ||||
| // 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. | ||||
|  | ||||
| #include "ftxui/component/button.hpp" | ||||
|  | ||||
| #include "ftxui/component/component.hpp" | ||||
| #include "ftxui/component/container.hpp" | ||||
| #include "ftxui/component/screen_interactive.hpp" | ||||
|  | ||||
| using namespace ftxui; | ||||
|  | ||||
| class MyComponent : public Component { | ||||
|  private: | ||||
|   std::vector<std::unique_ptr<Button>> buttons_; | ||||
|   Container container_ = Container::Horizontal(); | ||||
|  | ||||
|  public: | ||||
|   MyComponent() { | ||||
|     Add(&container_); | ||||
|  | ||||
|     auto button_add = std::make_unique<Button>(); | ||||
|     auto button_remove = std::make_unique<Button>(); | ||||
|     container_.Add(button_add.get()); | ||||
|     container_.Add(button_remove.get()); | ||||
|     button_add->label = L"Add one button"; | ||||
|     button_remove->label = L"Remove last button"; | ||||
|  | ||||
|     button_add->on_click = [&] { | ||||
|       auto extra_button = std::make_unique<Button>(); | ||||
|       extra_button->label = L"extra button"; | ||||
|       container_.Add(extra_button.get()); | ||||
|       buttons_.push_back(std::move(extra_button)); | ||||
|     }; | ||||
|  | ||||
|     button_remove->on_click = [&] { buttons_.resize(buttons_.size() - 1); }; | ||||
|  | ||||
|     buttons_.push_back(std::move(button_add)); | ||||
|     buttons_.push_back(std::move(button_remove)); | ||||
|   } | ||||
| }; | ||||
|  | ||||
| int main(int argc, const char* argv[]) { | ||||
|   auto screen = ScreenInteractive::TerminalOutput(); | ||||
|   MyComponent component; | ||||
|   screen.Loop(&component); | ||||
|   return 0; | ||||
| } | ||||
| @@ -3,6 +3,7 @@ | ||||
| // the LICENSE file. | ||||
|  | ||||
| #include "ftxui/component/checkbox.hpp" | ||||
| #include "ftxui/component/button.hpp" | ||||
| #include "ftxui/component/container.hpp" | ||||
| #include "ftxui/component/input.hpp" | ||||
| #include "ftxui/component/menu.hpp" | ||||
| @@ -21,6 +22,7 @@ class MyComponent : public Component { | ||||
|   CheckBox checkbox2; | ||||
|   RadioBox radiobox; | ||||
|   Input input; | ||||
|   Button button; | ||||
|  | ||||
|  public: | ||||
|   MyComponent() { | ||||
| @@ -55,6 +57,10 @@ class MyComponent : public Component { | ||||
|  | ||||
|     input.placeholder = L"Input placeholder"; | ||||
|     container.Add(&input); | ||||
|  | ||||
|     button.label = L"Quit"; | ||||
|     button.on_click = [&] { on_quit(); }; | ||||
|     container.Add(&button); | ||||
|   } | ||||
|  | ||||
|   Element Render(std::wstring name, Component& component) { | ||||
| @@ -66,24 +72,30 @@ class MyComponent : public Component { | ||||
|   } | ||||
|  | ||||
|   Element Render() override { | ||||
|     return vbox({ | ||||
|                Render(L"menu", menu), | ||||
|                separator(), | ||||
|                Render(L"toggle", toggle), | ||||
|                separator(), | ||||
|                Render(L"checkbox", checkbox_container), | ||||
|                separator(), | ||||
|                Render(L"radiobox", radiobox), | ||||
|                separator(), | ||||
|                Render(L"input", input) | size(WIDTH, LESS_THAN, 30), | ||||
|            }) | | ||||
|            border; | ||||
|     return  // | ||||
|         vbox({ | ||||
|             Render(L"menu", menu), | ||||
|             separator(), | ||||
|             Render(L"toggle", toggle), | ||||
|             separator(), | ||||
|             Render(L"checkbox", checkbox_container), | ||||
|             separator(), | ||||
|             Render(L"radiobox", radiobox), | ||||
|             separator(), | ||||
|             Render(L"input", input) | size(WIDTH, LESS_THAN, 30), | ||||
|             separator(), | ||||
|             Render(L"button", button), | ||||
|         }) | | ||||
|         border; | ||||
|   } | ||||
|  | ||||
|   std::function<void()> on_quit = [] {}; | ||||
| }; | ||||
|  | ||||
| int main(int argc, const char* argv[]) { | ||||
|   auto screen = ScreenInteractive::FitComponent(); | ||||
|   MyComponent component; | ||||
|   component.on_quit = screen.ExitLoopClosure(); | ||||
|   screen.Loop(&component); | ||||
|  | ||||
|   return 0; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user