Skip to content

Commit

Permalink
Make Memory::areas private
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 7, 2023
1 parent ae346e1 commit 4ca165b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
12 changes: 6 additions & 6 deletions crates/egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
}
Expand All @@ -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<Rect> = ctx.memory(|mem| {
mem.areas
mem.areas()
.visible_windows()
.into_iter()
.map(State::rect)
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/containers/combo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/containers/popup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/containers/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
18 changes: 9 additions & 9 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down Expand Up @@ -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()
})
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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<LayerId> = self.memory(|mem| mem.areas.order().to_vec());
let layers_ids: Vec<LayerId> = 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;
}
Expand Down
20 changes: 15 additions & 5 deletions crates/egui/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Areas>,
Expand Down Expand Up @@ -565,7 +565,7 @@ impl Memory {
used_ids: &IdMap<Rect>,
) {
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));
Expand All @@ -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<LayerId> {
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<Item = LayerId> + '_ {
self.areas.order().iter().copied()
self.areas().order().iter().copied()
}

pub(crate) fn had_focus_last_frame(&self, id: Id) -> bool {
Expand Down Expand Up @@ -725,7 +735,7 @@ impl Memory {

/// Obtain the previous rectangle of an area.
pub fn area_rect(&self, id: impl Into<Id>) -> Option<Rect> {
self.areas.get(id.into()).map(|state| state.rect())
self.areas().get(id.into()).map(|state| state.rect())
}
}

Expand Down

0 comments on commit 4ca165b

Please sign in to comment.