mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-29 16:39:34 +08:00
Component decorators (#354)
Add decorator variants for decorator components Add the "pipe" operator for components, similar to what was done for Elements. We are able to put something like: ``` Button(...) | Maybe(&show_button) ``` Add decorators for: - `Maybe` - `CatchEvent` - `Renderer` Signed-off-by: Kefu Chai <tchaikov@gmail.com> Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
@@ -55,6 +55,33 @@ Component CatchEvent(Component child,
|
||||
return out;
|
||||
}
|
||||
|
||||
/// @brief Decorate a component, using |on_event| to catch events. This function
|
||||
/// must returns true when the event has been handled, false otherwise.
|
||||
/// @param on_event The function drawing the interface.
|
||||
/// @ingroup component
|
||||
///
|
||||
/// ### Example
|
||||
///
|
||||
/// ```cpp
|
||||
/// auto screen = ScreenInteractive::TerminalOutput();
|
||||
/// auto renderer = Renderer([] { return text("Hello world"); });
|
||||
/// renderer |= CatchEvent([&](Event event) {
|
||||
/// if (event == Event::Character('q')) {
|
||||
/// screen.ExitLoopClosure()();
|
||||
/// return true;
|
||||
/// }
|
||||
/// return false;
|
||||
/// });
|
||||
/// screen.Loop(renderer);
|
||||
/// ```
|
||||
ComponentDecorator CatchEvent(std::function<bool(Event)> on_event) {
|
||||
return [on_event = std::move(on_event)](Component child) {
|
||||
return CatchEvent(child, [on_event = std::move(on_event)](Event event) {
|
||||
return on_event(event);
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
} // namespace ftxui
|
||||
|
||||
// Copyright 2021 Arthur Sonzogni. All rights reserved.
|
||||
|
Reference in New Issue
Block a user