#![warn(missing_docs)] // Let's keep this file well-documented.` to memory.rs use alloc::sync::Arc; use core::num::NonZeroUsize; use ahash::{HashMap, HashSet}; use epaint::emath::TSTransform; use crate::{ EventFilter, Id, IdMap, LayerId, Order, Pos2, Rangef, RawInput, Rect, Style, Vec2, ViewportId, ViewportIdMap, ViewportIdSet, area, vec2, }; mod theme; pub use theme::{Theme, ThemePreference}; // ---------------------------------------------------------------------------- /// The data that egui persists between frames. /// /// This includes window positions and sizes, /// how far the user has scrolled in a [`ScrollArea`](crate::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` feature. /// /// If you want to store data for your widgets, you should look at [`Memory::data`] #[derive(Clone, Debug)] #[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "persistence", serde(default))] pub struct Memory { /// Global egui options. pub options: Options, /// 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>` 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. pub data: crate::util::IdTypeMap, // ------------------------------------------ /// 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::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; /// /// # let mut ctx = egui::Context::default(); /// ctx.memory_mut(|mem| { /// let cache = mem.caches.cache::>(); /// assert_eq!(*cache.get("hello"), 5); /// }); /// ``` #[cfg_attr(feature = "persistence", serde(skip))] pub caches: crate::cache::CacheStorage, // ------------------------------------------ /// new fonts that will be applied at the start of the next frame #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) new_font_definitions: Option, /// add new font that will be applied at the start of the next frame #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) add_fonts: Vec, // Current active viewport #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) viewport_id: ViewportId, #[cfg_attr(feature = "persistence", serde(skip))] everything_is_visible: bool, /// Transforms per layer. /// /// Instead of using this directly, use: /// * [`crate::Context::set_transform_layer`] /// * [`crate::Context::layer_transform_to_global`] /// * [`crate::Context::layer_transform_from_global`] pub to_global: HashMap, // ------------------------------------------------- // Per-viewport: areas: ViewportIdMap, #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) interactions: ViewportIdMap, #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) focus: ViewportIdMap, /// Which popup-window is open on a viewport (if any)? /// Could be a combo box, color picker, menu, etc. /// Optionally stores the position of the popup (usually this would be the position where /// the user clicked). /// If position is [`None`], the popup position will be calculated based on some configuration /// (e.g. relative to some other widget). #[cfg_attr(feature = "persistence", serde(skip))] popups: ViewportIdMap, /// Whether to inform the backend to interrupt any ongoing IME composition /// this pass. #[cfg_attr(feature = "persistence", serde(skip))] requested_interrupt_ime: bool, /// Unpublished application requests survive hidden iterations and passes with no descriptor. #[cfg_attr(feature = "persistence", serde(skip))] requested_ime_resets: ViewportIdMap, } impl Default for Memory { fn default() -> Self { let mut slf = Self { options: Default::default(), data: Default::default(), caches: Default::default(), new_font_definitions: Default::default(), interactions: Default::default(), focus: Default::default(), viewport_id: Default::default(), areas: Default::default(), to_global: Default::default(), popups: Default::default(), everything_is_visible: Default::default(), add_fonts: Default::default(), requested_interrupt_ime: Default::default(), requested_ime_resets: Default::default(), }; slf.interactions.entry(slf.viewport_id).or_default(); slf.areas.entry(slf.viewport_id).or_default(); slf } } /// A direction in which to move the keyboard focus. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum FocusDirection { /// Select the widget closest above the current focused widget. Up, /// Select the widget to the right of the current focused widget. Right, /// Select the widget below the current focused widget. Down, /// Select the widget to the left of the current focused widget. Left, /// Select the previous widget that had focus. Previous, /// Select the next widget that wants focus. Next, /// Don't change focus. #[default] None, } impl FocusDirection { pub(crate) fn is_cardinal(&self) -> bool { match self { Self::Up | Self::Right | Self::Down | Self::Left => true, Self::Previous | Self::Next | Self::None => false, } } } // ---------------------------------------------------------------------------- /// Some global options that you can read and write. /// /// See also [`crate::style::DebugOptions`]. #[derive(Clone, Debug, PartialEq)] #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "serde", serde(default))] pub struct Options { /// The default style for new [`Ui`](crate::Ui):s in dark mode. #[cfg_attr(feature = "serde", serde(skip))] pub dark_style: Arc