pub struct Memory {
pub options: Options,
pub data: IdTypeMap,
pub caches: CacheStorage,
pub(crate) new_font_definitions: Option<FontDefinitions>,
pub(crate) viewport_id: ViewportId,
popup: Option<Id>,
everything_is_visible: bool,
pub layer_transforms: HashMap<LayerId, TSTransform>,
areas: ViewportIdMap<Areas>,
pub(crate) interactions: ViewportIdMap<InteractionState>,
pub(crate) focus: ViewportIdMap<Focus>,
}
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
Global egui options.
data: IdTypeMap
This map stores some superficial state for all widgets with custom Id
s.
This includes storing whether 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 time when you have something that may take 1-100ms to compute. Very slow operations (>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_font_definitions: Option<FontDefinitions>
new fonts that will be applied at the start of the next frame
viewport_id: ViewportId
§popup: Option<Id>
Which popup-window is open (if any)? Could be a combo box, color picker, menu, etc.
everything_is_visible: bool
§layer_transforms: HashMap<LayerId, TSTransform>
Transforms per layer
areas: ViewportIdMap<Areas>
§interactions: ViewportIdMap<InteractionState>
§focus: ViewportIdMap<Focus>
Implementations§
source§impl Memory
impl Memory
pub(crate) fn begin_pass( &mut self, new_raw_input: &RawInput, viewports: &ViewportIdSet, )
pub(crate) fn end_pass(&mut self, used_ids: &IdMap<Rect>)
pub(crate) fn set_viewport_id(&mut self, viewport_id: ViewportId)
sourcepub fn layer_id_at(&self, pos: Pos2) -> Option<LayerId>
pub fn layer_id_at(&self, pos: Pos2) -> 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 had_focus_last_frame(&self, id: Id) -> bool
pub fn had_focus_last_frame(&self, id: Id) -> bool
Check if the layer had focus last frame.
returns true
if the layer had focus last frame, but not this one.
sourcepub(crate) fn lost_focus(&self, id: Id) -> bool
pub(crate) fn lost_focus(&self, id: Id) -> bool
Check if the layer lost focus last frame.
returns true
if the layer lost focus last frame, but not this one.
sourcepub(crate) fn gained_focus(&self, id: Id) -> bool
pub(crate) fn gained_focus(&self, id: Id) -> bool
Check if the layer gained focus this frame.
returns true
if the layer gained focus this frame, but not last one.
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 set_focus_lock_filter(&mut self, id: Id, event_filter: EventFilter)
pub fn set_focus_lock_filter(&mut self, id: Id, event_filter: EventFilter)
Set an event filter for a widget.
This allows you to control whether the widget will loose focus when the user presses tab, arrow keys, or escape.
You must first give focus to the widget before calling this.
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 interested_in_focus(&mut self, id: Id)
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.
sourcepub fn stop_text_input(&mut self)
pub fn stop_text_input(&mut self)
Stop editing the active TextEdit
(if any).
sourcepub fn is_anything_being_dragged(&self) -> bool
👎Deprecated: Use Context::dragged_id
instead
pub fn is_anything_being_dragged(&self) -> bool
Context::dragged_id
insteadIs any widget being dragged?
sourcepub fn is_being_dragged(&self, id: Id) -> bool
👎Deprecated: Use Context::is_being_dragged
instead
pub fn is_being_dragged(&self, id: Id) -> bool
Context::is_being_dragged
insteadIs this specific widget being dragged?
A widget that sense both clicks and drags is only marked as “dragged”
when the mouse has moved a bit, but is_being_dragged
will return true immediately.
sourcepub fn dragged_id(&self) -> Option<Id>
👎Deprecated: Use Context::dragged_id
instead
pub fn dragged_id(&self) -> Option<Id>
Context::dragged_id
insteadGet the id of the widget being dragged, if any.
Note that this is set as soon as the mouse is pressed, so the widget may not yet be marked as “dragged”, as that can only happen after the mouse has moved a bit (at least if the widget is interesated in both clicks and drags).
sourcepub fn set_dragged_id(&mut self, id: Id)
👎Deprecated: Use Context::set_dragged_id
instead
pub fn set_dragged_id(&mut self, id: Id)
Context::set_dragged_id
insteadSet which widget is being dragged.
sourcepub fn stop_dragging(&mut self)
👎Deprecated: Use Context::stop_dragging
instead
pub fn stop_dragging(&mut self)
Context::stop_dragging
insteadStop dragging any widget.
sourcepub fn dragging_something_else(&self, not_this: Id) -> bool
👎Deprecated: Use Context::dragging_something_else
instead
pub fn dragging_something_else(&self, not_this: Id) -> bool
Context::dragging_something_else
insteadIs something else being dragged?
Returns true if we are dragging something, but not the given widget.
sourcepub fn reset_areas(&mut self)
pub fn reset_areas(&mut self)
Forget window positions, sizes etc. Can be used to auto-layout windows.
sourcepub fn area_rect(&self, id: impl Into<Id>) -> Option<Rect>
pub fn area_rect(&self, id: impl Into<Id>) -> Option<Rect>
Obtain the previous rectangle of an area.
pub(crate) fn interaction(&self) -> &InteractionState
pub(crate) fn interaction_mut(&mut self) -> &mut InteractionState
pub(crate) fn focus(&self) -> Option<&Focus>
pub(crate) fn focus_mut(&mut self) -> &mut Focus
source§impl Memory
impl Memory
§Popups
Popups are things like combo-boxes, color pickers, menus etc. Only one can be open at a time.
sourcepub fn is_popup_open(&self, popup_id: Id) -> bool
pub fn is_popup_open(&self, popup_id: Id) -> bool
Is the given popup open?
sourcepub fn any_popup_open(&self) -> bool
pub fn any_popup_open(&self) -> bool
Is any popup open?
sourcepub fn open_popup(&mut self, popup_id: Id)
pub fn open_popup(&mut self, popup_id: Id)
Open the given popup and close all others.
sourcepub fn close_popup(&mut self)
pub fn close_popup(&mut self)
Close the open popup, if any.
sourcepub fn toggle_popup(&mut self, popup_id: Id)
pub fn toggle_popup(&mut self, popup_id: Id)
Toggle the given popup between closed and open.
Note: At most, only one popup can be open at a time.
sourcepub fn everything_is_visible(&self) -> bool
pub fn everything_is_visible(&self) -> bool
If true, all windows, menus, tooltips, etc., will 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!