mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-11-01 02:58:12 +08:00 
			
		
		
		
	Add RadioBox.
This commit is contained in:
		| @@ -4,12 +4,13 @@ function(example name) | |||||||
|   target_link_libraries(${name} PUBLIC component) |   target_link_libraries(${name} PUBLIC component) | ||||||
| endfunction(example) | endfunction(example) | ||||||
|  |  | ||||||
|  | example(checkbox) | ||||||
| example(gallery) | example(gallery) | ||||||
| example(input) | example(input) | ||||||
| example(menu) | example(menu) | ||||||
| example(menu2) | example(menu2) | ||||||
| example(menu_style) | example(menu_style) | ||||||
| example(toggle) | example(radiobox) | ||||||
| example(tab_horizontal) | example(tab_horizontal) | ||||||
| example(tab_vertical) | example(tab_vertical) | ||||||
| example(checkbox) | example(toggle) | ||||||
|   | |||||||
| @@ -20,11 +20,21 @@ class MyComponent : public Component { | |||||||
|       box_1_.label = L"Build examples"; |       box_1_.label = L"Build examples"; | ||||||
|       box_2_.label = L"Build tests"; |       box_2_.label = L"Build tests"; | ||||||
|       box_3_.label = L"Use WebAssembly"; |       box_3_.label = L"Use WebAssembly"; | ||||||
|  |       box_3_.state = true; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     Element Render() { | ||||||
|  |       return | ||||||
|  |         window(text(L" Checkbox "), | ||||||
|  |           hbox( | ||||||
|  |             container_.Render() | ||||||
|  |           ) | ||||||
|  |         ); | ||||||
|     } |     } | ||||||
| }; | }; | ||||||
|  |  | ||||||
| int main(int argc, const char *argv[]) { | int main(int argc, const char *argv[]) { | ||||||
|   auto screen = ScreenInteractive::TerminalOutput(); |   auto screen = ScreenInteractive::FixedSize(30,5); | ||||||
|   MyComponent component; |   MyComponent component; | ||||||
|   screen.Loop(&component); |   screen.Loop(&component); | ||||||
|   return 0; |   return 0; | ||||||
|   | |||||||
							
								
								
									
										19
									
								
								examples/component/radiobox.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								examples/component/radiobox.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,19 @@ | |||||||
|  | #include "ftxui/component/radiobox.hpp" | ||||||
|  | #include "ftxui/component/component.hpp" | ||||||
|  | #include "ftxui/component/container.hpp" | ||||||
|  | #include "ftxui/component/screen_interactive.hpp" | ||||||
|  |  | ||||||
|  | using namespace ftxui; | ||||||
|  |  | ||||||
|  | int main(int argc, const char* argv[]) { | ||||||
|  |   auto screen = ScreenInteractive::TerminalOutput(); | ||||||
|  |   RadioBox radiobox; | ||||||
|  |   radiobox.entries = { | ||||||
|  |       L"Use gcc", | ||||||
|  |       L"Use clang", | ||||||
|  |       L"Use emscripten", | ||||||
|  |       L"Use tcc", | ||||||
|  |   }; | ||||||
|  |   screen.Loop(&radiobox); | ||||||
|  |   return 0; | ||||||
|  | } | ||||||
| @@ -2,7 +2,7 @@ | |||||||
| #include <thread> | #include <thread> | ||||||
|  |  | ||||||
| #include "ftxui/component/container.hpp" | #include "ftxui/component/container.hpp" | ||||||
| #include "ftxui/component/menu.hpp" | #include "ftxui/component/radiobox.hpp" | ||||||
| #include "ftxui/component/screen_interactive.hpp" | #include "ftxui/component/screen_interactive.hpp" | ||||||
| #include "ftxui/component/toggle.hpp" | #include "ftxui/component/toggle.hpp" | ||||||
| #include "ftxui/screen/string.hpp" | #include "ftxui/screen/string.hpp" | ||||||
| @@ -10,55 +10,59 @@ | |||||||
| using namespace ftxui; | using namespace ftxui; | ||||||
|  |  | ||||||
| class MyComponent : public Component { | class MyComponent : public Component { | ||||||
|   public: |  public: | ||||||
|    MyComponent() { |   MyComponent() { | ||||||
|      Add(&container_); |     Add(&container_); | ||||||
|      container_.Add(&toggle_); |     container_.Add(&toggle_); | ||||||
|  |  | ||||||
|      toggle_.entries = { |     toggle_.entries = { | ||||||
|        L"menu_1", |         L"tab_1", | ||||||
|        L"menu_2", |         L"tab_2", | ||||||
|        L"menu_3", |         L"tab_3", | ||||||
|      }; |     }; | ||||||
|  |  | ||||||
|      container_.Add(&tab_container_); |     container_.Add(&tab_container_); | ||||||
|      |  | ||||||
|      menu_1_.entries = { |  | ||||||
|        L"Forest", |  | ||||||
|        L"Water", |  | ||||||
|        L"I don't know" |  | ||||||
|      }; |  | ||||||
|      tab_container_.Add(&menu_1_); |  | ||||||
|  |  | ||||||
|      menu_2_.entries = { |     radiobox_1_.entries = {L"Forest", L"Water", L"I don't know"}; | ||||||
|        L"Hello", |     tab_container_.Add(&radiobox_1_); | ||||||
|        L"Hi", |  | ||||||
|        L"Hay", |  | ||||||
|      }; |  | ||||||
|      tab_container_.Add(&menu_2_); |  | ||||||
|  |  | ||||||
|      menu_3_.entries = { |     radiobox_2_.entries = { | ||||||
|        L"Table", |         L"Hello", | ||||||
|        L"Nothing", |         L"Hi", | ||||||
|        L"Is", |         L"Hay", | ||||||
|        L"Empty", |     }; | ||||||
|      }; |     tab_container_.Add(&radiobox_2_); | ||||||
|      tab_container_.Add(&menu_3_); |  | ||||||
|    } |  | ||||||
|  |  | ||||||
|    std::function<void()> on_enter = [](){}; |     radiobox_3_.entries = { | ||||||
|   private: |         L"Table", | ||||||
|    Toggle toggle_; |         L"Nothing", | ||||||
|    Container container_ = Container::Vertical(); |         L"Is", | ||||||
|    Container tab_container_ = Container::Tab(&(toggle_.selected)); |         L"Empty", | ||||||
|    Menu menu_1_; |     }; | ||||||
|    Menu menu_2_; |     tab_container_.Add(&radiobox_3_); | ||||||
|    Menu menu_3_; |   } | ||||||
|  |  | ||||||
|  |   std::function<void()> on_enter = []() {}; | ||||||
|  |  | ||||||
|  |   Element Render(){ | ||||||
|  |     return | ||||||
|  |       vbox( | ||||||
|  |         toggle_.Render(), | ||||||
|  |         separator(), | ||||||
|  |         tab_container_.Render() | ||||||
|  |       ) | frame; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |  private: | ||||||
|  |   Toggle toggle_; | ||||||
|  |   Container container_ = Container::Vertical(); | ||||||
|  |   Container tab_container_ = Container::Tab(&(toggle_.selected)); | ||||||
|  |   RadioBox radiobox_1_; | ||||||
|  |   RadioBox radiobox_2_; | ||||||
|  |   RadioBox radiobox_3_; | ||||||
| }; | }; | ||||||
|  |  | ||||||
|   | int main(int argc, const char* argv[]) { | ||||||
| int main(int argc, const char *argv[]) |  | ||||||
| { |  | ||||||
|   auto screen = ScreenInteractive::TerminalOutput(); |   auto screen = ScreenInteractive::TerminalOutput(); | ||||||
|   MyComponent component; |   MyComponent component; | ||||||
|   component.on_enter = screen.ExitLoopClosure(); |   component.on_enter = screen.ExitLoopClosure(); | ||||||
|   | |||||||
| @@ -36,6 +36,7 @@ add_library(component | |||||||
|   src/ftxui/component/event.cpp |   src/ftxui/component/event.cpp | ||||||
|   src/ftxui/component/input.cpp |   src/ftxui/component/input.cpp | ||||||
|   src/ftxui/component/menu.cpp |   src/ftxui/component/menu.cpp | ||||||
|  |   src/ftxui/component/radiobox.cpp | ||||||
|   src/ftxui/component/screen_interactive.cpp |   src/ftxui/component/screen_interactive.cpp | ||||||
|   src/ftxui/component/toggle.cpp |   src/ftxui/component/toggle.cpp | ||||||
| ) | ) | ||||||
|   | |||||||
| @@ -15,8 +15,10 @@ class CheckBox : public Component { | |||||||
|   bool state = false; |   bool state = false; | ||||||
|   std::wstring label = L"label"; |   std::wstring label = L"label"; | ||||||
|  |  | ||||||
|   std::wstring checked = L"[X] "; |   //std::wstring checked = L"[X] "; | ||||||
|   std::wstring unchecked = L"[ ] "; |   //std::wstring unchecked = L"[ ] "; | ||||||
|  |   std::wstring checked = L"☑ "; | ||||||
|  |   std::wstring unchecked = L"☐ "; | ||||||
|  |  | ||||||
|   // State update callback. |   // State update callback. | ||||||
|   std::function<void()> on_change = [](){}; |   std::function<void()> on_change = [](){}; | ||||||
|   | |||||||
							
								
								
									
										38
									
								
								ftxui/include/ftxui/component/radiobox.hpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										38
									
								
								ftxui/include/ftxui/component/radiobox.hpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,38 @@ | |||||||
|  | #ifndef FTXUI_COMPONENT_RADIOBOX_HPP | ||||||
|  | #define FTXUI_COMPONENT_RADIOBOX_HPP | ||||||
|  |  | ||||||
|  | #include "ftxui/component/component.hpp" | ||||||
|  | #include <functional> | ||||||
|  |  | ||||||
|  | namespace ftxui { | ||||||
|  |  | ||||||
|  | class RadioBox : public Component { | ||||||
|  |  public: | ||||||
|  |   // Constructor. | ||||||
|  |   RadioBox() = default; | ||||||
|  |   ~RadioBox() override = default; | ||||||
|  |  | ||||||
|  |   int selected = 0; | ||||||
|  |   int focused = 0; | ||||||
|  |   std::vector<std::wstring> entries; | ||||||
|  |  | ||||||
|  |   std::wstring checked = L"◉ "; | ||||||
|  |   std::wstring unchecked = L"○ "; | ||||||
|  |  | ||||||
|  |   Decorator focused_style = inverted; | ||||||
|  |   Decorator unfocused_style = nothing; | ||||||
|  |  | ||||||
|  |   // State update callback. | ||||||
|  |   std::function<void()> on_change = [](){}; | ||||||
|  |  | ||||||
|  |   // Component implementation. | ||||||
|  |   Element Render() override; | ||||||
|  |   bool OnEvent(Event) override; | ||||||
|  |  | ||||||
|  |  private: | ||||||
|  |   int cursor_position = 0; | ||||||
|  | }; | ||||||
|  |  | ||||||
|  | }  // namespace ftxui | ||||||
|  |  | ||||||
|  | #endif /* end of include guard: FTXUI_COMPONENT_RADIOBOX_HPP */ | ||||||
							
								
								
									
										41
									
								
								ftxui/src/ftxui/component/radiobox.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										41
									
								
								ftxui/src/ftxui/component/radiobox.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,41 @@ | |||||||
|  | #include "ftxui/component/radiobox.hpp" | ||||||
|  | #include <functional> | ||||||
|  |  | ||||||
|  | namespace ftxui { | ||||||
|  |  | ||||||
|  | Element RadioBox::Render() { | ||||||
|  |   std::vector<Element> elements; | ||||||
|  |   bool is_focused = Focused(); | ||||||
|  |   for (size_t i = 0; i < entries.size(); ++i) { | ||||||
|  |     auto style = | ||||||
|  |         (focused == int(i) && is_focused) ? focused_style : unfocused_style; | ||||||
|  |     const std::wstring& symbol = selected == int(i) ? checked : unchecked; | ||||||
|  |     elements.push_back(hbox(text(symbol), text(entries[i]) | style)); | ||||||
|  |   } | ||||||
|  |   return vbox(std::move(elements)); | ||||||
|  | } | ||||||
|  |  | ||||||
|  | bool RadioBox::OnEvent(Event event) { | ||||||
|  |   if (!Focused()) | ||||||
|  |     return false; | ||||||
|  |  | ||||||
|  |   int new_focused = focused; | ||||||
|  |   if (event == Event::ArrowUp || event == Event::Character('k')) | ||||||
|  |     new_focused--; | ||||||
|  |   if (event == Event::ArrowDown || event == Event::Character('j')) | ||||||
|  |     new_focused++; | ||||||
|  |   new_focused = std::max(0, std::min(int(entries.size()) - 1, new_focused)); | ||||||
|  |   if (focused != new_focused) { | ||||||
|  |     focused = new_focused; | ||||||
|  |     return true; | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   if (event == Event::Character(' ')) { | ||||||
|  |     selected = focused; | ||||||
|  |     on_change(); | ||||||
|  |   } | ||||||
|  |  | ||||||
|  |   return false; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | }  // namespace ftxui | ||||||
		Reference in New Issue
	
	Block a user
	 Arthur Sonzogni
					Arthur Sonzogni