Skip to content

Commit

Permalink
Some better naming and cleanup of viewport.rs
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 7, 2023
1 parent 96f1426 commit a3b0200
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 25 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 @@ -1086,7 +1086,7 @@ mod glow_integration {
let painter = Rc::downgrade(&painter);
let beginning = integration.beginning;

integration.egui_ctx.set_render_sync_callback(
integration.egui_ctx.set_immediate_viewport_renderer(
move |egui_ctx, viewport_builder, id_pair, viewport_ui_cb| {
if let (Some(glutin), Some(gl), Some(painter)) =
(glutin.upgrade(), gl.upgrade(), painter.upgrade())
Expand Down Expand Up @@ -2210,7 +2210,7 @@ mod wgpu_integration {
let viewport_maps = Rc::downgrade(&viewport_maps);
let beginning = integration.beginning;

integration.egui_ctx.set_render_sync_callback(
integration.egui_ctx.set_immediate_viewport_renderer(
move |egui_ctx, viewport_builder, id_pair, viewport_ui_cb| {
if let (
Some(viewports),
Expand Down
22 changes: 11 additions & 11 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ impl Repaint {
// ----------------------------------------------------------------------------

thread_local! {
static EGUI_RENDER_SYNC: RefCell<Option<Box<ViewportRenderSyncCallback>>> = Default::default();
static IMMEDIATE_VIEWPORT_RENDERER: RefCell<Option<Box<ImmediateViewportRendererCallback>>> = Default::default();
}

// ----------------------------------------------------------------------------
Expand Down Expand Up @@ -170,7 +170,7 @@ struct ContextImpl {

repaint: Repaint,

viewports: ViewportIdMap<Viewport>,
viewports: ViewportIdMap<ViewportState>,
viewport_commands: Vec<(ViewportId, ViewportCommand)>,

embed_viewports: bool,
Expand Down Expand Up @@ -2508,15 +2508,15 @@ impl Context {
///
/// When a viewport sync is created will be rendered by this function
///
/// Look in `crates/eframe/native/run.rs` and search for `set_render_sync_callback` to see for what is used.
/// Look in `crates/eframe/native/run.rs` and search for `set_immediate_viewport_renderer` to see for what is used.
#[allow(clippy::unused_self)]
pub fn set_render_sync_callback(
pub fn set_immediate_viewport_renderer(
&self,
callback: impl for<'a> Fn(&Context, ViewportBuilder, ViewportIdPair, Box<dyn FnOnce(&Context) + 'a>)
+ 'static,
) {
let callback = Box::new(callback);
EGUI_RENDER_SYNC.with(|render_sync| {
IMMEDIATE_VIEWPORT_RENDERER.with(|render_sync| {
render_sync.replace(Some(callback));
});
}
Expand Down Expand Up @@ -2586,7 +2586,7 @@ impl Context {
} else {
ctx.viewports.insert(
viewport_builder.id,
Viewport {
ViewportState {
id_pair: ViewportIdPair {
this: viewport_builder.id,
parent: viewport_id,
Expand Down Expand Up @@ -2628,9 +2628,9 @@ impl Context {
return viewport_ui_cb(self);
}

EGUI_RENDER_SYNC.with(|render_sync_viewport_cb| {
let render_sync_viewport_cb = render_sync_viewport_cb.borrow();
let Some(render_sync_viewport_cb) = render_sync_viewport_cb.as_ref() else {
IMMEDIATE_VIEWPORT_RENDERER.with(|immediate_viewport_renderer| {
let immediate_viewport_renderer = immediate_viewport_renderer.borrow();
let Some(immediate_viewport_renderer) = immediate_viewport_renderer.as_ref() else {
// This egui backend does not support multiple viewports.
return viewport_ui_cb(self);
};
Expand All @@ -2653,7 +2653,7 @@ impl Context {
};
ctx.viewports.insert(
viewport_builder.id,
Viewport {
ViewportState {
builder: viewport_builder.clone(),
id_pair,
used: true,
Expand All @@ -2668,7 +2668,7 @@ impl Context {
{
let out = &mut out;

render_sync_viewport_cb(
immediate_viewport_renderer(
self,
viewport_builder,
id_pair,
Expand Down
21 changes: 11 additions & 10 deletions crates/egui/src/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,12 @@ use crate::{Context, Id};

/// A unique identifier of a viewport.
///
/// Generated by [`Context`].
///
/// This is returned by [`Context::viewport_id`] and [`Context::parent_viewport_id`].
#[derive(Clone, Copy, Hash, PartialEq, Eq)]
pub struct ViewportId(pub Id);

impl Default for ViewportId {
#[inline]
fn default() -> Self {
Self::ROOT
}
Expand All @@ -39,6 +38,7 @@ impl ViewportId {
/// The `ViewportId` of the root viewport.
pub const ROOT: Self = Self(Id::null());

#[inline]
pub fn from_hash_of(source: impl std::hash::Hash) -> Self {
Self(Id::new(source))
}
Expand All @@ -61,19 +61,17 @@ pub type ViewportIdMap<T> = nohash_hasher::IntMap<ViewportId, T>;

// ----------------------------------------------------------------------------

/// This will deref to [`Self::this`].
/// A pair of [`ViewportId`], used to identify a viewport and its parent.
#[derive(Debug, Hash, Clone, Copy, PartialEq, Eq)]
pub struct ViewportIdPair {
pub this: ViewportId,
pub parent: ViewportId,
}

impl Default for ViewportIdPair {
#[inline]
fn default() -> Self {
Self {
this: ViewportId::ROOT,
parent: ViewportId::ROOT,
}
Self::ROOT
}
}

Expand All @@ -89,7 +87,7 @@ impl ViewportIdPair {
pub type ViewportUiCallback = dyn Fn(&Context) + Sync + Send;

/// Render the given viewport, calling the given ui callback.
pub type ViewportRenderSyncCallback =
pub type ImmediateViewportRendererCallback =
dyn for<'a> Fn(&Context, ViewportBuilder, ViewportIdPair, Box<dyn FnOnce(&Context) + 'a>);

/// Control the building of a new egui viewport (i.e. native window).
Expand Down Expand Up @@ -403,6 +401,8 @@ impl ViewportBuilder {
self
}

/// Update this `ViewportBuilder` with a delta,
/// returning a list of commands and a bool intdicating if the window needs to be recreated.
pub fn patch(&mut self, new: &ViewportBuilder) -> (Vec<ViewportCommand>, bool) {
let mut commands = Vec::new();

Expand Down Expand Up @@ -648,7 +648,7 @@ pub enum ViewportCommand {
}

#[derive(Clone)]
pub(crate) struct Viewport {
pub(crate) struct ViewportState {
pub(crate) builder: ViewportBuilder,

/// Id of us and our parent.
Expand All @@ -663,6 +663,7 @@ pub(crate) struct Viewport {
pub(crate) viewport_ui_cb: Option<Arc<Box<ViewportUiCallback>>>,
}

/// Describes a viewport, i.e. a native window.
#[derive(Clone)]
pub struct ViewportOutput {
pub builder: ViewportBuilder,
Expand All @@ -672,6 +673,6 @@ pub struct ViewportOutput {

/// The user-code that shows the GUI, used for deferred viewports.
///
/// `None` for immediate viewports.
/// `None` for immediate viewports and the ROOT viewport.
pub viewport_ui_cb: Option<Arc<Box<ViewportUiCallback>>>,
}
2 changes: 0 additions & 2 deletions crates/egui_glow/src/winit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ impl EguiGlow {
self.egui_winit.on_event(&self.egui_ctx, event)
}

/// Returns the `Duration` of the timeout after which egui should be repainted even if there's no new events.
///
/// Call [`Self::paint`] later to paint.
pub fn run(&mut self, window: &winit::window::Window, run_ui: impl FnMut(&egui::Context)) {
let raw_input = self.egui_winit.take_egui_input(window);
Expand Down

0 comments on commit a3b0200

Please sign in to comment.