diff --git a/Cargo.toml b/Cargo.toml index 638c871..1893d2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -23,7 +23,6 @@ serde = { version = "1.0.219", features = ["derive"], optional = true } [dev-dependencies] grafo = "0.9" -#grafo = { path = "../grafo" } winit = "0.30" futures = "0.3" env_logger = "0.11" diff --git a/src/lib.rs b/src/lib.rs index e819fac..1e033ea 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -57,8 +57,9 @@ //! let inner_size = state.inner_size(); //! } //! +//! let mut remove_ids = vec![]; //! // Optional: going to remove all states that were not accessed during the current frame -//! text_manager.end_frame(); +//! text_manager.end_frame(&mut remove_ids); //! ``` mod action; diff --git a/src/text_manager.rs b/src/text_manager.rs index ffad47b..07376ca 100644 --- a/src/text_manager.rs +++ b/src/text_manager.rs @@ -152,6 +152,7 @@ impl TextManager { /// Utility to do some simple garbage collection of text states if you don't want /// to implement a usage tracker yourself. Call this at the end of each frame, and this will /// remove any text states not marked as accessed since the last call to `start_frame`. + /// Accepts a mutable vector to which it will append the IDs of removed text states. /// /// This helps prevent memory leaks when text states are no longer needed. /// @@ -161,13 +162,19 @@ impl TextManager { /// /// let mut manager: TextManager<()> = TextManager::new(); /// + /// let mut removed_ids = Vec::new(); /// // At the end of each frame - /// manager.end_frame(); + /// manager.end_frame(&mut removed_ids); /// ``` - pub fn end_frame(&mut self) { + pub fn end_frame(&mut self, removed_ids: &mut Vec) { let accessed_states = self.text_context.usage_tracker.accessed_states(); - self.text_states - .retain(|id, _| accessed_states.contains(id)); + self.text_states.retain(|id, _| { + let accessed = accessed_states.contains(id); + if !accessed { + removed_ids.push(*id); + } + accessed + }); } /// Sets the global scale factor used for shaping and rasterization.