[yew-devel] [PATCH yew-comp 19/20] rrd: refactor selection rectangle calculation

Dominik Csapak d.csapak at proxmox.com
Fri May 30 14:22:01 CEST 2025


makes the view method a bit easier to read
we can omit the subtraction for start and end here, since the selection
must be int he correct range anyway.

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/rrd/graph.rs | 42 +++++++++++++++++++++++++-----------------
 1 file changed, 25 insertions(+), 17 deletions(-)

diff --git a/src/rrd/graph.rs b/src/rrd/graph.rs
index c54d288..5ffc29d 100644
--- a/src/rrd/graph.rs
+++ b/src/rrd/graph.rs
@@ -282,29 +282,18 @@ impl PwtRRDGraph {
             ]);
         }
 
+        // draw selection rectangle
         if let Some((start, end)) = &self.selection {
-            let start = (*start).min(data0.len() - 1);
-            let end = (*end).min(data0.len() - 1);
-
-            match (data0.get(start), data0.get(end)) {
+            match (data0.get(*start), data0.get(*end)) {
                 (Some(start_data), Some(end_data)) => {
-                    let mut start_x = self.graph_space.compute_x(*start_data);
-                    let mut end_x = self.graph_space.compute_x(*end_data);
-
-                    if start_x > end_x {
-                        std::mem::swap(&mut start_x, &mut end_x);
-                    }
-
-                    let (start_y, end_y) =
-                        self.graph_space.get_y_range(CoordinateRange::InsideBorder);
-
+                    let (x, y, width, height) = self.get_selection_rect(*start_data, *end_data);
                     children.push(
                         Rect::new()
                             .key("selection-rect")
                             .class("pwt-rrd-selection")
-                            .position(start_x as f32, end_y as f32)
-                            .width((end_x - start_x) as f32)
-                            .height((start_y - end_y) as f32)
+                            .position(x, y)
+                            .width(width)
+                            .height(height)
                             .into(),
                     );
                 }
@@ -397,6 +386,25 @@ impl PwtRRDGraph {
             .into()
     }
 
+    // returns x, y, width, height
+    fn get_selection_rect(&self, start: i64, end: i64) -> (f32, f32, f32, f32) {
+        let mut start_x = self.graph_space.compute_x(start);
+        let mut end_x = self.graph_space.compute_x(end);
+
+        if start_x > end_x {
+            std::mem::swap(&mut start_x, &mut end_x);
+        }
+
+        let (start_y, end_y) = self.graph_space.get_y_range(CoordinateRange::InsideBorder);
+
+        (
+            start_x as f32,
+            end_y as f32,
+            (end_x - start_x) as f32,
+            (start_y - end_y) as f32,
+        )
+    }
+
     fn offset_to_time_index(&self, x: i32, data0: &[i64]) -> usize {
         let t = self.graph_space.original_x(x as f64);
         let start_index = data0.partition_point(|&x| x < t);
-- 
2.39.5





More information about the yew-devel mailing list