From 16fa4c4bd668399e145e69ae5861d1b13f16f5c0 Mon Sep 17 00:00:00 2001 From: Integral Date: Fri, 1 Nov 2024 14:26:38 +0800 Subject: [PATCH] refactor: destructure tuples to enhance readability --- .../src/builders/penpathmodeledbuilder.rs | 12 ++++----- crates/rnote-compose/src/color.rs | 18 +++---------- crates/rnote-engine/src/document/format.rs | 6 ++--- crates/rnote-engine/src/engine/snapshot.rs | 10 ++++--- .../src/pens/pensconfig/brushconfig.rs | 8 +++--- .../rnote-engine/src/strokes/bitmapimage.rs | 18 +++++-------- crates/rnote-engine/src/strokes/resize.rs | 4 +-- .../rnote-engine/src/strokes/vectorimage.rs | 26 ++++++++----------- crates/rnote-ui/src/appwindow/appsettings.rs | 12 ++------- .../rnote-ui/src/canvas/widgetflagsboxed.rs | 4 +-- crates/rnote-ui/src/canvaswrapper.rs | 4 +-- .../strokewidthpicker/strokewidthpreview.rs | 16 ++++++------ 12 files changed, 57 insertions(+), 81 deletions(-) diff --git a/crates/rnote-compose/src/builders/penpathmodeledbuilder.rs b/crates/rnote-compose/src/builders/penpathmodeledbuilder.rs index 68f14ed6e0..074b389342 100644 --- a/crates/rnote-compose/src/builders/penpathmodeledbuilder.rs +++ b/crates/rnote-compose/src/builders/penpathmodeledbuilder.rs @@ -174,9 +174,9 @@ impl PenPathModeledBuilder { match self.stroke_modeler.update(modeler_input) { Ok(results) => self.buffer.extend(results.into_iter().map(|r| { - let pos = r.pos; + let (x, y) = r.pos; let pressure = r.pressure; - Element::new(na::vector![pos.0, pos.1], pressure) + Element::new(na::vector![x, y], pressure) })), Err(e) => { match e { @@ -224,9 +224,9 @@ impl PenPathModeledBuilder { Ok(results) => results .into_iter() .map(|r| { - let pos = r.pos; + let (x, y) = r.pos; let pressure = r.pressure; - Element::new(na::vector![pos.0, pos.1], pressure) + Element::new(na::vector![x, y], pressure) }) .collect::>(), Err(e) => { @@ -256,9 +256,9 @@ impl PenPathModeledBuilder { }) { Ok(results) => { self.buffer.extend(results.into_iter().map(|r| { - let pos = r.pos; + let (x, y) = r.pos; let pressure = r.pressure; - Element::new(na::vector![pos.0, pos.1], pressure) + Element::new(na::vector![x, y], pressure) })); } Err(e) => { diff --git a/crates/rnote-compose/src/color.rs b/crates/rnote-compose/src/color.rs index 436f4e7861..7569d57a95 100644 --- a/crates/rnote-compose/src/color.rs +++ b/crates/rnote-compose/src/color.rs @@ -168,13 +168,8 @@ impl Color { impl From for Color { fn from(piet_color: piet::Color) -> Self { - let piet_rgba = piet_color.as_rgba(); - Self { - r: piet_rgba.0, - g: piet_rgba.1, - b: piet_rgba.2, - a: piet_rgba.3, - } + let (r, g, b, a) = piet_color.as_rgba(); + Self { r, g, b, a } } } @@ -185,13 +180,8 @@ impl From for piet::Color { } impl From<(f64, f64, f64, f64)> for Color { - fn from(tuple: (f64, f64, f64, f64)) -> Self { - Self { - r: tuple.0, - g: tuple.1, - b: tuple.2, - a: tuple.3, - } + fn from((r, g, b, a): (f64, f64, f64, f64)) -> Self { + Self { r, g, b, a } } } diff --git a/crates/rnote-engine/src/document/format.rs b/crates/rnote-engine/src/document/format.rs index 940a1e7184..04e382c3f6 100644 --- a/crates/rnote-engine/src/document/format.rs +++ b/crates/rnote-engine/src/document/format.rs @@ -64,12 +64,12 @@ impl PredefinedFormat { PredefinedFormat::UsLegal => Some((215.9, 355.6)), PredefinedFormat::Custom => None, }; - if let Some(size_portrait) = &mut size_portrait { + if let Some((mut width, mut height)) = &mut size_portrait { if orientation == Orientation::Landscape { - std::mem::swap(&mut size_portrait.0, &mut size_portrait.1); + std::mem::swap(&mut width, &mut height); } } - size_portrait.map(|s| na::vector![s.0, s.1]) + size_portrait.map(|(width, height)| na::vector![width, height]) } } diff --git a/crates/rnote-engine/src/engine/snapshot.rs b/crates/rnote-engine/src/engine/snapshot.rs index 233bbea31b..b65f04eea4 100644 --- a/crates/rnote-engine/src/engine/snapshot.rs +++ b/crates/rnote-engine/src/engine/snapshot.rs @@ -82,10 +82,12 @@ impl EngineSnapshot { .pages .iter() .map(|page| (page.width, page.height)) - .fold((0_f64, 0_f64), |prev, next| { - // Max of width, sum heights - (prev.0.max(next.0), prev.1 + next.1) - }); + .fold( + (0_f64, 0_f64), + |(prev_width, prev_height), (next_width, next_height)| { + (prev_width.max(next_width), prev_height + next_height) + }, + ); let no_pages = xopp_file.xopp_root.pages.len() as u32; let mut engine = Engine::default(); diff --git a/crates/rnote-engine/src/pens/pensconfig/brushconfig.rs b/crates/rnote-engine/src/pens/pensconfig/brushconfig.rs index a9e868b737..ff448357ee 100644 --- a/crates/rnote-engine/src/pens/pensconfig/brushconfig.rs +++ b/crates/rnote-engine/src/pens/pensconfig/brushconfig.rs @@ -132,14 +132,14 @@ impl BrushConfig { pub(crate) fn style_for_current_options(&self) -> Style { match &self.style { BrushStyle::Marker => { - let options = self.marker_options.clone(); + let MarkerOptions(options) = self.marker_options.clone(); - Style::Smooth(options.0) + Style::Smooth(options) } BrushStyle::Solid => { - let options = self.solid_options.clone(); + let SolidOptions(options) = self.solid_options.clone(); - Style::Smooth(options.0) + Style::Smooth(options) } BrushStyle::Textured => { let options = self.textured_options.clone(); diff --git a/crates/rnote-engine/src/strokes/bitmapimage.rs b/crates/rnote-engine/src/strokes/bitmapimage.rs index d1af8f77f9..58d57ff71b 100644 --- a/crates/rnote-engine/src/strokes/bitmapimage.rs +++ b/crates/rnote-engine/src/strokes/bitmapimage.rs @@ -154,9 +154,9 @@ impl BitmapImage { let page = doc .page(page_i as i32) .ok_or_else(|| anyhow::anyhow!("no page at index '{page_i}"))?; - let intrinsic_size = page.size(); - let width = intrinsic_size.0 * page_zoom; - let height = intrinsic_size.1 * page_zoom; + let (intrinsic_width, intrinsic_height) = page.size(); + let width = intrinsic_width * page_zoom; + let height = intrinsic_height * page_zoom; let surface_width = (width * pdf_import_prefs.bitmap_scalefactor).round() as i32; let surface_height = (height * pdf_import_prefs.bitmap_scalefactor).round() as i32; let surface = cairo::ImageSurface::create( @@ -188,20 +188,16 @@ impl BitmapImage { if pdf_import_prefs.page_borders { // Draw outline around page - cx.set_source_rgba( - color::GNOME_REDS[4].as_rgba().0, - color::GNOME_REDS[4].as_rgba().1, - color::GNOME_REDS[4].as_rgba().2, - 1.0, - ); + let (red, green, blue, _) = color::GNOME_REDS[4].as_rgba(); + cx.set_source_rgba(red, green, blue, 1.0); let line_width = 1.0; cx.set_line_width(line_width); cx.rectangle( line_width * 0.5, line_width * 0.5, - intrinsic_size.0 - line_width, - intrinsic_size.1 - line_width, + intrinsic_width - line_width, + intrinsic_height - line_width, ); cx.stroke()?; } diff --git a/crates/rnote-engine/src/strokes/resize.rs b/crates/rnote-engine/src/strokes/resize.rs index 1cc8b050be..61e5f58cbb 100644 --- a/crates/rnote-engine/src/strokes/resize.rs +++ b/crates/rnote-engine/src/strokes/resize.rs @@ -107,7 +107,7 @@ pub fn calculate_resize_ratio( ratios .iter() .zip(apply_ratios) - .filter(|x| x.1) - .fold(1.0f64, |acc, x| acc.min(*x.0)) + .filter(|(_, x)| *x) + .fold(1.0f64, |acc, (x, _)| acc.min(*x)) .max(1e-15f64) //force the value to be positive as a zero would make transforms crash } diff --git a/crates/rnote-engine/src/strokes/vectorimage.rs b/crates/rnote-engine/src/strokes/vectorimage.rs index 75f932ac45..fcec15f28f 100644 --- a/crates/rnote-engine/src/strokes/vectorimage.rs +++ b/crates/rnote-engine/src/strokes/vectorimage.rs @@ -231,23 +231,23 @@ impl VectorImage { let svgs = page_range .filter_map(|page_i| { let page = doc.page(page_i as i32)?; - let intrinsic_size = page.size(); - let width = intrinsic_size.0 * page_zoom; - let height = intrinsic_size.1 * page_zoom; + let (intrinsic_width, intrinsic_height) = page.size(); + let width = intrinsic_width * page_zoom; + let height = intrinsic_height * page_zoom; let res = move || -> anyhow::Result { let svg_stream: Vec = vec![]; let mut svg_surface = cairo::SvgSurface::for_stream( - intrinsic_size.0, - intrinsic_size.1, + intrinsic_width, + intrinsic_height, svg_stream, ) .map_err(|e| { anyhow::anyhow!( "Creating SvgSurface with dimensions ({}, {}) failed, Err: {e:?}", - intrinsic_size.0, - intrinsic_size.1 + intrinsic_width, + intrinsic_height ) })?; @@ -268,20 +268,16 @@ impl VectorImage { if pdf_import_prefs.page_borders { // Draw outline around page - cx.set_source_rgba( - color::GNOME_REDS[4].as_rgba().0, - color::GNOME_REDS[4].as_rgba().1, - color::GNOME_REDS[4].as_rgba().2, - 1.0, - ); + let (red, green, blue, _) = color::GNOME_REDS[4].as_rgba(); + cx.set_source_rgba(red, green, blue, 1.0); let line_width = 1.0; cx.set_line_width(line_width); cx.rectangle( line_width * 0.5, line_width * 0.5, - intrinsic_size.0 - line_width, - intrinsic_size.1 - line_width, + intrinsic_width - line_width, + intrinsic_height - line_width, ); cx.stroke()?; } diff --git a/crates/rnote-ui/src/appwindow/appsettings.rs b/crates/rnote-ui/src/appwindow/appsettings.rs index 56aa2ded03..fbee1643be 100644 --- a/crates/rnote-ui/src/appwindow/appsettings.rs +++ b/crates/rnote-ui/src/appwindow/appsettings.rs @@ -137,16 +137,8 @@ impl RnAppWindow { // colorpicker palette let gdk_color_mapping = |var: &glib::Variant, _: glib::Type| { - let color = var.get::<(f64, f64, f64, f64)>()?; - Some( - gdk::RGBA::new( - color.0 as f32, - color.1 as f32, - color.2 as f32, - color.3 as f32, - ) - .to_value(), - ) + let (red, green, blue, alpha) = var.get::<(f64, f64, f64, f64)>()?; + Some(gdk::RGBA::new(red as f32, green as f32, blue as f32, alpha as f32).to_value()) }; let gdk_color_set_mapping = |val: &glib::Value, _: glib::VariantType| { let color = val.get::().ok()?; diff --git a/crates/rnote-ui/src/canvas/widgetflagsboxed.rs b/crates/rnote-ui/src/canvas/widgetflagsboxed.rs index 6bd37324fb..07dc97bb7d 100644 --- a/crates/rnote-ui/src/canvas/widgetflagsboxed.rs +++ b/crates/rnote-ui/src/canvas/widgetflagsboxed.rs @@ -13,8 +13,8 @@ impl From for WidgetFlagsBoxed { } impl From for WidgetFlags { - fn from(value: WidgetFlagsBoxed) -> Self { - value.0 + fn from(WidgetFlagsBoxed(value): WidgetFlagsBoxed) -> Self { + value } } diff --git a/crates/rnote-ui/src/canvaswrapper.rs b/crates/rnote-ui/src/canvaswrapper.rs index a860f336f8..59521ff51c 100644 --- a/crates/rnote-ui/src/canvaswrapper.rs +++ b/crates/rnote-ui/src/canvaswrapper.rs @@ -503,7 +503,7 @@ mod imp { bbcenter_begin.set( gesture .bounding_box_center() - .map(|coords| na::vector![coords.0, coords.1]), + .map(|(x, y)| na::vector![x, y]), ); offset_begin.set(canvaswrapper.canvas().engine_ref().camera.offset()); } @@ -536,7 +536,7 @@ mod imp { if let Some(bbcenter_current) = gesture .bounding_box_center() - .map(|coords| na::vector![coords.0, coords.1]) + .map(|(x, y)| na::vector![x, y]) { let bbcenter_begin = if let Some(bbcenter_begin) = bbcenter_begin.get() { diff --git a/crates/rnote-ui/src/strokewidthpicker/strokewidthpreview.rs b/crates/rnote-ui/src/strokewidthpicker/strokewidthpreview.rs index fc0d6890e8..da7645cfb0 100644 --- a/crates/rnote-ui/src/strokewidthpicker/strokewidthpreview.rs +++ b/crates/rnote-ui/src/strokewidthpicker/strokewidthpreview.rs @@ -103,8 +103,8 @@ mod imp { fn snapshot(&self, snapshot: >k4::Snapshot) { let obj = self.obj(); - let size = (obj.width() as f32, obj.height() as f32); - let center = (size.0 * 0.5, size.1 * 0.5); + let (width, height) = (obj.width() as f32, obj.height() as f32); + let (center_x, center_y) = (width * 0.5, height * 0.5); let stroke_width = self.stroke_width.get(); // accessing colors through the style context is deprecated, @@ -125,7 +125,7 @@ mod imp { (MAX_RADIUS * stroke_width * 0.5) / (MAX_RADIUS * 0.5 + stroke_width * 0.5); let cairo_cx = - snapshot.append_cairo(&graphene::Rect::new(0.0, 0.0, size.0, size.1)); + snapshot.append_cairo(&graphene::Rect::new(0.0, 0.0, width, height)); cairo_cx.set_source_rgba( window_fg_color.red() as f64, window_fg_color.green() as f64, @@ -133,8 +133,8 @@ mod imp { window_fg_color.alpha() as f64, ); cairo_cx.arc( - center.0 as f64, - center.1 as f64, + center_x as f64, + center_y as f64, circle_radius, 0.0, std::f64::consts::PI * 2.0, @@ -151,7 +151,7 @@ mod imp { / (MAX_HALF_EXTENTS * 0.5 + stroke_width * 0.5); let cairo_cx = - snapshot.append_cairo(&graphene::Rect::new(0.0, 0.0, size.0, size.1)); + snapshot.append_cairo(&graphene::Rect::new(0.0, 0.0, width, height)); cairo_cx.set_source_rgba( window_fg_color.red() as f64, window_fg_color.green() as f64, @@ -160,8 +160,8 @@ mod imp { ); cairo_rounded_rect( &cairo_cx, - size.0 as f64 * 0.5 - square_half_extents, - size.1 as f64 * 0.5 - square_half_extents, + width as f64 * 0.5 - square_half_extents, + height as f64 * 0.5 - square_half_extents, square_half_extents * 2.0, square_half_extents * 2.0, 3.0,