Skip to content

Commit

Permalink
ViewportId now wraps Id
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 3, 2023
1 parent 47e7b9d commit 3cb8f49
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 29 deletions.
4 changes: 2 additions & 2 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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
);
}
Expand Down
17 changes: 4 additions & 13 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,9 @@ struct ContextImpl {

repaint: Repaint,

viewports: HashMap<Id, Viewport>,
viewports: HashMap<ViewportId, Viewport>,
viewport_commands: Vec<(ViewportId, ViewportCommand)>,

viewport_id_generator: u64,
is_desktop: bool,
force_embedding: bool,

Expand Down Expand Up @@ -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<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.
///
/// This will only be set for the current thread.
Expand Down Expand Up @@ -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))),
},
Expand Down Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
35 changes: 25 additions & 10 deletions crates/egui/src/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};

Expand All @@ -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 {
Expand All @@ -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.
Expand Down Expand Up @@ -97,7 +112,7 @@ pub struct ViewportBuilder {
impl ViewportBuilder {
pub fn new(id: impl Into<Id>) -> Self {
Self {
id: id.into(),
id: ViewportId(id.into()),
title: "egui".into(),
name: None,
position: None,
Expand Down Expand Up @@ -125,7 +140,7 @@ impl ViewportBuilder {

pub fn empty(id: impl Into<Id>) -> Self {
Self {
id: id.into(),
id: ViewportId(id.into()),
title: "egui".into(),
name: None,
position: None,
Expand Down
7 changes: 5 additions & 2 deletions examples/viewports/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,11 @@ fn generic_ui(ui: &mut egui::Ui, container_id: impl Into<Id>) {

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);

Expand Down

0 comments on commit 3cb8f49

Please sign in to comment.