agg-gui 0.2.1

Immediate-mode Rust GUI library with AGG rendering, Y-up layout, widgets, text, SVG, and native/WASM adapters
Documentation
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use super::*;

impl Widget for TextField {
    fn type_name(&self) -> &'static str {
        "TextField"
    }
    fn bounds(&self) -> Rect {
        self.bounds
    }
    fn set_bounds(&mut self, b: Rect) {
        self.bounds = b;
    }
    fn children(&self) -> &[Box<dyn Widget>] {
        &self.children
    }
    fn children_mut(&mut self) -> &mut Vec<Box<dyn Widget>> {
        &mut self.children
    }
    fn is_focusable(&self) -> bool {
        true
    }

    fn focus_id(&self) -> Option<crate::focus::FocusId> {
        self.focus_request_id
    }

    fn accepts_text_input(&self) -> bool {
        !self.read_only
    }

    fn text_input_value(&self) -> Option<String> {
        Some(self.text())
    }

    fn text_input_mode(&self) -> crate::widgets::on_screen_keyboard::KeyboardInputMode {
        self.keyboard_mode()
    }

    /// Composite parents (e.g. `ColorWheelPicker`) use this to push a live
    /// value into the field without bypassing `set_text`'s cell sync and
    /// cache invalidation.  Skipped while the field is focused so the user
    /// isn't fighting the parent for cursor position mid-edit; the parent
    /// is expected to read back via [`TextField::text`] after focus is
    /// released to pick up any user-typed value.
    fn set_label_text(&mut self, text: &str) {
        if self.focused {
            return;
        }
        if self.edit.borrow().text == text {
            return;
        }
        self.set_text(text);
    }

    /// While focused, the cursor blinks at 500 ms half-period.  The field
    /// itself drives its own repaint cadence: [`needs_draw`] reports dirty
    /// whenever wall-clock time has crossed a flip boundary since the last
    /// paint, and [`next_draw_deadline`] returns the exact wall-clock
    /// instant of the next boundary so the host can `WaitUntil` it.
    ///
    /// Losing focus makes both return `None` / `false`, and the tree walk's
    /// visibility check drops the field entirely when its enclosing window
    /// is closed / collapsed / tab not selected — so an invisible focused
    /// field does NOT keep the loop awake.
    fn needs_draw(&self) -> bool {
        if !self.focused {
            return false;
        }
        let Some(t) = self.focus_time else {
            return false;
        };
        let current_phase = (t.elapsed().as_millis() / 500) as u64;
        current_phase != self.blink_last_phase.get()
    }

    fn next_draw_deadline(&self) -> Option<web_time::Instant> {
        if !self.focused {
            return None;
        }
        let t = self.focus_time?;
        let ms = t.elapsed().as_millis() as u64;
        let next_phase = (ms / 500) + 1;
        Some(t + std::time::Duration::from_millis(next_phase * 500))
    }

    fn margin(&self) -> Insets {
        self.base.margin
    }
    fn widget_base(&self) -> Option<&WidgetBase> {
        Some(&self.base)
    }
    fn widget_base_mut(&mut self) -> Option<&mut WidgetBase> {
        Some(&mut self.base)
    }
    fn h_anchor(&self) -> HAnchor {
        self.base.h_anchor
    }
    fn v_anchor(&self) -> VAnchor {
        self.base.v_anchor
    }
    fn min_size(&self) -> Size {
        self.base.min_size
    }
    fn max_size(&self) -> Size {
        self.base.max_size
    }

    fn backbuffer_cache_mut(&mut self) -> Option<&mut BackbufferCache> {
        Some(&mut self.cache)
    }

    fn backbuffer_mode(&self) -> BackbufferMode {
        if crate::font_settings::lcd_enabled() {
            BackbufferMode::LcdCoverage
        } else {
            BackbufferMode::Rgba
        }
    }

    fn layout(&mut self, available: Size) -> Size {
        self.sync_from_text_cell();
        // Sig excludes cursor-blink phase.  Cursor paints in
        // `paint_overlay` after cache blit — no blink-driven
        // invalidation.
        let st = self.edit.borrow();
        let font = self.active_font();
        let sig = TextFieldSig {
            text: st.text.clone(),
            cursor: st.cursor,
            anchor: st.anchor,
            focused: self.focused,
            hovered: self.hovered,
            scroll_x_bits: self.scroll_x.to_bits(),
            w_bits: self.bounds.width.to_bits(),
            h_bits: self.bounds.height.to_bits(),
            font_ptr: Arc::as_ptr(&font) as usize,
            font_size_bits: self.font_size.to_bits(),
        };
        drop(st);
        if self.last_sig.as_ref() != Some(&sig) {
            self.last_sig = Some(sig);
            self.cache.invalidate();
        }
        Size::new(available.width, (self.font_size * 2.4).max(28.0))
    }

    fn paint(&mut self, ctx: &mut dyn DrawCtx) {
        let w = self.bounds.width;
        let h = self.bounds.height;
        let r = self.theme.border_radius.unwrap_or(6.0);
        let pad = self.padding;
        let (raw_text, raw_cursor, raw_anchor) = {
            let st = self.edit.borrow();
            (st.text.clone(), st.cursor, st.anchor)
        };
        // In password mode render '•' for every character, but keep byte positions
        // consistent by recomputing them against the masked string.
        let (text, cursor, anchor) = if self.password_mode {
            const BULLET: char = '';
            const BULLET_LEN: usize = 3; // '•' is 3 bytes in UTF-8
            let n = raw_text.chars().count();
            let masked = BULLET.to_string().repeat(n);
            let cur = raw_text[..raw_cursor].chars().count() * BULLET_LEN;
            let anc = raw_text[..raw_anchor].chars().count() * BULLET_LEN;
            (masked, cur, anc)
        } else {
            (raw_text, raw_cursor, raw_anchor)
        };

        let v = ctx.visuals();
        let t = &self.theme;

        // ── Background ────────────────────────────────────────────────────
        ctx.set_fill_color(t.background.unwrap_or(v.widget_bg));
        ctx.begin_path();
        ctx.rounded_rect(0.0, 0.0, w, h, r);
        ctx.fill();

        // ── Text area clip ────────────────────────────────────────────────
        ctx.clip_rect(pad, 0.0, (w - pad * 2.0).max(0.0), h);

        let font = self.active_font();
        ctx.set_font(Arc::clone(&font));
        ctx.set_font_size(self.font_size);

        let m = ctx.measure_text("Ag").unwrap_or_default();
        let baseline_y = h * 0.5 - (m.ascent - m.descent) * 0.5;
        let text_x = pad - self.scroll_x;

        // ── Selection highlight ───────────────────────────────────────────
        if cursor != anchor {
            let lo = cursor.min(anchor);
            let hi = cursor.max(anchor);
            let lo_x = measure_advance(&font, &text[..lo], self.font_size);
            let hi_x = measure_advance(&font, &text[..hi], self.font_size);
            let sx = (text_x + lo_x).max(pad);
            let sw = (text_x + hi_x).min(w - pad) - sx;
            if sw > 0.0 {
                let hl_bot = baseline_y - m.descent;
                let hl_h = (m.ascent + m.descent) * 1.2;
                ctx.set_fill_color(if self.focused {
                    t.selection_bg.unwrap_or(v.selection_bg)
                } else {
                    t.selection_bg_unfocused.unwrap_or(v.selection_bg_unfocused)
                });
                ctx.begin_path();
                ctx.rect(sx, hl_bot - hl_h * 0.1, sw, hl_h);
                ctx.fill();
            }
        }

        // ── Text or placeholder ───────────────────────────────────────────
        if text.is_empty() && !self.focused {
            ctx.set_fill_color(t.placeholder_color.unwrap_or(v.text_dim));
            ctx.fill_text(&self.placeholder, text_x, baseline_y);
        } else {
            ctx.set_fill_color(t.text_color.unwrap_or(v.text_color));
            ctx.fill_text(&text, text_x, baseline_y);
        }

        // Cursor draws in `paint_overlay` — skipped here so blink
        // state doesn't force the cache to re-raster twice per second.

        ctx.reset_clip();

        // ── Border ────────────────────────────────────────────────────────
        let border_color = if self.focused {
            t.border_color_focused.unwrap_or(v.accent)
        } else if self.hovered {
            t.border_color_hovered.unwrap_or(v.widget_stroke_active)
        } else {
            t.border_color.unwrap_or(v.widget_stroke)
        };
        ctx.set_stroke_color(border_color);
        let border_w = if self.focused { 2.0 } else { 1.0 };
        ctx.set_line_width(border_w);
        ctx.begin_path();
        let inset = border_w * 0.5;
        ctx.rounded_rect(
            inset,
            inset,
            (w - border_w).max(0.0),
            (h - border_w).max(0.0),
            r,
        );
        ctx.stroke();
    }

    /// Cursor overlay — runs AFTER the cache blit on every frame, so
    /// blink-phase flips don't invalidate the backbuffer.  Reads the
    /// same edit state `paint()` does so cursor lands on the glyph the
    /// cached text shows.
    fn paint_overlay(&mut self, ctx: &mut dyn DrawCtx) {
        // Record the blink phase being drawn this frame.  The next tree
        // walk's `needs_draw` will compare against this and report dirty
        // once wall-clock time crosses the next 500 ms boundary — no
        // host-side deadline bookkeeping, the widget drives itself.
        if self.focused {
            if let Some(t) = self.focus_time {
                let phase = (t.elapsed().as_millis() / 500) as u64;
                self.blink_last_phase.set(phase);
            }
        }

        let cursor_visible = self.focused
            && {
                let st = self.edit.borrow();
                st.cursor == st.anchor
            }
            && match self.focus_time {
                Some(t) => (t.elapsed().as_millis() / 500) % 2 == 0,
                None => false,
            };
        if !cursor_visible {
            return;
        }

        let (text, cursor) = {
            let st = self.edit.borrow();
            let text = if self.password_mode {
                const BULLET: char = '';
                let n = st.text.chars().count();
                BULLET.to_string().repeat(n)
            } else {
                st.text.clone()
            };
            let cursor = if self.password_mode {
                const BULLET_LEN: usize = 3;
                st.text[..st.cursor].chars().count() * BULLET_LEN
            } else {
                st.cursor
            };
            (text, cursor)
        };

        let h = self.bounds.height;
        let pad = self.padding;
        let v = ctx.visuals();

        let font = self.active_font();
        ctx.set_font(Arc::clone(&font));
        ctx.set_font_size(self.font_size);
        let m = ctx.measure_text("Ag").unwrap_or_default();
        let baseline_y = h * 0.5 - (m.ascent - m.descent) * 0.5;
        let text_x = pad - self.scroll_x;
        let cx = text_x + measure_advance(&font, &text[..cursor], self.font_size);
        let top = baseline_y + m.ascent;
        let bot = baseline_y - m.descent;

        // Clip to the text area so the cursor can't spill past the
        // padding or the border.
        ctx.save();
        ctx.clip_rect(pad, 0.0, (self.bounds.width - pad * 2.0).max(0.0), h);
        ctx.set_stroke_color(self.theme.cursor_color.unwrap_or(v.accent));
        ctx.set_line_width(1.5);
        ctx.begin_path();
        ctx.move_to(cx, bot);
        ctx.line_to(cx, top);
        ctx.stroke();
        ctx.restore();
    }

    fn on_event(&mut self, event: &Event) -> EventResult {
        match event {
            Event::MouseMove { pos } => {
                let was = self.hovered;
                self.hovered = self.hit_test(*pos);
                if self.mouse_down && self.focused {
                    let tx = pos.x - self.padding + self.scroll_x;
                    let text = self.edit.borrow().text.clone();
                    let new_cur = self.click_to_cursor(&text, tx);
                    self.edit.borrow_mut().cursor = new_cur;
                    crate::animation::request_draw();
                    return EventResult::Consumed;
                }
                if was != self.hovered {
                    crate::animation::request_draw();
                    return EventResult::Consumed;
                }
                EventResult::Ignored
            }

            Event::MouseDown {
                pos,
                button: MouseButton::Left,
                modifiers: mods,
            } => {
                self.mouse_down = true;
                let tx = pos.x - self.padding + self.scroll_x;
                let text = self.edit.borrow().text.clone();
                let new_cur = self.click_to_cursor(&text, tx);

                // Double-click: select word
                let is_double = self
                    .last_click_time
                    .map(|t| t.elapsed().as_millis() < 350)
                    .unwrap_or(false);
                self.last_click_time = Some(Instant::now());

                if is_double && !mods.shift {
                    let (ws, we) = word_range_at(&text, new_cur);
                    self.edit.borrow_mut().anchor = ws;
                    self.edit.borrow_mut().cursor = we;
                } else if mods.shift {
                    self.edit.borrow_mut().cursor = new_cur;
                } else {
                    self.edit.borrow_mut().cursor = new_cur;
                    self.edit.borrow_mut().anchor = new_cur;
                }
                // Reset blink phase on click so cursor is immediately visible.
                self.focus_time = Some(Instant::now());
                crate::animation::request_draw();
                EventResult::Consumed
            }

            Event::MouseUp {
                button: MouseButton::Left,
                ..
            } => {
                self.mouse_down = false;
                EventResult::Ignored
            }

            Event::FocusGained => {
                self.focused = true;
                self.focus_time = Some(Instant::now());
                self.text_on_focus = self.text();
                if self.select_all_on_focus {
                    let len = self.edit.borrow().text.len();
                    self.edit.borrow_mut().anchor = 0;
                    self.edit.borrow_mut().cursor = len;
                }
                crate::animation::request_draw();
                EventResult::Ignored
            }

            Event::FocusLost => {
                let was_focused = self.focused;
                self.focused = false;
                self.focus_time = None;
                self.mouse_down = false;
                self.flush_pending();
                if self.text() != self.text_on_focus {
                    self.notify_edit_complete();
                }
                if was_focused {
                    crate::animation::request_draw();
                }
                EventResult::Ignored
            }

            Event::KeyDown { key, modifiers } if self.focused => {
                // Reset blink on any keypress so cursor is visible immediately.
                self.focus_time = Some(Instant::now());
                let result = self.handle_key(key, *modifiers);
                // Any text-editing keystroke that reached the focused field
                // visibly mutates the text / cursor / selection; repaint.
                if result == EventResult::Consumed {
                    crate::animation::request_draw();
                }
                result
            }

            _ => EventResult::Ignored,
        }
    }
}