diff --git a/crates/eframe/src/native/run.rs b/crates/eframe/src/native/run.rs index 7c7353b83..3d842f01e 100644 --- a/crates/eframe/src/native/run.rs +++ b/crates/eframe/src/native/run.rs @@ -1121,8 +1121,10 @@ mod glow_integration { glutin: &RefCell, gl: &glow::Context, painter: &RefCell, - time: Instant, + beginning: Instant, ) { + crate::profile_function!(); + let has_window = glutin.borrow().viewports.get(&pair).is_some(); // This will create a new native window if is needed @@ -1154,13 +1156,13 @@ mod glow_integration { } let win = glutin.viewports[&pair].clone(); - let event_loop; + #[allow(unsafe_code)] - unsafe { - event_loop = WINIT_EVENT_LOOP.with(|event_loop| { + let event_loop = unsafe { + WINIT_EVENT_LOOP.with(|event_loop| { event_loop.borrow().as_ref().expect("No winit event loop") - }); - } + }) + }; glutin .init_window(&win, event_loop) .expect("Cannot init window on egui::Context::create_viewport_sync"); @@ -1170,15 +1172,14 @@ mod glow_integration { let window = glutin.borrow().viewports.get(&pair).cloned(); let Some(window) = window else { return }; - let output; let window = &mut *window.borrow_mut(); let Some(winit_state) = &mut window.egui_winit else { return }; let Some(win) = window.window.clone() else { return }; let win = win.borrow(); let mut input = winit_state.take_egui_input(&win); - input.time = Some(time.elapsed().as_secs_f64()); - output = egui_ctx.run(input, pair, |ctx| { + input.time = Some(beginning.elapsed().as_secs_f64()); + let output = egui_ctx.run(input, pair, |ctx| { render(ctx); }); @@ -2219,10 +2220,12 @@ mod wgpu_integration { render: Box, viewports: &RefCell, builders: &RefCell>, - time: Instant, + beginning: Instant, painter: &RefCell, viewport_maps: &RefCell>, ) { + crate::profile_function!(); + // Creating a new native window if is needed if viewports.borrow().get(&pair).is_none() { let mut builders = builders.borrow_mut(); @@ -2243,18 +2246,16 @@ mod wgpu_integration { render: None, parent_id: pair.parent, }); - let _ = builders + builders .entry(pair.this) .or_insert(viewport_builder.clone()); - let event_loop; - #[allow(unsafe_code)] - unsafe { - event_loop = WINIT_EVENT_LOOP.with(|event_loop| { + let event_loop = unsafe { + WINIT_EVENT_LOOP.with(|event_loop| { event_loop.borrow().as_ref().expect("No winit event loop") - }); - } + }) + }; Self::init_window( pair.this, @@ -2267,17 +2268,15 @@ mod wgpu_integration { ); } - // render sync viewport - + // Render sync viewport: let viewport = viewports.borrow().get(&pair).cloned(); let Some(viewport) = viewport else { return }; - let output; let Some(winit_state) = &mut *viewport.state.borrow_mut() else { return }; let Some(window) = viewport.window else { return }; let win = window.borrow(); let mut input = winit_state.take_egui_input(&win); - input.time = Some(time.elapsed().as_secs_f64()); - output = egui_ctx.run(input, pair, |ctx| { + input.time = Some(beginning.elapsed().as_secs_f64()); + let output = egui_ctx.run(input, pair, |ctx| { render(ctx); }); diff --git a/crates/egui/src/context.rs b/crates/egui/src/context.rs index 5af7927bc..9ae4de188 100644 --- a/crates/egui/src/context.rs +++ b/crates/egui/src/context.rs @@ -2580,21 +2580,24 @@ impl Context { self.write(|ctx| ctx.viewport_commands.push((id, command))); } - /// This will be a native window if is possible! - /// You will need to wrap your viewport state in an `Arc>` or `Arc>`! - /// When this is called again with the same id in `ViewportBuilder` the render function for that viewport will be updated! + /// This creates a new native window, if possible. + /// + /// You will need to wrap your viewport state in an `Arc>` or `Arc>`. + /// When this is called again with the same id in `ViewportBuilder` the render function for that viewport will be updated. /// * `render`: will be called when the viewport receives a event or is requested to be rendered /// - /// If this is no more called that viewport will be destroyed! + /// If this is no more called that viewport will be destroyed. /// - /// If you use a `egui::CentralPanel` you need to check if the viewport is a new window like: + /// If you use a [`egui::CentralPanel`] you need to check if the viewport is a new window like: /// `ctx.viewport_id() != ctx.parent_viewport_id` if false you should create a `egui::Window` pub fn create_viewport( &self, viewport_builder: ViewportBuilder, render: impl Fn(&Context) + Send + Sync + 'static, ) { - if !self.force_embedding() { + if self.force_embedding() { + render(self); + } else { self.write(|ctx| { let viewport_id = ctx.viewport_id(); if let Some(window) = ctx.viewports.get_mut(&viewport_builder.id) { @@ -2619,20 +2622,21 @@ impl Context { ); } }); - } else { - render(self); } } - /// This can only be called in the main thread! + /// This creates a new native window, if possible. + /// + /// This can only be called in the main thread. + /// /// When this is called the current viewport will be paused - /// This will render in a native window if is possible! + /// This will render in a native window if is possible. /// When this finishes then the last viewport will continue drawing - /// This is bad for performance but easy to use! + /// This is bad for performance but easy to use. /// /// For better performance use `Self::create_viewport` /// - /// If this is no more called that viewport will be destroyed! + /// If this is no more called that viewport will be destroyed. /// /// If you use a `egui::CentralPanel` you need to check if the viewport is a new window like: /// `ctx.viewport_id() != ctx.parent_viewport_id` if false you should create a `egui::Window` @@ -2641,7 +2645,9 @@ impl Context { viewport_builder: ViewportBuilder, func: impl FnOnce(&Context) -> T, ) -> T { - if !self.force_embedding() { + if self.force_embedding() { + func(self) + } else { let mut id_pair = ViewportIdPair::MAIN; self.write(|ctx| { id_pair.parent = ctx.viewport_id(); @@ -2682,8 +2688,6 @@ impl Context { } out.expect("egui backend is implemented incorrectly! Context::set_render_sync_callback") - } else { - func(self) } } } diff --git a/crates/egui/src/data/output.rs b/crates/egui/src/data/output.rs index fb7d970c0..13ecaa4f4 100644 --- a/crates/egui/src/data/output.rs +++ b/crates/egui/src/data/output.rs @@ -623,16 +623,15 @@ impl WidgetInfo { } if typ == &WidgetType::TextEdit { - let text; - if let Some(text_value) = text_value { + let text = if let Some(text_value) = text_value { if text_value.is_empty() { - text = "blank".into(); + "blank".into() } else { - text = text_value.to_string(); + text_value.to_string() } } else { - text = "blank".into(); - } + "blank".into() + }; description = format!("{text}: {description}"); }