Skip to content

Commit

Permalink
Create struct ImmediateViewport to simplify argument lists
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Nov 14, 2023
1 parent 66662e4 commit 002110e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 49 deletions.
66 changes: 33 additions & 33 deletions crates/eframe/src/native/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,8 +455,8 @@ mod glow_integration {
};

use egui::{
epaint::ahash::HashMap, NumExt as _, ViewportIdMap, ViewportIdPair, ViewportIdSet,
ViewportOutput, ViewportUiCallback,
epaint::ahash::HashMap, ImmediateViewport, NumExt as _, ViewportIdMap, ViewportIdPair,
ViewportIdSet, ViewportOutput, ViewportUiCallback,
};
use egui_winit::{create_winit_window_builder, process_viewport_commands, EventResponse};

Expand Down Expand Up @@ -1362,7 +1362,7 @@ mod glow_integration {
let event_loop: *const EventLoopWindowTarget<UserEvent> = event_loop;

egui::Context::set_immediate_viewport_renderer(
move |egui_ctx, viewport_builder, ids, viewport_ui_cb| {
move |egui_ctx, immediate_viewport| {
if let (Some(glutin), Some(painter)) = (glutin.upgrade(), painter.upgrade())
{
// SAFETY: the event loop lives longer than
Expand All @@ -1373,12 +1373,10 @@ mod glow_integration {
render_immediate_viewport(
event_loop,
egui_ctx,
viewport_builder,
ids,
viewport_ui_cb,
&glutin,
&painter,
beginning,
immediate_viewport,
);
} else {
log::warn!("render_sync_callback called after window closed");
Expand All @@ -1400,28 +1398,30 @@ mod glow_integration {

/// This is called (via a callback) by user code to render immediate viewports,
/// i.e. viewport that are directly nested inside a parent viewport.
#[inline(always)]
#[allow(clippy::too_many_arguments)]
fn render_immediate_viewport(
event_loop: &EventLoopWindowTarget<UserEvent>,
egui_ctx: &egui::Context,
mut viewport_builder: ViewportBuilder,
ids: ViewportIdPair,
viewport_ui_cb: Box<dyn FnOnce(&egui::Context) + '_>,
glutin: &RefCell<GlutinWindowContext>,
painter: &RefCell<egui_glow::Painter>,
beginning: Instant,
immediate_viewport: ImmediateViewport<'_>,
) {
crate::profile_function!();

let ImmediateViewport {
ids,
mut builder,
viewport_ui_cb,
} = immediate_viewport;

if !glutin.borrow().viewports.contains_key(&ids.this) {
// A new viewport - create a window for it!

let mut glutin = glutin.borrow_mut();

// Inherit parent icon if none
if viewport_builder.icon.is_none() && glutin.builders.get(&ids.this).is_none() {
viewport_builder.icon = glutin
if builder.icon.is_none() && glutin.builders.get(&ids.this).is_none() {
builder.icon = glutin
.builders
.get(&ids.parent)
.and_then(|b| b.icon.clone());
Expand All @@ -1432,7 +1432,7 @@ mod glow_integration {
.entry(ids.this)
.or_insert(Viewport::new(ids));

glutin.builders.entry(ids.this).or_insert(viewport_builder);
glutin.builders.entry(ids.this).or_insert(builder);

glutin
.init_viewport(ids.this, event_loop)
Expand Down Expand Up @@ -1820,8 +1820,8 @@ mod wgpu_integration {
use parking_lot::Mutex;

use egui::{
FullOutput, ViewportIdMap, ViewportIdPair, ViewportIdSet, ViewportOutput,
ViewportUiCallback,
FullOutput, ImmediateViewport, ViewportIdMap, ViewportIdPair, ViewportIdSet,
ViewportOutput, ViewportUiCallback,
};
use egui_winit::{create_winit_window_builder, process_viewport_commands};

Expand Down Expand Up @@ -2075,7 +2075,7 @@ mod wgpu_integration {
let event_loop: *const EventLoopWindowTarget<UserEvent> = event_loop;

egui::Context::set_immediate_viewport_renderer(
move |egui_ctx, viewport_builder, ids, viewport_ui_cb| {
move |egui_ctx, immediate_viewport| {
if let Some(shared) = shared.upgrade() {
// SAFETY: the event loop lives longer than
// the Rc:s we just upgraded above.
Expand All @@ -2085,11 +2085,9 @@ mod wgpu_integration {
render_immediate_viewport(
event_loop,
egui_ctx,
viewport_builder,
ids,
viewport_ui_cb,
beginning,
&shared,
immediate_viewport,
);
} else {
log::warn!("render_sync_callback called after window closed");
Expand Down Expand Up @@ -2125,19 +2123,21 @@ mod wgpu_integration {
Ok((window, window_builder))
}

#[inline(always)]
#[allow(clippy::too_many_arguments)]
fn render_immediate_viewport(
event_loop: &EventLoopWindowTarget<UserEvent>,
egui_ctx: &egui::Context,
mut viewport_builder: ViewportBuilder,
ids: ViewportIdPair,
viewport_ui_cb: Box<dyn FnOnce(&egui::Context) + '_>,
beginning: Instant,
shared: &RefCell<SharedState>,
immediate_viewport: ImmediateViewport<'_>,
) {
crate::profile_function!();

let ImmediateViewport {
ids,
mut builder,
viewport_ui_cb,
} = immediate_viewport;

let input = {
let mut shared = shared.borrow_mut();
let SharedState {
Expand All @@ -2148,15 +2148,15 @@ mod wgpu_integration {

// Creating a new native window if is needed
if !viewports.contains_key(&ids.this) {
if viewport_builder.icon.is_none() {
viewport_builder.icon = viewports
if builder.icon.is_none() {
builder.icon = viewports
.get(&ids.parent)
.and_then(|vp| vp.builder.icon.clone());
}

let viewport = viewports.entry(ids.this).or_insert(Viewport {
ids,
builder: viewport_builder,
builder,
viewport_ui_cb: None,
window: None,
egui_winit: None,
Expand Down Expand Up @@ -2586,16 +2586,16 @@ mod wgpu_integration {

// Add new viewports, and update existing ones:
for ViewportOutput {
builder: mut new_builder,
ids,
mut builder,
viewport_ui_cb,
} in out_viewports
{
active_viewports_ids.insert(ids.this);

if new_builder.icon.is_none() {
if builder.icon.is_none() {
// Inherit icon from parent
new_builder.icon = viewports
builder.icon = viewports
.get_mut(&ids.parent)
.and_then(|vp| vp.builder.icon.clone());
}
Expand All @@ -2605,7 +2605,7 @@ mod wgpu_integration {
// New viewport:
entry.insert(Viewport {
ids,
builder: new_builder,
builder,
viewport_ui_cb,
window: None,
egui_winit: None,
Expand All @@ -2619,7 +2619,7 @@ mod wgpu_integration {
viewport.ids.parent = ids.parent;
viewport.viewport_ui_cb = viewport_ui_cb;

let (commands, recreate) = viewport.builder.patch(&new_builder);
let (commands, recreate) = viewport.builder.patch(&builder);

if recreate {
if let Some(viewport) = viewports.get_mut(&ids.this) {
Expand Down
26 changes: 13 additions & 13 deletions crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2517,8 +2517,7 @@ impl Context {
/// 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_immediate_viewport_renderer(
callback: impl for<'a> Fn(&Context, ViewportBuilder, ViewportIdPair, Box<dyn FnOnce(&Context) + 'a>)
+ 'static,
callback: impl for<'a> Fn(&Context, ImmediateViewport<'a>) + 'static,
) {
let callback = Box::new(callback);
IMMEDIATE_VIEWPORT_RENDERER.with(|render_sync| {
Expand Down Expand Up @@ -2628,7 +2627,7 @@ impl Context {
/// `ctx.viewport_id() != ctx.parent_viewport_id` if false you should create a [`crate::Window`].
pub fn show_viewport_immediate<T>(
&self,
viewport_builder: &ViewportBuilder,
builder: ViewportBuilder,
viewport_ui_cb: impl FnOnce(&Context) -> T,
) -> T {
crate::profile_function!();
Expand All @@ -2647,23 +2646,23 @@ impl Context {
let ids = self.write(|ctx| {
let parent = ctx.viewport_id();

if let Some(window) = ctx.viewports.get_mut(&viewport_builder.id) {
if let Some(window) = ctx.viewports.get_mut(&builder.id) {
// Existing
window.builder = viewport_builder.clone();
window.builder = builder.clone();
window.ids.parent = parent;
window.used = true;
window.viewport_ui_cb = None;
window.ids
} else {
// New
let ids = ViewportIdPair {
this: viewport_builder.id,
this: builder.id,
parent,
};
ctx.viewports.insert(
viewport_builder.id,
builder.id,
ViewportState {
builder: viewport_builder.clone(),
builder: builder.clone(),
ids,
used: true,
viewport_ui_cb: None,
Expand All @@ -2677,12 +2676,13 @@ impl Context {
{
let out = &mut out;

immediate_viewport_renderer(
self,
viewport_builder.clone(),
let viewport = ImmediateViewport {
ids,
Box::new(move |context| *out = Some(viewport_ui_cb(context))),
);
builder,
viewport_ui_cb: Box::new(move |context| *out = Some(viewport_ui_cb(context))),
};

immediate_viewport_renderer(self, viewport);
}

out.expect(
Expand Down
14 changes: 12 additions & 2 deletions crates/egui/src/viewport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ impl ViewportIdPair {
pub type ViewportUiCallback = dyn Fn(&Context) + Sync + Send;

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

/// Control the building of a new egui viewport (i.e. native window).
///
Expand Down Expand Up @@ -734,3 +733,14 @@ impl ViewportOutput {
self.ids.this
}
}

/// Viewport for immediate rendering.
pub struct ImmediateViewport<'a> {
/// Id of us and our parent.
pub ids: ViewportIdPair,

pub builder: ViewportBuilder,

/// The user-code that shows the GUI.
pub viewport_ui_cb: Box<dyn FnOnce(&Context) + 'a>,
}
2 changes: 1 addition & 1 deletion examples/test_viewports/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl ViewportState {

if immediate {
let mut vp_state = vp_state.write();
ctx.show_viewport_immediate(&vp_builder, move |ctx| {
ctx.show_viewport_immediate(vp_builder, move |ctx| {
show_as_popup(ctx, &title, vp_id.into(), |ui: &mut egui::Ui| {
generic_child_ui(ui, &mut vp_state);
});
Expand Down

0 comments on commit 002110e

Please sign in to comment.