mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-16 16:08:08 +08:00
Feature: the Modal
component. (#418)
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
#include <cmath>
|
||||
#include <cmath> // IWYU pragma: keep
|
||||
#include <ratio> // for ratio
|
||||
#include <utility> // for move
|
||||
|
||||
|
@@ -1,5 +1,6 @@
|
||||
#include "ftxui/component/component_options.hpp"
|
||||
|
||||
#include <ftxui/screen/color.hpp> // for Color, Color::Black, Color::White, Color::GrayDark, Color::GrayLight
|
||||
#include <memory> // for shared_ptr
|
||||
#include <utility> // for move
|
||||
|
||||
|
@@ -43,7 +43,7 @@ Component Maybe(Component child, std::function<bool()> show) {
|
||||
/// ### Example
|
||||
///
|
||||
/// ```cpp
|
||||
/// auto component = Renderer([]{ return "Hello World!"; });
|
||||
/// auto component = Renderer([]{ return text("Hello World!"); });
|
||||
/// auto maybe_component = component | Maybe([&]{ return counter == 42; });
|
||||
/// ```
|
||||
ComponentDecorator Maybe(std::function<bool()> show) {
|
||||
@@ -60,7 +60,7 @@ ComponentDecorator Maybe(std::function<bool()> show) {
|
||||
/// ### Example
|
||||
///
|
||||
/// ```cpp
|
||||
/// auto component = Renderer([]{ return "Hello World!"; });
|
||||
/// auto component = Renderer([]{ return text("Hello World!"); });
|
||||
/// auto maybe_component = Maybe(component, &show);
|
||||
/// ```
|
||||
Component Maybe(Component child, const bool* show) {
|
||||
@@ -74,7 +74,7 @@ Component Maybe(Component child, const bool* show) {
|
||||
/// ### Example
|
||||
///
|
||||
/// ```cpp
|
||||
/// auto component = Renderer([]{ return "Hello World!"; });
|
||||
/// auto component = Renderer([]{ return text("Hello World!"); });
|
||||
/// auto maybe_component = component | Maybe(&show);
|
||||
/// ```
|
||||
ComponentDecorator Maybe(const bool* show) {
|
||||
|
66
src/ftxui/component/modal.cpp
Normal file
66
src/ftxui/component/modal.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <ftxui/component/event.hpp> // for Event
|
||||
#include <ftxui/dom/elements.hpp> // for operator|, Element, center, clear_under, dbox
|
||||
#include <memory> // for __shared_ptr_access, shared_ptr
|
||||
#include <utility> // for move
|
||||
|
||||
#include "ftxui/component/component.hpp" // for Make, Tab, ComponentDecorator, Modal
|
||||
#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
// Add a |modal| window on top of the |main| component. It is shown one on the
|
||||
// top of the other when |show_modal| is true.
|
||||
/// @ingroup component
|
||||
// NOLINTNEXTLINE
|
||||
Component Modal(Component main, Component modal, const bool* show_modal) {
|
||||
class Impl : public ComponentBase {
|
||||
public:
|
||||
explicit Impl(Component main, Component modal, const bool* show_modal)
|
||||
: main_(std::move(main)),
|
||||
modal_(std::move(modal)),
|
||||
show_modal_(show_modal) {
|
||||
selector_ = *show_modal_;
|
||||
Add(Container::Tab({main_, modal_}, &selector_));
|
||||
}
|
||||
|
||||
private:
|
||||
Element Render() override {
|
||||
selector_ = *show_modal_;
|
||||
auto document = main_->Render();
|
||||
if (*show_modal_) {
|
||||
document = dbox({
|
||||
document,
|
||||
modal_->Render() | clear_under | center,
|
||||
});
|
||||
}
|
||||
return document;
|
||||
}
|
||||
|
||||
bool OnEvent(Event event) override {
|
||||
selector_ = *show_modal_;
|
||||
return ComponentBase::OnEvent(event);
|
||||
}
|
||||
|
||||
Component main_;
|
||||
Component modal_;
|
||||
const bool* show_modal_;
|
||||
int selector_ = 0;
|
||||
};
|
||||
return Make<Impl>(main, modal, show_modal);
|
||||
}
|
||||
|
||||
// Decorate a component. Add a |modal| window on top of it. It is shown one on
|
||||
// the top of the other when |show_modal| is true.
|
||||
/// @ingroup component
|
||||
// NOLINTNEXTLINE
|
||||
ComponentDecorator Modal(Component modal, const bool* show_modal) {
|
||||
return [modal, show_modal](Component main) {
|
||||
return Modal(std::move(main), modal, show_modal);
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
// Copyright 2022 Arthur Sonzogni. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be found in
|
||||
// the LICENSE file.
|
47
src/ftxui/component/modal_test.cpp
Normal file
47
src/ftxui/component/modal_test.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <gtest/gtest-message.h> // for Message
|
||||
#include <gtest/gtest-test-part.h> // for SuiteApiResolver, TestPartResult, TestFactoryImpl
|
||||
#include <ftxui/dom/elements.hpp> // for Element, operator|, text, border
|
||||
#include <memory> // for shared_ptr, allocator, __shared_ptr_access
|
||||
|
||||
#include "ftxui/component/component.hpp" // for Renderer, Modal
|
||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
||||
#include "ftxui/dom/node.hpp" // for Render
|
||||
#include "ftxui/screen/screen.hpp" // for Screen
|
||||
#include "gtest/gtest_pred_impl.h" // for Test, EXPECT_EQ, TEST
|
||||
|
||||
namespace ftxui {
|
||||
|
||||
TEST(ModalTest, Basic) {
|
||||
auto main = Renderer([] { return text("main") | border; });
|
||||
auto modal = Renderer([] { return text("modal") | border; });
|
||||
bool show_modal = false;
|
||||
auto component = Modal(main, modal, &show_modal);
|
||||
|
||||
Screen screen(10, 7);
|
||||
Render(screen, component->Render());
|
||||
EXPECT_EQ(screen.ToString(),
|
||||
"╭────────╮\r\n"
|
||||
"│main │\r\n"
|
||||
"│ │\r\n"
|
||||
"│ │\r\n"
|
||||
"│ │\r\n"
|
||||
"│ │\r\n"
|
||||
"╰────────╯");
|
||||
|
||||
show_modal = true;
|
||||
Render(screen, component->Render());
|
||||
EXPECT_EQ(screen.ToString(),
|
||||
"╭────────╮\r\n"
|
||||
"│main │\r\n"
|
||||
"│╭─────╮ │\r\n"
|
||||
"││modal│ │\r\n"
|
||||
"│╰─────╯ │\r\n"
|
||||
"│ │\r\n"
|
||||
"╰────────╯");
|
||||
}
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
// Copyright 2022 Arthur Sonzogni. All rights reserved.
|
||||
// Use of this source code is governed by the MIT license that can be found in
|
||||
// the LICENSE file.
|
@@ -3,8 +3,10 @@
|
||||
#include <chrono> // for operator-, milliseconds, duration, operator>=, time_point, common_type<>::type
|
||||
#include <csignal> // for signal, raise, SIGTSTP, SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM, SIGWINCH
|
||||
#include <cstdio> // for fileno, size_t, stdin
|
||||
#include <functional> // for function
|
||||
#include <initializer_list> // for initializer_list
|
||||
#include <ftxui/component/task.hpp> // for Task, Closure, AnimationTask
|
||||
#include <ftxui/screen/screen.hpp> // for Pixel, Screen::Cursor, Screen
|
||||
#include <functional> // for function
|
||||
#include <initializer_list> // for initializer_list
|
||||
#include <iostream> // for cout, ostream, basic_ostream, operator<<, endl, flush
|
||||
#include <stack> // for stack
|
||||
#include <thread> // for thread, sleep_for
|
||||
@@ -17,7 +19,7 @@
|
||||
#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse, CapturedMouseInterface
|
||||
#include "ftxui/component/component_base.hpp" // for ComponentBase
|
||||
#include "ftxui/component/event.hpp" // for Event
|
||||
#include "ftxui/component/receiver.hpp" // for ReceiverImpl, Sender, MakeReceiver, SenderImpl, Receiver
|
||||
#include "ftxui/component/receiver.hpp" // for Sender, ReceiverImpl, MakeReceiver, SenderImpl, Receiver
|
||||
#include "ftxui/component/screen_interactive.hpp"
|
||||
#include "ftxui/component/terminal_input_parser.hpp" // for TerminalInputParser
|
||||
#include "ftxui/dom/node.hpp" // for Node, Render
|
||||
|
@@ -1,6 +1,7 @@
|
||||
#include <gtest/gtest-message.h> // for Message
|
||||
#include <gtest/gtest-test-part.h> // for SuiteApiResolver, TestFactoryImpl, TestPartResult
|
||||
#include <csignal> // for raise, SIGABRT, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM
|
||||
#include <ftxui/component/event.hpp> // for Event, Event::Custom
|
||||
|
||||
#include "ftxui/component/component.hpp" // for Renderer
|
||||
#include "ftxui/component/screen_interactive.hpp"
|
||||
|
@@ -1,8 +1,10 @@
|
||||
#include "ftxui/component/terminal_input_parser.hpp"
|
||||
|
||||
#include <cstdint> // for uint32_t
|
||||
#include <memory> // for unique_ptr
|
||||
#include <utility> // for move
|
||||
#include <cstdint> // for uint32_t
|
||||
#include <ftxui/component/mouse.hpp> // for Mouse, Mouse::Button, Mouse::Motion
|
||||
#include <ftxui/component/receiver.hpp> // for SenderImpl, Sender
|
||||
#include <memory> // for unique_ptr, allocator
|
||||
#include <utility> // for move
|
||||
|
||||
#include "ftxui/component/event.hpp" // for Event
|
||||
#include "ftxui/component/task.hpp" // for Task
|
||||
|
@@ -1,11 +1,13 @@
|
||||
#include <gtest/gtest-message.h> // for Message
|
||||
#include <gtest/gtest-test-part.h> // for TestPartResult, SuiteApiResolver, TestFactoryImpl
|
||||
#include <algorithm> // for max
|
||||
#include <initializer_list> // for initializer_list
|
||||
#include <memory> // for unique_ptr, allocator
|
||||
#include <variant> // for get
|
||||
#include <ftxui/component/mouse.hpp> // for Mouse, Mouse::Left, Mouse::Middle, Mouse::Pressed, Mouse::Released, Mouse::Right
|
||||
#include <ftxui/component/task.hpp> // for Task
|
||||
#include <initializer_list> // for initializer_list
|
||||
#include <memory> // for allocator, unique_ptr
|
||||
#include <variant> // for get
|
||||
|
||||
#include "ftxui/component/event.hpp" // for Event, Event::Return, Event::ArrowDown, Event::ArrowLeft, Event::ArrowRight, Event::ArrowUp, Event::Backspace, Event::Custom, Event::Delete, Event::End, Event::Escape, Event::F10, Event::F11, Event::F12, Event::F5, Event::F6, Event::F7, Event::F8, Event::F9, Event::Home, Event::PageDown, Event::PageUp, Event::Tab, Event::TabReverse
|
||||
#include "ftxui/component/event.hpp" // for Event, Event::Return, Event::ArrowDown, Event::ArrowLeft, Event::ArrowRight, Event::ArrowUp, Event::Backspace, Event::Custom, Event::Delete, Event::End, Event::F10, Event::F11, Event::F12, Event::F5, Event::F6, Event::F7, Event::F8, Event::F9, Event::Home, Event::PageDown, Event::PageUp, Event::Tab, Event::TabReverse, Event::Escape
|
||||
#include "ftxui/component/receiver.hpp" // for MakeReceiver, ReceiverImpl
|
||||
#include "ftxui/component/terminal_input_parser.hpp"
|
||||
#include "gtest/gtest_pred_impl.h" // for AssertionResult, Test, EXPECT_EQ, EXPECT_TRUE, TEST, EXPECT_FALSE
|
||||
|
Reference in New Issue
Block a user