rust_widgets 0.9.6

Pure Rust cross-platform native GUI library with hardware-adaptive rendering, 60+ widgets, touch/gesture support, i18n, and SVG-pipeline-accurate output
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
//! SearchBar widget — iOS-style search bar with search icon, clear button,
//! and optional cancel button when active.
//!
//! Unlike the existing SearchBox, this widget is designed for mobile-style
//! search UX with active state management and a cancel button.

use crate::core::{Color, Font, Point, Rect};
use crate::event::{Event, EventHandler};
use crate::render::RenderContext;
use crate::signal::{GenericSignal, Signal1};
use crate::widget::{BaseWidget, Draw, Widget, WidgetKind};

/// iOS-style search bar widget.
///
/// Displays a rounded search field with a search icon on the left, optional
/// clear button when text is present, and a cancel button on the right when
/// the search bar is active.
pub struct SearchBar {
    base: BaseWidget,
    text: String,
    placeholder: String,
    is_active: bool,
    cancel_button_visible: bool,
    /// Emitted when the text content changes.
    pub text_changed: Signal1<String>,
    /// Emitted when the user submits a search (Enter key).
    pub search_submitted: Signal1<String>,
    /// Emitted when the user taps the cancel button.
    pub canceled: GenericSignal,
}

impl SearchBar {
    /// Creates a new SearchBar widget with the given geometry.
    pub fn new(geometry: Rect) -> Self {
        Self {
            base: BaseWidget::new(WidgetKind::SearchBar, geometry, "SearchBar"),
            text: String::new(),
            placeholder: "Search".to_string(),
            is_active: false,
            cancel_button_visible: true,
            text_changed: Signal1::new(),
            search_submitted: Signal1::new(),
            canceled: GenericSignal::new(),
        }
    }

    /// Returns the current search text.
    pub fn text(&self) -> &str {
        &self.text
    }

    /// Sets the search text. Emits `text_changed` signal.
    pub fn set_text(&mut self, text: impl Into<String>) {
        let new_text = text.into();
        if self.text != new_text {
            self.text = new_text.clone();
            self.text_changed.emit(new_text);
            self.base.request_redraw();
        }
    }

    /// Returns the placeholder text.
    pub fn placeholder(&self) -> &str {
        &self.placeholder
    }

    /// Sets the placeholder text.
    pub fn set_placeholder(&mut self, placeholder: impl Into<String>) {
        self.placeholder = placeholder.into();
        self.base.request_redraw();
    }

    /// Sets whether the search bar is active (showing cancel button).
    pub fn set_active(&mut self, active: bool) {
        if self.is_active != active {
            self.is_active = active;
            self.base.request_redraw();
        }
    }

    /// Returns whether the search bar is active.
    pub fn is_active(&self) -> bool {
        self.is_active
    }

    /// Sets whether the cancel button is visible when active.
    pub fn set_cancel_button_visible(&mut self, visible: bool) {
        self.cancel_button_visible = visible;
        self.base.request_redraw();
    }

    /// Returns whether the cancel button is visible when active.
    pub fn cancel_button_visible(&self) -> bool {
        self.cancel_button_visible
    }

    /// Clears the search text. Emits `text_changed` signal.
    pub fn clear(&mut self) {
        if !self.text.is_empty() {
            self.text.clear();
            self.text_changed.emit(String::new());
            self.base.request_redraw();
        }
    }

    /// Submits the current search text.
    fn submit(&mut self) {
        let text = self.text.clone();
        if !text.is_empty() {
            self.search_submitted.emit(text);
        }
    }
}

impl Widget for SearchBar {
    fn base(&self) -> &BaseWidget {
        &self.base
    }
    fn base_mut(&mut self) -> &mut BaseWidget {
        &mut self.base
    }
}

impl Draw for SearchBar {
    fn draw(&mut self, context: &mut RenderContext) {
        let rect = self.geometry();
        let is_enabled = self.base.is_enabled();
        let cancel_width: u32 = if self.is_active && self.cancel_button_visible { 60 } else { 0 };
        let field_width = rect.width.saturating_sub(cancel_width + 4);

        // Cancel button area
        let cancel_x = rect.x + rect.width as i32 - cancel_width as i32;
        if self.is_active && self.cancel_button_visible {
            let _cancel_rect = Rect::new(cancel_x, rect.y, cancel_width, rect.height);
            let cancel_font = Font::simple("sans-serif", 14.0);
            let cancel_text = "Cancel";
            let metrics = context.measure_text(cancel_text, &cancel_font);
            let text_x = cancel_x + (cancel_width as i32 - metrics.width as i32) / 2;
            let text_y =
                rect.y + (rect.height as i32 - metrics.height as i32) / 2 + metrics.ascent as i32;
            context.draw_text(
                Point::new(text_x, text_y),
                cancel_text,
                &cancel_font,
                Color::rgba(52, 120, 246, 255),
            );
        }

        // Search field background
        let field_rect = Rect::new(rect.x, rect.y, field_width, rect.height);
        let field_color = if !is_enabled {
            Color::rgba(235, 235, 235, 200)
        } else {
            Color::rgba(200, 200, 205, 200)
        };
        let corner_radius = rect.height / 2;
        context.fill_rounded_rect(field_rect, corner_radius, field_color);

        // Search icon (magnifying glass)
        let icon_size: u32 = 14;
        let icon_left = rect.x + 10;
        let icon_top = rect.y + (rect.height as i32 - icon_size as i32) / 2;
        let icon_color = Color::rgba(140, 140, 140, 255);
        context.draw_circle_stroke(
            Point::new(icon_left + (icon_size / 2) as i32, icon_top + (icon_size / 2) as i32),
            icon_size / 2,
            icon_color,
            2,
        );
        // Search icon handle (diagonal line from bottom-right of circle)
        let handle_offset = icon_size as i32;
        context.draw_line(
            Point::new(icon_left + handle_offset - 2, icon_top + handle_offset - 2),
            Point::new(icon_left + handle_offset + 3, icon_top + handle_offset + 3),
            icon_color,
        );

        // Text or placeholder
        let text_left = icon_left + icon_size as i32 + 6;
        let text_width = if self.text.is_empty() {
            field_width.saturating_sub((text_left - rect.x) as u32)
        } else {
            field_width.saturating_sub((text_left - rect.x) as u32 + 24)
        };
        let font = Font::simple("sans-serif", 14.0);

        if self.text.is_empty() {
            // Draw placeholder text
            let metrics = context.measure_text(&self.placeholder, &font);
            if text_width >= metrics.width as u32 {
                let text_y = rect.y
                    + (rect.height as i32 - metrics.height as i32) / 2
                    + metrics.ascent as i32;
                context.draw_text(
                    Point::new(text_left, text_y),
                    &self.placeholder,
                    &font,
                    Color::rgba(160, 160, 160, 255),
                );
            }
        } else {
            // Draw text
            let metrics = context.measure_text(&self.text, &font);
            if text_width >= metrics.width as u32 {
                let text_y = rect.y
                    + (rect.height as i32 - metrics.height as i32) / 2
                    + metrics.ascent as i32;
                context.draw_text(
                    Point::new(text_left, text_y),
                    &self.text,
                    &font,
                    Color::rgba(40, 40, 40, 255),
                );
            }

            // Draw clear button (X)
            let clear_x = rect.x + field_width as i32 - 22;
            let clear_y = rect.y + (rect.height as i32 - 16) / 2;
            let clear_center = Point::new(clear_x + 8, clear_y + 8);
            context.fill_circle(clear_center, 8, Color::rgba(180, 180, 180, 200));
            // X lines
            context.draw_line(
                Point::new(clear_center.x - 3, clear_center.y - 3),
                Point::new(clear_center.x + 3, clear_center.y + 3),
                Color::WHITE,
            );
            context.draw_line(
                Point::new(clear_center.x + 3, clear_center.y - 3),
                Point::new(clear_center.x - 3, clear_center.y + 3),
                Color::WHITE,
            );
        }
    }
}

impl EventHandler for SearchBar {
    fn handle_event(&mut self, event: &Event) {
        if !self.base.is_enabled() {
            return;
        }
        match event {
            Event::MousePress { pos, button } => {
                if *button == 1 {
                    let rect = self.geometry();
                    let cancel_width: u32 =
                        if self.is_active && self.cancel_button_visible { 60 } else { 0 };
                    let cancel_x = rect.x + rect.width as i32 - cancel_width as i32;

                    // Cancel button click
                    if self.is_active && self.cancel_button_visible && pos.x >= cancel_x {
                        self.is_active = false;
                        self.canceled.emit();
                        self.base.request_redraw();
                        return;
                    }

                    // Clear button click (when text is non-empty)
                    let field_width = rect.width.saturating_sub(cancel_width + 4);
                    let clear_x = rect.x + field_width as i32 - 22;
                    if !self.text.is_empty()
                        && pos.x >= clear_x
                        && pos.x < clear_x + 16
                        && pos.y >= rect.y
                        && pos.y < rect.y + rect.height as i32
                    {
                        self.clear();
                        return;
                    }

                    // Focus the search bar
                    if !self.is_active {
                        self.set_active(true);
                    }
                }
            }
            Event::KeyPress { key, modifiers: _ } => {
                if *key == 13 || *key == 10 {
                    // Enter key — submit search
                    self.submit();
                } else if *key == 8 {
                    // Backspace — remove last character
                    if !self.text.is_empty() {
                        let mut chars: Vec<char> = self.text.chars().collect();
                        chars.pop();
                        self.set_text(chars.into_iter().collect::<String>());
                    }
                } else if *key >= 32 && *key <= 126 {
                    // Printable ASCII — append character
                    if let Some(c) = char::from_u32(*key) {
                        let mut new_text = self.text.clone();
                        new_text.push(c);
                        self.set_text(new_text);
                    }
                }
            }
            _ => {
                self.base.handle_event(event);
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::core::Point;
    use crate::widget::svg::render_to_svg;
    use std::sync::atomic::{AtomicBool, Ordering};
    use std::sync::{Arc, Mutex};

    fn make_search_bar() -> SearchBar {
        SearchBar::new(Rect::new(0, 0, 300, 40))
    }

    #[test]
    fn search_bar_default_state() {
        let sb = make_search_bar();
        assert_eq!(sb.text(), "");
        assert_eq!(sb.placeholder(), "Search");
        assert!(!sb.is_active());
        assert!(sb.cancel_button_visible());
        assert_eq!(sb.kind(), WidgetKind::SearchBar);
    }

    #[test]
    fn search_bar_set_text() {
        let mut sb = make_search_bar();
        sb.set_text("hello");
        assert_eq!(sb.text(), "hello");
    }

    #[test]
    fn search_bar_text_changed_signal() {
        let mut sb = make_search_bar();
        let captured = Arc::new(Mutex::new(None));
        sb.text_changed.connect({
            let captured = Arc::clone(&captured);
            move |val: Arc<String>| {
                *captured.lock().unwrap() = Some((*val).clone());
            }
        });

        sb.set_text("test");
        assert_eq!(captured.lock().unwrap().as_deref(), Some("test"));
    }

    #[test]
    fn search_bar_submit_signal() {
        let mut sb = make_search_bar();
        sb.set_text("query");
        let captured = Arc::new(Mutex::new(None));
        sb.search_submitted.connect({
            let captured = Arc::clone(&captured);
            move |val: Arc<String>| {
                *captured.lock().unwrap() = Some((*val).clone());
            }
        });

        // Simulate Enter key press
        sb.handle_event(&Event::KeyPress { key: 13, modifiers: 0 });
        assert_eq!(captured.lock().unwrap().as_deref(), Some("query"));
    }

    #[test]
    fn search_bar_clear_button_and_signal() {
        let mut sb = make_search_bar();
        sb.set_text("hello");
        let captured = Arc::new(Mutex::new(None));
        sb.text_changed.connect({
            let captured = Arc::clone(&captured);
            move |val: Arc<String>| {
                *captured.lock().unwrap() = Some((*val).clone());
            }
        });

        sb.clear();
        assert_eq!(sb.text(), "");
        assert_eq!(captured.lock().unwrap().as_deref(), Some(""));
    }

    #[test]
    fn search_bar_cancel_signal() {
        let mut sb = make_search_bar();
        sb.set_active(true);
        let captured = Arc::new(AtomicBool::new(false));
        sb.canceled.connect({
            let captured = Arc::clone(&captured);
            move || {
                captured.store(true, Ordering::SeqCst);
            }
        });

        // Click cancel button (at x=240+, since width=300, cancel starts at 300-60=240)
        sb.handle_event(&Event::MousePress { pos: Point::new(260, 20), button: 1 });
        assert!(!sb.is_active());
        assert!(captured.load(Ordering::SeqCst));
    }

    #[test]
    fn search_bar_set_placeholder() {
        let mut sb = make_search_bar();
        sb.set_placeholder("Find...");
        assert_eq!(sb.placeholder(), "Find...");
    }

    #[test]
    fn search_bar_svg_output() {
        let mut sb = make_search_bar();
        sb.set_text("foo");
        let svg = render_to_svg(&mut sb);
        assert!(svg.starts_with("<svg"));
        assert!(svg.ends_with("</svg>"));
    }
}