mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-11-04 13:38:14 +08:00 
			
		
		
		
	Remove input.hpp
This commit is contained in:
		
				
					committed by
					
						
						Arthur Sonzogni
					
				
			
			
				
	
			
			
			
						parent
						
							7ee6edfd1f
						
					
				
				
					commit
					26db8228f9
				
			@@ -16,7 +16,7 @@ class ButtonBase : public ComponentBase {
 | 
			
		||||
 public:
 | 
			
		||||
  ButtonBase(ConstStringRef label,
 | 
			
		||||
             std::function<void()> on_click,
 | 
			
		||||
             ConstRef<ButtonOption> option)
 | 
			
		||||
             Ref<ButtonOption> option)
 | 
			
		||||
      : label_(label), on_click_(on_click), option_(std::move(option)) {}
 | 
			
		||||
 | 
			
		||||
  ~ButtonBase() override = default;
 | 
			
		||||
@@ -55,7 +55,7 @@ class ButtonBase : public ComponentBase {
 | 
			
		||||
  ConstStringRef label_;
 | 
			
		||||
  std::function<void()> on_click_;
 | 
			
		||||
  Box box_;
 | 
			
		||||
  ConstRef<ButtonOption> option_;
 | 
			
		||||
  Ref<ButtonOption> option_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
}  // namespace
 | 
			
		||||
@@ -84,7 +84,7 @@ class ButtonBase : public ComponentBase {
 | 
			
		||||
/// ```
 | 
			
		||||
Component Button(ConstStringRef label,
 | 
			
		||||
                 std::function<void()> on_click,
 | 
			
		||||
                 ConstRef<ButtonOption> option) {
 | 
			
		||||
                 Ref<ButtonOption> option) {
 | 
			
		||||
  return Make<ButtonBase>(label, std::move(on_click), std::move(option));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -25,7 +25,7 @@ class CheckboxBase : public ComponentBase {
 | 
			
		||||
 public:
 | 
			
		||||
  CheckboxBase(ConstStringRef label,
 | 
			
		||||
               bool* state,
 | 
			
		||||
               ConstRef<CheckboxOption> option)
 | 
			
		||||
               Ref<CheckboxOption> option)
 | 
			
		||||
      : label_(label), state_(state), option_(std::move(option)) {
 | 
			
		||||
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
 | 
			
		||||
    // Microsoft terminal do not use fonts able to render properly the default
 | 
			
		||||
@@ -84,7 +84,7 @@ class CheckboxBase : public ComponentBase {
 | 
			
		||||
  ConstStringRef label_;
 | 
			
		||||
  bool* const state_;
 | 
			
		||||
  Box box_;
 | 
			
		||||
  ConstRef<CheckboxOption> option_;
 | 
			
		||||
  Ref<CheckboxOption> option_;
 | 
			
		||||
};
 | 
			
		||||
}  // namespace
 | 
			
		||||
 | 
			
		||||
@@ -111,7 +111,7 @@ class CheckboxBase : public ComponentBase {
 | 
			
		||||
/// ```
 | 
			
		||||
Component Checkbox(ConstStringRef label,
 | 
			
		||||
                   bool* checked,
 | 
			
		||||
                   ConstRef<CheckboxOption> option) {
 | 
			
		||||
                   Ref<CheckboxOption> option) {
 | 
			
		||||
  return Make<CheckboxBase>(label, checked, std::move(option));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -3,13 +3,166 @@
 | 
			
		||||
#include <string>     // for wstring, allocator, basic_string
 | 
			
		||||
 | 
			
		||||
#include "ftxui/component/captured_mouse.hpp"  // for CapturedMouse
 | 
			
		||||
#include "ftxui/component/component.hpp"       // for Component
 | 
			
		||||
#include "ftxui/component/component_base.hpp"  // for Component
 | 
			
		||||
#include "ftxui/component/event.hpp"  // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Custom, Event::Delete, Event::End, Event::Home, Event::Return
 | 
			
		||||
#include "ftxui/component/input.hpp"
 | 
			
		||||
#include "ftxui/component/mouse.hpp"  // for Mouse, Mouse::Left, Mouse::Pressed
 | 
			
		||||
#include "ftxui/component/screen_interactive.hpp"  // for Component
 | 
			
		||||
#include "ftxui/util/ref.hpp"                      // for Component
 | 
			
		||||
 | 
			
		||||
namespace ftxui {
 | 
			
		||||
 | 
			
		||||
/// @brief An input box. The user can type text into it.
 | 
			
		||||
/// @ingroup component.
 | 
			
		||||
class InputBase : public ComponentBase {
 | 
			
		||||
 public:
 | 
			
		||||
  InputBase(StringRef content,
 | 
			
		||||
            ConstStringRef placeholder,
 | 
			
		||||
            Ref<InputOption> option)
 | 
			
		||||
      : content_(content), placeholder_(placeholder), option_(option) {}
 | 
			
		||||
  ~InputBase() override = default;
 | 
			
		||||
 | 
			
		||||
  int& cursor_position() { return *(option_->cursor_position); }
 | 
			
		||||
 | 
			
		||||
  // Component implementation:
 | 
			
		||||
  Element Render() override {
 | 
			
		||||
    cursor_position() =
 | 
			
		||||
        std::max(0, std::min<int>(content_->size(), cursor_position()));
 | 
			
		||||
    auto main_decorator = flex | size(HEIGHT, EQUAL, 1);
 | 
			
		||||
    bool is_focused = Focused();
 | 
			
		||||
 | 
			
		||||
    // placeholder.
 | 
			
		||||
    if (content_->size() == 0) {
 | 
			
		||||
      if (is_focused)
 | 
			
		||||
        return text(*placeholder_) | focus | dim | inverted | main_decorator |
 | 
			
		||||
               reflect(input_box_);
 | 
			
		||||
      else
 | 
			
		||||
        return text(*placeholder_) | dim | main_decorator | reflect(input_box_);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Not focused.
 | 
			
		||||
    if (!is_focused)
 | 
			
		||||
      return text(*content_) | main_decorator | reflect(input_box_);
 | 
			
		||||
 | 
			
		||||
    std::wstring part_before_cursor = content_->substr(0, cursor_position());
 | 
			
		||||
    std::wstring part_at_cursor = cursor_position() < (int)content_->size()
 | 
			
		||||
                                      ? content_->substr(cursor_position(), 1)
 | 
			
		||||
                                      : L" ";
 | 
			
		||||
    std::wstring part_after_cursor =
 | 
			
		||||
        cursor_position() < (int)content_->size() - 1
 | 
			
		||||
            ? content_->substr(cursor_position() + 1)
 | 
			
		||||
            : L"";
 | 
			
		||||
    auto focused = is_focused ? focus : select;
 | 
			
		||||
 | 
			
		||||
    // clang-format off
 | 
			
		||||
  return
 | 
			
		||||
    hbox(
 | 
			
		||||
      text(part_before_cursor),
 | 
			
		||||
      text(part_at_cursor) | underlined | focused | reflect(cursor_box_),
 | 
			
		||||
      text(part_after_cursor)
 | 
			
		||||
    ) | flex | inverted | frame | main_decorator | reflect(input_box_);
 | 
			
		||||
    // clang-format on
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  bool OnEvent(Event event) override {
 | 
			
		||||
    cursor_position() =
 | 
			
		||||
        std::max(0, std::min<int>(content_->size(), cursor_position()));
 | 
			
		||||
 | 
			
		||||
    if (event.is_mouse())
 | 
			
		||||
      return OnMouseEvent(event);
 | 
			
		||||
 | 
			
		||||
    std::wstring c;
 | 
			
		||||
 | 
			
		||||
    // Backspace.
 | 
			
		||||
    if (event == Event::Backspace) {
 | 
			
		||||
      if (cursor_position() == 0)
 | 
			
		||||
        return false;
 | 
			
		||||
      content_->erase(cursor_position() - 1, 1);
 | 
			
		||||
      cursor_position()--;
 | 
			
		||||
      option_->on_change();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Delete
 | 
			
		||||
    if (event == Event::Delete) {
 | 
			
		||||
      if (cursor_position() == int(content_->size()))
 | 
			
		||||
        return false;
 | 
			
		||||
      content_->erase(cursor_position(), 1);
 | 
			
		||||
      option_->on_change();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Enter.
 | 
			
		||||
    if (event == Event::Return) {
 | 
			
		||||
      option_->on_enter();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (event == Event::Custom) {
 | 
			
		||||
      return false;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (event == Event::ArrowLeft && cursor_position() > 0) {
 | 
			
		||||
      cursor_position()--;
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (event == Event::ArrowRight &&
 | 
			
		||||
        cursor_position() < (int)content_->size()) {
 | 
			
		||||
      cursor_position()++;
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (event == Event::Home) {
 | 
			
		||||
      cursor_position() = 0;
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    if (event == Event::End) {
 | 
			
		||||
      cursor_position() = (int)content_->size();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // Content
 | 
			
		||||
    if (event.is_character()) {
 | 
			
		||||
      content_->insert(cursor_position(), 1, event.character());
 | 
			
		||||
      cursor_position()++;
 | 
			
		||||
      option_->on_change();
 | 
			
		||||
      return true;
 | 
			
		||||
    }
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
 private:
 | 
			
		||||
  bool OnMouseEvent(Event event) {
 | 
			
		||||
    if (!CaptureMouse(event))
 | 
			
		||||
      return false;
 | 
			
		||||
    if (!input_box_.Contain(event.mouse().x, event.mouse().y))
 | 
			
		||||
      return false;
 | 
			
		||||
 | 
			
		||||
    TakeFocus();
 | 
			
		||||
 | 
			
		||||
    if (event.mouse().button == Mouse::Left &&
 | 
			
		||||
        event.mouse().motion == Mouse::Pressed) {
 | 
			
		||||
      int new_cursor_position =
 | 
			
		||||
          cursor_position() + event.mouse().x - cursor_box_.x_min;
 | 
			
		||||
      new_cursor_position =
 | 
			
		||||
          std::max(0, std::min<int>(content_->size(), new_cursor_position));
 | 
			
		||||
      if (cursor_position() != new_cursor_position) {
 | 
			
		||||
        cursor_position() = new_cursor_position;
 | 
			
		||||
        option_->on_change();
 | 
			
		||||
      }
 | 
			
		||||
    }
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
  StringRef content_;
 | 
			
		||||
  ConstStringRef placeholder_;
 | 
			
		||||
 | 
			
		||||
  Box input_box_;
 | 
			
		||||
  Box cursor_box_;
 | 
			
		||||
  Ref<InputOption> option_;
 | 
			
		||||
};
 | 
			
		||||
 | 
			
		||||
/// @brief An input box for editing text.
 | 
			
		||||
/// @param content The editable content.
 | 
			
		||||
/// @param placeholder The text displayed when content is still empty.
 | 
			
		||||
@@ -33,149 +186,10 @@ namespace ftxui {
 | 
			
		||||
/// ```
 | 
			
		||||
Component Input(StringRef content,
 | 
			
		||||
                ConstStringRef placeholder,
 | 
			
		||||
                ConstRef<InputOption> option) {
 | 
			
		||||
                Ref<InputOption> option) {
 | 
			
		||||
  return Make<InputBase>(content, placeholder, std::move(option));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// static
 | 
			
		||||
InputBase* InputBase::From(Component component) {
 | 
			
		||||
  return static_cast<InputBase*>(component.get());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
InputBase::InputBase(StringRef content,
 | 
			
		||||
                     ConstStringRef placeholder,
 | 
			
		||||
                     ConstRef<InputOption> option)
 | 
			
		||||
    : content_(content), placeholder_(placeholder), option_(option) {}
 | 
			
		||||
 | 
			
		||||
// Component implementation.
 | 
			
		||||
Element InputBase::Render() {
 | 
			
		||||
  cursor_position =
 | 
			
		||||
      std::max(0, std::min<int>(content_->size(), cursor_position));
 | 
			
		||||
  auto main_decorator = flex | size(HEIGHT, EQUAL, 1);
 | 
			
		||||
  bool is_focused = Focused();
 | 
			
		||||
 | 
			
		||||
  // placeholder.
 | 
			
		||||
  if (content_->size() == 0) {
 | 
			
		||||
    if (is_focused)
 | 
			
		||||
      return text(*placeholder_) | focus | dim | inverted | main_decorator |
 | 
			
		||||
             reflect(input_box_);
 | 
			
		||||
    else
 | 
			
		||||
      return text(*placeholder_) | dim | main_decorator | reflect(input_box_);
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Not focused.
 | 
			
		||||
  if (!is_focused)
 | 
			
		||||
    return text(*content_) | main_decorator | reflect(input_box_);
 | 
			
		||||
 | 
			
		||||
  std::wstring part_before_cursor = content_->substr(0, cursor_position);
 | 
			
		||||
  std::wstring part_at_cursor = cursor_position < (int)content_->size()
 | 
			
		||||
                                    ? content_->substr(cursor_position, 1)
 | 
			
		||||
                                    : L" ";
 | 
			
		||||
  std::wstring part_after_cursor = cursor_position < (int)content_->size() - 1
 | 
			
		||||
                                       ? content_->substr(cursor_position + 1)
 | 
			
		||||
                                       : L"";
 | 
			
		||||
  auto focused = is_focused ? focus : select;
 | 
			
		||||
 | 
			
		||||
  // clang-format off
 | 
			
		||||
  return
 | 
			
		||||
    hbox(
 | 
			
		||||
      text(part_before_cursor),
 | 
			
		||||
      text(part_at_cursor) | underlined | focused | reflect(cursor_box_),
 | 
			
		||||
      text(part_after_cursor)
 | 
			
		||||
    ) | flex | inverted | frame | main_decorator | reflect(input_box_);
 | 
			
		||||
  // clang-format on
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool InputBase::OnEvent(Event event) {
 | 
			
		||||
  cursor_position =
 | 
			
		||||
      std::max(0, std::min<int>(content_->size(), cursor_position));
 | 
			
		||||
 | 
			
		||||
  if (event.is_mouse())
 | 
			
		||||
    return OnMouseEvent(event);
 | 
			
		||||
 | 
			
		||||
  std::wstring c;
 | 
			
		||||
 | 
			
		||||
  // Backspace.
 | 
			
		||||
  if (event == Event::Backspace) {
 | 
			
		||||
    if (cursor_position == 0)
 | 
			
		||||
      return false;
 | 
			
		||||
    content_->erase(cursor_position - 1, 1);
 | 
			
		||||
    cursor_position--;
 | 
			
		||||
    option_->on_change();
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Delete
 | 
			
		||||
  if (event == Event::Delete) {
 | 
			
		||||
    if (cursor_position == int(content_->size()))
 | 
			
		||||
      return false;
 | 
			
		||||
    content_->erase(cursor_position, 1);
 | 
			
		||||
    option_->on_change();
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Enter.
 | 
			
		||||
  if (event == Event::Return) {
 | 
			
		||||
    option_->on_enter();
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (event == Event::Custom) {
 | 
			
		||||
    return false;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (event == Event::ArrowLeft && cursor_position > 0) {
 | 
			
		||||
    cursor_position--;
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (event == Event::ArrowRight && cursor_position < (int)content_->size()) {
 | 
			
		||||
    cursor_position++;
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (event == Event::Home) {
 | 
			
		||||
    cursor_position = 0;
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  if (event == Event::End) {
 | 
			
		||||
    cursor_position = (int)content_->size();
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  // Content
 | 
			
		||||
  if (event.is_character()) {
 | 
			
		||||
    content_->insert(cursor_position, 1, event.character());
 | 
			
		||||
    cursor_position++;
 | 
			
		||||
    option_->on_change();
 | 
			
		||||
    return true;
 | 
			
		||||
  }
 | 
			
		||||
  return false;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
bool InputBase::OnMouseEvent(Event event) {
 | 
			
		||||
  if (!CaptureMouse(event))
 | 
			
		||||
    return false;
 | 
			
		||||
  if (!input_box_.Contain(event.mouse().x, event.mouse().y))
 | 
			
		||||
    return false;
 | 
			
		||||
 | 
			
		||||
  TakeFocus();
 | 
			
		||||
 | 
			
		||||
  if (event.mouse().button == Mouse::Left &&
 | 
			
		||||
      event.mouse().motion == Mouse::Pressed) {
 | 
			
		||||
    int new_cursor_position =
 | 
			
		||||
        cursor_position + event.mouse().x - cursor_box_.x_min;
 | 
			
		||||
    new_cursor_position =
 | 
			
		||||
        std::max(0, std::min<int>(content_->size(), new_cursor_position));
 | 
			
		||||
    if (cursor_position != new_cursor_position) {
 | 
			
		||||
      cursor_position = new_cursor_position;
 | 
			
		||||
      option_->on_change();
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
  return true;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
}  // namespace ftxui
 | 
			
		||||
 | 
			
		||||
// Copyright 2020 Arthur Sonzogni. All rights reserved.
 | 
			
		||||
 
 | 
			
		||||
@@ -4,68 +4,72 @@
 | 
			
		||||
#include <string>                   // for wstring, allocator
 | 
			
		||||
 | 
			
		||||
#include "ftxui/component/captured_mouse.hpp"  // for ftxui
 | 
			
		||||
#include "ftxui/component/component.hpp"
 | 
			
		||||
#include "ftxui/component/component_options.hpp"
 | 
			
		||||
#include "ftxui/component/event.hpp"  // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Delete, Event::End, Event::Home
 | 
			
		||||
#include "ftxui/component/input.hpp"
 | 
			
		||||
#include "gtest/gtest_pred_impl.h"  // for Test, EXPECT_EQ, TEST
 | 
			
		||||
#include "gtest/gtest_pred_impl.h"    // for Test, EXPECT_EQ, TEST
 | 
			
		||||
 | 
			
		||||
using namespace ftxui;
 | 
			
		||||
 | 
			
		||||
TEST(InputTest, Init) {
 | 
			
		||||
  std::wstring content;
 | 
			
		||||
  std::wstring placeholder;
 | 
			
		||||
  Component input = Input(&content, &placeholder);
 | 
			
		||||
  auto option = InputOption();
 | 
			
		||||
  Component input = Input(&content, &placeholder, &option);
 | 
			
		||||
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 0);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 0);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(InputTest, Type) {
 | 
			
		||||
  std::wstring content;
 | 
			
		||||
  std::wstring placeholder;
 | 
			
		||||
  Component input = Input(&content, &placeholder);
 | 
			
		||||
  auto option = InputOption();
 | 
			
		||||
  Component input = Input(&content, &placeholder, &option);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Character('a'));
 | 
			
		||||
  EXPECT_EQ(content, L"a");
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 1u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 1u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Character('b'));
 | 
			
		||||
  EXPECT_EQ(content, L"ab");
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 2u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 2u);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(InputTest, Arrow) {
 | 
			
		||||
  std::wstring content;
 | 
			
		||||
  std::wstring placeholder;
 | 
			
		||||
  Component input = Input(&content, &placeholder);
 | 
			
		||||
  auto option = InputOption();
 | 
			
		||||
  auto input = Input(&content, &placeholder, &option);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Character('a'));
 | 
			
		||||
  input->OnEvent(Event::Character('b'));
 | 
			
		||||
  input->OnEvent(Event::Character('c'));
 | 
			
		||||
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 3u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 3u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::ArrowLeft);
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 2u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 2u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::ArrowLeft);
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 1u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 1u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::ArrowLeft);
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 0u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 0u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::ArrowLeft);
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 0u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 0u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::ArrowRight);
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 1u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 1u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::ArrowRight);
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 2u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 2u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::ArrowRight);
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 3u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 3u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::ArrowRight);
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 3u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 3u);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(InputTest, Insert) {
 | 
			
		||||
@@ -97,16 +101,17 @@ TEST(InputTest, Insert) {
 | 
			
		||||
TEST(InputTest, Home) {
 | 
			
		||||
  std::wstring content;
 | 
			
		||||
  std::wstring placeholder;
 | 
			
		||||
  Component input = Input(&content, &placeholder);
 | 
			
		||||
  auto option = InputOption();
 | 
			
		||||
  auto input = Input(&content, &placeholder, &option);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Character('a'));
 | 
			
		||||
  input->OnEvent(Event::Character('b'));
 | 
			
		||||
  input->OnEvent(Event::Character('c'));
 | 
			
		||||
  EXPECT_EQ(content, L"abc");
 | 
			
		||||
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 3u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 3u);
 | 
			
		||||
  input->OnEvent(Event::Home);
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 0u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 0u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Character('-'));
 | 
			
		||||
  EXPECT_EQ(content, L"-abc");
 | 
			
		||||
@@ -115,7 +120,8 @@ TEST(InputTest, Home) {
 | 
			
		||||
TEST(InputTest, End) {
 | 
			
		||||
  std::wstring content;
 | 
			
		||||
  std::wstring placeholder;
 | 
			
		||||
  Component input = Input(&content, &placeholder);
 | 
			
		||||
  auto option = InputOption();
 | 
			
		||||
  auto input = Input(&content, &placeholder, &option);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Character('a'));
 | 
			
		||||
  input->OnEvent(Event::Character('b'));
 | 
			
		||||
@@ -124,15 +130,16 @@ TEST(InputTest, End) {
 | 
			
		||||
  input->OnEvent(Event::ArrowLeft);
 | 
			
		||||
  input->OnEvent(Event::ArrowLeft);
 | 
			
		||||
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 1u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 1u);
 | 
			
		||||
  input->OnEvent(Event::End);
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 3u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 3u);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(InputTest, Delete) {
 | 
			
		||||
  std::wstring content;
 | 
			
		||||
  std::wstring placeholder;
 | 
			
		||||
  Component input = Input(&content, &placeholder);
 | 
			
		||||
  auto option = InputOption();
 | 
			
		||||
  auto input = Input(&content, &placeholder, &option);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Character('a'));
 | 
			
		||||
  input->OnEvent(Event::Character('b'));
 | 
			
		||||
@@ -140,21 +147,22 @@ TEST(InputTest, Delete) {
 | 
			
		||||
  input->OnEvent(Event::ArrowLeft);
 | 
			
		||||
 | 
			
		||||
  EXPECT_EQ(content, L"abc");
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 2u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 2u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Delete);
 | 
			
		||||
  EXPECT_EQ(content, L"ab");
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 2u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 2u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Delete);
 | 
			
		||||
  EXPECT_EQ(content, L"ab");
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 2u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 2u);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
TEST(InputTest, Backspace) {
 | 
			
		||||
  std::wstring content;
 | 
			
		||||
  std::wstring placeholder;
 | 
			
		||||
  Component input = Input(&content, &placeholder);
 | 
			
		||||
  auto option = InputOption();
 | 
			
		||||
  auto input = Input(&content, &placeholder, &option);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Character('a'));
 | 
			
		||||
  input->OnEvent(Event::Character('b'));
 | 
			
		||||
@@ -162,19 +170,19 @@ TEST(InputTest, Backspace) {
 | 
			
		||||
  input->OnEvent(Event::ArrowLeft);
 | 
			
		||||
 | 
			
		||||
  EXPECT_EQ(content, L"abc");
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 2u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 2u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Backspace);
 | 
			
		||||
  EXPECT_EQ(content, L"ac");
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 1u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 1u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Backspace);
 | 
			
		||||
  EXPECT_EQ(content, L"c");
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 0u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 0u);
 | 
			
		||||
 | 
			
		||||
  input->OnEvent(Event::Backspace);
 | 
			
		||||
  EXPECT_EQ(content, L"c");
 | 
			
		||||
  EXPECT_EQ(InputBase::From(input)->cursor_position, 0u);
 | 
			
		||||
  EXPECT_EQ(option.cursor_position(), 0u);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Copyright 2021 Arthur Sonzogni. All rights reserved.
 | 
			
		||||
 
 | 
			
		||||
@@ -40,7 +40,7 @@ namespace ftxui {
 | 
			
		||||
/// ```
 | 
			
		||||
Component Menu(const std::vector<std::wstring>* entries,
 | 
			
		||||
               int* selected,
 | 
			
		||||
               ConstRef<MenuOption> option) {
 | 
			
		||||
               Ref<MenuOption> option) {
 | 
			
		||||
  return Make<MenuBase>(entries, selected, std::move(option));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -51,7 +51,7 @@ MenuBase* MenuBase::From(Component component) {
 | 
			
		||||
 | 
			
		||||
MenuBase::MenuBase(const std::vector<std::wstring>* entries,
 | 
			
		||||
                   int* selected,
 | 
			
		||||
                   ConstRef<MenuOption> option)
 | 
			
		||||
                   Ref<MenuOption> option)
 | 
			
		||||
    : entries_(entries), selected_(selected), option_(option) {}
 | 
			
		||||
 | 
			
		||||
Element MenuBase::Render() {
 | 
			
		||||
 
 | 
			
		||||
@@ -41,7 +41,7 @@ namespace ftxui {
 | 
			
		||||
/// ```
 | 
			
		||||
Component Radiobox(const std::vector<std::wstring>* entries,
 | 
			
		||||
                   int* selected,
 | 
			
		||||
                   ConstRef<RadioboxOption> option) {
 | 
			
		||||
                   Ref<RadioboxOption> option) {
 | 
			
		||||
  return Make<RadioboxBase>(entries, selected, std::move(option));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -52,7 +52,7 @@ RadioboxBase* RadioboxBase::From(Component component) {
 | 
			
		||||
 | 
			
		||||
RadioboxBase::RadioboxBase(const std::vector<std::wstring>* entries,
 | 
			
		||||
                           int* selected,
 | 
			
		||||
                           ConstRef<RadioboxOption> option)
 | 
			
		||||
                           Ref<RadioboxOption> option)
 | 
			
		||||
    : entries_(entries), selected_(selected), option_(std::move(option)) {
 | 
			
		||||
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
 | 
			
		||||
  // Microsoft terminal do not use fonts able to render properly the default
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@ namespace ftxui {
 | 
			
		||||
 | 
			
		||||
Component Toggle(const std::vector<std::wstring>* entries,
 | 
			
		||||
                 int* selected,
 | 
			
		||||
                 ConstRef<ToggleOption> option) {
 | 
			
		||||
                 Ref<ToggleOption> option) {
 | 
			
		||||
  return Make<ToggleBase>(entries, selected, std::move(option));
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@@ -23,7 +23,7 @@ ToggleBase* ToggleBase::From(Component component) {
 | 
			
		||||
 | 
			
		||||
ToggleBase::ToggleBase(const std::vector<std::wstring>* entries,
 | 
			
		||||
                       int* selected,
 | 
			
		||||
                       ConstRef<ToggleOption> option)
 | 
			
		||||
                       Ref<ToggleOption> option)
 | 
			
		||||
    : entries_(entries), selected_(selected), option_(std::move(option)) {}
 | 
			
		||||
 | 
			
		||||
Element ToggleBase::Render() {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user