From 8972c5efb9182fe48fbc1cb5a4bbb495c2a54a20 Mon Sep 17 00:00:00 2001 From: Sophie Herold Date: Thu, 29 Jan 2026 19:04:26 +0100 Subject: [PATCH] code: Use chained if-let's --- src/decoder.rs | 8 +- src/metadata.rs | 9 +- src/util/gettext.rs | 9 +- src/widgets/edit_window.rs | 118 +++++++++++++-------------- src/widgets/image/input_handling.rs | 20 ++--- src/widgets/image/loading.rs | 43 +++++----- src/widgets/image/rendering.rs | 122 ++++++++++++++-------------- src/widgets/image/rotation.rs | 16 ++-- src/widgets/image_view.rs | 30 +++---- src/widgets/image_window.rs | 19 ++--- src/widgets/print.rs | 14 ++-- src/widgets/sliding_view.rs | 46 +++++------ 12 files changed, 219 insertions(+), 235 deletions(-) diff --git a/src/decoder.rs b/src/decoder.rs index eb70027..d31a32d 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -201,10 +201,10 @@ impl Decoder { /// Request missing tiles pub fn request(&self, tile_request: TileRequest) { - if let FormatDecoder::Svg(svg) = &self.decoder { - if let Err(err) = svg.request(tile_request) { - tracing::error!("Failed to request new SVG tiles: {err}"); - } + if let FormatDecoder::Svg(svg) = &self.decoder + && let Err(err) = svg.request(tile_request) + { + tracing::error!("Failed to request new SVG tiles: {err}"); } } diff --git a/src/metadata.rs b/src/metadata.rs index 42537ac..270250c 100644 --- a/src/metadata.rs +++ b/src/metadata.rs @@ -369,12 +369,11 @@ impl Metadata { let location_exact = libgweather::Location::new_detached("", None, lat, lon); // do not use city if more than 15 km away - if nearest_city.distance(&location_exact) < 15. { - if let (Some(city), Some(country)) = + if nearest_city.distance(&location_exact) < 15. + && let (Some(city), Some(country)) = (nearest_city.city_name(), nearest_city.country_name()) - { - return Some(format!("{city}, {country}")); - } + { + return Some(format!("{city}, {country}")); } } diff --git a/src/util/gettext.rs b/src/util/gettext.rs index f84e744..e6fb4fd 100644 --- a/src/util/gettext.rs +++ b/src/util/gettext.rs @@ -63,10 +63,11 @@ pub fn apply_unicode_escapes(s: impl AsRef) -> Option { while let Some(c) = char_iter.next() { match state { State::None if c == '\\' => { - if let (Some(c1), Some(c2)) = (char_iter.next(), char_iter.next()) { - if c1 == 'u' && c2 == '{' { - state = State::UnicodeHex; - } + if let (Some(c1), Some(c2)) = (char_iter.next(), char_iter.next()) + && c1 == 'u' + && c2 == '{' + { + state = State::UnicodeHex; } } State::None => new.push(c), diff --git a/src/widgets/edit_window.rs b/src/widgets/edit_window.rs index d9808d1..d1aa901 100644 --- a/src/widgets/edit_window.rs +++ b/src/widgets/edit_window.rs @@ -252,87 +252,81 @@ mod imp { let obj = self.obj(); self.save.popdown(); - if let Some(original_file) = obj.original_image().file() { - if let Some(current_path) = original_file.path() { - if let Some(mut file_stem) = current_path.file_stem().map(|x| x.to_os_string()) - { - let mut tmp_path = current_path.clone(); - file_stem.push(".tmp"); - if let Some(ext) = current_path.extension() { - file_stem.push("."); - file_stem.push(ext); - } - tmp_path.set_file_name(file_stem); + if let Some(original_file) = obj.original_image().file() + && let Some(current_path) = original_file.path() + && let Some(mut file_stem) = current_path.file_stem().map(|x| x.to_os_string()) + { + let mut tmp_path = current_path.clone(); + file_stem.push(".tmp"); + if let Some(ext) = current_path.extension() { + file_stem.push("."); + file_stem.push(ext); + } + tmp_path.set_file_name(file_stem); - let tmp_file = gio::File::for_path(&tmp_path); + let tmp_file = gio::File::for_path(&tmp_path); - let cancellable = gio::Cancellable::new(); - if let Some(old_cancellable) = - self.save_cancellable.replace(Some(cancellable.clone())) - { - old_cancellable.cancel(); - } + let cancellable = gio::Cancellable::new(); + if let Some(old_cancellable) = + self.save_cancellable.replace(Some(cancellable.clone())) + { + old_cancellable.cancel(); + } - let written = self - .save(original_file.clone(), tmp_file.clone(), cancellable.clone()) - .await; + let written = self + .save(original_file.clone(), tmp_file.clone(), cancellable.clone()) + .await; - if written { - tracing::debug!("Moving image to trash '{}'", original_file.uri()); - let trash_result = gio::CancellableFuture::new( - original_file.trash_future(glib::Priority::DEFAULT), - cancellable.clone(), - ) - .await; + if written { + tracing::debug!("Moving image to trash '{}'", original_file.uri()); + let trash_result = gio::CancellableFuture::new( + original_file.trash_future(glib::Priority::DEFAULT), + cancellable.clone(), + ) + .await; - if trash_result.is_err() { - // Canceled - tracing::debug!("Trashing image canceled"); - return; - } else if let Ok(Err(err)) = trash_result { - obj.window_show_error( + if trash_result.is_err() { + // Canceled + tracing::debug!("Trashing image canceled"); + return; + } else if let Ok(Err(err)) = trash_result { + obj.window_show_error( &gettext("Failed to save image."), &format!("Failed to move image {current_path:?} to trash and therefore couldn't save image: {err}"), ErrorType::General, ); - return; - } + return; + } - tracing::debug!( - "Moving '{}' to '{}'", - tmp_file.uri(), - original_file.uri() - ); - let move_result = gio::CancellableFuture::new( - tmp_file - .move_future( - &original_file, - gio::FileCopyFlags::NONE, - glib::Priority::DEFAULT, - ) - .0, - cancellable.clone(), + tracing::debug!("Moving '{}' to '{}'", tmp_file.uri(), original_file.uri()); + let move_result = gio::CancellableFuture::new( + tmp_file + .move_future( + &original_file, + gio::FileCopyFlags::NONE, + glib::Priority::DEFAULT, ) - .await; + .0, + cancellable.clone(), + ) + .await; - if move_result.is_err() { - // Canceled - tracing::debug!("Moving image canceled"); - return; - } else if let Ok(Err(err)) = move_result { - obj.window_show_error( + if move_result.is_err() { + // Canceled + tracing::debug!("Moving image canceled"); + return; + } else if let Ok(Err(err)) = move_result { + obj.window_show_error( &gettext("Failed to save image."), &format!( "Failed to move image image to {current_path:?}: {err} move {:?} to {:?}", tmp_file.path().unwrap(), original_file.path().unwrap() ), ErrorType::General, ); - return; - } - - obj.window_inspect(|w| w.show_specific_image(original_file)); - } + return; } + + obj.window_inspect(|w| w.show_specific_image(original_file)); } } } diff --git a/src/widgets/image/input_handling.rs b/src/widgets/image/input_handling.rs index e69317c..c72abdf 100644 --- a/src/widgets/image/input_handling.rs +++ b/src/widgets/image/input_handling.rs @@ -303,17 +303,17 @@ impl imp::LpImage { let zoom = imp.zoom_target.get() * scale; // Move image with fingers on touchscreens - if gesture.device().map(|x| x.source()) == Some(gdk::InputSource::Touchscreen) { - if let p1 @ Some((x1, y1)) = gesture.bounding_box_center() { - if let Some((x0, y0)) = imp.zoom_gesture_center.get() { - imp.set_hadj_value(imp.hadj_value() + x0 - x1); - imp.set_vadj_value(imp.vadj_value() + y0 - y1); - } else { - tracing::warn!("Zoom bounding box center: No previous value"); - } - - imp.zoom_gesture_center.set(p1); + if gesture.device().map(|x| x.source()) == Some(gdk::InputSource::Touchscreen) + && let p1 @ Some((x1, y1)) = gesture.bounding_box_center() + { + if let Some((x0, y0)) = imp.zoom_gesture_center.get() { + imp.set_hadj_value(imp.hadj_value() + x0 - x1); + imp.set_vadj_value(imp.vadj_value() + y0 - y1); + } else { + tracing::warn!("Zoom bounding box center: No previous value"); } + + imp.zoom_gesture_center.set(p1); } let zoom_out_threshold = 1. / ZOOM_GESTURE_LOCK_THRESHOLD; diff --git a/src/widgets/image/loading.rs b/src/widgets/image/loading.rs index ea9c6c4..f448d3a 100644 --- a/src/widgets/image/loading.rs +++ b/src/widgets/image/loading.rs @@ -186,19 +186,19 @@ impl imp::LpImage { } pub(super) fn request_tiles(&self) { - if let Some(decoder) = self.decoder.borrow().as_ref() { - if self.zoom_animation().state() != adw::AnimationState::Playing { - // Force minimum tile size of 1000x1000 since with smaller - // tiles the tiled rendering advantage disappears - let x_inset = f32::min(-3., (self.viewport().width() - 1000.) / 2.); - let y_inset = f32::min(-3., (self.viewport().height() - 1000.) / 2.); + if let Some(decoder) = self.decoder.borrow().as_ref() + && self.zoom_animation().state() != adw::AnimationState::Playing + { + // Force minimum tile size of 1000x1000 since with smaller + // tiles the tiled rendering advantage disappears + let x_inset = f32::min(-3., (self.viewport().width() - 1000.) / 2.); + let y_inset = f32::min(-3., (self.viewport().height() - 1000.) / 2.); - decoder.request(crate::decoder::TileRequest { - viewport: self.viewport().inset_r(x_inset, y_inset), - area: self.preload_area(), - zoom: self.zoom_target.get(), - }); - } + decoder.request(crate::decoder::TileRequest { + viewport: self.viewport().inset_r(x_inset, y_inset), + area: self.preload_area(), + zoom: self.zoom_target.get(), + }); } } @@ -287,19 +287,16 @@ impl imp::LpImage { async fn check_editable(&self) { let obj = self.obj(); - if let Some(mime_type) = obj.metadata().mime_type() { - if let Ok(supported_formats) = glycin::config::Config::cached() + if let Some(mime_type) = obj.metadata().mime_type() + && let Ok(supported_formats) = glycin::config::Config::cached() .await .editor(&mime_type.as_str().into()) - { - if LpEditWindow::REQUIRED_OPERATIONS - .iter() - .all(|x| supported_formats.operations.contains(x)) - { - self.editable.set(true); - obj.notify_editable(); - } - } + && LpEditWindow::REQUIRED_OPERATIONS + .iter() + .all(|x| supported_formats.operations.contains(x)) + { + self.editable.set(true); + obj.notify_editable(); }; } } diff --git a/src/widgets/image/rendering.rs b/src/widgets/image/rendering.rs index 8c3e3d9..4fd3069 100644 --- a/src/widgets/image/rendering.rs +++ b/src/widgets/image/rendering.rs @@ -158,62 +158,61 @@ impl WidgetImpl for imp::LpImage { let image_width = image_width_i32 as f64; let image_height = image_height_i32 as f64; - if image_width_i32 > 0 && image_height_i32 > 0 { - if let Some((monitor_width, monitor_height)) = self.monitor_size() { - let hidpi_scale = self.scaling(); - tracing::trace!("Physical monitor dimensions: {monitor_width} x {monitor_height}"); + if image_width_i32 > 0 + && image_height_i32 > 0 + && let Some((monitor_width, monitor_height)) = self.monitor_size() + { + let hidpi_scale = self.scaling(); + tracing::trace!("Physical monitor dimensions: {monitor_width} x {monitor_height}"); - // areas - let monitor_area = monitor_width * monitor_height; - let logical_monitor_area = monitor_area * hidpi_scale.powi(2); - let image_area = image_width * image_height; + // areas + let monitor_area = monitor_width * monitor_height; + let logical_monitor_area = monitor_area * hidpi_scale.powi(2); + let image_area = image_width * image_height; - let occupy_area_factor = if logical_monitor_area <= SMALL_SCREEN_AREA { - tracing::trace!( - "Small monitor detected: Using {SMALL_OCCUPY_SCREEN} screen area" - ); - SMALL_OCCUPY_SCREEN - } else { - tracing::trace!( - "Sufficiently large monitor detected: Using {DEFAULT_OCCUPY_SCREEN} screen area" - ); - DEFAULT_OCCUPY_SCREEN - }; + let occupy_area_factor = if logical_monitor_area <= SMALL_SCREEN_AREA { + tracing::trace!("Small monitor detected: Using {SMALL_OCCUPY_SCREEN} screen area"); + SMALL_OCCUPY_SCREEN + } else { + tracing::trace!( + "Sufficiently large monitor detected: Using {DEFAULT_OCCUPY_SCREEN} screen area" + ); + DEFAULT_OCCUPY_SCREEN + }; - // factor for width and height that will achieve the desired area - // occupation derived from: - // monitor_area * occupy_area_factor == - // (image_width * size_scale) * (image_height * size_scale) - let size_scale = f64::sqrt(monitor_area / image_area * occupy_area_factor); - // ensure that we never increase image size - let target_scale = f64::min(1.0, size_scale); - let mut nat_width = image_width * target_scale; - let mut nat_height = image_height * target_scale; + // factor for width and height that will achieve the desired area + // occupation derived from: + // monitor_area * occupy_area_factor == + // (image_width * size_scale) * (image_height * size_scale) + let size_scale = f64::sqrt(monitor_area / image_area * occupy_area_factor); + // ensure that we never increase image size + let target_scale = f64::min(1.0, size_scale); + let mut nat_width = image_width * target_scale; + let mut nat_height = image_height * target_scale; - // Scale down if targeted occupation does not fit horizontally - // Add some margin to not touch corners - let max_width = monitor_width - 20.; - if nat_width > max_width { - nat_width = max_width; - nat_height = image_height * nat_width / image_width; - } - - // Same for vertical size - // Additionally substract some space for HeaderBar and Shell bar - let max_height = monitor_height - (50. + 35. + 20.) * hidpi_scale; - if nat_height > max_height { - nat_height = max_height; - nat_width = image_width * nat_height / image_height; - } - - let size = match orientation { - gtk::Orientation::Horizontal => (nat_width / hidpi_scale).round(), - gtk::Orientation::Vertical => (nat_height / hidpi_scale).round(), - _ => unreachable!(), - }; - - return (0, size as i32, -1, -1); + // Scale down if targeted occupation does not fit horizontally + // Add some margin to not touch corners + let max_width = monitor_width - 20.; + if nat_width > max_width { + nat_width = max_width; + nat_height = image_height * nat_width / image_width; } + + // Same for vertical size + // Additionally substract some space for HeaderBar and Shell bar + let max_height = monitor_height - (50. + 35. + 20.) * hidpi_scale; + if nat_height > max_height { + nat_height = max_height; + nat_width = image_width * nat_height / image_height; + } + + let size = match orientation { + gtk::Orientation::Horizontal => (nat_width / hidpi_scale).round(), + gtk::Orientation::Vertical => (nat_height / hidpi_scale).round(), + _ => unreachable!(), + }; + + return (0, size as i32, -1, -1); } // fallback if monitor size or image size is not known: @@ -338,18 +337,17 @@ impl imp::LpImage { pub fn monitor_size(&self) -> Option<(f64, f64)> { let obj = self.obj(); - if let Some(display) = gdk::Display::default() { - if let Some(surface) = obj.native().and_then(|x| x.surface()) { - if let Some(monitor) = display.monitor_at_surface(&surface) { - let hidpi_scale = self.scaling(); - let monitor_geometry = monitor.geometry(); + if let Some(display) = gdk::Display::default() + && let Some(surface) = obj.native().and_then(|x| x.surface()) + && let Some(monitor) = display.monitor_at_surface(&surface) + { + let hidpi_scale = self.scaling(); + let monitor_geometry = monitor.geometry(); - return Some(( - monitor_geometry.width() as f64 * hidpi_scale, - monitor_geometry.height() as f64 * hidpi_scale, - )); - } - } + return Some(( + monitor_geometry.width() as f64 * hidpi_scale, + monitor_geometry.height() as f64 * hidpi_scale, + )); } None diff --git a/src/widgets/image/rotation.rs b/src/widgets/image/rotation.rs index cb3720f..25ae67e 100644 --- a/src/widgets/image/rotation.rs +++ b/src/widgets/image/rotation.rs @@ -104,14 +104,14 @@ impl LpImage { zoom_animation.play(); } - if let Ok(r) = gufo_common::orientation::Rotation::try_from(angle) { - if r != gufo_common::orientation::Rotation::_0 { - tracing::debug!("Editing image to rotate by {r:?}"); - let operation = glycin::Operation::Rotate(r); - let editing_queue = &self.imp().editing_queue; - editing_queue.push(operation); - editing_queue.write_to_image(self); - } + if let Ok(r) = gufo_common::orientation::Rotation::try_from(angle) + && r != gufo_common::orientation::Rotation::_0 + { + tracing::debug!("Editing image to rotate by {r:?}"); + let operation = glycin::Operation::Rotate(r); + let editing_queue = &self.imp().editing_queue; + editing_queue.push(operation); + editing_queue.write_to_image(self); } } } diff --git a/src/widgets/image_view.rs b/src/widgets/image_view.rs index 2499fc1..11fa466 100644 --- a/src/widgets/image_view.rs +++ b/src/widgets/image_view.rs @@ -303,10 +303,10 @@ mod imp { #[weak] obj, move |_| { - if let Some(page) = obj.current_page() { - if let Some(value) = obj.zoom_value_f64() { - page.image().zoom_to_no_best_fit(value); - } + if let Some(page) = obj.current_page() + && let Some(value) = obj.zoom_value_f64() + { + page.image().zoom_to_no_best_fit(value); } } )); @@ -717,13 +717,13 @@ impl LpImageView { /// Handle files are added or removed from directory fn model_content_changed_cb(&self, file_event: &FileEvent) { // Animate to image that was restored from trash - if let Some(trash_restore) = self.trash_restore() { - if self.model().contains_file(&trash_restore) { - self.set_trash_restore(gio::File::NONE); - self.update_sliding_view(&trash_restore); - self.scroll_sliding_view(&trash_restore, true); - return; - } + if let Some(trash_restore) = self.trash_restore() + && self.model().contains_file(&trash_restore) + { + self.set_trash_restore(gio::File::NONE); + self.update_sliding_view(&trash_restore); + self.scroll_sliding_view(&trash_restore, true); + return; } let Some(current_file) = self.current_file() else { @@ -742,10 +742,10 @@ impl LpImageView { page.image().init(&target_file); } // File that delayed move is set for is now available - if let Some(delayed_current_file) = self.delayed_current_file() { - if target_file.equal(&delayed_current_file) { - self.navigate_to_file(&delayed_current_file); - } + if let Some(delayed_current_file) = self.delayed_current_file() + && target_file.equal(&delayed_current_file) + { + self.navigate_to_file(&delayed_current_file); } } FileEvent::New(new) => { diff --git a/src/widgets/image_window.rs b/src/widgets/image_window.rs index 6d4052d..b11bde5 100644 --- a/src/widgets/image_window.rs +++ b/src/widgets/image_window.rs @@ -575,10 +575,10 @@ impl LpImageWindow { if let Some(ref file) = imp.image_view.current_file() { let launcher = gtk::FileLauncher::new(Some(file)); launcher.set_always_ask(true); - if let Err(e) = launcher.launch_future(self.try_window().as_ref()).await { - if !e.matches(gtk::DialogError::Dismissed) { - tracing::error!("Could not open image in external program: {}", e); - } + if let Err(e) = launcher.launch_future(self.try_window().as_ref()).await + && !e.matches(gtk::DialogError::Dismissed) + { + tracing::error!("Could not open image in external program: {}", e); } } else { tracing::error!("Could not load a path for the current image.") @@ -611,12 +611,11 @@ impl LpImageWindow { return false; }; - if let Some(focus_widget) = GtkWindowExt::focus(&window) { - if focus_widget.is_ancestor(&*self.imp().properties_view) { - if let Ok(label) = focus_widget.downcast::() { - return label.selection_bounds().is_some(); - } - } + if let Some(focus_widget) = GtkWindowExt::focus(&window) + && focus_widget.is_ancestor(&*self.imp().properties_view) + && let Ok(label) = focus_widget.downcast::() + { + return label.selection_bounds().is_some(); } false diff --git a/src/widgets/print.rs b/src/widgets/print.rs index 5fb519d..b1fdb4a 100644 --- a/src/widgets/print.rs +++ b/src/widgets/print.rs @@ -388,17 +388,15 @@ mod imp { .set_value(size_unit.round(obj.original_size().0 * obj.width_unit_factor())); // Default to inch for USA and Liberia - if let Some(unit_locale) = getlocale(gettextrs::LocaleCategory::LcMeasurement) { - if let Some(locale) = unit_locale + if let Some(unit_locale) = getlocale(gettextrs::LocaleCategory::LcMeasurement) + && let Some(locale) = unit_locale .split(|x| *x == b'_') .nth(1) .and_then(|x| x.get(0..2)) - { - if locale == b"US" || locale == b"LR" { - imp.margin_unit.set_selected(Unit::Inch as u32); - imp.size_unit.set_selected(Unit::Inch as u32); - } - } + && (locale == b"US" || locale == b"LR") + { + imp.margin_unit.set_selected(Unit::Inch as u32); + imp.size_unit.set_selected(Unit::Inch as u32); } let orientation = match obj.page_setup().orientation() { diff --git a/src/widgets/sliding_view.rs b/src/widgets/sliding_view.rs index c2681be..9bbb606 100644 --- a/src/widgets/sliding_view.rs +++ b/src/widgets/sliding_view.rs @@ -246,20 +246,19 @@ mod imp { if matches!( *self.position_tracking.borrow(), PositionTracking::Position(_) - ) { - if let Some(mut child) = obj.first_child() { - let mut children = vec![child.clone()]; - while let Some(new_child) = child.next_sibling() { - child = new_child; - children.push(child.clone()); - } + ) && let Some(mut child) = obj.first_child() + { + let mut children = vec![child.clone()]; + while let Some(new_child) = child.next_sibling() { + child = new_child; + children.push(child.clone()); + } - for child in children { - if let Ok(page) = child.downcast::() { - if obj.index_of(&page).is_none() { - page.unparent(); - } - } + for child in children { + if let Ok(page) = child.downcast::() + && obj.index_of(&page).is_none() + { + page.unparent(); } } } @@ -612,19 +611,18 @@ impl LpSlidingView { /// Move to image with cards animation fn step_to(&self, page: &LpImagePage) { let animation = self.step_animation(); - if let Some(prev_page) = self.current_page() { - if let (Some(prev_index), Some(new_index)) = + if let Some(prev_page) = self.current_page() + && let (Some(prev_index), Some(new_index)) = (self.index_of(&prev_page), self.index_of(page)) - { - let backward = new_index < prev_index; + { + let backward = new_index < prev_index; - *self.imp().position_tracking.borrow_mut() = - PositionTracking::StackedCards(StackedCards::new(prev_page, backward)); - // Set current page here as well, to make sure it's set when animation completes - // for disabled animations - self.set_current_page(Some(page)); - animation.play(); - } + *self.imp().position_tracking.borrow_mut() = + PositionTracking::StackedCards(StackedCards::new(prev_page, backward)); + // Set current page here as well, to make sure it's set when animation completes + // for disabled animations + self.set_current_page(Some(page)); + animation.play(); } self.set_current_page(Some(page)); }