1
0
mirror of https://github.com/emilk/egui.git synced 2026-09-03 07:10:04 -04:00

ViewportId now wraps Id

This commit is contained in:
Emil Ernerfeldt
2023-11-03 14:18:15 +01:00
parent 47e7b9d2dc
commit 3cb8f49b46
5 changed files with 38 additions and 29 deletions

View File

@@ -2031,7 +2031,7 @@ mod wgpu_integration {
windows_id.insert(new_window.id(), id); windows_id.insert(new_window.id(), id);
if let Err(err) = pollster::block_on(painter.set_window(id, Some(&new_window))) { 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))); *window = Some(Rc::new(RefCell::new(new_window)));
*state = Some(egui_winit::State::new(event_loop)); *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))) { if let Err(err) = pollster::block_on(painter.set_window(id_pair.this, Some(&win))) {
log::error!( log::error!(
"when rendering viewport_id: {}, set_window Error {err}", "when rendering viewport_id={:?}, set_window Error {err}",
id_pair.this id_pair.this
); );
} }

View File

@@ -170,10 +170,9 @@ struct ContextImpl {
repaint: Repaint, repaint: Repaint,
viewports: HashMap<Id, Viewport>, viewports: HashMap<ViewportId, Viewport>,
viewport_commands: Vec<(ViewportId, ViewportCommand)>, viewport_commands: Vec<(ViewportId, ViewportCommand)>,
viewport_id_generator: u64,
is_desktop: bool, is_desktop: bool,
force_embedding: bool, force_embedding: bool,
@@ -2515,11 +2514,6 @@ impl Context {
self.read(|ctx| ctx.parent_viewport_id()) 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<Id>) -> Option<ViewportIdPair> {
self.read(|ctx| ctx.viewports.get(&id.into()).map(|v| v.id_pair))
}
/// For integrations: Is used to render a sync viewport. /// For integrations: Is used to render a sync viewport.
/// ///
/// This will only be set for the current thread. /// This will only be set for the current thread.
@@ -2594,16 +2588,14 @@ impl Context {
window.used = true; window.used = true;
window.viewport_ui_cb = Some(Arc::new(Box::new(viewport_ui_cb))); window.viewport_ui_cb = Some(Arc::new(Box::new(viewport_ui_cb)));
} else { } else {
ctx.viewport_id_generator += 1;
let id = ViewportId(ctx.viewport_id_generator);
ctx.viewports.insert( ctx.viewports.insert(
viewport_builder.id, viewport_builder.id,
Viewport { Viewport {
builder: viewport_builder,
id_pair: ViewportIdPair { id_pair: ViewportIdPair {
this: id, this: viewport_builder.id,
parent: viewport_id, parent: viewport_id,
}, },
builder: viewport_builder,
used: true, used: true,
viewport_ui_cb: Some(Arc::new(Box::new(viewport_ui_cb))), viewport_ui_cb: Some(Arc::new(Box::new(viewport_ui_cb))),
}, },
@@ -2661,9 +2653,8 @@ impl Context {
window.id_pair window.id_pair
} else { } else {
// New // New
ctx.viewport_id_generator += 1;
let id_pair = ViewportIdPair { let id_pair = ViewportIdPair {
this: ViewportId(ctx.viewport_id_generator), this: viewport_builder.id,
parent, parent,
}; };
ctx.viewports.insert( ctx.viewports.insert(

View File

@@ -35,11 +35,11 @@ impl Id {
/// ///
/// The null [`Id`] is still a valid id to use in all circumstances, /// 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! /// 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) Self(0)
} }
pub(crate) fn background() -> Self { pub(crate) const fn background() -> Self {
Self(1) Self(1)
} }

View File

@@ -5,7 +5,7 @@
//! * Sync viewports are executed immediately. //! * Sync viewports are executed immediately.
//! * Async viewports are executed later. //! * Async viewports are executed later.
use std::{fmt::Display, sync::Arc}; use std::sync::Arc;
use epaint::{ColorImage, Pos2, Vec2}; use epaint::{ColorImage, Pos2, Vec2};
@@ -16,27 +16,42 @@ use crate::{Context, Id};
/// Generated by [`Context`]. /// Generated by [`Context`].
/// ///
/// This is returned by [`Context::viewport_id`] and [`Context::parent_viewport_id`]. /// This is returned by [`Context::viewport_id`] and [`Context::parent_viewport_id`].
#[derive(Default, Debug, Hash, Clone, Copy, PartialEq, Eq)] #[derive(Hash, Clone, Copy, PartialEq, Eq)]
pub struct ViewportId(pub(crate) u64); 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 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.0.fmt(f) self.0.short_debug_format().fmt(f)
} }
} }
impl ViewportId { impl ViewportId {
/// This will return the `ViewportId` of the main viewport /// 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`]. /// 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 struct ViewportIdPair {
pub this: ViewportId, pub this: ViewportId,
pub parent: ViewportId, pub parent: ViewportId,
} }
impl Default for ViewportIdPair {
fn default() -> Self {
Self {
this: ViewportId::MAIN,
parent: ViewportId::MAIN,
}
}
}
impl ViewportIdPair { impl ViewportIdPair {
/// This will return the `ViewportIdPair` of the main viewport /// This will return the `ViewportIdPair` of the main viewport
pub const MAIN: Self = Self { pub const MAIN: Self = Self {
@@ -61,7 +76,7 @@ pub type ViewportRenderSyncCallback =
#[derive(PartialEq, Eq, Clone)] #[derive(PartialEq, Eq, Clone)]
#[allow(clippy::option_option)] #[allow(clippy::option_option)]
pub struct ViewportBuilder { pub struct ViewportBuilder {
pub id: Id, pub id: ViewportId,
/// The title of the vieweport. /// The title of the vieweport.
/// `eframe` will use this as the title of the native window. /// `eframe` will use this as the title of the native window.
@@ -97,7 +112,7 @@ pub struct ViewportBuilder {
impl ViewportBuilder { impl ViewportBuilder {
pub fn new(id: impl Into<Id>) -> Self { pub fn new(id: impl Into<Id>) -> Self {
Self { Self {
id: id.into(), id: ViewportId(id.into()),
title: "egui".into(), title: "egui".into(),
name: None, name: None,
position: None, position: None,
@@ -125,7 +140,7 @@ impl ViewportBuilder {
pub fn empty(id: impl Into<Id>) -> Self { pub fn empty(id: impl Into<Id>) -> Self {
Self { Self {
id: id.into(), id: ViewportId(id.into()),
title: "egui".into(), title: "egui".into(),
name: None, name: None,
position: None, position: None,

View File

@@ -331,8 +331,11 @@ fn generic_ui(ui: &mut egui::Ui, container_id: impl Into<Id>) {
ui.add_space(8.0); ui.add_space(8.0);
ui.label(format!("Viewport Id: {}", ctx.viewport_id())); ui.label(format!("Viewport Id: {:?}", ctx.viewport_id()));
ui.label(format!("Parent Viewport Id: {}", ctx.parent_viewport_id())); ui.label(format!(
"Parent Viewport Id: {:?}",
ctx.parent_viewport_id()
));
ui.add_space(8.0); ui.add_space(8.0);