1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-01 06:10: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

@@ -459,7 +459,7 @@ fn run_and_exit(event_loop: EventLoop<UserEvent>, mut winit_app: impl WinitApp +
mod glow_integration { mod glow_integration {
use egui::{ use egui::{
epaint::ahash::HashMap, NumExt as _, ViewportIdPair, ViewportMap, ViewportOutput, epaint::ahash::HashMap, NumExt as _, ViewportIdMap, ViewportIdPair, ViewportOutput,
ViewportUiCallback, ViewportUiCallback,
}; };
use egui_winit::{ use egui_winit::{
@@ -533,11 +533,11 @@ mod glow_integration {
current_gl_context: Option<glutin::context::PossiblyCurrentContext>, current_gl_context: Option<glutin::context::PossiblyCurrentContext>,
not_current_gl_context: Option<glutin::context::NotCurrentContext>, not_current_gl_context: Option<glutin::context::NotCurrentContext>,
viewports: ViewportMap<Rc<RefCell<Viewport>>>, viewports: ViewportIdMap<Rc<RefCell<Viewport>>>,
viewport_maps: HashMap<winit::window::WindowId, ViewportId>, viewport_maps: HashMap<winit::window::WindowId, ViewportId>,
window_maps: ViewportMap<winit::window::WindowId>, window_maps: ViewportIdMap<winit::window::WindowId>,
builders: ViewportMap<ViewportBuilder>, builders: ViewportIdMap<ViewportBuilder>,
} }
impl GlutinWindowContext { impl GlutinWindowContext {
@@ -660,13 +660,13 @@ mod glow_integration {
let not_current_gl_context = Some(gl_context); let not_current_gl_context = Some(gl_context);
let mut viewport_maps = HashMap::default(); let mut viewport_maps = HashMap::default();
let mut window_maps = ViewportMap::default(); let mut window_maps = ViewportIdMap::default();
if let Some(window) = &window { if let Some(window) = &window {
viewport_maps.insert(window.id(), ViewportId::ROOT); viewport_maps.insert(window.id(), ViewportId::ROOT);
window_maps.insert(ViewportId::ROOT, window.id()); window_maps.insert(ViewportId::ROOT, window.id());
} }
let mut windows = ViewportMap::default(); let mut windows = ViewportIdMap::default();
windows.insert( windows.insert(
ViewportId::ROOT, ViewportId::ROOT,
Rc::new(RefCell::new(Viewport { Rc::new(RefCell::new(Viewport {
@@ -678,7 +678,7 @@ mod glow_integration {
})), })),
); );
let mut builders = ViewportMap::default(); let mut builders = ViewportIdMap::default();
builders.insert(ViewportId::ROOT, window_builder); builders.insert(ViewportId::ROOT, window_builder);
// the fun part with opengl gl is that we never know whether there is an error. the context creation might have failed, but // the fun part with opengl gl is that we never know whether there is an error. the context creation might have failed, but
@@ -1888,7 +1888,7 @@ pub use glow_integration::run_glow;
#[cfg(feature = "wgpu")] #[cfg(feature = "wgpu")]
mod wgpu_integration { mod wgpu_integration {
use egui::{ViewportIdPair, ViewportMap, ViewportOutput, ViewportUiCallback}; use egui::{ViewportIdMap, ViewportIdPair, ViewportOutput, ViewportUiCallback};
use egui_winit::create_winit_window_builder; use egui_winit::create_winit_window_builder;
use parking_lot::Mutex; use parking_lot::Mutex;
@@ -1907,10 +1907,10 @@ mod wgpu_integration {
} }
#[derive(Clone, Default)] #[derive(Clone, Default)]
pub struct Viewports(ViewportMap<Viewport>); pub struct Viewports(ViewportIdMap<Viewport>);
impl std::ops::Deref for Viewports { impl std::ops::Deref for Viewports {
type Target = ViewportMap<Viewport>; type Target = ViewportIdMap<Viewport>;
fn deref(&self) -> &Self::Target { fn deref(&self) -> &Self::Target {
&self.0 &self.0
@@ -1931,7 +1931,7 @@ mod wgpu_integration {
integration: Rc<RefCell<epi_integration::EpiIntegration>>, integration: Rc<RefCell<epi_integration::EpiIntegration>>,
app: Box<dyn epi::App>, app: Box<dyn epi::App>,
viewports: Rc<RefCell<Viewports>>, viewports: Rc<RefCell<Viewports>>,
builders: Rc<RefCell<ViewportMap<ViewportBuilder>>>, builders: Rc<RefCell<ViewportIdMap<ViewportBuilder>>>,
viewport_maps: Rc<RefCell<HashMap<winit::window::WindowId, ViewportId>>>, viewport_maps: Rc<RefCell<HashMap<winit::window::WindowId, ViewportId>>>,
} }
@@ -2178,7 +2178,7 @@ mod wgpu_integration {
}, },
); );
let builders = Rc::new(RefCell::new(ViewportMap::default())); let builders = Rc::new(RefCell::new(ViewportIdMap::default()));
builders.borrow_mut().insert(ViewportId::ROOT, builder); builders.borrow_mut().insert(ViewportId::ROOT, builder);
let painter = Rc::new(RefCell::new(painter)); let painter = Rc::new(RefCell::new(painter));
@@ -2243,7 +2243,7 @@ mod wgpu_integration {
id_pair: ViewportIdPair, id_pair: ViewportIdPair,
viewport_ui_cb: Box<dyn FnOnce(&egui::Context) + '_>, viewport_ui_cb: Box<dyn FnOnce(&egui::Context) + '_>,
viewports: &RefCell<Viewports>, viewports: &RefCell<Viewports>,
builders: &RefCell<ViewportMap<ViewportBuilder>>, builders: &RefCell<ViewportIdMap<ViewportBuilder>>,
beginning: Instant, beginning: Instant,
painter: &RefCell<egui_wgpu::winit::Painter>, painter: &RefCell<egui_wgpu::winit::Painter>,
viewport_maps: &RefCell<HashMap<winit::window::WindowId, ViewportId>>, viewport_maps: &RefCell<HashMap<winit::window::WindowId, ViewportId>>,

View File

@@ -1,6 +1,6 @@
use std::{num::NonZeroU32, sync::Arc}; use std::{num::NonZeroU32, sync::Arc};
use egui::{ViewportId, ViewportMap}; use egui::{ViewportId, ViewportIdMap};
use crate::{renderer, RenderState, SurfaceErrorAction, WgpuConfiguration}; use crate::{renderer, RenderState, SurfaceErrorAction, WgpuConfiguration};
@@ -79,13 +79,13 @@ pub struct Painter {
msaa_samples: u32, msaa_samples: u32,
support_transparent_backbuffer: bool, support_transparent_backbuffer: bool,
depth_format: Option<wgpu::TextureFormat>, depth_format: Option<wgpu::TextureFormat>,
depth_texture_view: ViewportMap<wgpu::TextureView>, depth_texture_view: ViewportIdMap<wgpu::TextureView>,
msaa_texture_view: ViewportMap<wgpu::TextureView>, msaa_texture_view: ViewportIdMap<wgpu::TextureView>,
screen_capture_state: Option<CaptureState>, screen_capture_state: Option<CaptureState>,
instance: wgpu::Instance, instance: wgpu::Instance,
render_state: Option<RenderState>, render_state: Option<RenderState>,
surfaces: ViewportMap<SurfaceState>, surfaces: ViewportIdMap<SurfaceState>,
} }
unsafe impl Send for Painter {} unsafe impl Send for Painter {}

View File

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

View File

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