Skip to main content

ferrum_flow/
viewport.rs

1use gpui::{Bounds, Pixels, Point, px};
2
3#[derive(Debug, Clone)]
4pub struct Viewport {
5    pub zoom: f32,
6    pub offset: Point<Pixels>,
7    pub window_bounds: Option<Bounds<Pixels>>,
8}
9
10impl Viewport {
11    pub fn new() -> Self {
12        Self {
13            zoom: 1.0,
14            offset: Point::new(px(0.0), px(0.0)),
15            window_bounds: None,
16        }
17    }
18
19    pub fn world_to_screen(&self, p: Point<Pixels>) -> Point<Pixels> {
20        Point::new(
21            p.x * self.zoom + self.offset.x,
22            p.y * self.zoom + self.offset.y,
23        )
24    }
25
26    pub fn screen_to_world(&self, p: Point<Pixels>) -> Point<Pixels> {
27        Point::new(
28            (p.x - self.offset.x) / self.zoom,
29            (p.y - self.offset.y) / self.zoom,
30        )
31    }
32}