diff --git a/CHANGELOG.md b/CHANGELOG.md index e088d08..7bcc527 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,9 @@ Changelog unreleased (development) ------------------------ +# Component: +- Bugfix: Input shouldn't take focus when hovered by the mouse. + 0.11.1 ------ diff --git a/examples/component/checkbox.cpp b/examples/component/checkbox.cpp index 13be0df..65e05f5 100644 --- a/examples/component/checkbox.cpp +++ b/examples/component/checkbox.cpp @@ -1,25 +1,21 @@ -#include "ftxui/component/captured_mouse.hpp" // for ftxui -#include "ftxui/component/component.hpp" // for Checkbox, Vertical -#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive - -using namespace ftxui; +#include "ftxui/component/screen_interactive.hpp" // for Component, ScreenInteractive +#include "ftxui/dom/elements.hpp" +#include "ftxui/component/component.hpp" int main(int argc, const char* argv[]) { - bool build_examples_state = false; - bool build_tests_state = false; - bool use_webassembly_state = true; + using namespace ftxui; - auto component = Container::Vertical({ - Checkbox("Build examples", &build_examples_state), - Checkbox("Build tests", &build_tests_state), - Checkbox("Use WebAssembly", &use_webassembly_state), + Component input_list = Container::Vertical({}); + std::vector items(100, ""); + for (int i = 0; i < items.size(); ++i) { + input_list->Add(Input(&(items[i]), "placeholder " + std::to_string(i))); + } + + auto renderer = Renderer(input_list, [&] { + return input_list->Render() | vscroll_indicator | frame | border | + size(HEIGHT, LESS_THAN, 10); }); auto screen = ScreenInteractive::TerminalOutput(); - screen.Loop(component); - return 0; + screen.Loop(renderer); } - -// 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. diff --git a/src/ftxui/component/input.cpp b/src/ftxui/component/input.cpp index c78bf60..846e383 100644 --- a/src/ftxui/component/input.cpp +++ b/src/ftxui/component/input.cpp @@ -50,16 +50,22 @@ class WideInputBase : public ComponentBase { // placeholder. if (content.size() == 0) { + bool hovered = hovered_; + Decorator decorator = dim | main_decorator; if (is_focused) - return text(*placeholder_) | focus | dim | inverted | main_decorator | - reflect(box_); - else - return text(*placeholder_) | dim | main_decorator | reflect(box_); + decorator = decorator | focus | bold; + if (hovered || is_focused) + decorator = decorator | inverted; + return text(*placeholder_) | decorator | reflect(box_); } // Not focused. - if (!is_focused) - return text(content) | main_decorator | reflect(box_); + if (!is_focused) { + if (hovered_) + return text(content) | main_decorator | inverted | reflect(box_); + else + return text(content) | main_decorator | reflect(box_); + } std::wstring part_before_cursor = content.substr(0, cursor_position()); std::wstring part_at_cursor = cursor_position() < (int)content.size() @@ -68,15 +74,14 @@ class WideInputBase : public ComponentBase { std::wstring part_after_cursor = cursor_position() < (int)content.size() - 1 ? content.substr(cursor_position() + 1) : L""; - auto focused = is_focused ? focus : select; - + auto focused = (is_focused || hovered_) ? 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(box_); + ) | flex | inverted | frame | bold |main_decorator | reflect(box_); // clang-format on } @@ -151,29 +156,31 @@ class WideInputBase : public ComponentBase { private: bool OnMouseEvent(Event event) { - if (!CaptureMouse(event)) + hovered_ = + box_.Contain(event.mouse().x, event.mouse().y) && CaptureMouse(event); + if (!hovered_) return false; - if (!box_.Contain(event.mouse().x, event.mouse().y)) + + if (event.mouse().button != Mouse::Left || + event.mouse().motion != Mouse::Pressed) { 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(content_->size(), new_cursor_position)); - if (cursor_position() != new_cursor_position) { - cursor_position() = new_cursor_position; - option_->on_change(); - } + int new_cursor_position = + cursor_position() + event.mouse().x - cursor_box_.x_min; + new_cursor_position = + std::max(0, std::min(content_->size(), new_cursor_position)); + if (cursor_position() != new_cursor_position) { + cursor_position() = new_cursor_position; + option_->on_change(); } return true; } bool Focusable() const final { return true; } + bool hovered_ = false; WideStringRef content_; ConstStringRef placeholder_; diff --git a/src/ftxui/dom/blink.cpp b/src/ftxui/dom/blink.cpp index d426f6e..97d5f7b 100644 --- a/src/ftxui/dom/blink.cpp +++ b/src/ftxui/dom/blink.cpp @@ -23,6 +23,7 @@ class Blink : public NodeDecorator { } }; + /// @brief The text drawn alternates in between visible and hidden. /// @ingroup dom Element blink(Element child) {