2021-07-10 19:20:43 +08:00
|
|
|
#include <algorithm> // for max, min
|
|
|
|
#include <functional> // for function
|
|
|
|
#include <memory> // for shared_ptr, allocator
|
|
|
|
#include <string> // for wstring, basic_string
|
|
|
|
#include <utility> // for move
|
2021-05-10 02:32:27 +08:00
|
|
|
|
2021-07-10 19:20:43 +08:00
|
|
|
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
|
|
|
|
#include "ftxui/component/component.hpp" // for Make, Input
|
|
|
|
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
|
|
|
#include "ftxui/component/component_options.hpp" // for InputOption
|
2021-05-10 02:32:27 +08:00
|
|
|
#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/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
|
|
|
|
#include "ftxui/component/screen_interactive.hpp" // for Component
|
2021-07-10 19:20:43 +08:00
|
|
|
#include "ftxui/dom/elements.hpp" // for operator|, Element, text, reflect, dim, flex, focus, inverted, hbox, size, frame, select, underlined, Decorator, EQUAL, HEIGHT
|
|
|
|
#include "ftxui/screen/box.hpp" // for Box
|
|
|
|
#include "ftxui/util/ref.hpp" // for StringRef, Ref, ConstStringRef
|
2021-05-10 02:32:27 +08:00
|
|
|
|
|
|
|
namespace ftxui {
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2021-07-10 20:23:46 +08:00
|
|
|
// An input box. The user can type text into it.
|
2021-07-10 18:29:39 +08:00
|
|
|
class InputBase : public ComponentBase {
|
|
|
|
public:
|
|
|
|
InputBase(StringRef content,
|
|
|
|
ConstStringRef placeholder,
|
|
|
|
Ref<InputOption> option)
|
|
|
|
: content_(content), placeholder_(placeholder), option_(option) {}
|
|
|
|
|
|
|
|
int& cursor_position() { return *(option_->cursor_position); }
|
|
|
|
|
|
|
|
// Component implementation:
|
|
|
|
Element Render() override {
|
2021-07-17 16:36:50 +08:00
|
|
|
std::wstring password_content;
|
|
|
|
if (option_->password)
|
|
|
|
password_content = std::wstring(content_->size(), U'•');
|
|
|
|
std::wstring& content = option_->password ? password_content : *content_;
|
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
cursor_position() =
|
2021-07-17 16:36:50 +08:00
|
|
|
std::max(0, std::min<int>(content.size(), cursor_position()));
|
2021-07-10 18:29:39 +08:00
|
|
|
auto main_decorator = flex | size(HEIGHT, EQUAL, 1);
|
|
|
|
bool is_focused = Focused();
|
|
|
|
|
|
|
|
// placeholder.
|
2021-07-17 16:36:50 +08:00
|
|
|
if (content.size() == 0) {
|
2021-07-10 18:29:39 +08:00
|
|
|
if (is_focused)
|
|
|
|
return text(*placeholder_) | focus | dim | inverted | main_decorator |
|
|
|
|
reflect(input_box_);
|
|
|
|
else
|
|
|
|
return text(*placeholder_) | dim | main_decorator | reflect(input_box_);
|
|
|
|
}
|
2019-01-20 05:06:05 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
// Not focused.
|
|
|
|
if (!is_focused)
|
2021-07-17 16:36:50 +08:00
|
|
|
return text(content) | main_decorator | reflect(input_box_);
|
2021-07-10 18:29:39 +08:00
|
|
|
|
2021-07-17 16:36:50 +08:00
|
|
|
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)
|
2021-07-10 18:29:39 +08:00
|
|
|
: L" ";
|
|
|
|
std::wstring part_after_cursor =
|
2021-07-17 16:36:50 +08:00
|
|
|
cursor_position() < (int)content.size() - 1
|
|
|
|
? content.substr(cursor_position() + 1)
|
2021-07-10 18:29:39 +08:00
|
|
|
: L"";
|
|
|
|
auto focused = is_focused ? focus : select;
|
|
|
|
|
|
|
|
// clang-format off
|
2019-01-05 09:03:49 +08:00
|
|
|
return
|
|
|
|
hbox(
|
|
|
|
text(part_before_cursor),
|
2021-04-25 22:58:16 +08:00
|
|
|
text(part_at_cursor) | underlined | focused | reflect(cursor_box_),
|
2019-01-05 09:03:49 +08:00
|
|
|
text(part_after_cursor)
|
2021-04-25 22:58:16 +08:00
|
|
|
) | flex | inverted | frame | main_decorator | reflect(input_box_);
|
2021-07-10 18:29:39 +08:00
|
|
|
// clang-format on
|
|
|
|
}
|
2021-04-25 22:58:16 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
bool OnEvent(Event event) override {
|
|
|
|
cursor_position() =
|
|
|
|
std::max(0, std::min<int>(content_->size(), cursor_position()));
|
2021-04-25 22:58:16 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
if (event.is_mouse())
|
|
|
|
return OnMouseEvent(event);
|
2021-04-25 22:58:16 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
std::wstring c;
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
// 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;
|
|
|
|
}
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
if (event == Event::Custom) {
|
2019-07-01 06:40:55 +08:00
|
|
|
return false;
|
2021-07-10 18:29:39 +08:00
|
|
|
}
|
2019-07-01 06:40:55 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
if (event == Event::ArrowLeft && cursor_position() > 0) {
|
|
|
|
cursor_position()--;
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
if (event == Event::ArrowRight &&
|
|
|
|
cursor_position() < (int)content_->size()) {
|
|
|
|
cursor_position()++;
|
|
|
|
return true;
|
|
|
|
}
|
2019-01-27 09:33:06 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
if (event == Event::Home) {
|
|
|
|
cursor_position() = 0;
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-21 20:18:11 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
if (event == Event::End) {
|
|
|
|
cursor_position() = (int)content_->size();
|
|
|
|
return true;
|
|
|
|
}
|
2018-10-21 20:18:11 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
// Content
|
|
|
|
if (event.is_character()) {
|
|
|
|
content_->insert(cursor_position(), 1, event.character());
|
|
|
|
cursor_position()++;
|
|
|
|
option_->on_change();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2021-03-28 08:01:04 +08:00
|
|
|
}
|
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
private:
|
|
|
|
bool OnMouseEvent(Event event) {
|
|
|
|
if (!CaptureMouse(event))
|
|
|
|
return false;
|
|
|
|
if (!input_box_.Contain(event.mouse().x, event.mouse().y))
|
|
|
|
return false;
|
2021-03-28 08:01:04 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 04:58:38 +08:00
|
|
|
return true;
|
|
|
|
}
|
2021-07-10 18:29:39 +08:00
|
|
|
StringRef content_;
|
|
|
|
ConstStringRef placeholder_;
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
Box input_box_;
|
|
|
|
Box cursor_box_;
|
|
|
|
Ref<InputOption> option_;
|
|
|
|
};
|
2021-04-25 22:58:16 +08:00
|
|
|
|
2021-07-10 18:29:39 +08:00
|
|
|
/// @brief An input box for editing text.
|
|
|
|
/// @param content The editable content.
|
|
|
|
/// @param placeholder The text displayed when content is still empty.
|
2021-07-10 20:23:46 +08:00
|
|
|
/// @param option Additional optional parameters.
|
2021-07-10 18:29:39 +08:00
|
|
|
/// @ingroup component
|
|
|
|
/// @see InputBase
|
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
///
|
|
|
|
/// ```cpp
|
|
|
|
/// auto screen = ScreenInteractive::FitComponent();
|
|
|
|
/// std::wstring content= L"";
|
|
|
|
/// std::wstring placeholder = L"placeholder";
|
|
|
|
/// Component input = Input(&content, &placeholder);
|
|
|
|
/// screen.Loop(input);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ### Output
|
|
|
|
///
|
|
|
|
/// ```bash
|
|
|
|
/// placeholder
|
|
|
|
/// ```
|
|
|
|
Component Input(StringRef content,
|
|
|
|
ConstStringRef placeholder,
|
|
|
|
Ref<InputOption> option) {
|
|
|
|
return Make<InputBase>(content, placeholder, std::move(option));
|
2021-04-25 22:58:16 +08:00
|
|
|
}
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
} // namespace ftxui
|
2020-08-16 06:24:18 +08:00
|
|
|
|
|
|
|
// 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.
|