mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-18 09:08:08 +08:00
Merge dom and component focus (#978)
Instead of two levels of focus with `focus` and `selected`, use a recursive level. The components set the one "active" and hbox/vbox/dbox Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
@@ -50,7 +50,10 @@ class ComponentBase {
|
||||
void DetachAllChildren();
|
||||
|
||||
// Renders the component.
|
||||
virtual Element Render();
|
||||
Element Render();
|
||||
|
||||
// Override this function modify how `Render` works.
|
||||
virtual Element OnRender();
|
||||
|
||||
// Handles an event.
|
||||
// By default, reduce on children with a lazy OR.
|
||||
@@ -94,6 +97,7 @@ class ComponentBase {
|
||||
|
||||
private:
|
||||
ComponentBase* parent_ = nullptr;
|
||||
bool in_render = false;
|
||||
};
|
||||
|
||||
} // namespace ftxui
|
||||
|
@@ -161,7 +161,7 @@ Element frame(Element);
|
||||
Element xframe(Element);
|
||||
Element yframe(Element);
|
||||
Element focus(Element);
|
||||
Element select(Element);
|
||||
Element select(Element e); // Deprecated - Alias for focus.
|
||||
|
||||
// --- Cursor ---
|
||||
// Those are similar to `focus`, but also change the shape of the cursor.
|
||||
|
@@ -59,6 +59,8 @@ class Node {
|
||||
};
|
||||
virtual void Check(Status* status);
|
||||
|
||||
friend void Render(Screen& screen, Node* node, Selection& selection);
|
||||
|
||||
protected:
|
||||
Elements children_;
|
||||
Requirement requirement_;
|
||||
|
@@ -5,8 +5,10 @@
|
||||
#define FTXUI_DOM_REQUIREMENT_HPP
|
||||
|
||||
#include "ftxui/screen/box.hpp"
|
||||
#include "ftxui/screen/screen.hpp"
|
||||
|
||||
namespace ftxui {
|
||||
class Node;
|
||||
|
||||
struct Requirement {
|
||||
// The required size to fully draw the element.
|
||||
@@ -20,13 +22,28 @@ struct Requirement {
|
||||
int flex_shrink_y = 0;
|
||||
|
||||
// Focus management to support the frame/focus/select element.
|
||||
enum Selection {
|
||||
NORMAL = 0,
|
||||
SELECTED = 1,
|
||||
FOCUSED = 2,
|
||||
struct Focused {
|
||||
bool enabled = false;
|
||||
Box box;
|
||||
Node* node = nullptr;
|
||||
Screen::Cursor::Shape cursor_shape = Screen::Cursor::Shape::Hidden;
|
||||
|
||||
// Internal for interactions with components.
|
||||
bool component_active = false;
|
||||
|
||||
// Return whether this requirement should be preferred over the other.
|
||||
bool Prefer(const Focused& other) const {
|
||||
if (!other.enabled) {
|
||||
return false;
|
||||
}
|
||||
if (!enabled) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return other.component_active && !component_active;
|
||||
}
|
||||
};
|
||||
Selection selection = NORMAL;
|
||||
Box selected_box;
|
||||
Focused focused;
|
||||
};
|
||||
|
||||
} // namespace ftxui
|
||||
|
@@ -14,6 +14,7 @@ struct Box {
|
||||
|
||||
static auto Intersection(Box a, Box b) -> Box;
|
||||
static auto Union(Box a, Box b) -> Box;
|
||||
void Shift(int x, int y);
|
||||
bool Contain(int x, int y) const;
|
||||
bool IsEmpty() const;
|
||||
bool operator==(const Box& other) const;
|
||||
|
Reference in New Issue
Block a user