mirror of
https://github.com/emilk/egui.git
synced 2026-08-29 04:40:03 -04:00
Use a lot more let-else (#7582)
This commit is contained in:
@@ -14,10 +14,10 @@ pub struct AppTitleIconSetter {
|
||||
|
||||
impl AppTitleIconSetter {
|
||||
pub fn new(title: String, mut icon_data: Option<Arc<IconData>>) -> Self {
|
||||
if let Some(icon) = &icon_data {
|
||||
if **icon == IconData::default() {
|
||||
icon_data = None;
|
||||
}
|
||||
if let Some(icon) = &icon_data
|
||||
&& **icon == IconData::default()
|
||||
{
|
||||
icon_data = None;
|
||||
}
|
||||
|
||||
Self {
|
||||
@@ -275,13 +275,12 @@ fn set_title_and_icon_mac(title: &str, icon_data: Option<&IconData>) -> AppIconS
|
||||
}
|
||||
|
||||
// Change the title in the top bar - for python processes this would be again "python" otherwise.
|
||||
if let Some(main_menu) = app.mainMenu() {
|
||||
if let Some(item) = main_menu.itemAtIndex(0) {
|
||||
if let Some(app_menu) = item.submenu() {
|
||||
profiling::scope!("setTitle_");
|
||||
app_menu.setTitle(&NSString::from_str(title));
|
||||
}
|
||||
}
|
||||
if let Some(main_menu) = app.mainMenu()
|
||||
&& let Some(item) = main_menu.itemAtIndex(0)
|
||||
&& let Some(app_menu) = item.submenu()
|
||||
{
|
||||
profiling::scope!("setTitle_");
|
||||
app_menu.setTitle(&NSString::from_str(title));
|
||||
}
|
||||
|
||||
// The title in the Dock apparently can't be changed.
|
||||
|
||||
@@ -52,14 +52,13 @@ pub fn viewport_builder(
|
||||
viewport_builder = viewport_builder.with_position(pos);
|
||||
}
|
||||
|
||||
if clamp_size_to_monitor_size {
|
||||
if let Some(initial_window_size) = viewport_builder.inner_size {
|
||||
let initial_window_size = egui::NumExt::at_most(
|
||||
initial_window_size,
|
||||
largest_monitor_point_size(egui_zoom_factor, event_loop),
|
||||
);
|
||||
viewport_builder = viewport_builder.with_inner_size(initial_window_size);
|
||||
}
|
||||
if clamp_size_to_monitor_size && let Some(initial_window_size) = viewport_builder.inner_size
|
||||
{
|
||||
let initial_window_size = egui::NumExt::at_most(
|
||||
initial_window_size,
|
||||
largest_monitor_point_size(egui_zoom_factor, event_loop),
|
||||
);
|
||||
viewport_builder = viewport_builder.with_inner_size(initial_window_size);
|
||||
}
|
||||
|
||||
viewport_builder.inner_size
|
||||
@@ -332,15 +331,15 @@ impl EpiIntegration {
|
||||
if let Some(storage) = self.frame.storage_mut() {
|
||||
profiling::function_scope!();
|
||||
|
||||
if let Some(window) = _window {
|
||||
if self.persist_window {
|
||||
profiling::scope!("native_window");
|
||||
epi::set_value(
|
||||
storage,
|
||||
STORAGE_WINDOW_KEY,
|
||||
&WindowSettings::from_window(self.egui_ctx.zoom_factor(), window),
|
||||
);
|
||||
}
|
||||
if let Some(window) = _window
|
||||
&& self.persist_window
|
||||
{
|
||||
profiling::scope!("native_window");
|
||||
epi::set_value(
|
||||
storage,
|
||||
STORAGE_WINDOW_KEY,
|
||||
&WindowSettings::from_window(self.egui_ctx.zoom_factor(), window),
|
||||
);
|
||||
}
|
||||
if _app.persist_egui_memory() {
|
||||
profiling::scope!("egui_memory");
|
||||
|
||||
@@ -193,12 +193,11 @@ impl crate::Storage for FileStorage {
|
||||
fn save_to_disk(file_path: &PathBuf, kv: &HashMap<String, String>) {
|
||||
profiling::function_scope!();
|
||||
|
||||
if let Some(parent_dir) = file_path.parent() {
|
||||
if !parent_dir.exists() {
|
||||
if let Err(err) = std::fs::create_dir_all(parent_dir) {
|
||||
log::warn!("Failed to create directory {parent_dir:?}: {err}");
|
||||
}
|
||||
}
|
||||
if let Some(parent_dir) = file_path.parent()
|
||||
&& !parent_dir.exists()
|
||||
&& let Err(err) = std::fs::create_dir_all(parent_dir)
|
||||
{
|
||||
log::warn!("Failed to create directory {parent_dir:?}: {err}");
|
||||
}
|
||||
|
||||
match std::fs::File::create(file_path) {
|
||||
|
||||
@@ -281,10 +281,9 @@ impl<'app> GlowWinitApp<'app> {
|
||||
.viewport
|
||||
.mouse_passthrough
|
||||
.unwrap_or(false)
|
||||
&& let Err(err) = glutin.window(ViewportId::ROOT).set_cursor_hittest(false)
|
||||
{
|
||||
if let Err(err) = glutin.window(ViewportId::ROOT).set_cursor_hittest(false) {
|
||||
log::warn!("set_cursor_hittest(false) failed: {err}");
|
||||
}
|
||||
log::warn!("set_cursor_hittest(false) failed: {err}");
|
||||
}
|
||||
|
||||
let app_creator = std::mem::take(&mut self.app_creator)
|
||||
@@ -440,20 +439,20 @@ impl WinitApp for GlowWinitApp<'_> {
|
||||
_: winit::event::DeviceId,
|
||||
event: winit::event::DeviceEvent,
|
||||
) -> crate::Result<EventResult> {
|
||||
if let winit::event::DeviceEvent::MouseMotion { delta } = event {
|
||||
if let Some(running) = &mut self.running {
|
||||
let mut glutin = running.glutin.borrow_mut();
|
||||
if let Some(viewport) = glutin
|
||||
.focused_viewport
|
||||
.and_then(|viewport| glutin.viewports.get_mut(&viewport))
|
||||
{
|
||||
if let Some(egui_winit) = viewport.egui_winit.as_mut() {
|
||||
egui_winit.on_mouse_motion(delta);
|
||||
}
|
||||
if let winit::event::DeviceEvent::MouseMotion { delta } = event
|
||||
&& let Some(running) = &mut self.running
|
||||
{
|
||||
let mut glutin = running.glutin.borrow_mut();
|
||||
if let Some(viewport) = glutin
|
||||
.focused_viewport
|
||||
.and_then(|viewport| glutin.viewports.get_mut(&viewport))
|
||||
{
|
||||
if let Some(egui_winit) = viewport.egui_winit.as_mut() {
|
||||
egui_winit.on_mouse_motion(delta);
|
||||
}
|
||||
|
||||
if let Some(window) = viewport.window.as_ref() {
|
||||
return Ok(EventResult::RepaintNext(window.id()));
|
||||
}
|
||||
if let Some(window) = viewport.window.as_ref() {
|
||||
return Ok(EventResult::RepaintNext(window.id()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -480,16 +479,15 @@ impl WinitApp for GlowWinitApp<'_> {
|
||||
|
||||
if let Some(running) = &self.running {
|
||||
let mut glutin = running.glutin.borrow_mut();
|
||||
if let Some(viewport_id) = glutin.viewport_from_window.get(&event.window_id).copied() {
|
||||
if let Some(viewport) = glutin.viewports.get_mut(&viewport_id) {
|
||||
if let Some(egui_winit) = &mut viewport.egui_winit {
|
||||
return Ok(winit_integration::on_accesskit_window_event(
|
||||
egui_winit,
|
||||
event.window_id,
|
||||
&event.window_event,
|
||||
));
|
||||
}
|
||||
}
|
||||
if let Some(viewport_id) = glutin.viewport_from_window.get(&event.window_id).copied()
|
||||
&& let Some(viewport) = glutin.viewports.get_mut(&viewport_id)
|
||||
&& let Some(egui_winit) = &mut viewport.egui_winit
|
||||
{
|
||||
return Ok(winit_integration::on_accesskit_window_event(
|
||||
egui_winit,
|
||||
event.window_id,
|
||||
&event.window_event,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -527,10 +525,10 @@ impl GlowWinitRunning<'_> {
|
||||
if is_immediate && viewport_id != ViewportId::ROOT {
|
||||
// This will only happen if this is an immediate viewport.
|
||||
// That means that the viewport cannot be rendered by itself and needs his parent to be rendered.
|
||||
if let Some(parent_viewport) = glutin.viewports.get(&viewport.ids.parent) {
|
||||
if let Some(window) = parent_viewport.window.as_ref() {
|
||||
return Ok(EventResult::RepaintNext(window.id()));
|
||||
}
|
||||
if let Some(parent_viewport) = glutin.viewports.get(&viewport.ids.parent)
|
||||
&& let Some(window) = parent_viewport.window.as_ref()
|
||||
{
|
||||
return Ok(EventResult::RepaintNext(window.id()));
|
||||
}
|
||||
return Ok(EventResult::Wait);
|
||||
}
|
||||
@@ -727,10 +725,10 @@ impl GlowWinitRunning<'_> {
|
||||
|
||||
// give it time to settle:
|
||||
#[cfg(feature = "__screenshot")]
|
||||
if integration.egui_ctx.cumulative_pass_nr() == 2 {
|
||||
if let Ok(path) = std::env::var("EFRAME_SCREENSHOT_TO") {
|
||||
save_screenshot_and_exit(&path, &painter, screen_size_in_pixels);
|
||||
}
|
||||
if integration.egui_ctx.cumulative_pass_nr() == 2
|
||||
&& let Ok(path) = std::env::var("EFRAME_SCREENSHOT_TO")
|
||||
{
|
||||
save_screenshot_and_exit(&path, &painter, screen_size_in_pixels);
|
||||
}
|
||||
|
||||
glutin.handle_viewport_output(event_loop, &integration.egui_ctx, &viewport_output);
|
||||
@@ -785,11 +783,12 @@ impl GlowWinitRunning<'_> {
|
||||
// Resize with 0 width and height is used by winit to signal a minimize event on Windows.
|
||||
// See: https://github.com/rust-windowing/winit/issues/208
|
||||
// This solves an issue where the app would panic when minimizing on Windows.
|
||||
if 0 < physical_size.width && 0 < physical_size.height {
|
||||
if let Some(viewport_id) = viewport_id {
|
||||
repaint_asap = true;
|
||||
glutin.resize(viewport_id, *physical_size);
|
||||
}
|
||||
if 0 < physical_size.width
|
||||
&& 0 < physical_size.height
|
||||
&& let Some(viewport_id) = viewport_id
|
||||
{
|
||||
repaint_asap = true;
|
||||
glutin.resize(viewport_id, *physical_size);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -803,19 +802,19 @@ impl GlowWinitRunning<'_> {
|
||||
|
||||
log::debug!("Received WindowEvent::CloseRequested for viewport {viewport_id:?}");
|
||||
|
||||
if let Some(viewport_id) = viewport_id {
|
||||
if let Some(viewport) = glutin.viewports.get_mut(&viewport_id) {
|
||||
// Tell viewport it should close:
|
||||
viewport.info.events.push(egui::ViewportEvent::Close);
|
||||
if let Some(viewport_id) = viewport_id
|
||||
&& let Some(viewport) = glutin.viewports.get_mut(&viewport_id)
|
||||
{
|
||||
// Tell viewport it should close:
|
||||
viewport.info.events.push(egui::ViewportEvent::Close);
|
||||
|
||||
// We may need to repaint both us and our parent to close the window,
|
||||
// and perhaps twice (once to notice the close-event, once again to enforce it).
|
||||
// `request_repaint_of` does a double-repaint though:
|
||||
self.integration.egui_ctx.request_repaint_of(viewport_id);
|
||||
self.integration
|
||||
.egui_ctx
|
||||
.request_repaint_of(viewport.ids.parent);
|
||||
}
|
||||
// We may need to repaint both us and our parent to close the window,
|
||||
// and perhaps twice (once to notice the close-event, once again to enforce it).
|
||||
// `request_repaint_of` does a double-repaint though:
|
||||
self.integration.egui_ctx.request_repaint_of(viewport_id);
|
||||
self.integration
|
||||
.egui_ctx
|
||||
.request_repaint_of(viewport.ids.parent);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
@@ -1232,21 +1231,21 @@ impl GlutinWindowContext {
|
||||
let width_px = NonZeroU32::new(physical_size.width).unwrap_or(NonZeroU32::MIN);
|
||||
let height_px = NonZeroU32::new(physical_size.height).unwrap_or(NonZeroU32::MIN);
|
||||
|
||||
if let Some(viewport) = self.viewports.get(&viewport_id) {
|
||||
if let Some(gl_surface) = &viewport.gl_surface {
|
||||
change_gl_context(
|
||||
&mut self.current_gl_context,
|
||||
&mut self.not_current_gl_context,
|
||||
gl_surface,
|
||||
);
|
||||
gl_surface.resize(
|
||||
self.current_gl_context
|
||||
.as_ref()
|
||||
.expect("failed to get current context to resize surface"),
|
||||
width_px,
|
||||
height_px,
|
||||
);
|
||||
}
|
||||
if let Some(viewport) = self.viewports.get(&viewport_id)
|
||||
&& let Some(gl_surface) = &viewport.gl_surface
|
||||
{
|
||||
change_gl_context(
|
||||
&mut self.current_gl_context,
|
||||
&mut self.not_current_gl_context,
|
||||
gl_surface,
|
||||
);
|
||||
gl_surface.resize(
|
||||
self.current_gl_context
|
||||
.as_ref()
|
||||
.expect("failed to get current context to resize surface"),
|
||||
width_px,
|
||||
height_px,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -94,15 +94,15 @@ impl<T: WinitApp> WinitAppWrapper<T> {
|
||||
|
||||
let mut event_result = event_result;
|
||||
|
||||
if cfg!(target_os = "windows") {
|
||||
if let Ok(EventResult::RepaintNow(window_id)) = event_result {
|
||||
log::trace!("RepaintNow of {window_id:?}");
|
||||
self.windows_next_repaint_times
|
||||
.insert(window_id, Instant::now());
|
||||
if cfg!(target_os = "windows")
|
||||
&& let Ok(EventResult::RepaintNow(window_id)) = event_result
|
||||
{
|
||||
log::trace!("RepaintNow of {window_id:?}");
|
||||
self.windows_next_repaint_times
|
||||
.insert(window_id, Instant::now());
|
||||
|
||||
// Fix flickering on Windows, see https://github.com/emilk/egui/pull/2280
|
||||
event_result = self.winit_app.run_ui_and_paint(event_loop, window_id);
|
||||
}
|
||||
// Fix flickering on Windows, see https://github.com/emilk/egui/pull/2280
|
||||
event_result = self.winit_app.run_ui_and_paint(event_loop, window_id);
|
||||
}
|
||||
|
||||
let combined_result = event_result.map(|event_result| match event_result {
|
||||
|
||||
@@ -429,20 +429,20 @@ impl WinitApp for WgpuWinitApp<'_> {
|
||||
_: winit::event::DeviceId,
|
||||
event: winit::event::DeviceEvent,
|
||||
) -> crate::Result<EventResult> {
|
||||
if let winit::event::DeviceEvent::MouseMotion { delta } = event {
|
||||
if let Some(running) = &mut self.running {
|
||||
let mut shared = running.shared.borrow_mut();
|
||||
if let Some(viewport) = shared
|
||||
.focused_viewport
|
||||
.and_then(|viewport| shared.viewports.get_mut(&viewport))
|
||||
{
|
||||
if let Some(egui_winit) = viewport.egui_winit.as_mut() {
|
||||
egui_winit.on_mouse_motion(delta);
|
||||
}
|
||||
if let winit::event::DeviceEvent::MouseMotion { delta } = event
|
||||
&& let Some(running) = &mut self.running
|
||||
{
|
||||
let mut shared = running.shared.borrow_mut();
|
||||
if let Some(viewport) = shared
|
||||
.focused_viewport
|
||||
.and_then(|viewport| shared.viewports.get_mut(&viewport))
|
||||
{
|
||||
if let Some(egui_winit) = viewport.egui_winit.as_mut() {
|
||||
egui_winit.on_mouse_motion(delta);
|
||||
}
|
||||
|
||||
if let Some(window) = viewport.window.as_ref() {
|
||||
return Ok(EventResult::RepaintNext(window.id()));
|
||||
}
|
||||
if let Some(window) = viewport.window.as_ref() {
|
||||
return Ok(EventResult::RepaintNext(window.id()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -478,14 +478,13 @@ impl WinitApp for WgpuWinitApp<'_> {
|
||||
if let Some(viewport) = viewport_from_window
|
||||
.get(&event.window_id)
|
||||
.and_then(|id| viewports.get_mut(id))
|
||||
&& let Some(egui_winit) = &mut viewport.egui_winit
|
||||
{
|
||||
if let Some(egui_winit) = &mut viewport.egui_winit {
|
||||
return Ok(winit_integration::on_accesskit_window_event(
|
||||
egui_winit,
|
||||
event.window_id,
|
||||
&event.window_event,
|
||||
));
|
||||
}
|
||||
return Ok(winit_integration::on_accesskit_window_event(
|
||||
egui_winit,
|
||||
event.window_id,
|
||||
&event.window_event,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -563,10 +562,10 @@ impl WgpuWinitRunning<'_> {
|
||||
if viewport.viewport_ui_cb.is_none() {
|
||||
// This will only happen if this is an immediate viewport.
|
||||
// That means that the viewport cannot be rendered by itself and needs his parent to be rendered.
|
||||
if let Some(viewport) = viewports.get(&viewport.ids.parent) {
|
||||
if let Some(window) = viewport.window.as_ref() {
|
||||
return Ok(EventResult::RepaintNext(window.id()));
|
||||
}
|
||||
if let Some(viewport) = viewports.get(&viewport.ids.parent)
|
||||
&& let Some(window) = viewport.window.as_ref()
|
||||
{
|
||||
return Ok(EventResult::RepaintNext(window.id()));
|
||||
}
|
||||
return Ok(EventResult::Wait);
|
||||
}
|
||||
@@ -730,13 +729,13 @@ impl WgpuWinitRunning<'_> {
|
||||
|
||||
integration.maybe_autosave(app.as_mut(), window.map(|w| w.as_ref()));
|
||||
|
||||
if let Some(window) = window {
|
||||
if window.is_minimized() == Some(true) {
|
||||
// On Mac, a minimized Window uses up all CPU:
|
||||
// https://github.com/emilk/egui/issues/325
|
||||
profiling::scope!("minimized_sleep");
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
if let Some(window) = window
|
||||
&& window.is_minimized() == Some(true)
|
||||
{
|
||||
// On Mac, a minimized Window uses up all CPU:
|
||||
// https://github.com/emilk/egui/issues/325
|
||||
profiling::scope!("minimized_sleep");
|
||||
std::thread::sleep(std::time::Duration::from_millis(10));
|
||||
}
|
||||
|
||||
if integration.should_close() {
|
||||
@@ -784,14 +783,14 @@ impl WgpuWinitRunning<'_> {
|
||||
// Resize with 0 width and height is used by winit to signal a minimize event on Windows.
|
||||
// See: https://github.com/rust-windowing/winit/issues/208
|
||||
// This solves an issue where the app would panic when minimizing on Windows.
|
||||
if let Some(viewport_id) = viewport_id {
|
||||
if let (Some(width), Some(height)) = (
|
||||
if let Some(viewport_id) = viewport_id
|
||||
&& let (Some(width), Some(height)) = (
|
||||
NonZeroU32::new(physical_size.width),
|
||||
NonZeroU32::new(physical_size.height),
|
||||
) {
|
||||
repaint_asap = true;
|
||||
shared.painter.on_window_resized(viewport_id, width, height);
|
||||
}
|
||||
)
|
||||
{
|
||||
repaint_asap = true;
|
||||
shared.painter.on_window_resized(viewport_id, width, height);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -805,17 +804,17 @@ impl WgpuWinitRunning<'_> {
|
||||
|
||||
log::debug!("Received WindowEvent::CloseRequested for viewport {viewport_id:?}");
|
||||
|
||||
if let Some(viewport_id) = viewport_id {
|
||||
if let Some(viewport) = shared.viewports.get_mut(&viewport_id) {
|
||||
// Tell viewport it should close:
|
||||
viewport.info.events.push(egui::ViewportEvent::Close);
|
||||
if let Some(viewport_id) = viewport_id
|
||||
&& let Some(viewport) = shared.viewports.get_mut(&viewport_id)
|
||||
{
|
||||
// Tell viewport it should close:
|
||||
viewport.info.events.push(egui::ViewportEvent::Close);
|
||||
|
||||
// We may need to repaint both us and our parent to close the window,
|
||||
// and perhaps twice (once to notice the close-event, once again to enforce it).
|
||||
// `request_repaint_of` does a double-repaint though:
|
||||
integration.egui_ctx.request_repaint_of(viewport_id);
|
||||
integration.egui_ctx.request_repaint_of(viewport.ids.parent);
|
||||
}
|
||||
// We may need to repaint both us and our parent to close the window,
|
||||
// and perhaps twice (once to notice the close-event, once again to enforce it).
|
||||
// `request_repaint_of` does a double-repaint though:
|
||||
integration.egui_ctx.request_repaint_of(viewport_id);
|
||||
integration.egui_ctx.request_repaint_of(viewport.ids.parent);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1087,13 +1086,13 @@ fn handle_viewport_output(
|
||||
// For Wayland : https://github.com/emilk/egui/issues/4196
|
||||
if cfg!(target_os = "linux") {
|
||||
let new_inner_size = window.inner_size();
|
||||
if new_inner_size != old_inner_size {
|
||||
if let (Some(width), Some(height)) = (
|
||||
if new_inner_size != old_inner_size
|
||||
&& let (Some(width), Some(height)) = (
|
||||
NonZeroU32::new(new_inner_size.width),
|
||||
NonZeroU32::new(new_inner_size.height),
|
||||
) {
|
||||
painter.on_window_resized(viewport_id, width, height);
|
||||
}
|
||||
)
|
||||
{
|
||||
painter.on_window_resized(viewport_id, width, height);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user