1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-02 06:40:06 -04:00

Introduce ViewportIdSet

This commit is contained in:
Emil Ernerfeldt
2023-11-07 10:24:56 +01:00
parent 7bc5698079
commit d6a5c8b6bf
5 changed files with 50 additions and 47 deletions

View File

@@ -59,13 +59,13 @@ struct Repaint {
/// The current frame number.
///
/// Incremented at the end of each frame.
viewports_frame_nr: ViewportMap<u64>,
viewports_frame_nr: ViewportIdMap<u64>,
/// While positive, keep requesting repaints. Decrement at the start of each frame.
repaint_request: ViewportMap<u8>,
repaint_request: ViewportIdMap<u8>,
request_repaint_callback: Option<Box<dyn Fn(RequestRepaintInfo) + Send + Sync>>,
requested_repaint_last_frame: ViewportMap<bool>,
requested_repaint_last_frame: ViewportIdMap<bool>,
}
impl Repaint {
@@ -110,7 +110,7 @@ impl Repaint {
}
// returns what is needed to be repainted
fn end_frame(&mut self, viewport_id: ViewportId, viewports: &[ViewportId]) {
fn end_frame(&mut self, viewport_id: ViewportId, viewports: &ViewportIdSet) {
*self.viewports_frame_nr.entry(viewport_id).or_default() += 1;
self.requested_repaint_last_frame
@@ -154,32 +154,32 @@ struct ContextImpl {
os: OperatingSystem,
input: ViewportMap<InputState>,
input: ViewportIdMap<InputState>,
/// State that is collected during a frame and then cleared
frame_state: ViewportMap<FrameState>,
frame_state: ViewportIdMap<FrameState>,
/// How deeply nested are we?
viewport_stack: Vec<ViewportIdPair>,
// The output of a frame:
graphics: ViewportMap<GraphicLayers>,
output: ViewportMap<PlatformOutput>,
graphics: ViewportIdMap<GraphicLayers>,
output: ViewportIdMap<PlatformOutput>,
paint_stats: PaintStats,
repaint: Repaint,
viewports: ViewportMap<Viewport>,
viewports: ViewportIdMap<Viewport>,
viewport_commands: Vec<(ViewportId, ViewportCommand)>,
embed_viewports: bool,
/// Written to during the frame.
layer_rects_this_frame: ViewportMap<HashMap<LayerId, Vec<(Id, Rect)>>>,
layer_rects_this_frame: ViewportIdMap<HashMap<LayerId, Vec<(Id, Rect)>>>,
/// Read
layer_rects_prev_frame: ViewportMap<HashMap<LayerId, Vec<(Id, Rect)>>>,
layer_rects_prev_frame: ViewportIdMap<HashMap<LayerId, Vec<(Id, Rect)>>>,
#[cfg(feature = "accesskit")]
is_accesskit_enabled: bool,
@@ -1534,13 +1534,13 @@ impl Context {
let shapes = self.drain_paint_lists();
// If there are no viewport that contains the current viewport that viewport needs to be destroyed!
let available_viewports = self.read(|ctx| {
let mut available_viewports = vec![ViewportId::ROOT];
let all_viewport_ids = self.read(|ctx| {
let mut all_viewport_ids = ViewportIdSet::default();
all_viewport_ids.insert(ViewportId::ROOT);
for vp in ctx.viewports.values() {
available_viewports.push(vp.id_pair.this);
all_viewport_ids.insert(vp.id_pair.this);
}
available_viewports
all_viewport_ids
});
let viewport_id = self.viewport_id();
@@ -1559,8 +1559,9 @@ impl Context {
id_pair: viewport.id_pair,
viewport_ui_cb: viewport.viewport_ui_cb.clone(),
});
(was_used || viewport_id != viewport.id_pair.parent)
&& available_viewports.contains(&viewport.id_pair.parent)
&& all_viewport_ids.contains(&viewport.id_pair.parent)
});
});
@@ -1573,16 +1574,15 @@ impl Context {
if is_last {
// Context Cleanup
self.write(|ctx| {
ctx.input.retain(|id, _| available_viewports.contains(id));
ctx.input.retain(|id, _| all_viewport_ids.contains(id));
ctx.layer_rects_prev_frame
.retain(|id, _| available_viewports.contains(id));
.retain(|id, _| all_viewport_ids.contains(id));
ctx.layer_rects_this_frame
.retain(|id, _| available_viewports.contains(id));
ctx.output.retain(|id, _| available_viewports.contains(id));
.retain(|id, _| all_viewport_ids.contains(id));
ctx.output.retain(|id, _| all_viewport_ids.contains(id));
ctx.frame_state
.retain(|id, _| available_viewports.contains(id));
ctx.graphics
.retain(|id, _| available_viewports.contains(id));
.retain(|id, _| all_viewport_ids.contains(id));
ctx.graphics.retain(|id, _| all_viewport_ids.contains(id));
});
} else {
let viewport_id = self.viewport_id();
@@ -1591,7 +1591,7 @@ impl Context {
});
}
self.write(|ctx| ctx.repaint.end_frame(viewport_id, &available_viewports));
self.write(|ctx| ctx.repaint.end_frame(viewport_id, &all_viewport_ids));
FullOutput {
platform_output,

View File

@@ -4,7 +4,7 @@ use epaint::{emath::Rangef, vec2, Vec2};
use crate::{
area, window, EventFilter, Id, IdMap, InputState, LayerId, Pos2, Rect, Style, ViewportId,
ViewportMap,
ViewportIdMap,
};
// ----------------------------------------------------------------------------
@@ -78,7 +78,7 @@ pub struct Memory {
pub(crate) new_font_definitions: Option<epaint::text::FontDefinitions>,
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) interactions: ViewportMap<Interaction>,
pub(crate) interactions: ViewportIdMap<Interaction>,
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) interaction: Interaction,
@@ -91,7 +91,7 @@ pub struct Memory {
pub(crate) window_interaction: Option<window::WindowInteraction>,
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) window_interactions: ViewportMap<window::WindowInteraction>,
pub(crate) window_interactions: ViewportIdMap<window::WindowInteraction>,
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) drag_value: crate::widgets::drag_value::MonoState,
@@ -100,7 +100,7 @@ pub struct Memory {
pub(crate) areas: Areas,
#[cfg_attr(feature = "persistence", serde(skip))]
pub(crate) viewports_areas: ViewportMap<Areas>,
pub(crate) viewports_areas: ViewportIdMap<Areas>,
/// Which popup-window is open (if any)?
/// Could be a combo box, color picker, menu etc.

View File

@@ -51,8 +51,11 @@ impl From<ViewportId> for Id {
impl nohash_hasher::IsEnabled for ViewportId {}
/// A fast hash set of [`ViewportId`].
pub type ViewportIdSet = nohash_hasher::IntSet<ViewportId>;
/// A fast hash map from [`ViewportId`] to `T`.
pub type ViewportMap<T> = nohash_hasher::IntMap<ViewportId, T>;
pub type ViewportIdMap<T> = nohash_hasher::IntMap<ViewportId, T>;
// ----------------------------------------------------------------------------