Files
FTXUI/src/ftxui/component/input.cpp

113 lines
2.7 KiB
C++
Raw Normal View History

2018-10-18 22:58:38 +02:00
#include "ftxui/component/input.hpp"
2020-03-22 22:32:44 +01:00
#include <algorithm>
2018-10-18 22:58:38 +02:00
#include "ftxui/screen/string.hpp"
namespace ftxui {
2018-10-18 22:58:38 +02:00
// Component implementation.
Element Input::Render() {
2019-01-27 02:33:06 +01:00
cursor_position = std::max(0, std::min<int>(content.size(), cursor_position));
auto main_decorator = flex | size(HEIGHT, EQUAL, 1);
2018-10-21 14:18:11 +02:00
bool is_focused = Focused();
// Placeholder.
if (content.size() == 0) {
if (is_focused)
return text(placeholder) | focus | dim | inverted | main_decorator;
2018-10-21 14:18:11 +02:00
else
2019-01-27 02:33:06 +01:00
return text(placeholder) | dim | main_decorator;
2018-10-21 14:18:11 +02:00
}
// Not focused.
if (!is_focused)
2019-01-27 02:33:06 +01:00
return text(content) | main_decorator;
2018-10-21 14:18:11 +02:00
std::wstring part_before_cursor = content.substr(0, cursor_position);
2018-10-21 14:18:11 +02: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"";
auto focused = is_focused ? focus : select;
2019-01-19 22:06:05 +01:00
2020-03-22 22:32:44 +01:00
// clang-format off
return
hbox(
text(part_before_cursor),
2019-01-19 22:06:05 +01:00
text(part_at_cursor) | underlined | focused,
text(part_after_cursor)
2019-01-27 02:33:06 +01:00
) | flex | inverted | frame | main_decorator;
2020-03-22 22:32:44 +01:00
// clang-format off
2018-10-18 22:58:38 +02:00
}
bool Input::OnEvent(Event event) {
2019-01-27 02:33:06 +01:00
cursor_position = std::max(0, std::min<int>(content.size(), cursor_position));
2018-10-18 22:58:38 +02:00
std::wstring c;
2018-10-21 14:18:11 +02:00
// Backspace.
2018-10-18 22:58:38 +02:00
if (event == Event::Backspace) {
2018-10-21 14:18:11 +02:00
if (cursor_position == 0)
return false;
content.erase(cursor_position - 1, 1);
cursor_position--;
2020-09-20 11:47:06 +02:00
on_change();
2018-10-18 22:58:38 +02:00
return true;
}
// Delete
if (event == Event::Delete) {
if (cursor_position == int(content.size()))
return false;
content.erase(cursor_position, 1);
2020-09-20 11:47:06 +02:00
on_change();
return true;
}
2018-10-21 14:18:11 +02:00
// Enter.
2018-10-18 22:58:38 +02:00
if (event == Event::Return) {
2019-01-27 02:33:06 +01:00
on_enter();
2018-10-18 22:58:38 +02:00
return true;
}
2019-01-27 02:33:06 +01:00
if (event == Event::Custom) {
return false;
}
2018-10-21 14:18:11 +02: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;
}
if (event == Event::Home) {
cursor_position = 0;
return true;
}
if (event == Event::End) {
cursor_position = (int)content.size();
return true;
}
2018-10-21 14:18:11 +02:00
// Content
if (event.is_character()) {
content.insert(cursor_position, 1, event.character());
2018-10-21 14:18:11 +02:00
cursor_position++;
2020-09-20 11:47:06 +02:00
on_change();
2018-10-18 22:58:38 +02:00
return true;
}
return false;
}
} // 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.