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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! Backend-agnostic Form demo showcasing `FieldKind::ToggleGroup`,
//! `FieldKind::ButtonRow`, and [`FocusRing`] for Tab/Shift+Tab cycling.
//!
//! Single [`AppLogic`] impl drives both the TUI runner and the GTK
//! runner. The thin shells in `examples/{tui,gtk}_form_groups.rs` are
//! each ~10 lines.
//!
//! Shape: a mini search/replace panel with:
//! - TextInput: search query
//! - ToggleGroup: Aa (case) / Ab| (word) / .* (regex)
//! - TextInput: replace string
//! - ButtonRow: Find Next / Replace / Replace All
//!
//! Controls:
//! - mouse click on toggle → flip value
//! - mouse click on button → log to status bar
//! - `Tab` / `Shift+Tab` → cycle focused field (via FocusRing)
//! - `q` / `Esc` → quit
use quadraui::{
AppLogic, Backend, ButtonRowItem, Color, FieldKind, FocusRing, Form, FormField, FormHit,
InteractionState, Key, MouseButton, NamedKey, Reaction, Rect, StatusBar, StatusBarSegment,
StyledText, ToggleGroupItem, Toolbar, ToolbarButton, UiEvent, WidgetId,
};
pub struct FormGroupsApp {
search_query: String,
replace_query: String,
case_sensitive: bool,
whole_word: bool,
regex: bool,
focus: FocusRing,
last_action: String,
/// Which scope preset is active (0=Workspace, 1=File, 2=Selection).
scope: usize,
}
impl FormGroupsApp {
pub fn new() -> Self {
Self {
search_query: "hello".into(),
replace_query: String::new(),
case_sensitive: true,
whole_word: false,
regex: false,
focus: FocusRing::new(vec!["search", "toggles", "replace", "buttons", "scope"]),
last_action: "—".into(),
scope: 0,
}
}
fn build_form(&self) -> Form {
Form {
id: WidgetId::new("find-replace"),
fields: vec![
FormField {
id: WidgetId::new("search"),
label: StyledText::plain("Find"),
kind: FieldKind::TextInput {
value: self.search_query.clone(),
placeholder: "Search…".into(),
cursor: Some(self.search_query.len()),
selection_anchor: None,
},
hint: StyledText::default(),
disabled: false,
validation: None,
},
FormField {
id: WidgetId::new("toggles"),
label: StyledText::default(),
kind: FieldKind::ToggleGroup {
toggles: vec![
ToggleGroupItem {
id: WidgetId::new("case"),
label: "Aa".into(),
value: self.case_sensitive,
},
ToggleGroupItem {
id: WidgetId::new("word"),
label: "Ab|".into(),
value: self.whole_word,
},
ToggleGroupItem {
id: WidgetId::new("regex"),
label: ".*".into(),
value: self.regex,
},
],
},
hint: StyledText::default(),
disabled: false,
validation: None,
},
FormField {
id: WidgetId::new("replace"),
label: StyledText::plain("Replace"),
kind: FieldKind::TextInput {
value: self.replace_query.clone(),
placeholder: "Replace…".into(),
cursor: Some(self.replace_query.len()),
selection_anchor: None,
},
hint: StyledText::default(),
disabled: false,
validation: None,
},
FormField {
id: WidgetId::new("buttons"),
label: StyledText::default(),
kind: FieldKind::ButtonRow {
buttons: vec![
ButtonRowItem {
id: WidgetId::new("find-next"),
label: "Find Next".into(),
disabled: false,
icon: None,
},
ButtonRowItem {
id: WidgetId::new("replace-one"),
label: "Replace".into(),
disabled: false,
icon: None,
},
ButtonRowItem {
id: WidgetId::new("replace-all"),
label: "Replace All".into(),
disabled: self.search_query.is_empty(),
icon: None,
},
],
},
hint: StyledText::default(),
disabled: false,
validation: None,
},
// ── Gap B demo: embedded toolbar inside a form field ──────
FormField {
id: WidgetId::new("scope"),
label: StyledText::plain("Scope"),
kind: FieldKind::Toolbar(
Toolbar::new(WidgetId::new("scope-toolbar")).with_buttons(vec![
ToolbarButton::Action {
id: WidgetId::new("scope-ws"),
label: "Workspace".into(),
icon: None,
key_hint: None,
enabled: true,
is_active: self.scope == 0,
tooltip: "Search entire workspace".into(),
},
ToolbarButton::Action {
id: WidgetId::new("scope-file"),
label: "File".into(),
icon: None,
key_hint: None,
enabled: true,
is_active: self.scope == 1,
tooltip: "Search current file only".into(),
},
ToolbarButton::Action {
id: WidgetId::new("scope-sel"),
label: "Selection".into(),
icon: None,
key_hint: None,
enabled: true,
is_active: self.scope == 2,
tooltip: "Search selected text only".into(),
},
]),
),
hint: StyledText::default(),
disabled: false,
validation: None,
},
],
focused_field: self.focus.current().cloned(),
scroll_offset: 0,
has_focus: true,
}
}
fn form_rect(backend: &dyn Backend) -> Rect {
let vp = backend.viewport();
let status_h = backend.line_height() * 1.5;
Rect::new(0.0, 0.0, vp.width, (vp.height - status_h).max(0.0))
}
fn status_rect(backend: &dyn Backend) -> Rect {
let vp = backend.viewport();
let status_h = backend.line_height() * 1.5;
Rect::new(0.0, (vp.height - status_h).max(0.0), vp.width, status_h)
}
fn click(&mut self, backend: &mut dyn Backend, x: f32, y: f32) {
let form = self.build_form();
let area = Self::form_rect(backend);
let layout = backend.form_layout(area, &form);
let hit = layout.hit_test(x, y);
match hit {
FormHit::Field(ref id) => {
if id.as_str() == "case" {
self.case_sensitive = !self.case_sensitive;
self.last_action = format!("case={}", self.case_sensitive);
} else if id.as_str() == "word" {
self.whole_word = !self.whole_word;
self.last_action = format!("word={}", self.whole_word);
} else if id.as_str() == "regex" {
self.regex = !self.regex;
self.last_action = format!("regex={}", self.regex);
} else if id.as_str() == "find-next" {
self.last_action = "Find Next".into();
} else if id.as_str() == "replace-one" {
self.last_action = "Replace".into();
} else if id.as_str() == "replace-all" {
if !self.search_query.is_empty() {
self.last_action = "Replace All".into();
}
} else if id.as_str() == "scope-ws" {
self.scope = 0;
self.last_action = "scope=Workspace".into();
} else if id.as_str() == "scope-file" {
self.scope = 1;
self.last_action = "scope=File".into();
} else if id.as_str() == "scope-sel" {
self.scope = 2;
self.last_action = "scope=Selection".into();
} else {
self.focus.set(id);
self.last_action = format!("focus → {}", id.as_str());
}
}
FormHit::Empty => {}
}
}
fn build_status_bar(&self) -> StatusBar {
let fg = Color::rgb(220, 220, 220);
let bg = Color::rgb(40, 40, 60);
StatusBar {
id: WidgetId::new("status"),
left_segments: vec![StatusBarSegment {
text: format!(" last: {} ", self.last_action),
fg,
bg,
bold: false,
action_id: None,
}],
right_segments: vec![StatusBarSegment {
text: " click / Tab / Shift+Tab / q ".into(),
fg,
bg,
bold: false,
action_id: None,
}],
}
}
}
impl Default for FormGroupsApp {
fn default() -> Self {
Self::new()
}
}
impl AppLogic for FormGroupsApp {
type AreaId = ();
fn render(&self, backend: &mut dyn Backend, _area: ()) {
let form_rect = Self::form_rect(backend);
let status_rect = Self::status_rect(backend);
backend.draw_form(form_rect, &self.build_form());
let _hits = backend.draw_status_bar_interactive(
status_rect,
&self.build_status_bar(),
&InteractionState::new(),
);
}
fn handle(&mut self, event: UiEvent, backend: &mut dyn Backend) -> Reaction {
match event {
UiEvent::MouseDown {
button: MouseButton::Left,
position,
..
} => {
self.click(backend, position.x, position.y);
Reaction::Redraw
}
UiEvent::KeyPressed { key, .. } => match key {
Key::Char('q') | Key::Named(NamedKey::Escape) => Reaction::Exit,
Key::Named(NamedKey::Tab) => {
self.focus.advance();
Reaction::Redraw
}
Key::Named(NamedKey::BackTab) => {
self.focus.retreat();
Reaction::Redraw
}
_ => Reaction::Continue,
},
UiEvent::WindowResized { .. } => Reaction::Redraw,
_ => Reaction::Continue,
}
}
}