1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//! # Example: Reactive Data Binding
//!
//! Demonstrates how A2UI's data model drives dynamic text content through
//! JSON Pointer bindings. Updating the data model automatically changes
//! what the UI displays.
//!
//! ## What it demonstrates
//! - Creating a surface with an initial `dataModel`
//! - Dynamic text via `{"path": "/some/field"}` bindings
//! - Using `updateDataModel` to reactively update the UI
//!
//! ## Run
//! ```sh
//! cargo run --example 03_data_binding
//! ```
use std::io;
use crossterm::{
event::{self, Event, KeyCode},
execute,
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::{
Terminal,
backend::CrosstermBackend,
layout::{Constraint, Direction, Layout},
style::{Color, Style},
text::Line,
widgets::Paragraph,
};
use a2ui::core::catalog::Catalog;
use a2ui::core::message_processor::MessageProcessor;
use a2ui::tui::catalogs::basic::{build_basic_catalog, build_basic_registry};
fn main() -> Result<(), Box<dyn std::error::Error>> {
let registry = build_basic_registry();
let render_catalog = Catalog::new("placeholder");
let mut processor = MessageProcessor::new(vec![build_basic_catalog()]);
// ── 1. Create a surface with an initial data model ───────────────────
let create_msg = serde_json::json!({
"version": "v1.0",
"createSurface": {
"surfaceId": "profile",
"catalogId": "https://a2ui.org/specification/v1_0/catalogs/basic/catalog.json",
"dataModel": {
"name": "Alice",
"role": "Engineer",
"count": 0
}
}
});
processor.process_message(MessageProcessor::parse_message(&create_msg.to_string())?)?;
// ── 2. Define components that bind to the data model ─────────────────
// The `text` field uses a DynamicString binding: `{"path": "/name"}`
// resolves to the current value at that JSON Pointer in the data model.
let update_msg = serde_json::json!({
"version": "v1.0",
"updateComponents": {
"surfaceId": "profile",
"components": [
{
"id": "root",
"component": "Column",
"children": ["greeting", "role_line", "counter", "help"],
"justify": "center",
"align": "center"
},
{
"id": "greeting",
"component": "Text",
"text": {"path": "/name"},
"variant": "h1"
},
{
"id": "role_line",
"component": "Text",
"text": {"path": "/role"},
"variant": "h3"
},
{
"id": "counter",
"component": "Text",
"text": {"path": "/count"},
"variant": "body"
},
{
"id": "help",
"component": "Text",
"text": "n: change name r: change role c: increment counter q: quit",
"variant": "caption"
}
]
}
});
processor.process_message(MessageProcessor::parse_message(&update_msg.to_string())?)?;
// ── 3. Interactive loop: update data model and watch UI react ────────
enable_raw_mode()?;
let mut stdout = io::stderr();
execute!(stdout, EnterAlternateScreen)?;
let backend = CrosstermBackend::new(io::stderr());
let mut terminal = Terminal::new(backend)?;
let names = ["Alice", "Bob", "Charlie", "Diana"];
let roles = ["Engineer", "Designer", "Manager", "Scientist"];
let mut name_idx = 0;
let mut role_idx = 0;
let mut count = 0u32;
loop {
terminal.draw(|frame| {
let area = frame.area();
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(5), Constraint::Length(2)])
.split(area);
if let Some(surface) = processor.model.get_surface("profile") {
let renderer = a2ui::tui::surface::SurfaceRenderer::new(
surface, ®istry, &render_catalog,
);
renderer.render(frame, chunks[0], None);
}
let data = format!(
" Data: name={:?} role={:?} count={} ",
names[name_idx], roles[role_idx], count
);
let bar = Paragraph::new(Line::from(data))
.style(Style::default().fg(Color::DarkGray));
frame.render_widget(bar, chunks[1]);
})?;
if event::poll(std::time::Duration::from_millis(100))? {
if let Event::Key(key) = event::read()? {
match key.code {
KeyCode::Char('q') => break,
KeyCode::Char('n') => {
// Cycle through names and update the data model.
name_idx = (name_idx + 1) % names.len();
let msg = serde_json::json!({
"version": "v1.0",
"updateDataModel": {
"surfaceId": "profile",
"path": "/name",
"value": names[name_idx]
}
});
let _ = processor.process_message(
MessageProcessor::parse_message(&msg.to_string()).unwrap()
);
}
KeyCode::Char('r') => {
// Cycle through roles.
role_idx = (role_idx + 1) % roles.len();
let msg = serde_json::json!({
"version": "v1.0",
"updateDataModel": {
"surfaceId": "profile",
"path": "/role",
"value": roles[role_idx]
}
});
let _ = processor.process_message(
MessageProcessor::parse_message(&msg.to_string()).unwrap()
);
}
KeyCode::Char('c') => {
// Increment counter.
count += 1;
let msg = serde_json::json!({
"version": "v1.0",
"updateDataModel": {
"surfaceId": "profile",
"path": "/count",
"value": count
}
});
let _ = processor.process_message(
MessageProcessor::parse_message(&msg.to_string()).unwrap()
);
}
_ => {}
}
}
}
}
disable_raw_mode()?;
execute!(stdout, LeaveAlternateScreen)?;
Ok(())
}