From 7ee6edfd1f8ed305e3098941f049ac99a94ece88 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Sat, 10 Jul 2021 12:02:42 +0200 Subject: [PATCH] Remove checkbox.hpp --- CMakeLists.txt | 1 - examples/component/checkbox.cpp | 1 - include/ftxui/component/checkbox.hpp | 49 ---------- src/ftxui/component/checkbox.cpp | 139 +++++++++++++++------------ 4 files changed, 80 insertions(+), 110 deletions(-) delete mode 100644 include/ftxui/component/checkbox.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 13f37a17..e8fb5784 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -74,7 +74,6 @@ add_library(dom STATIC add_library(component STATIC include/ftxui/component/captured_mouse.hpp - include/ftxui/component/checkbox.hpp include/ftxui/component/component.hpp include/ftxui/component/component_base.hpp include/ftxui/component/container.hpp diff --git a/examples/component/checkbox.cpp b/examples/component/checkbox.cpp index 361ee683..13be0df6 100644 --- a/examples/component/checkbox.cpp +++ b/examples/component/checkbox.cpp @@ -1,4 +1,3 @@ -#include "ftxui/component/checkbox.hpp" #include "ftxui/component/captured_mouse.hpp" // for ftxui #include "ftxui/component/component.hpp" // for Checkbox, Vertical #include "ftxui/component/screen_interactive.hpp" // for ScreenInteractive diff --git a/include/ftxui/component/checkbox.hpp b/include/ftxui/component/checkbox.hpp deleted file mode 100644 index a43114ca..00000000 --- a/include/ftxui/component/checkbox.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef FTXUI_COMPONENT_CHECKBOX_HPP -#define FTXUI_COMPONENT_CHECKBOX_HPP - -#include // for function -#include // for allocator, wstring - -#include "ftxui/component/component.hpp" // for Component -#include "ftxui/component/component_base.hpp" // for ComponentBase -#include "ftxui/dom/elements.hpp" // for Element, Decorator, inverted, nothing -#include "ftxui/screen/box.hpp" // for Box -#include "ftxui/screen/string.hpp" // for ConstStringRef - -namespace ftxui { -struct Event; - -/// @brief A Checkbox. It can be checked or unchecked.Display an element on a -/// ftxui::Screen. -/// @ingroup dom -class CheckboxBase : public ComponentBase { - public: - // Access this interface from a Component - static CheckboxBase* From(Component component); - - // Constructor. - CheckboxBase(ConstStringRef label, - bool* state, - ConstRef option = {}); - ~CheckboxBase() override = default; - - // Component implementation. - Element Render() override; - bool OnEvent(Event) override; - - private: - bool OnMouseEvent(Event event); - - ConstStringRef label_; - bool* const state_; - Box box_; - ConstRef option_; -}; - -} // namespace ftxui - -#endif /* end of include guard: FTXUI_COMPONENT_CHECKBOX_HPP */ - -// 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/checkbox.cpp b/src/ftxui/component/checkbox.cpp index 9324689b..2cdc7872 100644 --- a/src/ftxui/component/checkbox.cpp +++ b/src/ftxui/component/checkbox.cpp @@ -2,13 +2,92 @@ #include // for shared_ptr #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 +#include // for function +#include // for allocator, wstring + +#include "ftxui/component/component.hpp" // for Component +#include "ftxui/component/component_base.hpp" // for ComponentBase +#include "ftxui/dom/elements.hpp" // for Element, Decorator, inverted, nothing +#include "ftxui/screen/box.hpp" // for Box +#include "ftxui/screen/string.hpp" // for ConstStringRef + namespace ftxui { +namespace { +/// @brief A Checkbox. It can be checked or unchecked.Display an element on a +/// ftxui::Screen. +/// @ingroup dom +class CheckboxBase : public ComponentBase { + public: + CheckboxBase(ConstStringRef label, + bool* state, + ConstRef option) + : label_(label), state_(state), option_(std::move(option)) { +#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK) + // Microsoft terminal do not use fonts able to render properly the default + // radiobox glyph. + if (option->checked == L"▣ ") + option->checked = L"[X]"; + if (option->unchecked == L"☐ ") + option->unchecked = L"[ ]"; +#endif + } + + ~CheckboxBase() override = default; + + private: + // Component implementation. + Element Render() override { + bool is_focused = Focused(); + auto style = is_focused ? option_->style_focused : option_->style_unfocused; + auto focus_management = is_focused ? focus : *state_ ? select : nothing; + return hbox(text(*state_ ? option_->style_checked + : option_->style_unchecked), + text(*label_) | style | focus_management) | + reflect(box_); + } + + bool OnEvent(Event event) override { + if (event.is_mouse()) + return OnMouseEvent(event); + + if (event == Event::Character(' ') || event == Event::Return) { + *state_ = !*state_; + option_->on_change(); + return true; + } + return false; + } + + bool OnMouseEvent(Event event) { + if (!CaptureMouse(event)) + return false; + if (!box_.Contain(event.mouse().x, event.mouse().y)) + return false; + + TakeFocus(); + + if (event.mouse().button == Mouse::Left && + event.mouse().motion == Mouse::Pressed) { + *state_ = !*state_; + option_->on_change(); + return true; + } + + return false; + } + + ConstStringRef label_; + bool* const state_; + Box box_; + ConstRef option_; +}; +} // namespace + /// @brief Draw checkable element. /// @param label The label of the checkbox. /// @param checked Whether the checkbox is checked or not. @@ -36,64 +115,6 @@ Component Checkbox(ConstStringRef label, return Make(label, checked, std::move(option)); } -// static -CheckboxBase* CheckboxBase::From(Component component) { - return static_cast(component.get()); -} - -CheckboxBase::CheckboxBase(ConstStringRef label, - bool* state, - ConstRef option) - : label_(label), state_(state), option_(std::move(option)) { -#if defined(FTXUI_MICROSOFT_TERMINAL_FALLBACK) - // Microsoft terminal do not use fonts able to render properly the default - // radiobox glyph. - if (option->checked == L"▣ ") - option->checked = L"[X]"; - if (option->unchecked == L"☐ ") - option->unchecked = L"[ ]"; -#endif -} - -Element CheckboxBase::Render() { - bool is_focused = Focused(); - auto style = is_focused ? option_->style_focused : option_->style_unfocused; - auto focus_management = is_focused ? focus : *state_ ? select : nothing; - return hbox(text(*state_ ? option_->style_checked : option_->style_unchecked), - text(*label_) | style | focus_management) | - reflect(box_); -} - -bool CheckboxBase::OnEvent(Event event) { - if (event.is_mouse()) - return OnMouseEvent(event); - - if (event == Event::Character(' ') || event == Event::Return) { - *state_ = !*state_; - option_->on_change(); - return true; - } - return false; -} - -bool CheckboxBase::OnMouseEvent(Event event) { - if (!CaptureMouse(event)) - return false; - if (!box_.Contain(event.mouse().x, event.mouse().y)) - return false; - - TakeFocus(); - - if (event.mouse().button == Mouse::Left && - event.mouse().motion == Mouse::Pressed) { - *state_ = !*state_; - option_->on_change(); - return true; - } - - return false; -} - } // namespace ftxui // Copyright 2020 Arthur Sonzogni. All rights reserved.