Skip to content

Commit

Permalink
refactor: destructure tuples to enhance readability
Browse files Browse the repository at this point in the history
  • Loading branch information
Integral-Tech committed Nov 1, 2024
1 parent d7f693d commit 16fa4c4
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 81 deletions.
12 changes: 6 additions & 6 deletions crates/rnote-compose/src/builders/penpathmodeledbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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::<Vec<Element>>(),
Err(e) => {
Expand Down Expand Up @@ -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) => {
Expand Down
18 changes: 4 additions & 14 deletions crates/rnote-compose/src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,8 @@ impl Color {

impl From<piet::Color> 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 }
}
}

Expand All @@ -185,13 +180,8 @@ impl From<Color> 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 }
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/rnote-engine/src/document/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
}

Expand Down
10 changes: 6 additions & 4 deletions crates/rnote-engine/src/engine/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions crates/rnote-engine/src/pens/pensconfig/brushconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
18 changes: 7 additions & 11 deletions crates/rnote-engine/src/strokes/bitmapimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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()?;
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rnote-engine/src/strokes/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
26 changes: 11 additions & 15 deletions crates/rnote-engine/src/strokes/vectorimage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
let svg_stream: Vec<u8> = 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
)
})?;

Expand All @@ -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()?;
}
Expand Down
12 changes: 2 additions & 10 deletions crates/rnote-ui/src/appwindow/appsettings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<gdk::RGBA>().ok()?;
Expand Down
4 changes: 2 additions & 2 deletions crates/rnote-ui/src/canvas/widgetflagsboxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ impl From<WidgetFlags> for WidgetFlagsBoxed {
}

impl From<WidgetFlagsBoxed> for WidgetFlags {
fn from(value: WidgetFlagsBoxed) -> Self {
value.0
fn from(WidgetFlagsBoxed(value): WidgetFlagsBoxed) -> Self {
value
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/rnote-ui/src/canvaswrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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()
{
Expand Down
16 changes: 8 additions & 8 deletions crates/rnote-ui/src/strokewidthpicker/strokewidthpreview.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ mod imp {

fn snapshot(&self, snapshot: &gtk4::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,
Expand All @@ -125,16 +125,16 @@ 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,
window_fg_color.blue() as f64,
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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 16fa4c4

Please sign in to comment.