FTXUI/src/ftxui/component/checkbox.cpp

102 lines
2.9 KiB
C++
Raw Normal View History

2021-05-02 02:40:35 +08:00
#include <functional> // for function
#include <memory> // for shared_ptr
2020-03-23 05:32:44 +08:00
2021-05-02 02:40:35 +08:00
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
#include "ftxui/component/checkbox.hpp"
#include "ftxui/component/event.hpp" // for Event, Event::Return
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
#include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive
2019-01-13 05:25:49 +08:00
namespace ftxui {
2021-05-10 02:32:27 +08:00
/// @brief Draw checkable element.
/// @param label The label of the checkbox.
/// @param checked Whether the checkbox is checked or not.
/// @ingroup component
/// @see CheckboxBase
///
/// ### Example
///
/// ```cpp
/// auto screen = ScreenInteractive::FitComponent();
/// std::wstring label = L"Make a sandwidth";
/// bool checked = false;
/// Component checkbox = Checkbox(&label, &checked);
/// screen.Loop(checkbox)
/// ```
///
/// ### Output
///
/// ```bash
/// ☐ Make a sandwitch
/// ```
2021-07-08 04:37:50 +08:00
Component Checkbox(ConstStringRef label,
bool* checked,
ConstRef<CheckboxOption> option) {
return Make<CheckboxBase>(label, checked, std::move(option));
2021-05-10 02:32:27 +08:00
}
// static
2021-05-15 04:00:49 +08:00
CheckboxBase* CheckboxBase::From(Component component) {
2021-05-10 02:32:27 +08:00
return static_cast<CheckboxBase*>(component.get());
}
2021-07-08 04:37:50 +08:00
CheckboxBase::CheckboxBase(ConstStringRef label,
bool* state,
ConstRef<CheckboxOption> option)
: label_(label), state_(state), option_(std::move(option)) {
Implement Fallback for microsoft's terminals. (#138) I finally got access to a computer using the Microsoft's Windows OS. That's the opportunity to find and mitigate all the problems encountered. This patch: 1. Introduce an option and a C++ definition to enable fallback for Microsoft's terminal emulators. This allows me to see/test the Microsoft output from Linux. This also allows Windows users to remove the fallback and target non Microsoft terminals on Windows if needed. 2. Microsoft's terminal suffer from a race condition bug when reporting the cursor position: https://github.com/microsoft/terminal/pull/7583. The mitigation is not to ask for the cursor position in fullscreen mode where it isn't really needed and request it less often. This fixes: https://github.com/ArthurSonzogni/FTXUI/issues/136 3. Microsoft's terminal do not handle properly hidding the cursor. Instead the character under the cursor is hidden, which is a big problem. As a result, we don't enable setting the cursor to the best position for [input method editors](https://en.wikipedia.org/wiki/Input_method), It will be displayed at the bottom right corner. See: - https://github.com/microsoft/terminal/issues/1203 - https://github.com/microsoft/terminal/issues/3093 4. Microsoft's terminals do not provide a way to query if they support colors. As a fallback, assume true colors is supported. See issue: - https://github.com/microsoft/terminal/issues/1040 This mitigates: - https://github.com/ArthurSonzogni/FTXUI/issues/135 5. The "cmd" on Windows do not properly report its dimension. Powershell works correctly. As a fallback, use a 80x80 size instead of 0x0. 6. There are several dom elements and component displayed incorrectly, because the font used is missing several unicode glyph. Use alternatives or less detailled one as a fallback.
2021-07-04 23:38:31 +08:00
#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK)
// Microsoft terminal do not use fonts able to render properly the default
// radiobox glyph.
2021-07-08 04:37:50 +08:00
if (option->checked == L"")
option->checked = L"[X]";
if (option->unchecked == L"")
option->unchecked = L"[ ]";
Implement Fallback for microsoft's terminals. (#138) I finally got access to a computer using the Microsoft's Windows OS. That's the opportunity to find and mitigate all the problems encountered. This patch: 1. Introduce an option and a C++ definition to enable fallback for Microsoft's terminal emulators. This allows me to see/test the Microsoft output from Linux. This also allows Windows users to remove the fallback and target non Microsoft terminals on Windows if needed. 2. Microsoft's terminal suffer from a race condition bug when reporting the cursor position: https://github.com/microsoft/terminal/pull/7583. The mitigation is not to ask for the cursor position in fullscreen mode where it isn't really needed and request it less often. This fixes: https://github.com/ArthurSonzogni/FTXUI/issues/136 3. Microsoft's terminal do not handle properly hidding the cursor. Instead the character under the cursor is hidden, which is a big problem. As a result, we don't enable setting the cursor to the best position for [input method editors](https://en.wikipedia.org/wiki/Input_method), It will be displayed at the bottom right corner. See: - https://github.com/microsoft/terminal/issues/1203 - https://github.com/microsoft/terminal/issues/3093 4. Microsoft's terminals do not provide a way to query if they support colors. As a fallback, assume true colors is supported. See issue: - https://github.com/microsoft/terminal/issues/1040 This mitigates: - https://github.com/ArthurSonzogni/FTXUI/issues/135 5. The "cmd" on Windows do not properly report its dimension. Powershell works correctly. As a fallback, use a 80x80 size instead of 0x0. 6. There are several dom elements and component displayed incorrectly, because the font used is missing several unicode glyph. Use alternatives or less detailled one as a fallback.
2021-07-04 23:38:31 +08:00
#endif
}
2021-05-10 02:32:27 +08:00
Element CheckboxBase::Render() {
2019-01-20 05:06:05 +08:00
bool is_focused = Focused();
2021-07-10 17:50:17 +08:00
auto style = is_focused ? option_->style_focused : option_->style_unfocused;
2021-05-10 02:32:27 +08:00
auto focus_management = is_focused ? focus : *state_ ? select : nothing;
2021-07-10 17:50:17 +08:00
return hbox(text(*state_ ? option_->style_checked : option_->style_unchecked),
2021-05-10 02:32:27 +08:00
text(*label_) | style | focus_management) |
reflect(box_);
2019-01-13 05:25:49 +08:00
}
2021-05-10 02:32:27 +08:00
bool CheckboxBase::OnEvent(Event event) {
if (event.is_mouse())
return OnMouseEvent(event);
2019-01-13 05:25:49 +08:00
if (event == Event::Character(' ') || event == Event::Return) {
2021-05-10 02:32:27 +08:00
*state_ = !*state_;
2021-07-08 04:37:50 +08:00
option_->on_change();
2019-01-13 05:25:49 +08:00
return true;
}
2020-03-23 05:32:44 +08:00
return false;
2019-01-13 05:25:49 +08:00
}
2021-05-10 02:32:27 +08:00
bool CheckboxBase::OnMouseEvent(Event event) {
if (!CaptureMouse(event))
return false;
2021-04-25 21:22:38 +08:00
if (!box_.Contain(event.mouse().x, event.mouse().y))
return false;
2021-04-25 21:22:38 +08:00
TakeFocus();
2021-04-25 21:22:38 +08:00
if (event.mouse().button == Mouse::Left &&
event.mouse().motion == Mouse::Pressed) {
2021-05-10 02:32:27 +08:00
*state_ = !*state_;
2021-07-08 04:37:50 +08:00
option_->on_change();
return true;
}
return false;
}
2019-01-13 05:25:49 +08:00
} // 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.