diff --git a/crates/egui/src/containers/area.rs b/crates/egui/src/containers/area.rs index 52c02b18ba0..5614df1ef41 100644 --- a/crates/egui/src/containers/area.rs +++ b/crates/egui/src/containers/area.rs @@ -259,7 +259,7 @@ impl Area { let layer_id = LayerId::new(order, id); - let state = ctx.memory(|mem| mem.areas.get(id).copied()); + let state = ctx.memory(|mem| mem.areas().get(id).copied()); let is_new = state.is_none(); if is_new { ctx.request_repaint(); // if we don't know the previous size we are likely drawing the area in the wrong place @@ -307,9 +307,9 @@ impl Area { if (move_response.dragged() || move_response.clicked()) || pointer_pressed_on_area(ctx, layer_id) - || !ctx.memory(|m| m.areas.visible_last_frame(&layer_id)) + || !ctx.memory(|m| m.areas().visible_last_frame(&layer_id)) { - ctx.memory_mut(|m| m.areas.move_to_top(layer_id)); + ctx.memory_mut(|m| m.areas_mut().move_to_top(layer_id)); ctx.request_repaint(); } @@ -353,7 +353,7 @@ impl Area { } let layer_id = LayerId::new(self.order, self.id); - let area_rect = ctx.memory(|mem| mem.areas.get(self.id).map(|area| area.rect())); + let area_rect = ctx.memory(|mem| mem.areas().get(self.id).map(|area| area.rect())); if let Some(area_rect) = area_rect { let clip_rect = ctx.available_rect(); let painter = Painter::new(ctx.clone(), layer_id, clip_rect); @@ -441,7 +441,7 @@ impl Prepared { state.size = content_ui.min_size(); - ctx.memory_mut(|m| m.areas.set_state(layer_id, state)); + ctx.memory_mut(|m| m.areas_mut().set_state(layer_id, state)); move_response } @@ -458,7 +458,7 @@ fn pointer_pressed_on_area(ctx: &Context, layer_id: LayerId) -> bool { fn automatic_area_position(ctx: &Context) -> Pos2 { let mut existing: Vec = ctx.memory(|mem| { - mem.areas + mem.areas() .visible_windows() .into_iter() .map(State::rect) diff --git a/crates/egui/src/containers/combo_box.rs b/crates/egui/src/containers/combo_box.rs index e29b12b4a2c..e840a8e1243 100644 --- a/crates/egui/src/containers/combo_box.rs +++ b/crates/egui/src/containers/combo_box.rs @@ -244,7 +244,7 @@ fn combo_box_dyn<'c, R>( let is_popup_open = ui.memory(|m| m.is_popup_open(popup_id)); - let popup_height = ui.memory(|m| m.areas.get(popup_id).map_or(100.0, |state| state.size.y)); + let popup_height = ui.memory(|m| m.areas().get(popup_id).map_or(100.0, |state| state.size.y)); let above_or_below = if ui.next_widget_position().y + ui.spacing().interact_size.y + popup_height diff --git a/crates/egui/src/containers/popup.rs b/crates/egui/src/containers/popup.rs index 78be080ba47..9a82fce2b05 100644 --- a/crates/egui/src/containers/popup.rs +++ b/crates/egui/src/containers/popup.rs @@ -280,7 +280,7 @@ pub fn was_tooltip_open_last_frame(ctx: &Context, tooltip_id: Id) -> bool { if *individual_id == tooltip_id { let area_id = common_id.with(count); let layer_id = LayerId::new(Order::Tooltip, area_id); - if ctx.memory(|mem| mem.areas.visible_last_frame(&layer_id)) { + if ctx.memory(|mem| mem.areas().visible_last_frame(&layer_id)) { return true; } } diff --git a/crates/egui/src/containers/window.rs b/crates/egui/src/containers/window.rs index c12199d40ac..4eaa3539a7b 100644 --- a/crates/egui/src/containers/window.rs +++ b/crates/egui/src/containers/window.rs @@ -580,7 +580,7 @@ fn interact( } } - ctx.memory_mut(|mem| mem.areas.move_to_top(area_layer_id)); + ctx.memory_mut(|mem| mem.areas_mut().move_to_top(area_layer_id)); Some(window_interaction) } diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 763f1046125..2fa144cce6f 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -251,7 +251,7 @@ impl ContextImpl { // Ensure we register the background area so panels and background ui can catch clicks: let input = &self.input[&viewport_id]; let screen_rect = input.screen_rect(); - self.memory.areas.set_state( + self.memory.areas_mut().set_state( LayerId::background(), containers::area::State { pivot_pos: screen_rect.left_top(), @@ -1624,7 +1624,7 @@ impl Context { ctx.graphics .entry(ctx.viewport_id()) .or_default() - .drain(ctx.memory.areas.order()) + .drain(ctx.memory.areas().order()) .collect() }) } @@ -1702,7 +1702,7 @@ impl Context { pub fn used_rect(&self) -> Rect { self.read(|ctx| { let mut used = ctx.frame_state[&ctx.viewport_id()].used_by_panels; - for window in ctx.memory.areas.visible_windows() { + for window in ctx.memory.areas().visible_windows() { used = used.union(window.rect()); } used @@ -1830,7 +1830,7 @@ impl Context { /// /// [`Area`]:s and [`Window`]:s also do this automatically when being clicked on or interacted with. pub fn move_to_top(&self, layer_id: LayerId) { - self.memory_mut(|mem| mem.areas.move_to_top(layer_id)); + self.memory_mut(|mem| mem.areas_mut().move_to_top(layer_id)); } pub(crate) fn rect_contains_pointer(&self, layer_id: LayerId, rect: Rect) -> bool { @@ -2091,20 +2091,20 @@ impl Context { ui.horizontal(|ui| { ui.label(format!( "{} areas (panels, windows, popups, …)", - self.memory(|mem| mem.areas.count()) + self.memory(|mem| mem.areas().count()) )); if ui.button("Reset").clicked() { - self.memory_mut(|mem| mem.areas = Default::default()); + self.memory_mut(|mem| *mem.areas_mut() = Default::default()); } }); ui.indent("areas", |ui| { ui.label("Visible areas, ordered back to front."); ui.label("Hover to highlight"); - let layers_ids: Vec = self.memory(|mem| mem.areas.order().to_vec()); + let layers_ids: Vec = self.memory(|mem| mem.areas().order().to_vec()); for layer_id in layers_ids { - let area = self.memory(|mem| mem.areas.get(layer_id.id).copied()); + let area = self.memory(|mem| mem.areas().get(layer_id.id).copied()); if let Some(area) = area { - let is_visible = self.memory(|mem| mem.areas.is_visible(&layer_id)); + let is_visible = self.memory(|mem| mem.areas().is_visible(&layer_id)); if !is_visible { continue; } diff --git a/crates/egui/src/memory.rs b/crates/egui/src/memory.rs index f87c61f2ee9..234eb80174e 100644 --- a/crates/egui/src/memory.rs +++ b/crates/egui/src/memory.rs @@ -97,7 +97,7 @@ pub struct Memory { pub(crate) drag_value: crate::widgets::drag_value::MonoState, #[cfg_attr(feature = "persistence", serde(skip))] - pub(crate) areas: Areas, + areas: Areas, #[cfg_attr(feature = "persistence", serde(skip))] pub(crate) viewports_areas: ViewportIdMap, @@ -565,7 +565,7 @@ impl Memory { used_ids: &IdMap, ) { self.caches.update(); - self.areas.end_frame(); + self.areas_mut().end_frame(); self.interaction.focus.end_frame(used_ids); self.interactions .insert(self.viewport_id, std::mem::take(&mut self.interaction)); @@ -588,14 +588,24 @@ impl Memory { self.window_interaction = self.window_interactions.remove(&viewport_id); } + /// Access memory of the [`Area`](crate::containers::area::Area)s, such as `Window`s. + pub fn areas(&self) -> &Areas { + &self.areas + } + + /// Access memory of the [`Area`](crate::containers::area::Area)s, such as `Window`s. + pub fn areas_mut(&mut self) -> &mut Areas { + &mut self.areas + } + /// Top-most layer at the given position. pub fn layer_id_at(&self, pos: Pos2, resize_interact_radius_side: f32) -> Option { - self.areas.layer_id_at(pos, resize_interact_radius_side) + self.areas().layer_id_at(pos, resize_interact_radius_side) } /// An iterator over all layers. Back-to-front. Top is last. pub fn layer_ids(&self) -> impl ExactSizeIterator + '_ { - self.areas.order().iter().copied() + self.areas().order().iter().copied() } pub(crate) fn had_focus_last_frame(&self, id: Id) -> bool { @@ -725,7 +735,7 @@ impl Memory { /// Obtain the previous rectangle of an area. pub fn area_rect(&self, id: impl Into) -> Option { - self.areas.get(id.into()).map(|state| state.rect()) + self.areas().get(id.into()).map(|state| state.rect()) } }