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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
use alloc::collections::VecDeque;
use std::time::{SystemTime, UNIX_EPOCH};
const MAX_HISTORY_ENTRIES: usize = 100;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct HistoryEntry {
    pub url: String,
    pub title: String,
    pub visit_count: u32,
    pub last_visit: u64,
}
impl HistoryEntry {
    pub fn new(url: String, title: String) -> Self {
        Self {
            url,
            title,
            visit_count: 1,
            last_visit: SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs(),
        }
    }
    pub fn touch(&mut self) {
        self.visit_count += 1;
        self.last_visit =
            SystemTime::now().duration_since(UNIX_EPOCH).unwrap_or_default().as_secs();
    }
}
#[derive(Debug, Clone, Default)]
pub struct BrowserHistory {
    entries: VecDeque<HistoryEntry>,
    max_entries: usize,
}
impl BrowserHistory {
    pub fn new() -> Self {
        Self::with_capacity(MAX_HISTORY_ENTRIES)
    }
    pub fn with_capacity(max_entries: usize) -> Self {
        Self { entries: VecDeque::with_capacity(max_entries), max_entries }
    }
    pub fn add_entry(&mut self, url: String, title: String) {
        if let Some(existing) = self.entries.iter_mut().find(|e| e.url == url) {
            existing.touch();
            return;
        }
        if self.entries.len() >= self.max_entries {
            self.entries.pop_front();
        }
        self.entries.push_back(HistoryEntry::new(url, title));
    }
    pub fn remove_entry(&mut self, url: &str) -> bool {
        if let Some(pos) = self.entries.iter().position(|e| e.url == url) {
            self.entries.remove(pos);
            true
        } else {
            false
        }
    }
    pub fn clear(&mut self) {
        self.entries.clear();
    }
    pub fn entries(&self) -> &VecDeque<HistoryEntry> {
        &self.entries
    }
    pub fn search(&self, query: &str) -> Vec<&HistoryEntry> {
        let query_lower = query.to_lowercase();
        self.entries
            .iter()
            .filter(|e| {
                e.url.to_lowercase().contains(&query_lower)
                    || e.title.to_lowercase().contains(&query_lower)
            })
            .collect()
    }
    pub fn most_visited(&self, limit: usize) -> Vec<&HistoryEntry> {
        let mut entries: Vec<_> = self.entries.iter().collect();
        entries.sort_by_key(|b| std::cmp::Reverse(b.visit_count));
        entries.into_iter().take(limit).collect()
    }
    pub fn recent(&self, limit: usize) -> Vec<&HistoryEntry> {
        let mut entries: Vec<_> = self.entries.iter().collect();
        entries.sort_by_key(|b| std::cmp::Reverse(b.last_visit));
        entries.into_iter().take(limit).collect()
    }
    pub fn len(&self) -> usize {
        self.entries.len()
    }
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }
}
#[derive(Debug, Clone)]
pub struct SessionHistory {
    back_stack: VecDeque<String>,
    forward_stack: VecDeque<String>,
    current: Option<String>,
    max_size: usize,
}
impl Default for SessionHistory {
    fn default() -> Self {
        Self::new(50)
    }
}
impl SessionHistory {
    pub fn new(max_size: usize) -> Self {
        Self {
            back_stack: VecDeque::with_capacity(max_size),
            forward_stack: VecDeque::with_capacity(max_size),
            current: None,
            max_size,
        }
    }
    pub fn navigate(&mut self, url: String) {
        if let Some(current) = self.current.take() {
            if self.back_stack.len() >= self.max_size {
                self.back_stack.pop_front();
            }
            self.back_stack.push_back(current);
        }
        self.forward_stack.clear();
        self.current = Some(url);
    }
    pub fn can_go_back(&self) -> bool {
        !self.back_stack.is_empty()
    }
    pub fn can_go_forward(&self) -> bool {
        !self.forward_stack.is_empty()
    }
    pub fn go_back(&mut self) -> Option<String> {
        if !self.can_go_back() {
            return None;
        }
        if let Some(current) = self.current.take() {
            if self.forward_stack.len() >= self.max_size {
                self.forward_stack.pop_front();
            }
            self.forward_stack.push_back(current);
        }
        self.current = self.back_stack.pop_back();
        self.current.clone()
    }
    pub fn go_forward(&mut self) -> Option<String> {
        if !self.can_go_forward() {
            return None;
        }
        if let Some(current) = self.current.take() {
            if self.back_stack.len() >= self.max_size {
                self.back_stack.pop_front();
            }
            self.back_stack.push_back(current);
        }
        self.current = self.forward_stack.pop_front();
        self.current.clone()
    }
    pub fn current(&self) -> Option<&String> {
        self.current.as_ref()
    }
    pub fn back_entries(&self) -> &VecDeque<String> {
        &self.back_stack
    }
    pub fn forward_entries(&self) -> &VecDeque<String> {
        &self.forward_stack
    }
    pub fn clear(&mut self) {
        self.back_stack.clear();
        self.forward_stack.clear();
        self.current = None;
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::time::Duration;

    // ── HistoryEntry tests ──

    #[test]
    fn test_history_entry_new() {
        let entry = HistoryEntry::new("https://example.com".to_string(), "Example".to_string());
        assert_eq!(entry.url, "https://example.com");
        assert_eq!(entry.title, "Example");
        assert_eq!(entry.visit_count, 1);
        assert!(entry.last_visit > 1_600_000_000);
    }

    #[test]
    fn test_history_entry_touch() {
        let mut entry = HistoryEntry::new("https://example.com".to_string(), "Example".to_string());
        let first_visit = entry.last_visit;
        std::thread::sleep(Duration::from_millis(2));
        entry.touch();
        assert_eq!(entry.visit_count, 2);
        assert!(entry.last_visit >= first_visit);
    }

    // ── BrowserHistory tests ──

    #[test]
    fn test_browser_history_new() {
        let history = BrowserHistory::new();
        assert!(history.is_empty());
        assert_eq!(history.len(), 0);
    }

    #[test]
    fn test_browser_history_with_capacity() {
        let history = BrowserHistory::with_capacity(5);
        assert!(history.is_empty());
        assert_eq!(history.len(), 0);
    }

    #[test]
    fn test_browser_history_add_entry() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://example.com".to_string(), "Example".to_string());
        assert_eq!(history.len(), 1);
        assert!(!history.is_empty());
    }

    #[test]
    fn test_browser_history_add_duplicate_touches() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://example.com".to_string(), "Example".to_string());
        history.add_entry("https://example.com".to_string(), "Example".to_string());
        // Duplicate URL should increment visit_count, not add another entry
        assert_eq!(history.len(), 1);
        let entries = history.entries();
        let entry = entries.front().unwrap();
        assert_eq!(entry.visit_count, 2);
    }

    #[test]
    fn test_browser_history_add_multiple() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://a.com".to_string(), "A".to_string());
        history.add_entry("https://b.com".to_string(), "B".to_string());
        history.add_entry("https://c.com".to_string(), "C".to_string());
        assert_eq!(history.len(), 3);
    }

    #[test]
    fn test_browser_history_remove_entry() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://example.com".to_string(), "Example".to_string());
        assert!(history.remove_entry("https://example.com"));
        assert!(history.is_empty());
        // Removing a non-existent entry returns false
        assert!(!history.remove_entry("https://nonexistent.com"));
    }

    #[test]
    fn test_browser_history_clear() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://a.com".to_string(), "A".to_string());
        history.add_entry("https://b.com".to_string(), "B".to_string());
        history.clear();
        assert!(history.is_empty());
        assert_eq!(history.len(), 0);
    }

    #[test]
    fn test_browser_history_entries() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://a.com".to_string(), "A".to_string());
        history.add_entry("https://b.com".to_string(), "B".to_string());
        let entries = history.entries();
        assert_eq!(entries.len(), 2);
        assert_eq!(entries[0].url, "https://a.com");
        assert_eq!(entries[1].url, "https://b.com");
    }

    #[test]
    fn test_browser_history_search_by_url() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://example.com".to_string(), "Example".to_string());
        history.add_entry("https://rust-lang.org".to_string(), "Rust".to_string());
        let results = history.search("rust");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].url, "https://rust-lang.org");
    }

    #[test]
    fn test_browser_history_search_by_title() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://example.com".to_string(), "Example Page".to_string());
        history.add_entry("https://other.com".to_string(), "Other Site".to_string());
        let results = history.search("page");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].url, "https://example.com");
    }

    #[test]
    fn test_browser_history_search_case_insensitive() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://EXAMPLE.COM".to_string(), "Example".to_string());
        let results = history.search("example");
        assert_eq!(results.len(), 1);
    }

    #[test]
    fn test_browser_history_most_visited() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://a.com".to_string(), "A".to_string());
        history.add_entry("https://b.com".to_string(), "B".to_string());
        history.add_entry("https://a.com".to_string(), "A".to_string()); // visit_count = 2
        let top = history.most_visited(1);
        assert_eq!(top.len(), 1);
        assert_eq!(top[0].url, "https://a.com");
    }

    #[test]
    fn test_browser_history_most_visited_limit() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://a.com".to_string(), "A".to_string());
        history.add_entry("https://b.com".to_string(), "B".to_string());
        history.add_entry("https://c.com".to_string(), "C".to_string());
        let top = history.most_visited(2);
        assert_eq!(top.len(), 2);
    }

    #[test]
    fn test_browser_history_recent() {
        let mut history = BrowserHistory::new();
        history.add_entry("https://a.com".to_string(), "A".to_string());
        history.add_entry("https://b.com".to_string(), "B".to_string());
        let recent = history.recent(3);
        // Both entries should be returned when limit is high enough
        assert_eq!(recent.len(), 2);
        let urls: Vec<&str> = recent.iter().map(|e| e.url.as_str()).collect();
        assert!(urls.contains(&"https://a.com"));
        assert!(urls.contains(&"https://b.com"));
    }

    #[test]
    fn test_browser_history_max_entries_eviction() {
        let mut history = BrowserHistory::with_capacity(2);
        history.add_entry("https://a.com".to_string(), "A".to_string());
        history.add_entry("https://b.com".to_string(), "B".to_string());
        history.add_entry("https://c.com".to_string(), "C".to_string());
        assert_eq!(history.len(), 2);
        assert_eq!(history.entries().front().unwrap().url, "https://b.com");
    }

    // ── SessionHistory tests ──

    #[test]
    fn test_session_history_new() {
        let history = SessionHistory::new(10);
        assert!(history.current().is_none());
        assert!(!history.can_go_back());
        assert!(!history.can_go_forward());
    }

    #[test]
    fn test_session_history_default() {
        let history = SessionHistory::default();
        assert!(history.current().is_none());
    }

    #[test]
    fn test_session_history_navigate() {
        let mut history = SessionHistory::new(10);
        history.navigate("https://example.com".to_string());
        assert_eq!(history.current().unwrap(), "https://example.com");
        assert!(!history.can_go_back());
    }

    #[test]
    fn test_session_history_navigate_multiple() {
        let mut history = SessionHistory::new(10);
        history.navigate("https://page1.com".to_string());
        history.navigate("https://page2.com".to_string());
        assert!(history.can_go_back());
        assert_eq!(history.current().unwrap(), "https://page2.com");
    }

    #[test]
    fn test_session_history_go_back() {
        let mut history = SessionHistory::new(10);
        history.navigate("https://page1.com".to_string());
        history.navigate("https://page2.com".to_string());
        let back = history.go_back();
        assert_eq!(back.as_deref(), Some("https://page1.com"));
        assert!(!history.can_go_back());
        assert!(history.can_go_forward());
    }

    #[test]
    fn test_session_history_go_back_none_when_empty() {
        let mut history = SessionHistory::new(10);
        assert!(history.go_back().is_none());
    }

    #[test]
    fn test_session_history_go_forward() {
        let mut history = SessionHistory::new(10);
        history.navigate("https://page1.com".to_string());
        history.navigate("https://page2.com".to_string());
        history.go_back();
        let fwd = history.go_forward();
        assert_eq!(fwd.as_deref(), Some("https://page2.com"));
        assert!(!history.can_go_forward());
        assert!(history.can_go_back());
    }

    #[test]
    fn test_session_history_go_forward_none_when_empty() {
        let mut history = SessionHistory::new(10);
        assert!(history.go_forward().is_none());
    }

    #[test]
    fn test_session_history_navigate_clears_forward() {
        let mut history = SessionHistory::new(10);
        history.navigate("https://page1.com".to_string());
        history.navigate("https://page2.com".to_string());
        history.go_back();
        assert!(history.can_go_forward());
        // Navigating to a new URL should clear the forward stack
        history.navigate("https://page3.com".to_string());
        assert!(!history.can_go_forward());
        assert!(history.can_go_back());
        assert_eq!(history.current().unwrap(), "https://page3.com");
    }

    #[test]
    fn test_session_history_back_entries() {
        let mut history = SessionHistory::new(10);
        history.navigate("https://a.com".to_string());
        history.navigate("https://b.com".to_string());
        history.navigate("https://c.com".to_string());
        let back = history.back_entries();
        assert_eq!(back.len(), 2);
    }

    #[test]
    fn test_session_history_forward_entries() {
        let mut history = SessionHistory::new(10);
        history.navigate("https://a.com".to_string());
        history.navigate("https://b.com".to_string());
        history.go_back();
        let fwd = history.forward_entries();
        assert_eq!(fwd.len(), 1);
    }

    #[test]
    fn test_session_history_clear() {
        let mut history = SessionHistory::new(10);
        history.navigate("https://a.com".to_string());
        history.navigate("https://b.com".to_string());
        history.clear();
        assert!(history.current().is_none());
        assert!(!history.can_go_back());
        assert!(!history.can_go_forward());
    }

    #[test]
    fn test_session_history_max_size_back_stack() {
        let mut history = SessionHistory::new(2);
        history.navigate("https://a.com".to_string());
        history.navigate("https://b.com".to_string());
        history.navigate("https://c.com".to_string());
        // Back stack should be capped at 2
        assert_eq!(history.back_entries().len(), 2);
    }
}