From e66ebe5443f34ba62492c50e32970e842a27b219 Mon Sep 17 00:00:00 2001 From: ArthurSonzogni Date: Sat, 10 Jul 2021 11:56:40 +0200 Subject: [PATCH] Remove button.hpp --- CMakeLists.txt | 1 - include/ftxui/component/button.hpp | 46 --------------- src/ftxui/component/button.cpp | 92 +++++++++++++++++------------- 3 files changed, 52 insertions(+), 87 deletions(-) delete mode 100644 include/ftxui/component/button.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index fcd5bf6..13f37a1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -73,7 +73,6 @@ add_library(dom STATIC ) add_library(component STATIC - include/ftxui/component/button.hpp include/ftxui/component/captured_mouse.hpp include/ftxui/component/checkbox.hpp include/ftxui/component/component.hpp diff --git a/include/ftxui/component/button.hpp b/include/ftxui/component/button.hpp deleted file mode 100644 index 1765f64..0000000 --- a/include/ftxui/component/button.hpp +++ /dev/null @@ -1,46 +0,0 @@ -#ifndef FTXUI_COMPONENT_BUTTON_HPP -#define FTXUI_COMPONENT_BUTTON_HPP - -#include // for function -#include // for wstring - -#include "ftxui/component/component.hpp" // for Component -#include "ftxui/component/component_base.hpp" // for ComponentBase -#include "ftxui/dom/elements.hpp" // for Element -#include "ftxui/screen/box.hpp" // for Box -#include "ftxui/screen/string.hpp" // for ConstStringRef - -namespace ftxui { -struct Event; - -/// @brief A button. An action is associated to the click event. -/// @ingroup dom -class ButtonBase : public ComponentBase { - public: - // Access this interface from a Component - static ButtonBase* From(Component); - - // Constructor. - ButtonBase(ConstStringRef label, - std::function on_click, - ConstRef option = {}); - ~ButtonBase() override = default; - - // Component implementation. - Element Render() override; - bool OnEvent(Event) override; - - private: - ConstStringRef label_; - std::function on_click_; - Box box_; - ConstRef option_; -}; - -} // namespace ftxui - -#endif /* end of include guard: FTXUI_COMPONENT_BUTTON_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/button.cpp b/src/ftxui/component/button.cpp index 7f44517..bfe3ca9 100644 --- a/src/ftxui/component/button.cpp +++ b/src/ftxui/component/button.cpp @@ -1,14 +1,65 @@ #include // for function #include // for shared_ptr -#include "ftxui/component/button.hpp" #include "ftxui/component/captured_mouse.hpp" // for CapturedMouse +#include "ftxui/component/component.hpp" // for CapturedMouse #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 namespace ftxui { +namespace { +/// @brief A button. An action is associated to the click event. +/// @ingroup dom +class ButtonBase : public ComponentBase { + public: + ButtonBase(ConstStringRef label, + std::function on_click, + ConstRef option) + : label_(label), on_click_(on_click), option_(std::move(option)) {} + + ~ButtonBase() override = default; + + // Component implementation: + Element Render() override { + auto style = Focused() ? inverted : nothing; + auto my_border = option_->border ? border : nothing; + return text(*label_) | my_border | style | reflect(box_); + } + + bool OnEvent(Event event) override { + if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) { + if (!CaptureMouse(event)) + return false; + + TakeFocus(); + + if (event.mouse().button == Mouse::Left && + event.mouse().motion == Mouse::Pressed) { + on_click_(); + return true; + } + + return false; + } + + if (event == Event::Return) { + on_click_(); + return true; + } + return false; + } + + private: + ConstStringRef label_; + std::function on_click_; + Box box_; + ConstRef option_; +}; + +} // namespace + /// @brief Draw a button. Execute a function when clicked. /// @param label The label of the button. /// @param on_click The action to execute when clicked. @@ -37,45 +88,6 @@ Component Button(ConstStringRef label, return Make(label, std::move(on_click), std::move(option)); } -// static -ButtonBase* ButtonBase::From(Component component) { - return static_cast(component.get()); -} - -ButtonBase::ButtonBase(ConstStringRef label, - std::function on_click, - ConstRef option) - : label_(label), on_click_(on_click), option_(std::move(option)) {} - -Element ButtonBase::Render() { - auto style = Focused() ? inverted : nothing; - auto my_border = option_->border ? border : nothing; - return text(*label_) | my_border | style | reflect(box_); -} - -bool ButtonBase::OnEvent(Event event) { - if (event.is_mouse() && box_.Contain(event.mouse().x, event.mouse().y)) { - if (!CaptureMouse(event)) - return false; - - TakeFocus(); - - if (event.mouse().button == Mouse::Left && - event.mouse().motion == Mouse::Pressed) { - on_click_(); - return true; - } - - return false; - } - - if (event == Event::Return) { - on_click_(); - return true; - } - return false; -} - } // namespace ftxui // Copyright 2020 Arthur Sonzogni. All rights reserved.