mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-11-04 05:28:15 +08:00 
			
		
		
		
	Update Input's options. (#195)
- Password is now taking a ref, allowing a shared state to be used by multiple passwords. - Password cursor position is now optional. It will be used only when set to something different from -1.
This commit is contained in:
		@@ -51,9 +51,11 @@ struct InputOption {
 | 
				
			|||||||
  std::function<void()> on_enter = [] {};
 | 
					  std::function<void()> on_enter = [] {};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  /// Obscure the input content using '*'.
 | 
					  /// Obscure the input content using '*'.
 | 
				
			||||||
  bool password = false;
 | 
					  Ref<bool> password = false;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  Ref<int> cursor_position = 0;
 | 
					  /// When set different from -1, this attributes is used to store the cursor
 | 
				
			||||||
 | 
					  /// position.
 | 
				
			||||||
 | 
					  Ref<int> cursor_position = -1;
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/// @brief Option for the Radiobox component.
 | 
					/// @brief Option for the Radiobox component.
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -28,14 +28,20 @@ class WideInputBase : public ComponentBase {
 | 
				
			|||||||
                Ref<InputOption> option)
 | 
					                Ref<InputOption> option)
 | 
				
			||||||
      : content_(content), placeholder_(placeholder), option_(option) {}
 | 
					      : content_(content), placeholder_(placeholder), option_(option) {}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  int& cursor_position() { return *(option_->cursor_position); }
 | 
					  int cursor_position_internal_ = 0;
 | 
				
			||||||
 | 
					  int& cursor_position() {
 | 
				
			||||||
 | 
					    int& opt = option_->cursor_position();
 | 
				
			||||||
 | 
					    if (opt != -1)
 | 
				
			||||||
 | 
					      return opt;
 | 
				
			||||||
 | 
					    return cursor_position_internal_;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  // Component implementation:
 | 
					  // Component implementation:
 | 
				
			||||||
  Element Render() override {
 | 
					  Element Render() override {
 | 
				
			||||||
    std::wstring password_content;
 | 
					    std::wstring password_content;
 | 
				
			||||||
    if (option_->password)
 | 
					    if (option_->password())
 | 
				
			||||||
      password_content = std::wstring(content_->size(), U'•');
 | 
					      password_content = std::wstring(content_->size(), U'•');
 | 
				
			||||||
    std::wstring& content = option_->password ? password_content : *content_;
 | 
					    std::wstring& content = option_->password() ? password_content : *content_;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    cursor_position() =
 | 
					    cursor_position() =
 | 
				
			||||||
        std::max(0, std::min<int>(content.size(), cursor_position()));
 | 
					        std::max(0, std::min<int>(content.size(), cursor_position()));
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -20,6 +20,7 @@ TEST(InputTest, Init) {
 | 
				
			|||||||
  std::string content;
 | 
					  std::string content;
 | 
				
			||||||
  std::string placeholder;
 | 
					  std::string placeholder;
 | 
				
			||||||
  auto option = InputOption();
 | 
					  auto option = InputOption();
 | 
				
			||||||
 | 
					  option.cursor_position = 0;
 | 
				
			||||||
  Component input = Input(&content, &placeholder, &option);
 | 
					  Component input = Input(&content, &placeholder, &option);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  EXPECT_EQ(option.cursor_position(), 0);
 | 
					  EXPECT_EQ(option.cursor_position(), 0);
 | 
				
			||||||
@@ -29,6 +30,7 @@ TEST(InputTest, Type) {
 | 
				
			|||||||
  std::string content;
 | 
					  std::string content;
 | 
				
			||||||
  std::string placeholder;
 | 
					  std::string placeholder;
 | 
				
			||||||
  auto option = InputOption();
 | 
					  auto option = InputOption();
 | 
				
			||||||
 | 
					  option.cursor_position = 0;
 | 
				
			||||||
  Component input = Input(&content, &placeholder, &option);
 | 
					  Component input = Input(&content, &placeholder, &option);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  input->OnEvent(Event::Character("a"));
 | 
					  input->OnEvent(Event::Character("a"));
 | 
				
			||||||
@@ -50,6 +52,7 @@ TEST(InputTest, TypePassword) {
 | 
				
			|||||||
  std::string content;
 | 
					  std::string content;
 | 
				
			||||||
  std::string placeholder;
 | 
					  std::string placeholder;
 | 
				
			||||||
  auto option = InputOption();
 | 
					  auto option = InputOption();
 | 
				
			||||||
 | 
					  option.cursor_position = 0;
 | 
				
			||||||
  option.password = true;
 | 
					  option.password = true;
 | 
				
			||||||
  Component input = Input(&content, &placeholder, &option);
 | 
					  Component input = Input(&content, &placeholder, &option);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -72,6 +75,7 @@ TEST(InputTest, Arrow) {
 | 
				
			|||||||
  std::string content;
 | 
					  std::string content;
 | 
				
			||||||
  std::string placeholder;
 | 
					  std::string placeholder;
 | 
				
			||||||
  auto option = InputOption();
 | 
					  auto option = InputOption();
 | 
				
			||||||
 | 
					  option.cursor_position = 0;
 | 
				
			||||||
  auto input = Input(&content, &placeholder, &option);
 | 
					  auto input = Input(&content, &placeholder, &option);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  input->OnEvent(Event::Character('a'));
 | 
					  input->OnEvent(Event::Character('a'));
 | 
				
			||||||
@@ -135,6 +139,7 @@ TEST(InputTest, Home) {
 | 
				
			|||||||
  std::string content;
 | 
					  std::string content;
 | 
				
			||||||
  std::string placeholder;
 | 
					  std::string placeholder;
 | 
				
			||||||
  auto option = InputOption();
 | 
					  auto option = InputOption();
 | 
				
			||||||
 | 
					  option.cursor_position = 0;
 | 
				
			||||||
  auto input = Input(&content, &placeholder, &option);
 | 
					  auto input = Input(&content, &placeholder, &option);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  input->OnEvent(Event::Character('a'));
 | 
					  input->OnEvent(Event::Character('a'));
 | 
				
			||||||
@@ -154,6 +159,7 @@ TEST(InputTest, End) {
 | 
				
			|||||||
  std::string content;
 | 
					  std::string content;
 | 
				
			||||||
  std::string placeholder;
 | 
					  std::string placeholder;
 | 
				
			||||||
  auto option = InputOption();
 | 
					  auto option = InputOption();
 | 
				
			||||||
 | 
					  option.cursor_position = 0;
 | 
				
			||||||
  auto input = Input(&content, &placeholder, &option);
 | 
					  auto input = Input(&content, &placeholder, &option);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  input->OnEvent(Event::Character('a'));
 | 
					  input->OnEvent(Event::Character('a'));
 | 
				
			||||||
@@ -172,6 +178,7 @@ TEST(InputTest, Delete) {
 | 
				
			|||||||
  std::string content;
 | 
					  std::string content;
 | 
				
			||||||
  std::string placeholder;
 | 
					  std::string placeholder;
 | 
				
			||||||
  auto option = InputOption();
 | 
					  auto option = InputOption();
 | 
				
			||||||
 | 
					  option.cursor_position = 0;
 | 
				
			||||||
  auto input = Input(&content, &placeholder, &option);
 | 
					  auto input = Input(&content, &placeholder, &option);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  input->OnEvent(Event::Character('a'));
 | 
					  input->OnEvent(Event::Character('a'));
 | 
				
			||||||
@@ -195,6 +202,7 @@ TEST(InputTest, Backspace) {
 | 
				
			|||||||
  std::string content;
 | 
					  std::string content;
 | 
				
			||||||
  std::string placeholder;
 | 
					  std::string placeholder;
 | 
				
			||||||
  auto option = InputOption();
 | 
					  auto option = InputOption();
 | 
				
			||||||
 | 
					  option.cursor_position = 0;
 | 
				
			||||||
  auto input = Input(&content, &placeholder, &option);
 | 
					  auto input = Input(&content, &placeholder, &option);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  input->OnEvent(Event::Character('a'));
 | 
					  input->OnEvent(Event::Character('a'));
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user