FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
modal.cpp
Go to the documentation of this file.
1// Copyright 2022 Arthur Sonzogni. 版權所有。
2// 本原始碼的使用受 MIT 授權條款約束,詳情請參閱
3// LICENSE 文件。
4#include <ftxui/component/event.hpp> // for Event
5#include <ftxui/dom/elements.hpp> // for operator|, Element, center, clear_under, dbox
6#include <memory> // for __shared_ptr_access, shared_ptr
7#include <utility> // for move
8
9#include "ftxui/component/component.hpp" // for Make, Tab, ComponentDecorator, Modal
10#include "ftxui/component/component_base.hpp" // for Component, ComponentBase
11
12namespace ftxui {
13
14// 在 |main| 元件的頂部添加一個 |modal| 視窗。當 |show_modal| 為 true 時,
15// 它們會一個接一個地顯示。
16/// @ingroup component
17// NOLINTNEXTLINE
18Component Modal(Component main, Component modal, const bool* show_modal) {
19 class Impl : public ComponentBase {
20 public:
21 explicit Impl(Component main, Component modal, const bool* show_modal)
22 : main_(std::move(main)),
23 modal_(std::move(modal)),
24 show_modal_(show_modal) {
25 Add(Container::Tab({main_, modal_}, &selector_));
26 }
27
28 private:
29 Element OnRender() override {
30 selector_ = *show_modal_;
31 auto document = main_->Render();
32 if (*show_modal_) {
33 document = dbox({
34 document,
35 modal_->Render() | clear_under | center,
36 });
37 }
38 return document;
39 }
40
41 bool OnEvent(Event event) override {
42 selector_ = *show_modal_;
43 return ComponentBase::OnEvent(event);
44 }
45
46 Component main_;
47 Component modal_;
48 const bool* show_modal_;
49 int selector_ = *show_modal_;
50 };
51 return Make<Impl>(main, modal, show_modal);
52}
53
54// 裝飾一個元件。在其頂部添加一個 |modal| 視窗。當 |show_modal| 為 true 時,
55// 它們會一個接一個地顯示。
56/// @ingroup component
57// NOLINTNEXTLINE
58ComponentDecorator Modal(Component modal, const bool* show_modal) {
59 return [modal, show_modal](Component main) {
60 return Modal(std::move(main), modal, show_modal);
61 };
62}
63
64} // namespace ftxui
它將自己實作為 ftxui::Element 進行渲染。它透過回應 ftxui::Event 來實現鍵盤導航。
Component Modal(Component main, Component modal, const bool *show_modal)
Definition modal.cpp:18
代表一個事件。它可以是按鍵事件、終端機大小調整,或更多...
Definition event.hpp:27
FTXUI 的 ftxui:: 命名空間
Definition animation.hpp:10
std::shared_ptr< Node > Element
Definition elements.hpp:22
return Make< Impl >(option)
std::function< Component(Component)> ComponentDecorator
Definition component.hpp:31
std::shared_ptr< ComponentBase > Component