From 011c59c2ada2a23cf6a7f98c3ed0b45509caa60b Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Fri, 19 Dec 2025 18:43:58 +0100 Subject: [PATCH] Apply nightly clippy suggestions/fixes (#7794) * Reduce the diff for https://github.com/emilk/egui/pull/7793 --- .typos.toml | 4 +- crates/eframe/Cargo.toml | 5 +- crates/eframe/src/web/events.rs | 245 +++++++++++----------- crates/eframe/src/web/mod.rs | 22 +- crates/eframe/src/web/web_painter_glow.rs | 18 +- crates/eframe/src/web/web_painter_wgpu.rs | 34 ++- crates/egui/src/containers/panel.rs | 8 +- crates/egui/src/input_state/mod.rs | 6 +- crates/egui_extras/src/table.rs | 2 +- crates/egui_kittest/README.md | 36 ++-- 10 files changed, 183 insertions(+), 197 deletions(-) diff --git a/.typos.toml b/.typos.toml index 3ae860ad9..16659f4c7 100644 --- a/.typos.toml +++ b/.typos.toml @@ -5,8 +5,8 @@ [default.extend-words] ime = "ime" # Input Method Editor nknown = "nknown" # part of @55nknown username -isse = "isse" # part of @IsseW username -tye = "tye" # part of @tye-exe username +isse = "isse" # part of @IsseW username +tye = "tye" # part of @tye-exe username ro = "ro" # read-only, also part of the username @Phen-Ro typ = "typ" # Often used because `type` is a keyword in Rust diff --git a/crates/eframe/Cargo.toml b/crates/eframe/Cargo.toml index 6924633f1..86f63c50e 100644 --- a/crates/eframe/Cargo.toml +++ b/crates/eframe/Cargo.toml @@ -251,7 +251,10 @@ web-sys = { workspace = true, features = [ ] } # optional web: -egui-wgpu = { workspace = true, optional = true, features = ["capture"] } # if wgpu is used, use it without (!) winit +egui-wgpu = { workspace = true, optional = true, features = [ + # if wgpu is used, use it without (!) winit: + "capture", +] } wgpu = { workspace = true, optional = true } # Native dev dependencies for testing diff --git a/crates/eframe/src/web/events.rs b/crates/eframe/src/web/events.rs index 88bedab35..d77444563 100644 --- a/crates/eframe/src/web/events.rs +++ b/crates/eframe/src/web/events.rs @@ -137,25 +137,24 @@ fn install_keydown(runner_ref: &WebRunner, target: &EventTarget) -> Result<(), J && !modifiers.command // When text agent is focused, it is responsible for handling input events && !runner.text_agent.has_focus() + && let Some(text) = text_from_keyboard_event(&event) { - if let Some(text) = text_from_keyboard_event(&event) { - let egui_event = egui::Event::Text(text); - let should_stop_propagation = - (runner.web_options.should_stop_propagation)(&egui_event); - let should_prevent_default = - (runner.web_options.should_prevent_default)(&egui_event); - runner.input.raw.events.push(egui_event); - runner.needs_repaint.repaint_asap(); + let egui_event = egui::Event::Text(text); + let should_stop_propagation = + (runner.web_options.should_stop_propagation)(&egui_event); + let should_prevent_default = + (runner.web_options.should_prevent_default)(&egui_event); + runner.input.raw.events.push(egui_event); + runner.needs_repaint.repaint_asap(); - // If this is indeed text, then prevent any other action. - if should_prevent_default { - event.prevent_default(); - } + // If this is indeed text, then prevent any other action. + if should_prevent_default { + event.prevent_default(); + } - // Use web options to tell if the event should be propagated to parent elements. - if should_stop_propagation { - event.stop_propagation(); - } + // Use web options to tell if the event should be propagated to parent elements. + if should_stop_propagation { + event.stop_propagation(); } } @@ -321,30 +320,28 @@ fn install_copy_cut_paste(runner_ref: &WebRunner, target: &EventTarget) -> Resul return; // The eframe app is not interested } - if let Some(data) = event.clipboard_data() { - if let Ok(text) = data.get_data("text") { - let text = text.replace("\r\n", "\n"); + if let Some(data) = event.clipboard_data() + && let Ok(text) = data.get_data("text") + { + let text = text.replace("\r\n", "\n"); - let mut should_stop_propagation = true; - let mut should_prevent_default = true; - if !text.is_empty() { - let egui_event = egui::Event::Paste(text); - should_stop_propagation = - (runner.web_options.should_stop_propagation)(&egui_event); - should_prevent_default = - (runner.web_options.should_prevent_default)(&egui_event); - runner.input.raw.events.push(egui_event); - runner.needs_repaint.repaint_asap(); - } + let mut should_stop_propagation = true; + let mut should_prevent_default = true; + if !text.is_empty() { + let egui_event = egui::Event::Paste(text); + should_stop_propagation = (runner.web_options.should_stop_propagation)(&egui_event); + should_prevent_default = (runner.web_options.should_prevent_default)(&egui_event); + runner.input.raw.events.push(egui_event); + runner.needs_repaint.repaint_asap(); + } - // Use web options to tell if the web event should be propagated to parent elements based on the egui event. - if should_stop_propagation { - event.stop_propagation(); - } + // Use web options to tell if the web event should be propagated to parent elements based on the egui event. + if should_stop_propagation { + event.stop_propagation(); + } - if should_prevent_default { - event.prevent_default(); - } + if should_prevent_default { + event.prevent_default(); } } })?; @@ -562,45 +559,44 @@ fn install_pointerup(runner_ref: &WebRunner, target: &EventTarget) -> Result<(), if is_interested_in_pointer_event( runner, egui::pos2(event.client_x() as f32, event.client_y() as f32), - ) { - if let Some(button) = button_from_mouse_event(&event) { - let modifiers = runner.input.raw.modifiers; - let egui_event = egui::Event::PointerButton { - pos, - button, - pressed: false, - modifiers, - }; - let should_stop_propagation = - (runner.web_options.should_stop_propagation)(&egui_event); - let should_prevent_default = - (runner.web_options.should_prevent_default)(&egui_event); - runner.input.raw.events.push(egui_event); + ) && let Some(button) = button_from_mouse_event(&event) + { + let modifiers = runner.input.raw.modifiers; + let egui_event = egui::Event::PointerButton { + pos, + button, + pressed: false, + modifiers, + }; + let should_stop_propagation = + (runner.web_options.should_stop_propagation)(&egui_event); + let should_prevent_default = + (runner.web_options.should_prevent_default)(&egui_event); + runner.input.raw.events.push(egui_event); - // Previously on iOS, the canvas would not receive focus on - // any touch event, which resulted in the on-screen keyboard - // not working when focusing on a text field in an egui app. - // This attempts to fix that by forcing the focus on any - // click on the canvas. - runner.canvas().focus().ok(); + // Previously on iOS, the canvas would not receive focus on + // any touch event, which resulted in the on-screen keyboard + // not working when focusing on a text field in an egui app. + // This attempts to fix that by forcing the focus on any + // click on the canvas. + runner.canvas().focus().ok(); - // In Safari we are only allowed to do certain things - // (like playing audio, start a download, etc) - // on user action, such as a click. - // So we need to run the app logic here and now: - runner.logic(); + // In Safari we are only allowed to do certain things + // (like playing audio, start a download, etc) + // on user action, such as a click. + // So we need to run the app logic here and now: + runner.logic(); - // Make sure we paint the output of the above logic call asap: - runner.needs_repaint.repaint_asap(); + // Make sure we paint the output of the above logic call asap: + runner.needs_repaint.repaint_asap(); - if should_prevent_default { - event.prevent_default(); - } + if should_prevent_default { + event.prevent_default(); + } - // Use web options to tell if the web event should be propagated to parent elements based on the egui event. - if should_stop_propagation { - event.stop_propagation(); - } + // Use web options to tell if the web event should be propagated to parent elements based on the egui event. + if should_stop_propagation { + event.stop_propagation(); } } }, @@ -713,29 +709,27 @@ fn install_touchstart(runner_ref: &WebRunner, target: &EventTarget) -> Result<() fn install_touchmove(runner_ref: &WebRunner, target: &EventTarget) -> Result<(), JsValue> { runner_ref.add_event_listener(target, "touchmove", |event: web_sys::TouchEvent, runner| { - if let Some((pos, touch)) = primary_touch_pos(runner, &event) { - if is_interested_in_pointer_event( + if let Some((pos, touch)) = primary_touch_pos(runner, &event) + && is_interested_in_pointer_event( runner, egui::pos2(touch.client_x() as f32, touch.client_y() as f32), - ) { - let egui_event = egui::Event::PointerMoved(pos); - let should_stop_propagation = - (runner.web_options.should_stop_propagation)(&egui_event); - let should_prevent_default = - (runner.web_options.should_prevent_default)(&egui_event); - runner.input.raw.events.push(egui_event); + ) + { + let egui_event = egui::Event::PointerMoved(pos); + let should_stop_propagation = (runner.web_options.should_stop_propagation)(&egui_event); + let should_prevent_default = (runner.web_options.should_prevent_default)(&egui_event); + runner.input.raw.events.push(egui_event); - push_touches(runner, egui::TouchPhase::Move, &event); - runner.needs_repaint.repaint(); + push_touches(runner, egui::TouchPhase::Move, &event); + runner.needs_repaint.repaint(); - // Use web options to tell if the web event should be propagated to parent elements based on the egui event. - if should_stop_propagation { - event.stop_propagation(); - } + // Use web options to tell if the web event should be propagated to parent elements based on the egui event. + if should_stop_propagation { + event.stop_propagation(); + } - if should_prevent_default { - event.prevent_default(); - } + if should_prevent_default { + event.prevent_default(); } } }) @@ -743,50 +737,49 @@ fn install_touchmove(runner_ref: &WebRunner, target: &EventTarget) -> Result<(), fn install_touchend(runner_ref: &WebRunner, target: &EventTarget) -> Result<(), JsValue> { runner_ref.add_event_listener(target, "touchend", |event: web_sys::TouchEvent, runner| { - if let Some((pos, touch)) = primary_touch_pos(runner, &event) { - if is_interested_in_pointer_event( + if let Some((pos, touch)) = primary_touch_pos(runner, &event) + && is_interested_in_pointer_event( runner, egui::pos2(touch.client_x() as f32, touch.client_y() as f32), - ) { - // First release mouse to click: - let mut should_stop_propagation = true; - let mut should_prevent_default = true; - let egui_event = egui::Event::PointerButton { - pos, - button: egui::PointerButton::Primary, - pressed: false, - modifiers: runner.input.raw.modifiers, - }; - should_stop_propagation &= - (runner.web_options.should_stop_propagation)(&egui_event); - should_prevent_default &= (runner.web_options.should_prevent_default)(&egui_event); - runner.input.raw.events.push(egui_event); - // Then remove hover effect: - should_stop_propagation &= - (runner.web_options.should_stop_propagation)(&egui::Event::PointerGone); - should_prevent_default &= - (runner.web_options.should_prevent_default)(&egui::Event::PointerGone); - runner.input.raw.events.push(egui::Event::PointerGone); + ) + { + // First release mouse to click: + let mut should_stop_propagation = true; + let mut should_prevent_default = true; + let egui_event = egui::Event::PointerButton { + pos, + button: egui::PointerButton::Primary, + pressed: false, + modifiers: runner.input.raw.modifiers, + }; + should_stop_propagation &= (runner.web_options.should_stop_propagation)(&egui_event); + should_prevent_default &= (runner.web_options.should_prevent_default)(&egui_event); + runner.input.raw.events.push(egui_event); + // Then remove hover effect: + should_stop_propagation &= + (runner.web_options.should_stop_propagation)(&egui::Event::PointerGone); + should_prevent_default &= + (runner.web_options.should_prevent_default)(&egui::Event::PointerGone); + runner.input.raw.events.push(egui::Event::PointerGone); - push_touches(runner, egui::TouchPhase::End, &event); + push_touches(runner, egui::TouchPhase::End, &event); - runner.needs_repaint.repaint_asap(); + runner.needs_repaint.repaint_asap(); - // Use web options to tell if the web event should be propagated to parent elements based on the egui event. - if should_stop_propagation { - event.stop_propagation(); - } + // Use web options to tell if the web event should be propagated to parent elements based on the egui event. + if should_stop_propagation { + event.stop_propagation(); + } - if should_prevent_default { - event.prevent_default(); - } + if should_prevent_default { + event.prevent_default(); + } - // Fix virtual keyboard IOS - // Need call focus at the same time of event - if runner.text_agent.has_focus() { - runner.text_agent.set_focus(false); - runner.text_agent.set_focus(true); - } + // Fix virtual keyboard IOS + // Need call focus at the same time of event + if runner.text_agent.has_focus() { + runner.text_agent.set_focus(false); + runner.text_agent.set_focus(true); } } }) diff --git a/crates/eframe/src/web/mod.rs b/crates/eframe/src/web/mod.rs index ac4c637db..df40129eb 100644 --- a/crates/eframe/src/web/mod.rs +++ b/crates/eframe/src/web/mod.rs @@ -145,18 +145,18 @@ fn canvas_content_rect(canvas: &web_sys::HtmlCanvasElement) -> egui::Rect { ); // We need to subtract padding and border: - if let Some(window) = web_sys::window() { - if let Ok(Some(style)) = window.get_computed_style(canvas) { - let get_property = |name: &str| -> Option { - let property = style.get_property_value(name).ok()?; - property.trim_end_matches("px").parse::().ok() - }; + if let Some(window) = web_sys::window() + && let Ok(Some(style)) = window.get_computed_style(canvas) + { + let get_property = |name: &str| -> Option { + let property = style.get_property_value(name).ok()?; + property.trim_end_matches("px").parse::().ok() + }; - rect.min.x += get_property("padding-left").unwrap_or_default(); - rect.min.y += get_property("padding-top").unwrap_or_default(); - rect.max.x -= get_property("padding-right").unwrap_or_default(); - rect.max.y -= get_property("padding-bottom").unwrap_or_default(); - } + rect.min.x += get_property("padding-left").unwrap_or_default(); + rect.min.y += get_property("padding-top").unwrap_or_default(); + rect.max.x -= get_property("padding-right").unwrap_or_default(); + rect.max.y -= get_property("padding-bottom").unwrap_or_default(); } rect diff --git a/crates/eframe/src/web/web_painter_glow.rs b/crates/eframe/src/web/web_painter_glow.rs index a6a863b05..562a51c29 100644 --- a/crates/eframe/src/web/web_painter_glow.rs +++ b/crates/eframe/src/web/web_painter_glow.rs @@ -192,17 +192,13 @@ fn is_safari_and_webkit_gtk(gl: &web_sys::WebGlRenderingContext) -> bool { .get_extension("WEBGL_debug_renderer_info") .unwrap() .is_some() - { - if let Ok(renderer) = + && let Ok(renderer) = gl.get_parameter(web_sys::WebglDebugRendererInfo::UNMASKED_RENDERER_WEBGL) - { - if let Some(renderer) = renderer.as_string() { - if renderer.contains("Apple") { - return true; - } - } - } + && let Some(renderer) = renderer.as_string() + && renderer.contains("Apple") + { + true + } else { + false } - - false } diff --git a/crates/eframe/src/web/web_painter_wgpu.rs b/crates/eframe/src/web/web_painter_wgpu.rs index ef1127f9c..264ce6adc 100644 --- a/crates/eframe/src/web/web_painter_wgpu.rs +++ b/crates/eframe/src/web/web_painter_wgpu.rs @@ -282,14 +282,12 @@ impl WebPainter for WebPainterWgpu { let mut capture_buffer = None; - if capture { - if let Some(capture_state) = &mut self.screen_capture_state { - capture_buffer = Some(capture_state.copy_textures( - &render_state.device, - &output_frame, - &mut encoder, - )); - } + if capture && let Some(capture_state) = &mut self.screen_capture_state { + capture_buffer = Some(capture_state.copy_textures( + &render_state.device, + &output_frame, + &mut encoder, + )); } Some((output_frame, capture_buffer)) @@ -301,16 +299,16 @@ impl WebPainter for WebPainterWgpu { .submit(user_cmd_bufs.into_iter().chain([encoder.finish()])); if let Some((frame, capture_buffer)) = frame_and_capture_buffer { - if let Some(capture_buffer) = capture_buffer { - if let Some(capture_state) = &self.screen_capture_state { - capture_state.read_screen_rgba( - self.ctx.clone(), - capture_buffer, - capture_data, - self.capture_tx.clone(), - ViewportId::ROOT, - ); - } + if let Some(capture_buffer) = capture_buffer + && let Some(capture_state) = &self.screen_capture_state + { + capture_state.read_screen_rgba( + self.ctx.clone(), + capture_buffer, + capture_data, + self.capture_tx.clone(), + ViewportId::ROOT, + ); } frame.present(); diff --git a/crates/egui/src/containers/panel.rs b/crates/egui/src/containers/panel.rs index 6bdd538e0..6281e6b41 100644 --- a/crates/egui/src/containers/panel.rs +++ b/crates/egui/src/containers/panel.rs @@ -255,9 +255,7 @@ impl<'a> PanelSizer<'a> { let side = self.panel.side; let size_range = self.panel.size_range; - if is_resizing && pointer.is_some() { - let pointer = pointer.unwrap(); - + if is_resizing && let Some(pointer) = pointer { match side { PanelSide::Vertical(side) => { self.size = (pointer.x - side.side_x(self.panel_rect)).abs(); @@ -811,9 +809,7 @@ impl Panel { let resize_id = self.id.with("__resize"); let resize_response = ui.ctx().read_response(resize_id); - if resize_response.is_some() { - let resize_response = resize_response.unwrap(); - + if let Some(resize_response) = resize_response { // NOTE(sharky98): The original code was initializing to // false first, but it doesn't seem necessary. let is_resizing = resize_response.dragged(); diff --git a/crates/egui/src/input_state/mod.rs b/crates/egui/src/input_state/mod.rs index 0c99b6ac3..62e8d0b2e 100644 --- a/crates/egui/src/input_state/mod.rs +++ b/crates/egui/src/input_state/mod.rs @@ -631,8 +631,10 @@ impl InputState { /// A positive Y-value indicates the content is being moved down, as when swiping down on a touch-screen or track-pad with natural scrolling. #[inline(always)] pub fn translation_delta(&self) -> Vec2 { - self.multi_touch() - .map_or(self.smooth_scroll_delta(), |touch| touch.translation_delta) + self.multi_touch().map_or_else( + || self.smooth_scroll_delta(), + |touch| touch.translation_delta, + ) } /// True if there is an active scroll action that might scroll more when using [`Self::smooth_scroll_delta`]. diff --git a/crates/egui_extras/src/table.rs b/crates/egui_extras/src/table.rs index e39bfa786..25e838735 100644 --- a/crates/egui_extras/src/table.rs +++ b/crates/egui_extras/src/table.rs @@ -1325,7 +1325,7 @@ impl TableRow<'_, '_> { *self.response = Some( self.response .as_ref() - .map_or(response.clone(), |r| r.union(response.clone())), + .map_or_else(|| response.clone(), |r| r.union(response.clone())), ); (used_rect, response) diff --git a/crates/egui_kittest/README.md b/crates/egui_kittest/README.md index 8c3c4bc30..638c61522 100644 --- a/crates/egui_kittest/README.md +++ b/crates/egui_kittest/README.md @@ -12,30 +12,28 @@ Ui testing library for egui, based on [kittest](https://github.com/rerun-io/kitt use egui::accesskit::Toggled; use egui_kittest::{Harness, kittest::{Queryable, NodeT}}; -fn main() { - let mut checked = false; - let app = |ui: &mut egui::Ui| { - ui.checkbox(&mut checked, "Check me!"); - }; +let mut checked = false; +let app = |ui: &mut egui::Ui| { + ui.checkbox(&mut checked, "Check me!"); +}; - let mut harness = Harness::new_ui(app); +let mut harness = Harness::new_ui(app); - let checkbox = harness.get_by_label("Check me!"); - assert_eq!(checkbox.accesskit_node().toggled(), Some(Toggled::False)); - checkbox.click(); +let checkbox = harness.get_by_label("Check me!"); +assert_eq!(checkbox.accesskit_node().toggled(), Some(Toggled::False)); +checkbox.click(); - harness.run(); +harness.run(); - let checkbox = harness.get_by_label("Check me!"); - assert_eq!(checkbox.accesskit_node().toggled(), Some(Toggled::True)); +let checkbox = harness.get_by_label("Check me!"); +assert_eq!(checkbox.accesskit_node().toggled(), Some(Toggled::True)); - // Shrink the window size to the smallest size possible - harness.fit_contents(); +// Shrink the window size to the smallest size possible +harness.fit_contents(); - // You can even render the ui and do image snapshot tests - #[cfg(all(feature = "wgpu", feature = "snapshot"))] - harness.snapshot("readme_example"); -} +// You can even render the ui and do image snapshot tests +#[cfg(all(feature = "wgpu", feature = "snapshot"))] +harness.snapshot("readme_example"); ``` ## Configuration @@ -44,7 +42,7 @@ You can configure test settings via a `kittest.toml` file in your workspace root All possible settings and their defaults: ```toml # path to the snapshot directory -output_path = "tests/snapshots" +output_path = "tests/snapshots" # default threshold for image comparison tests threshold = 0.6