Skip to content

Commit

Permalink
chore: appease clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
syakupov committed Oct 29, 2023
1 parent 3a5279d commit 3073e20
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
63 changes: 41 additions & 22 deletions src/ng/svg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ use leptos::*;

use crate::ng::{use_frame, Align, Frame, VAlign};

struct SvgProperties {
width: i32,
height: i32,
align: Align,
valign: VAlign,
scale: f32,
flip_x: bool,
flip_y: bool,
}

/// SVG widget.
#[component]
pub fn Svg(
Expand All @@ -15,7 +25,16 @@ pub fn Svg(
children: Children,
) -> impl IntoView {
let f = use_frame();
let transform = calc_transform(&f, width, height, align, valign, scale, flip_x, flip_y);
let props = SvgProperties {
width,
height,
align,
valign,
scale,
flip_x,
flip_y,
};
let transform = calc_transform(&f, &props);

view! { <g transform=transform>{children()}</g> }
}
Expand All @@ -33,38 +52,38 @@ pub fn SvgFile(
src: &'static str,
) -> impl IntoView {
let f = use_frame();
let transform = calc_transform(&f, width, height, align, valign, scale, flip_x, flip_y);
let props = SvgProperties {
width,
height,
align,
valign,
scale,
flip_x,
flip_y,
};
let transform = calc_transform(&f, &props);

view! { <g transform=transform inner_html=src></g> }
}

fn calc_transform(
f: &Frame,
width: i32,
height: i32,
align: Align,
valign: VAlign,
scale: f32,
flip_x: bool,
flip_y: bool,
) -> String {
let scale = if matches!(align, Align::Fill) || matches!(valign, VAlign::Fill) {
let sx = f.width as f32 / width as f32;
let sy = f.height as f32 / height as f32;
fn calc_transform(f: &Frame, props: &SvgProperties) -> String {
let scale = if matches!(props.align, Align::Fill) || matches!(props.valign, VAlign::Fill) {
let sx = f.width as f32 / props.width as f32;
let sy = f.height as f32 / props.height as f32;
sx.min(sy)
} else {
scale
props.scale
};

let width = (scale * width as f32).round() as i32;
let height = (scale * height as f32).round() as i32;
let width = (scale * props.width as f32).round() as i32;
let height = (scale * props.height as f32).round() as i32;

let mut x = match align {
let mut x = match props.align {
Align::Left => f.x,
Align::Center | Align::Fill => f.x + (f.width - width) / 2,
Align::Right => f.x + f.width - width,
};
let mut y = match valign {
let mut y = match props.valign {
VAlign::Top => f.y,
VAlign::Middle | VAlign::Fill => f.y + (f.height - height) / 2,
VAlign::Bottom => f.y + f.height - height,
Expand All @@ -73,11 +92,11 @@ fn calc_transform(
let mut sx = scale;
let mut sy = scale;

if flip_x {
if props.flip_x {
sx = -sx;
x += width;
}
if flip_y {
if props.flip_y {
sy = -sy;
y += height;
}
Expand Down
7 changes: 6 additions & 1 deletion src/ng/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ fn text_width(text: &str, canvas: &CanvasRenderingContext2d) -> i32 {
canvas.measure_text(text).unwrap().width() as i32
}

fn wrap(children: &[View], canvas: &CanvasRenderingContext2d, props: &TextProperties, frame: &Frame) -> Output {
fn wrap(
children: &[View],
canvas: &CanvasRenderingContext2d,
props: &TextProperties,
frame: &Frame,
) -> Output {
let children = children.iter().map(|item| {
if let View::Text(text) = item {
text.content.clone()
Expand Down

0 comments on commit 3073e20

Please sign in to comment.