pub struct Memory {
pub options: Options,
pub data: IdTypeMap,
pub caches: CacheStorage,
/* private fields */
}
Expand description
The data that egui persists between frames.
This includes window positions and sizes,
how far the user has scrolled in a ScrollArea
etc.
If you want this to persist when closing your app you should serialize Memory
and store it.
For this you need to enable the persistence
.
If you want to store data for your widgets, you should look at Memory::data
Fields
options: Options
data: IdTypeMap
This map stores some superficial state for all widgets with custom Id
s.
This includes storing if a crate::CollapsingHeader
is open, how far scrolled a
crate::ScrollArea
is, where the cursor in a crate::TextEdit
is, etc.
This is NOT meant to store any important data. Store that in your own structures!
Each read clones the data, so keep your values cheap to clone.
If you want to store a lot of data you should wrap it in Arc<Mutex<…>>
so it is cheap to clone.
This will be saved between different program runs if you use the persistence
feature.
To store a state common for all your widgets (a singleton), use Id::null
as the key.
caches: CacheStorage
Can be used to cache computations from one frame to another.
This is for saving CPU when you have something that may take 1-100ms to compute. Things that are very slow (>100ms) should instead be done async (i.e. in another thread) so as not to lock the UI thread.
use egui::util::cache::{ComputerMut, FrameCache};
#[derive(Default)]
struct CharCounter {}
impl ComputerMut<&str, usize> for CharCounter {
fn compute(&mut self, s: &str) -> usize {
s.chars().count() // you probably want to cache something more expensive than this
}
}
type CharCountCache<'a> = FrameCache<usize, CharCounter>;
let mut memory = ctx.memory();
let cache = memory.caches.cache::<CharCountCache<'_>>();
assert_eq!(cache.get("hello"), 5);
Implementations
sourceimpl Memory
impl Memory
sourcepub fn layer_id_at(
&self,
pos: Pos2,
resize_interact_radius_side: f32
) -> Option<LayerId>
pub fn layer_id_at(
&self,
pos: Pos2,
resize_interact_radius_side: f32
) -> Option<LayerId>
Top-most layer at the given position.
sourcepub fn layer_ids(&self) -> impl ExactSizeIterator<Item = LayerId> + '_
pub fn layer_ids(&self) -> impl ExactSizeIterator<Item = LayerId> + '_
An iterator over all layers. Back-to-front. Top is last.
sourcepub fn has_focus(&self, id: Id) -> bool
pub fn has_focus(&self, id: Id) -> bool
Does this widget have keyboard focus?
This function does not consider whether the UI as a whole (e.g. window) has the keyboard focus. That makes this function suitable for deciding widget state that should not be disrupted if the user moves away from the window and back.
sourcepub fn lock_focus(&mut self, id: Id, lock_focus: bool)
pub fn lock_focus(&mut self, id: Id, lock_focus: bool)
Prevent keyboard focus from moving away from this widget even if users presses the tab key. You must first give focus to the widget before calling this.
sourcepub fn has_lock_focus(&mut self, id: Id) -> bool
pub fn has_lock_focus(&mut self, id: Id) -> bool
Is the keyboard focus locked on this widget? If so the focus won’t move even if the user presses the tab key.
sourcepub fn request_focus(&mut self, id: Id)
pub fn request_focus(&mut self, id: Id)
Give keyboard focus to a specific widget.
See also crate::Response::request_focus
.
sourcepub fn surrender_focus(&mut self, id: Id)
pub fn surrender_focus(&mut self, id: Id)
Surrender keyboard focus for a specific widget.
See also crate::Response::surrender_focus
.
sourcepub fn stop_text_input(&mut self)
pub fn stop_text_input(&mut self)
Stop editing of active TextEdit
(if any).
pub fn is_anything_being_dragged(&self) -> bool
pub fn is_being_dragged(&self, id: Id) -> bool
pub fn set_dragged_id(&mut self, id: Id)
sourcepub fn reset_areas(&mut self)
pub fn reset_areas(&mut self)
Forget window positions, sizes etc. Can be used to auto-layout windows.
sourceimpl Memory
impl Memory
Popups
Popups are things like combo-boxes, color pickers, menus etc. Only one can be be open at a time.
pub fn is_popup_open(&self, popup_id: Id) -> bool
pub fn open_popup(&mut self, popup_id: Id)
pub fn close_popup(&mut self)
pub fn toggle_popup(&mut self, popup_id: Id)
sourcepub fn everything_is_visible(&self) -> bool
pub fn everything_is_visible(&self) -> bool
If true, all windows, menus, tooltips etc are to be visible at once.
This is useful for testing, benchmarking, pre-caching, etc.
Experimental feature!
sourcepub fn set_everything_is_visible(&mut self, value: bool)
pub fn set_everything_is_visible(&mut self, value: bool)
If true, all windows, menus, tooltips etc are to be visible at once.
This is useful for testing, benchmarking, pre-caching, etc.
Experimental feature!
Trait Implementations
sourceimpl<'de> Deserialize<'de> for Memory where
Memory: Default,
impl<'de> Deserialize<'de> for Memory where
Memory: Default,
sourcefn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error> where
__D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
Auto Trait Implementations
impl !RefUnwindSafe for Memory
impl Send for Memory
impl Sync for Memory
impl Unpin for Memory
impl !UnwindSafe for Memory
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more