Skip to main content

jellyflow_runtime/runtime/fit_view/
options.rs

1use crate::node_origin::normalize_node_origin;
2
3const MAX_FIT_VIEW_PADDING: f32 = 0.45;
4
5#[derive(Debug, Clone, Copy)]
6pub struct FitViewComputeOptions {
7    /// Viewport width in logical px.
8    pub viewport_width_px: f32,
9    /// Viewport height in logical px.
10    pub viewport_height_px: f32,
11    /// Node origin (anchor) used to interpret `FitViewNodeInfo.pos`.
12    ///
13    /// When `(0.0, 0.0)`, `pos` is treated as the node's top-left.
14    /// When `(0.5, 0.5)`, `pos` is treated as the node's center.
15    pub node_origin: (f32, f32),
16    /// Extra padding as a fraction of viewport size (0.0 .. 0.45 recommended).
17    ///
18    /// When `0.0`, `margin_px_fallback` is used instead.
19    pub padding: f32,
20    /// Fixed margin in logical px used when `padding == 0.0`.
21    pub margin_px_fallback: f32,
22    /// Minimum zoom clamp.
23    pub min_zoom: f32,
24    /// Maximum zoom clamp.
25    pub max_zoom: f32,
26}
27
28impl FitViewComputeOptions {
29    pub fn normalized(mut self) -> Option<Self> {
30        if !self.viewport_width_px.is_finite()
31            || !self.viewport_height_px.is_finite()
32            || self.viewport_width_px <= 1.0
33            || self.viewport_height_px <= 1.0
34        {
35            return None;
36        }
37
38        self.node_origin = normalize_node_origin(self.node_origin);
39
40        if !self.margin_px_fallback.is_finite() || self.margin_px_fallback < 0.0 {
41            self.margin_px_fallback = 0.0;
42        }
43
44        if !self.padding.is_finite() {
45            self.padding = 0.0;
46        }
47        self.padding = self.padding.clamp(0.0, MAX_FIT_VIEW_PADDING);
48
49        if !self.min_zoom.is_finite() || self.min_zoom <= 0.0 {
50            self.min_zoom = 1.0;
51        }
52        if !self.max_zoom.is_finite() || self.max_zoom <= 0.0 {
53            self.max_zoom = 1.0;
54        }
55        if self.min_zoom > self.max_zoom {
56            std::mem::swap(&mut self.min_zoom, &mut self.max_zoom);
57        }
58
59        Some(self)
60    }
61}