1
0
mirror of https://github.com/emilk/egui.git synced 2026-06-27 15:13:12 -04:00

Misc code cleanup

This commit is contained in:
Emil Ernerfeldt
2023-11-03 11:37:09 +01:00
parent 99417b45e3
commit 51b083a896
3 changed files with 45 additions and 43 deletions

View File

@@ -1121,8 +1121,10 @@ mod glow_integration {
glutin: &RefCell<GlutinWindowContext>,
gl: &glow::Context,
painter: &RefCell<egui_glow::Painter>,
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<dyn FnOnce(&egui::Context) + '_>,
viewports: &RefCell<Viewports>,
builders: &RefCell<HashMap<ViewportId, ViewportBuilder>>,
time: Instant,
beginning: Instant,
painter: &RefCell<egui_wgpu::winit::Painter>,
viewport_maps: &RefCell<HashMap<winit::window::WindowId, ViewportId>>,
) {
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);
});

View File

@@ -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<RwLock<T>>` or `Arc<Mutex<T>>`!
/// 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<RwLock<T>>` or `Arc<Mutex<T>>`.
/// 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)
}
}
}

View File

@@ -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}");
}