From 3cb8f49b46c83442653a9f10f2aa1d820433e24b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 3 Nov 2023 14:18:15 +0100 Subject: [PATCH] `ViewportId` now wraps `Id` --- crates/eframe/src/native/run.rs | 4 ++-- crates/egui/src/context.rs | 17 ++++------------ crates/egui/src/id.rs | 4 ++-- crates/egui/src/viewport.rs | 35 +++++++++++++++++++++++---------- examples/viewports/src/main.rs | 7 +++++-- 5 files changed, 38 insertions(+), 29 deletions(-) diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index cce5092dc..1f83bdab5 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -2031,7 +2031,7 @@ mod wgpu_integration { windows_id.insert(new_window.id(), id); if let Err(err) = pollster::block_on(painter.set_window(id, Some(&new_window))) { - log::error!("on set_window: viewport_id {id} {err}"); + log::error!("on set_window: viewport_id {id:?} {err}"); } *window = Some(Rc::new(RefCell::new(new_window))); *state = Some(egui_winit::State::new(event_loop)); @@ -2311,7 +2311,7 @@ mod wgpu_integration { if let Err(err) = pollster::block_on(painter.set_window(id_pair.this, Some(&win))) { log::error!( - "when rendering viewport_id: {}, set_window Error {err}", + "when rendering viewport_id={:?}, set_window Error {err}", id_pair.this ); } diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 1e7f80966..6396dc086 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -170,10 +170,9 @@ struct ContextImpl { repaint: Repaint, - viewports: HashMap, + viewports: HashMap, viewport_commands: Vec<(ViewportId, ViewportCommand)>, - viewport_id_generator: u64, is_desktop: bool, force_embedding: bool, @@ -2515,11 +2514,6 @@ impl Context { self.read(|ctx| ctx.parent_viewport_id()) } - /// This will return the `ViewportIdPair` of the specified id - pub fn viewport_id_pair(&self, id: impl Into) -> Option { - self.read(|ctx| ctx.viewports.get(&id.into()).map(|v| v.id_pair)) - } - /// For integrations: Is used to render a sync viewport. /// /// This will only be set for the current thread. @@ -2594,16 +2588,14 @@ impl Context { window.used = true; window.viewport_ui_cb = Some(Arc::new(Box::new(viewport_ui_cb))); } else { - ctx.viewport_id_generator += 1; - let id = ViewportId(ctx.viewport_id_generator); ctx.viewports.insert( viewport_builder.id, Viewport { - builder: viewport_builder, id_pair: ViewportIdPair { - this: id, + this: viewport_builder.id, parent: viewport_id, }, + builder: viewport_builder, used: true, viewport_ui_cb: Some(Arc::new(Box::new(viewport_ui_cb))), }, @@ -2661,9 +2653,8 @@ impl Context { window.id_pair } else { // New - ctx.viewport_id_generator += 1; let id_pair = ViewportIdPair { - this: ViewportId(ctx.viewport_id_generator), + this: viewport_builder.id, parent, }; ctx.viewports.insert( diff --git a/crates/egui/src/id.rs b/crates/egui/src/id.rs index 612314312..8ac4d9196 100644 --- a/crates/egui/src/id.rs +++ b/crates/egui/src/id.rs @@ -35,11 +35,11 @@ impl Id { /// /// The null [`Id`] is still a valid id to use in all circumstances, /// though obviously it will lead to a lot of collisions if you do use it! - pub fn null() -> Self { + pub const fn null() -> Self { Self(0) } - pub(crate) fn background() -> Self { + pub(crate) const fn background() -> Self { Self(1) } diff --git a/crates/egui/src/viewport.rs b/crates/egui/src/viewport.rs index 14cf8b564..60780ceb0 100644 --- a/crates/egui/src/viewport.rs +++ b/crates/egui/src/viewport.rs @@ -5,7 +5,7 @@ //! * Sync viewports are executed immediately. //! * Async viewports are executed later. -use std::{fmt::Display, sync::Arc}; +use std::sync::Arc; use epaint::{ColorImage, Pos2, Vec2}; @@ -16,27 +16,42 @@ use crate::{Context, Id}; /// Generated by [`Context`]. /// /// This is returned by [`Context::viewport_id`] and [`Context::parent_viewport_id`]. -#[derive(Default, Debug, Hash, Clone, Copy, PartialEq, Eq)] -pub struct ViewportId(pub(crate) u64); +#[derive(Hash, Clone, Copy, PartialEq, Eq)] +pub struct ViewportId(pub Id); -impl Display for ViewportId { +impl Default for ViewportId { + fn default() -> Self { + Self::MAIN + } +} + +impl std::fmt::Debug for ViewportId { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - self.0.fmt(f) + self.0.short_debug_format().fmt(f) } } impl ViewportId { /// This will return the `ViewportId` of the main viewport - pub const MAIN: Self = Self(0); + pub const MAIN: Self = Self(Id::null()); } /// This will deref to [`Self::this`]. -#[derive(Default, Debug, Hash, Clone, Copy, PartialEq, Eq)] +#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)] pub struct ViewportIdPair { pub this: ViewportId, pub parent: ViewportId, } +impl Default for ViewportIdPair { + fn default() -> Self { + Self { + this: ViewportId::MAIN, + parent: ViewportId::MAIN, + } + } +} + impl ViewportIdPair { /// This will return the `ViewportIdPair` of the main viewport pub const MAIN: Self = Self { @@ -61,7 +76,7 @@ pub type ViewportRenderSyncCallback = #[derive(PartialEq, Eq, Clone)] #[allow(clippy::option_option)] pub struct ViewportBuilder { - pub id: Id, + pub id: ViewportId, /// The title of the vieweport. /// `eframe` will use this as the title of the native window. @@ -97,7 +112,7 @@ pub struct ViewportBuilder { impl ViewportBuilder { pub fn new(id: impl Into) -> Self { Self { - id: id.into(), + id: ViewportId(id.into()), title: "egui".into(), name: None, position: None, @@ -125,7 +140,7 @@ impl ViewportBuilder { pub fn empty(id: impl Into) -> Self { Self { - id: id.into(), + id: ViewportId(id.into()), title: "egui".into(), name: None, position: None, diff --git a/examples/viewports/src/main.rs b/examples/viewports/src/main.rs index dbfa400f6..5a6128db4 100644 --- a/examples/viewports/src/main.rs +++ b/examples/viewports/src/main.rs @@ -331,8 +331,11 @@ fn generic_ui(ui: &mut egui::Ui, container_id: impl Into) { ui.add_space(8.0); - ui.label(format!("Viewport Id: {}", ctx.viewport_id())); - ui.label(format!("Parent Viewport Id: {}", ctx.parent_viewport_id())); + ui.label(format!("Viewport Id: {:?}", ctx.viewport_id())); + ui.label(format!( + "Parent Viewport Id: {:?}", + ctx.parent_viewport_id() + )); ui.add_space(8.0);