egui_selectable_table/auto_reload.rs
1use std::hash::Hash;
2
3use crate::{ColumnOperations, ColumnOrdering, SelectableTable};
4
5#[derive(Default)]
6pub struct AutoReload {
7 pub reload_after: Option<u32>,
8 pub reload_count: u32,
9}
10
11impl AutoReload {
12 /// Increase the current reload count and return bool based on if it is equal or above the count it is
13 /// supposed to reload at
14 pub(crate) const fn increment_count(&mut self) -> bool {
15 self.reload_count += 1;
16 if let Some(count) = self.reload_after {
17 let reload = self.reload_count >= count;
18 if reload {
19 self.reload_count = 0;
20 }
21 reload
22 } else {
23 false
24 }
25 }
26}
27
28/// Enables or configures auto-reloading behavior in the table view.
29impl<Row, F, Conf> SelectableTable<Row, F, Conf>
30where
31 Row: Clone + Send + Sync,
32 F: Eq
33 + Hash
34 + Clone
35 + Ord
36 + Send
37 + Sync
38 + Default
39 + ColumnOperations<Row, F, Conf>
40 + ColumnOrdering<Row>,
41 Conf: Default,
42{
43 /// Enable automatic row recreation in the UI after a specified number of modifications or new rows.
44 ///
45 /// This function configures the table to automatically recreate its displayed rows when a certain
46 /// number of new rows or row modifications have been made. Can be useful without having to
47 /// manually keep track of modifications or when to reload.
48 ///
49 /// # Parameters:
50 /// - `count`: The number of updates (inserts or modifications) that will trigger an automatic row recreation.
51 /// - If `count` is high, the table will refresh less frequently, leading to potentially outdated rows being shown in the UI for longer.
52 /// - If `count` is too low, frequent row recreation may result in performance overhead.
53 ///
54 /// # Considerations:
55 /// - Tune the `count` value based on the expected rate of updates. For instance, if new rows or modifications
56 /// occur at a rate of 1000 rows per second, a `count` between 500 and 1000 may offer the best balance between
57 /// performance and up-to-date display.
58 ///
59 /// # When to use:
60 /// This can be useful when streaming table data from a source and it gets updated
61 /// frequently. If for example 1k rows are added/modified per second, it might be a good idea
62 /// to set the count at 5k so it auto reloads the table every 5 seconds. This
63 /// allows not having to keep track of when to reload the table and reloading once at the end
64 /// of stream can result in the table showing the latest data.
65 ///
66 /// # Example:
67 /// ```rust,ignore
68 /// let table = SelectableTable::new(vec![col1, col2, col3])
69 /// .config(my_config).auto_reload(Some(500));
70 /// ```
71 #[must_use]
72 pub const fn auto_reload(mut self, count: u32) -> Self {
73 self.auto_reload.reload_after = Some(count);
74 self.auto_reload.reload_count = 0;
75 self
76 }
77 /// Manually set the auto-reload threshold. This lets you change the threshold dynamically.
78 ///
79 /// # Parameters:
80 /// - `count`: Optionally specify how many updates (new or modified rows) should occur before rows are automatically recreated.
81 /// - If `None` is provided, auto-reloading is disabled.
82 ///
83 /// # Example:
84 /// ```rust,ignore
85 /// table.set_auto_reload(Some(1000)); // Reload after 1000 updates.
86 /// table.set_auto_reload(None); // Disable auto-reloading.
87 /// ```
88 pub const fn set_auto_reload(&mut self, count: Option<u32>) {
89 self.auto_reload.reload_after = count;
90 self.auto_reload.reload_count = 0;
91 }
92}