Struct egui::Memory

source ·
pub struct Memory {
    pub options: Options,
    pub data: IdTypeMap,
    pub caches: CacheStorage,
    pub(crate) new_pixels_per_point: Option<f32>,
    pub(crate) new_font_definitions: Option<FontDefinitions>,
    pub(crate) interaction: Interaction,
    pub(crate) window_interaction: Option<WindowInteraction>,
    pub(crate) drag_value: MonoState,
    pub(crate) areas: Areas,
    popup: Option<Id>,
    everything_is_visible: bool,
}
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 Ids.

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>;

ctx.memory_mut(|mem| {
    let cache = mem.caches.cache::<CharCountCache<'_>>();
    assert_eq!(cache.get("hello"), 5);
});
§new_pixels_per_point: Option<f32>

new scale that will be applied at the start of the next frame

§new_font_definitions: Option<FontDefinitions>

new fonts that will be applied at the start of the next frame

§interaction: Interaction§window_interaction: Option<WindowInteraction>§drag_value: MonoState§areas: Areas§popup: Option<Id>

Which popup-window is open (if any)? Could be a combo box, color picker, menu etc.

§everything_is_visible: bool

Implementations§

source§

impl Memory

source

pub(crate) fn begin_frame( &mut self, prev_input: &InputState, new_input: &RawInput )

source

pub(crate) fn end_frame(&mut self, input: &InputState, used_ids: &IdMap<Rect>)

source

pub fn layer_id_at( &self, pos: Pos2, resize_interact_radius_side: f32 ) -> Option<LayerId>

Top-most layer at the given position.

source

pub fn layer_ids(&self) -> impl ExactSizeIterator<Item = LayerId> + '_

An iterator over all layers. Back-to-front. Top is last.

source

pub(crate) fn had_focus_last_frame(&self, id: Id) -> bool

source

pub(crate) fn lost_focus(&self, id: Id) -> bool

True if the given widget had keyboard focus last frame, but not this one.

source

pub(crate) fn gained_focus(&self, id: Id) -> bool

True if the given widget has keyboard focus this frame, but didn’t last frame.

source

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.

source

pub fn focus(&self) -> Option<Id>

Which widget has keyboard focus?

source

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.

source

pub fn has_lock_focus(&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.

source

pub fn request_focus(&mut self, id: Id)

Give keyboard focus to a specific widget. See also crate::Response::request_focus.

source

pub fn surrender_focus(&mut self, id: Id)

Surrender keyboard focus for a specific widget. See also crate::Response::surrender_focus.

source

pub fn interested_in_focus(&mut self, id: Id)

Register this widget as being interested in getting keyboard focus. This will allow the user to select it with tab and shift-tab. This is normally done automatically when handling interactions, but it is sometimes useful to pre-register interest in focus, e.g. before deciding which type of underlying widget to use, as in the crate::DragValue widget, so a widget can be focused and rendered correctly in a single frame.

source

pub fn stop_text_input(&mut self)

Stop editing of active TextEdit (if any).

source

pub fn is_anything_being_dragged(&self) -> bool

source

pub fn is_being_dragged(&self, id: Id) -> bool

source

pub fn set_dragged_id(&mut self, id: Id)

source

pub fn stop_dragging(&mut self)

source

pub fn reset_areas(&mut self)

Forget window positions, sizes etc. Can be used to auto-layout windows.

source§

impl Memory

Popups

Popups are things like combo-boxes, color pickers, menus etc. Only one can be be open at a time.

source

pub fn is_popup_open(&self, popup_id: Id) -> bool

source

pub fn any_popup_open(&self) -> bool

source

pub fn open_popup(&mut self, popup_id: Id)

source

pub fn close_popup(&mut self)

source

pub fn toggle_popup(&mut self, popup_id: Id)

source

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!

source

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§

source§

impl Clone for Memory

source§

fn clone(&self) -> Memory

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Memory

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Memory

source§

fn default() -> Memory

Returns the “default value” for a type. 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§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> SerializableAny for Twhere T: 'static + Any + Clone + Send + Sync,