mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-10-31 10:38:09 +08:00 
			
		
		
		
	Feature: Dropdown options with callback (#826)
Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
		| @@ -11,6 +11,7 @@ example(collapsible) | ||||
| example(composition) | ||||
| example(custom_loop) | ||||
| example(dropdown) | ||||
| example(dropdown_custom) | ||||
| example(flexbox_gallery) | ||||
| example(focus) | ||||
| example(focus_cursor) | ||||
|   | ||||
							
								
								
									
										104
									
								
								examples/component/dropdown_custom.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								examples/component/dropdown_custom.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,104 @@ | ||||
| // 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 <string>  // for basic_string, string, allocator | ||||
| #include <vector>  // for vector | ||||
|  | ||||
| #include "ftxui/component/captured_mouse.hpp"  // for ftxui | ||||
| #include "ftxui/component/component.hpp"  // for Dropdown, Horizontal, Vertical | ||||
| #include "ftxui/component/screen_interactive.hpp"  // for ScreenInteractive | ||||
|  | ||||
| int main() { | ||||
|   using namespace ftxui; | ||||
|  | ||||
|   std::vector<std::string> entries = { | ||||
|       "tribute",     "clearance", "ally",        "bend",        "electronics", | ||||
|       "module",      "era",       "cultural",    "sniff",       "nationalism", | ||||
|       "negotiation", "deliver",   "figure",      "east",        "tribute", | ||||
|       "clearance",   "ally",      "bend",        "electronics", "module", | ||||
|       "era",         "cultural",  "sniff",       "nationalism", "negotiation", | ||||
|       "deliver",     "figure",    "east",        "tribute",     "clearance", | ||||
|       "ally",        "bend",      "electronics", "module",      "era", | ||||
|       "cultural",    "sniff",     "nationalism", "negotiation", "deliver", | ||||
|       "figure",      "east", | ||||
|   }; | ||||
|  | ||||
|   auto dropdown_1 = Dropdown({ | ||||
|       .radiobox = {.entries = &entries}, | ||||
|       .transform = | ||||
|           [](bool open, Element checkbox, Element radiobox) { | ||||
|             if (open) { | ||||
|               return vbox({ | ||||
|                   checkbox | inverted, | ||||
|                   radiobox | vscroll_indicator | frame | | ||||
|                       size(HEIGHT, LESS_THAN, 10), | ||||
|                   filler(), | ||||
|               }); | ||||
|             } | ||||
|             return vbox({ | ||||
|                 checkbox, | ||||
|                 filler(), | ||||
|             }); | ||||
|           }, | ||||
|   }); | ||||
|  | ||||
|   auto dropdown_2 = Dropdown({ | ||||
|       .radiobox = {.entries = &entries}, | ||||
|       .transform = | ||||
|           [](bool open, Element checkbox, Element radiobox) { | ||||
|             if (open) { | ||||
|               return vbox({ | ||||
|                   checkbox | inverted, | ||||
|                   radiobox | vscroll_indicator | frame | | ||||
|                       size(HEIGHT, LESS_THAN, 10) | bgcolor(Color::Blue), | ||||
|                   filler(), | ||||
|               }); | ||||
|             } | ||||
|             return vbox({ | ||||
|                 checkbox | bgcolor(Color::Blue), | ||||
|                 filler(), | ||||
|             }); | ||||
|           }, | ||||
|   }); | ||||
|  | ||||
|   auto dropdown_3 = Dropdown({ | ||||
|       .radiobox = | ||||
|           { | ||||
|               .entries = &entries, | ||||
|               .transform = | ||||
|                   [](const EntryState& s) { | ||||
|                     auto t = text(s.label) | borderEmpty; | ||||
|                     if (s.active) { | ||||
|                       t |= bold; | ||||
|                     } | ||||
|                     if (s.focused) { | ||||
|                       t |= inverted; | ||||
|                     } | ||||
|                     return t; | ||||
|                   }, | ||||
|           }, | ||||
|       .transform = | ||||
|           [](bool open, Element checkbox, Element radiobox) { | ||||
|             checkbox |= borderEmpty; | ||||
|             if (open) { | ||||
|               return vbox({ | ||||
|                   checkbox | inverted, | ||||
|                   radiobox | vscroll_indicator | frame | | ||||
|                       size(HEIGHT, LESS_THAN, 20) | bgcolor(Color::Red), | ||||
|                   filler(), | ||||
|               }); | ||||
|             } | ||||
|             return vbox({ | ||||
|                 checkbox | bgcolor(Color::Red), | ||||
|                 filler(), | ||||
|             }); | ||||
|           }, | ||||
|   }); | ||||
|  | ||||
|   auto screen = ScreenInteractive::FitComponent(); | ||||
|   screen.Loop(Container::Horizontal({ | ||||
|       dropdown_1, | ||||
|       dropdown_2, | ||||
|       dropdown_3, | ||||
|   })); | ||||
| } | ||||
| @@ -494,11 +494,11 @@ int main() { | ||||
|       "Exit", [&] { screen.Exit(); }, ButtonOption::Animated()); | ||||
|  | ||||
|   auto main_container = Container::Vertical({ | ||||
|     Container::Horizontal({ | ||||
|         tab_selection, | ||||
|         exit_button, | ||||
|     }), | ||||
|     tab_content, | ||||
|       Container::Horizontal({ | ||||
|           tab_selection, | ||||
|           exit_button, | ||||
|       }), | ||||
|       tab_content, | ||||
|   }); | ||||
|  | ||||
|   auto main_renderer = Renderer(main_container, [&] { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 James
					James