FTXUI/src/ftxui/component/input.cpp

184 lines
5.1 KiB
C++
Raw Normal View History

2021-05-10 02:32:27 +08:00
#include <algorithm> // for max, min
#include <memory> // for shared_ptr
#include <string> // for wstring, allocator, basic_string
2021-05-10 02:32:27 +08:00
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Backspace, Event::Custom, Event::Delete, Event::End, Event::Home, Event::Return
2018-10-19 04:58:38 +08:00
#include "ftxui/component/input.hpp"
2021-05-10 02:32:27 +08:00
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
#include "ftxui/component/screen_interactive.hpp" // for Component
namespace ftxui {
2021-05-10 02:32:27 +08:00
/// @brief An input box for editing text.
/// @param content The editable content.
/// @param placeholder The text displayed when content is still empty.
/// @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
/// ```
2021-07-08 06:01:42 +08:00
Component Input(StringRef content,
ConstStringRef placeholder,
ConstRef<InputOption> option) {
return Make<InputBase>(content, placeholder, std::move(option));
2021-05-10 02:32:27 +08:00
}
2018-10-19 04:58:38 +08:00
2021-05-10 02:32:27 +08:00
// static
InputBase* InputBase::From(Component component) {
return static_cast<InputBase*>(component.get());
}
2021-07-08 06:01:42 +08:00
InputBase::InputBase(StringRef content,
ConstStringRef placeholder,
ConstRef<InputOption> option)
: content_(content), placeholder_(placeholder), option_(option) {}
2018-10-19 04:58:38 +08:00
// Component implementation.
2021-05-10 02:32:27 +08:00
Element InputBase::Render() {
cursor_position =
std::max(0, std::min<int>(content_->size(), cursor_position));
2019-01-27 09:33:06 +08:00
auto main_decorator = flex | size(HEIGHT, EQUAL, 1);
2018-10-21 20:18:11 +08:00
bool is_focused = Focused();
2021-05-10 02:32:27 +08:00
// placeholder.
if (content_->size() == 0) {
2018-10-21 20:18:11 +08:00
if (is_focused)
2021-05-10 02:32:27 +08:00
return text(*placeholder_) | focus | dim | inverted | main_decorator |
reflect(input_box_);
2018-10-21 20:18:11 +08:00
else
2021-05-10 02:32:27 +08:00
return text(*placeholder_) | dim | main_decorator | reflect(input_box_);
2018-10-21 20:18:11 +08:00
}
// Not focused.
if (!is_focused)
2021-05-10 02:32:27 +08:00
return text(*content_) | main_decorator | reflect(input_box_);
2018-10-21 20:18:11 +08:00
2021-05-10 02:32:27 +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)
2018-10-21 20:18:11 +08:00
: L" ";
2021-05-10 02:32:27 +08:00
std::wstring part_after_cursor = cursor_position < (int)content_->size() - 1
? content_->substr(cursor_position + 1)
2018-10-21 20:18:11 +08:00
: L"";
auto focused = is_focused ? focus : select;
2019-01-20 05:06:05 +08:00
2020-03-23 05:32:44 +08:00
// 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
2018-10-19 04:58:38 +08:00
}
2021-05-10 02:32:27 +08:00
bool InputBase::OnEvent(Event event) {
cursor_position =
std::max(0, std::min<int>(content_->size(), cursor_position));
if (event.is_mouse())
return OnMouseEvent(event);
2018-10-19 04:58:38 +08:00
std::wstring c;
2018-10-21 20:18:11 +08:00
// Backspace.
2018-10-19 04:58:38 +08:00
if (event == Event::Backspace) {
2018-10-21 20:18:11 +08:00
if (cursor_position == 0)
return false;
2021-05-10 02:32:27 +08:00
content_->erase(cursor_position - 1, 1);
2018-10-21 20:18:11 +08:00
cursor_position--;
2021-07-08 06:01:42 +08:00
option_->on_change();
2018-10-19 04:58:38 +08:00
return true;
}
// Delete
if (event == Event::Delete) {
2021-05-10 02:32:27 +08:00
if (cursor_position == int(content_->size()))
return false;
2021-05-10 02:32:27 +08:00
content_->erase(cursor_position, 1);
2021-07-08 06:01:42 +08:00
option_->on_change();
return true;
}
2018-10-21 20:18:11 +08:00
// Enter.
2018-10-19 04:58:38 +08:00
if (event == Event::Return) {
2021-07-08 06:01:42 +08:00
option_->on_enter();
2018-10-19 04:58:38 +08:00
return true;
}
2019-01-27 09:33:06 +08:00
if (event == Event::Custom) {
return false;
}
2018-10-21 20:18:11 +08:00
if (event == Event::ArrowLeft && cursor_position > 0) {
cursor_position--;
return true;
}
2021-05-10 02:32:27 +08:00
if (event == Event::ArrowRight && cursor_position < (int)content_->size()) {
2018-10-21 20:18:11 +08:00
cursor_position++;
return true;
}
if (event == Event::Home) {
cursor_position = 0;
return true;
}
if (event == Event::End) {
2021-05-10 02:32:27 +08:00
cursor_position = (int)content_->size();
return true;
}
2018-10-21 20:18:11 +08:00
// Content
if (event.is_character()) {
2021-05-10 02:32:27 +08:00
content_->insert(cursor_position, 1, event.character());
2018-10-21 20:18:11 +08:00
cursor_position++;
2021-07-08 06:01:42 +08:00
option_->on_change();
2018-10-19 04:58:38 +08:00
return true;
}
return false;
}
2021-05-10 02:32:27 +08:00
bool InputBase::OnMouseEvent(Event event) {
2021-05-02 02:40:35 +08:00
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 =
2021-05-10 02:32:27 +08:00
std::max(0, std::min<int>(content_->size(), new_cursor_position));
if (cursor_position != new_cursor_position) {
cursor_position = new_cursor_position;
2021-07-08 06:01:42 +08:00
option_->on_change();
}
}
return true;
}
} // namespace ftxui
// 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.