mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2024-11-22 18:59:59 +08:00
Remove toggle.hpp
This commit is contained in:
parent
bead2134d6
commit
7f514ff41c
@ -80,7 +80,6 @@ add_library(component STATIC
|
||||
include/ftxui/component/mouse.hpp
|
||||
include/ftxui/component/receiver.hpp
|
||||
include/ftxui/component/screen_interactive.hpp
|
||||
include/ftxui/component/toggle.hpp
|
||||
src/ftxui/component/button.cpp
|
||||
src/ftxui/component/catch_event.cpp
|
||||
src/ftxui/component/checkbox.cpp
|
||||
|
@ -74,6 +74,8 @@ struct ToggleOption {
|
||||
std::function<void()> on_change = [] {};
|
||||
/// Called when the user presses enter.
|
||||
std::function<void()> on_enter = [] {};
|
||||
|
||||
Ref<int> focused_entry = 0;
|
||||
};
|
||||
|
||||
}; // namespace ftxui
|
||||
|
@ -1,51 +0,0 @@
|
||||
#ifndef FTXUI_COMPONENT_TOGGLE_H_
|
||||
#define FTXUI_COMPONENT_TOGGLE_H_
|
||||
|
||||
#include <functional> // for function
|
||||
#include <string> // for wstring
|
||||
#include <vector> // for vector
|
||||
|
||||
#include "ftxui/component/component.hpp" // for Component
|
||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
||||
#include "ftxui/dom/elements.hpp" // for Element, Decorator, operator|, bold, dim, inverted
|
||||
#include "ftxui/screen/box.hpp" // for Box
|
||||
|
||||
namespace ftxui {
|
||||
struct Event;
|
||||
|
||||
/// @brief An horizontal list of elements. The user can navigate through them.
|
||||
/// @ingroup component
|
||||
class ToggleBase : public ComponentBase {
|
||||
public:
|
||||
// Access this interface from a Component
|
||||
static ToggleBase* From(Component component);
|
||||
|
||||
// Constructor.
|
||||
ToggleBase(const std::vector<std::wstring>* entries,
|
||||
int* selected,
|
||||
Ref<ToggleOption> option = {});
|
||||
~ToggleBase() override = default;
|
||||
|
||||
// State.
|
||||
int focused = 0;
|
||||
|
||||
// Component implementation.
|
||||
Element Render() override;
|
||||
bool OnEvent(Event) override;
|
||||
|
||||
protected:
|
||||
const std::vector<std::wstring>* const entries_;
|
||||
int* selected_ = 0;
|
||||
|
||||
bool OnMouseEvent(Event event);
|
||||
std::vector<Box> boxes_;
|
||||
Ref<ToggleOption> option_;
|
||||
};
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
#endif /* end of include guard: FTXUI_COMPONENT_TOGGLE_H_ */
|
||||
|
||||
// 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.
|
@ -4,29 +4,27 @@
|
||||
#include <utility> // for move
|
||||
|
||||
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse
|
||||
#include "ftxui/component/component.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
|
||||
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Return, Event::Tab, Event::TabReverse
|
||||
#include "ftxui/component/mouse.hpp" // for Mouse, Mouse::Left, Mouse::Pressed
|
||||
#include "ftxui/component/toggle.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
Component Toggle(const std::vector<std::wstring>* entries,
|
||||
int* selected,
|
||||
Ref<ToggleOption> option) {
|
||||
return Make<ToggleBase>(entries, selected, std::move(option));
|
||||
}
|
||||
namespace {
|
||||
|
||||
// static
|
||||
ToggleBase* ToggleBase::From(Component component) {
|
||||
return static_cast<ToggleBase*>(component.get());
|
||||
}
|
||||
|
||||
ToggleBase::ToggleBase(const std::vector<std::wstring>* entries,
|
||||
/// @brief An horizontal list of elements. The user can navigate through them.
|
||||
/// @ingroup component
|
||||
class ToggleBase : public ComponentBase {
|
||||
public:
|
||||
ToggleBase(const std::vector<std::wstring>* entries,
|
||||
int* selected,
|
||||
Ref<ToggleOption> option)
|
||||
: entries_(entries), selected_(selected), option_(std::move(option)) {}
|
||||
|
||||
Element ToggleBase::Render() {
|
||||
~ToggleBase() override = default;
|
||||
|
||||
private:
|
||||
Element Render() override {
|
||||
Elements children;
|
||||
bool is_toggle_focused = Focused();
|
||||
boxes_.resize(entries_->size());
|
||||
@ -35,7 +33,7 @@ Element ToggleBase::Render() {
|
||||
if (i != 0)
|
||||
children.push_back(separator());
|
||||
|
||||
bool is_focused = (focused == int(i)) && is_toggle_focused;
|
||||
bool is_focused = (focused_entry() == int(i)) && is_toggle_focused;
|
||||
bool is_selected = (*selected_ == int(i));
|
||||
|
||||
auto style = is_selected ? (is_focused ? option_->style_selected_focused
|
||||
@ -51,7 +49,7 @@ Element ToggleBase::Render() {
|
||||
return hbox(std::move(children));
|
||||
}
|
||||
|
||||
bool ToggleBase::OnEvent(Event event) {
|
||||
bool OnEvent(Event event) override{
|
||||
if (event.is_mouse())
|
||||
return OnMouseEvent(event);
|
||||
|
||||
@ -68,7 +66,7 @@ bool ToggleBase::OnEvent(Event event) {
|
||||
*selected_ = std::max(0, std::min(int(entries_->size()) - 1, *selected_));
|
||||
|
||||
if (old_selected != *selected_) {
|
||||
focused = *selected_;
|
||||
focused_entry() = *selected_;
|
||||
option_->on_change();
|
||||
return true;
|
||||
}
|
||||
@ -81,7 +79,7 @@ bool ToggleBase::OnEvent(Event event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ToggleBase::OnMouseEvent(Event event) {
|
||||
bool OnMouseEvent(Event event) {
|
||||
if (!CaptureMouse(event))
|
||||
return false;
|
||||
for (int i = 0; i < int(boxes_.size()); ++i) {
|
||||
@ -89,7 +87,7 @@ bool ToggleBase::OnMouseEvent(Event event) {
|
||||
continue;
|
||||
|
||||
TakeFocus();
|
||||
focused = i;
|
||||
focused_entry() = i;
|
||||
if (event.mouse().button == Mouse::Left &&
|
||||
event.mouse().motion == Mouse::Pressed) {
|
||||
TakeFocus();
|
||||
@ -103,6 +101,25 @@ bool ToggleBase::OnMouseEvent(Event event) {
|
||||
return false;
|
||||
}
|
||||
|
||||
int& focused_entry() { return option_->focused_entry(); }
|
||||
|
||||
const std::vector<std::wstring>* const entries_;
|
||||
int* selected_ = 0;
|
||||
|
||||
std::vector<Box> boxes_;
|
||||
Ref<ToggleOption> option_;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
/// @brief An horizontal list of elements. The user can navigate through them.
|
||||
/// @ingroup component
|
||||
Component Toggle(const std::vector<std::wstring>* entries,
|
||||
int* selected,
|
||||
Ref<ToggleOption> option) {
|
||||
return Make<ToggleBase>(entries, selected, std::move(option));
|
||||
}
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
// Copyright 2020 Arthur Sonzogni. All rights reserved.
|
||||
|
@ -2,9 +2,9 @@
|
||||
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
|
||||
#include <memory> // for __shared_ptr_access, shared_ptr
|
||||
|
||||
#include "ftxui/component/component.hpp"
|
||||
#include "ftxui/component/event.hpp" // for Event, Event::ArrowLeft, Event::ArrowRight, Event::Return, Event::Tab, Event::TabReverse
|
||||
#include "ftxui/component/mouse.hpp" // for ftxui
|
||||
#include "ftxui/component/toggle.hpp"
|
||||
#include "gtest/gtest_pred_impl.h" // for AssertionResult, EXPECT_EQ, Test, EXPECT_TRUE, EXPECT_FALSE, TEST
|
||||
|
||||
using namespace ftxui;
|
||||
|
Loading…
Reference in New Issue
Block a user