2018-10-19 04:58:38 +08:00
|
|
|
#include "ftxui/component/input.hpp"
|
2021-05-02 00:13:56 +08:00
|
|
|
#include "ftxui/component/screen_interactive.hpp"
|
2020-03-24 04:26:00 +08:00
|
|
|
|
2020-03-23 05:32:44 +08:00
|
|
|
#include <algorithm>
|
2018-10-19 04:58:38 +08:00
|
|
|
|
2020-03-24 04:26:00 +08:00
|
|
|
#include "ftxui/screen/string.hpp"
|
|
|
|
|
2019-01-12 22:00:08 +08:00
|
|
|
namespace ftxui {
|
2018-10-19 04:58:38 +08:00
|
|
|
|
|
|
|
// Component implementation.
|
2019-01-12 22:00:08 +08:00
|
|
|
Element Input::Render() {
|
2019-01-27 09:33:06 +08:00
|
|
|
cursor_position = std::max(0, std::min<int>(content.size(), cursor_position));
|
|
|
|
auto main_decorator = flex | size(HEIGHT, EQUAL, 1);
|
2018-10-21 20:18:11 +08:00
|
|
|
bool is_focused = Focused();
|
|
|
|
|
|
|
|
// Placeholder.
|
|
|
|
if (content.size() == 0) {
|
|
|
|
if (is_focused)
|
2021-04-25 22:58:16 +08:00
|
|
|
return text(placeholder) | focus | dim | inverted | main_decorator |
|
|
|
|
reflect(input_box_);
|
2018-10-21 20:18:11 +08:00
|
|
|
else
|
2021-04-25 22:58:16 +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-04-25 22:58:16 +08:00
|
|
|
return text(content) | main_decorator | reflect(input_box_);
|
2018-10-21 20:18:11 +08:00
|
|
|
|
2020-03-24 04:26:00 +08:00
|
|
|
std::wstring part_before_cursor = content.substr(0, cursor_position);
|
2018-10-21 20:18:11 +08:00
|
|
|
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"";
|
2020-03-24 04:26:00 +08:00
|
|
|
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
|
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_);
|
|
|
|
// clang-format on
|
2018-10-19 04:58:38 +08:00
|
|
|
}
|
2021-04-25 22:58:16 +08:00
|
|
|
|
2018-10-19 04:58:38 +08:00
|
|
|
bool Input::OnEvent(Event event) {
|
2019-01-27 09:33:06 +08:00
|
|
|
cursor_position = std::max(0, std::min<int>(content.size(), cursor_position));
|
2021-04-25 22:58:16 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
content.erase(cursor_position - 1, 1);
|
|
|
|
cursor_position--;
|
2020-09-20 17:47:06 +08:00
|
|
|
on_change();
|
2018-10-19 04:58:38 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-07-01 06:40:55 +08:00
|
|
|
// Delete
|
|
|
|
if (event == Event::Delete) {
|
|
|
|
if (cursor_position == int(content.size()))
|
|
|
|
return false;
|
|
|
|
content.erase(cursor_position, 1);
|
2020-09-20 17:47:06 +08:00
|
|
|
on_change();
|
2019-07-01 06:40:55 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-10-21 20:18:11 +08:00
|
|
|
// Enter.
|
2018-10-19 04:58:38 +08:00
|
|
|
if (event == Event::Return) {
|
2019-01-27 09:33:06 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event == Event::ArrowRight && cursor_position < (int)content.size()) {
|
|
|
|
cursor_position++;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Content
|
2019-06-23 23:47:33 +08:00
|
|
|
if (event.is_character()) {
|
|
|
|
content.insert(cursor_position, 1, event.character());
|
2018-10-21 20:18:11 +08:00
|
|
|
cursor_position++;
|
2020-09-20 17:47:06 +08:00
|
|
|
on_change();
|
2018-10-19 04:58:38 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-25 22:58:16 +08:00
|
|
|
bool Input::OnMouseEvent(Event event) {
|
2021-05-02 00:13:56 +08:00
|
|
|
if (!event.screen()->CaptureMouse())
|
|
|
|
return false;
|
2021-04-25 22:58:16 +08:00
|
|
|
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;
|
|
|
|
on_change();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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.
|