Skip to content

Commit

Permalink
refactor: Use f32 as much as possible
Browse files Browse the repository at this point in the history
  • Loading branch information
marc2332 committed Sep 3, 2022
1 parent 354f869 commit 9c2f1c2
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 97 deletions.
12 changes: 6 additions & 6 deletions layers-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ use std::collections::HashMap;
pub struct NodeData {
pub width: SizeMode,
pub height: SizeMode,
pub padding: (i32, i32, i32, i32),
pub padding: (f32, f32, f32, f32),
pub node: Option<Node<NodeState>>,
}

#[derive(Default, Copy, Clone, Debug, PartialEq, Eq)]
#[derive(Default, Copy, Clone, Debug, PartialEq)]
pub struct NodeArea {
pub x: i32,
pub y: i32,
pub width: i32,
pub height: i32,
pub x: f32,
pub y: f32,
pub width: f32,
pub height: f32,
}

#[derive(Default, Clone, Debug)]
Expand Down
10 changes: 5 additions & 5 deletions layout-engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn calculate_area(node: &NodeData, mut area: NodeArea, parent_area: NodeArea) ->
area.width = w;
}
SizeMode::Percentage(per) => {
area.width = ((parent_area.width as f32) / 100.0 * (per as f32)).round() as i32;
area.width = (parent_area.width / 100.0 * per).round();
}
SizeMode::Auto => {}
}
Expand All @@ -19,13 +19,13 @@ fn calculate_area(node: &NodeData, mut area: NodeArea, parent_area: NodeArea) ->
area.height = h;
}
SizeMode::Percentage(per) => {
area.height = ((parent_area.height as f32) / 100.0 * (per as f32)).round() as i32;
area.height = (parent_area.height / 100.0 * per).round();
}
SizeMode::Auto => {
if let Some(node) = &node.node {
if let NodeType::Element { tag, .. } = &node.node_type {
if tag == "text" {
area.height = 18;
area.height = 18.0;
}
}
}
Expand Down Expand Up @@ -105,13 +105,13 @@ pub fn calculate_node<T>(
remaining_inner_area.width -= child_node_area.width;

if child_node_area.width > remaining_inner_area.width
|| remaining_inner_area.width == 0
|| remaining_inner_area.width == 0.0
{
remaining_inner_area.width = child_node_area.width;
}

if child_node_area.height > remaining_inner_area.height
|| remaining_inner_area.height == 0
|| remaining_inner_area.height == 0.0
{
remaining_inner_area.height = child_node_area.height;
}
Expand Down
108 changes: 54 additions & 54 deletions layout-engine/tests/layouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,62 +6,62 @@ use state::node::SizeMode;
fn percentage() {
let result = calculate_node(
&NodeData {
width: SizeMode::Percentage(100),
height: SizeMode::Percentage(100),
padding: (0, 0, 0, 0),
width: SizeMode::Percentage(100.0),
height: SizeMode::Percentage(100.0),
padding: (0.0, 0.0, 0.0, 0.0),
node: None,
},
NodeArea {
x: 0,
y: 0,
height: 300,
width: 200,
x: 0.0,
y: 0.0,
height: 300.0,
width: 200.0,
},
NodeArea {
x: 0,
y: 0,
height: 300,
width: 200,
x: 0.0,
y: 0.0,
height: 300.0,
width: 200.0,
},
&mut (),
&mut Layers::default(),
|_, _| None,
0,
);

assert_eq!(result.height, 300);
assert_eq!(result.width, 200);
assert_eq!(result.height, 300.0);
assert_eq!(result.width, 200.0);
}

#[test]
fn manual() {
let result = calculate_node(
&NodeData {
width: SizeMode::Manual(250),
height: SizeMode::Manual(150),
padding: (0, 0, 0, 0),
width: SizeMode::Manual(250.0),
height: SizeMode::Manual(150.0),
padding: (0.0, 0.0, 0.0, 0.0),
node: None,
},
NodeArea {
x: 0,
y: 0,
height: 300,
width: 200,
x: 0.0,
y: 0.0,
height: 300.0,
width: 200.0,
},
NodeArea {
x: 0,
y: 0,
height: 300,
width: 200,
x: 0.0,
y: 0.0,
height: 300.0,
width: 200.0,
},
&mut (),
&mut Layers::default(),
|_, _| None,
0,
);

assert_eq!(result.height, 150);
assert_eq!(result.width, 250);
assert_eq!(result.height, 150.0);
assert_eq!(result.width, 250.0);
}

#[test]
Expand All @@ -70,36 +70,36 @@ fn auto() {
&NodeData {
width: SizeMode::Auto,
height: SizeMode::Auto,
padding: (0, 0, 0, 0),
padding: (0.0, 0.0, 0.0, 0.0),
node: None,
},
NodeArea {
x: 0,
y: 0,
height: 300,
width: 200,
x: 0.0,
y: 0.0,
height: 300.0,
width: 200.0,
},
NodeArea {
x: 0,
y: 0,
height: 300,
width: 200,
x: 0.0,
y: 0.0,
height: 300.0,
width: 200.0,
},
&mut (),
&mut Layers::default(),
|_, _| {
Some(NodeData {
width: SizeMode::Manual(170),
height: SizeMode::Percentage(25),
padding: (0, 0, 0, 0),
width: SizeMode::Manual(170.0),
height: SizeMode::Percentage(25.0),
padding: (0.0, 0.0, 0.0, 0.0),
node: None,
})
},
0,
);

assert_eq!(result.height, 300);
assert_eq!(result.width, 200);
assert_eq!(result.height, 300.0);
assert_eq!(result.width, 200.0);
}

#[test]
Expand All @@ -108,34 +108,34 @@ fn x_y() {
&NodeData {
width: SizeMode::Auto,
height: SizeMode::Auto,
padding: (0, 0, 0, 0),
padding: (0.0, 0.0, 0.0, 0.0),
node: None,
},
NodeArea {
x: 15,
y: 25,
height: 300,
width: 200,
x: 15.0,
y: 25.0,
height: 300.0,
width: 200.0,
},
NodeArea {
x: 15,
y: 25,
height: 300,
width: 200,
x: 15.0,
y: 25.0,
height: 300.0,
width: 200.0,
},
&mut (),
&mut Layers::default(),
|_, _| {
Some(NodeData {
width: SizeMode::Manual(170),
height: SizeMode::Percentage(25),
padding: (0, 0, 0, 0),
width: SizeMode::Manual(170.0),
height: SizeMode::Percentage(25.0),
padding: (0.0, 0.0, 0.0, 0.0),
node: None,
})
},
0,
);

assert_eq!(result.x, 15);
assert_eq!(result.y, 25);
assert_eq!(result.x, 15.0);
assert_eq!(result.y, 25.0);
}
8 changes: 4 additions & 4 deletions renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,10 @@ pub fn run(skia_dom: SkiaDom, rev_render: Receiver<()>, event_emitter: EventEmit
&self.skia_dom,
canvas,
NodeArea {
width: window_size.width as i32,
height: window_size.height as i32,
x: 0,
y: 0,
width: window_size.width as f32,
height: window_size.height as f32,
x: 0.0,
y: 0.0,
},
self.renderer_requests.clone(),
&self.event_emitter,
Expand Down
6 changes: 2 additions & 4 deletions renderer/src/renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,8 @@ pub fn render_skia(
let x2 = x + area.width;
let y2 = y + area.height;

//

let radius = node.state.style.radius;
let radius = if radius < 0 { 0 } else { radius };
let radius = if radius < 0.0 { 0.0 } else { radius };

let mut path = Path::new();

Expand Down Expand Up @@ -108,7 +106,7 @@ pub fn render_skia(
};

let x = area.x;
let y = area.y + 12; /* Line height, wip */
let y = area.y + 12.0; /* Line height, wip */

canvas.draw_str_align(text, (x, y), &font, &paint, Align::Left);
}
Expand Down
6 changes: 3 additions & 3 deletions renderer/src/work_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pub fn work_loop(

calculate_node(
&NodeData {
width: SizeMode::Percentage(100),
height: SizeMode::Percentage(100),
padding: (0, 0, 0, 0),
width: SizeMode::Percentage(100.0),
height: SizeMode::Percentage(100.0),
padding: (0.0, 0.0, 0.0, 0.0),
node: Some(root),
},
area.clone(),
Expand Down

0 comments on commit 9c2f1c2

Please sign in to comment.