Pulp 1.88.0
Cross-platform audio plugin framework
Loading...
Searching...
No Matches
relative_point.hpp
Go to the documentation of this file.
1#pragma once
2
3// RelativePoint — expression-based positioning primitive.
4//
5// Pulp's flex + grid layout covers most authored layouts; RelativePoint
6// fills the remaining "I want this anchored to another element's edge" cases
7// without forcing a full constraint solver.
8//
9// Grammar:
10//
11// point ::= expr ',' expr // (x, y) pair
12// expr ::= term (('+' | '-') term)*
13// term ::= factor (('*' | '/') factor)*
14// factor ::= number
15// | identifier // 'left', 'right', 'top', 'bottom',
16// // 'width', 'height',
17// // 'centerx', 'centery'
18// | qualified // <name>.<identifier> e.g.
19// // parent.right, prev.left
20// | '(' expr ')'
21// | '-' factor // unary minus
22// number ::= [0-9]+ ('.' [0-9]+)?
23// identifier ::= [a-zA-Z_][a-zA-Z0-9_]*
24// qualified ::= identifier '.' identifier
25//
26// Built-in names (unqualified) refer to the `parent` rectangle. Qualified
27// names route to the `named` map at evaluate time. Unknown qualified
28// names cause `evaluate()` to throw — that's a runtime error, not a
29// parse error, because the named-rect environment is only known at
30// layout time.
31//
32// Numeric values are treated as device pixels (no DimensionUnit support —
33// keep this primitive narrow; if you need %/vw/vh, compose with the
34// existing Dimension type at the caller).
35
37
38#include <map>
39#include <memory>
40#include <stdexcept>
41#include <string>
42#include <variant>
43#include <vector>
44
45namespace pulp::view {
46
47class RelativeExpression {
48public:
52 float evaluate(const Rect& parent,
53 const std::map<std::string, Rect>& named = {}) const;
54
57 std::string to_string() const;
58
60 static float evaluate(const std::string& source,
61 const Rect& parent,
62 const std::map<std::string, Rect>& named = {});
63
67 static RelativeExpression parse(const std::string& source);
68
69 // Internal AST node (public for to_string traversal). Hidden from
70 // generic consumers behind opaque ptr.
71 struct Node;
72
73private:
74 explicit RelativeExpression(std::shared_ptr<Node> root) : root_(std::move(root)) {}
75 std::shared_ptr<Node> root_;
76};
77
78class RelativePoint {
79public:
82 static RelativePoint parse(const std::string& source);
83
85 Point evaluate(const Rect& parent,
86 const std::map<std::string, Rect>& named = {}) const;
87
88 const RelativeExpression& x() const noexcept { return x_; }
89 const RelativeExpression& y() const noexcept { return y_; }
90
91private:
93 : x_(std::move(x)), y_(std::move(y)) {}
94 RelativeExpression x_;
95 RelativeExpression y_;
96};
97
98} // namespace pulp::view
Definition relative_point.hpp:47
static RelativeExpression parse(const std::string &source)
Parse a single-axis expression (e.g.
float evaluate(const Rect &parent, const std::map< std::string, Rect > &named={}) const
Evaluate this expression against the given parent rectangle and optional environment of named rectang...
static float evaluate(const std::string &source, const Rect &parent, const std::map< std::string, Rect > &named={})
Convenience: parse + evaluate in one shot. Throws on parse error.
std::string to_string() const
Round-trip text form for diagnostics.
Definition relative_point.hpp:78
Point evaluate(const Rect &parent, const std::map< std::string, Rect > &named={}) const
Evaluate both axes against the given environment.
static RelativePoint parse(const std::string &source)
Parse a "x, y" expression.
const RelativeExpression & x() const noexcept
Definition relative_point.hpp:88
const RelativeExpression & y() const noexcept
Definition relative_point.hpp:89
Hover-cursor tracking for platform window hosts.
Definition fwd.hpp:27
STL namespace.
Definition geometry.hpp:20
Axis-aligned rectangle in continuous (float) coordinates.
Definition geometry.hpp:52