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