Skip to main content

slint_node_editor/
grid.rs

1/// Generate SVG path commands for grid lines
2///
3/// Creates a string of SVG path commands for rendering an infinite grid.
4/// The grid adjusts based on pan offset and zoom level.
5///
6/// # Arguments
7/// * `width` - Canvas width in pixels
8/// * `height` - Canvas height in pixels
9/// * `zoom` - Current zoom level
10/// * `pan_x` - Pan offset X in pixels
11/// * `pan_y` - Pan offset Y in pixels
12/// * `spacing` - Base grid spacing (before zoom)
13///
14/// # Returns
15/// SVG path commands string (e.g., "M 24 0 L 24 600 M 48 0 L 48 600...")
16pub fn generate_grid_commands(
17    width: f32,
18    height: f32,
19    zoom: f32,
20    pan_x: f32,
21    pan_y: f32,
22    spacing: f32,
23) -> String {
24    let effective_spacing = spacing * zoom;
25
26    // Skip if spacing is too small to be visible
27    if effective_spacing < 4.0 {
28        return String::new();
29    }
30
31    // Calculate grid offset based on pan (modulo spacing for infinite grid effect)
32    let offset_x = pan_x.rem_euclid(effective_spacing);
33    let offset_y = pan_y.rem_euclid(effective_spacing);
34
35    let mut commands = String::with_capacity(10000);
36
37    // Generate vertical lines
38    let mut x = offset_x;
39    while x < width + effective_spacing {
40        if !commands.is_empty() {
41            commands.push(' ');
42        }
43        commands.push_str(&format!("M {} 0 L {} {}", x, x, height));
44        x += effective_spacing;
45    }
46
47    // Generate horizontal lines
48    let mut y = offset_y;
49    while y < height + effective_spacing {
50        commands.push(' ');
51        commands.push_str(&format!("M 0 {} L {} {}", y, width, y));
52        y += effective_spacing;
53    }
54
55    commands
56}
57
58#[cfg(test)]
59mod tests {
60    use super::*;
61
62    // ========================================================================
63    // Basic Grid Generation
64    // ========================================================================
65
66    #[test]
67    fn test_grid_commands() {
68        let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 24.0);
69        assert!(commands.contains("M 0 0 L 0 100")); // First vertical
70        assert!(commands.contains("M 24 0 L 24 100")); // Second vertical
71    }
72
73    #[test]
74    fn test_grid_commands_format() {
75        let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 50.0);
76        // Should have vertical lines
77        assert!(commands.contains("L 0 100")); // Vertical line to bottom
78                                               // Should have horizontal lines
79        assert!(commands.contains("L 100")); // Horizontal line to right edge
80    }
81
82    #[test]
83    fn test_grid_contains_horizontal_lines() {
84        let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 25.0);
85        // Horizontal lines start with M 0 y and end with L width y
86        assert!(commands.contains("M 0 0 L 100 0")); // First horizontal
87        assert!(commands.contains("M 0 25 L 100 25")); // Second horizontal
88    }
89
90    // ========================================================================
91    // Zoom Behavior
92    // ========================================================================
93
94    #[test]
95    fn test_grid_commands_zoom_affects_spacing() {
96        let commands1 = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 20.0);
97        let commands2 = generate_grid_commands(100.0, 100.0, 2.0, 0.0, 0.0, 20.0);
98
99        // With zoom=2, effective spacing is 40, so fewer lines
100        // Count vertical lines (each starts with "M x 0")
101        let count1 = commands1.matches("M ").count();
102        let count2 = commands2.matches("M ").count();
103
104        // Higher zoom = larger spacing = fewer lines
105        assert!(count1 > count2);
106    }
107
108    #[test]
109    fn test_grid_commands_very_small_zoom() {
110        // When zoom is very small, effective_spacing < 4.0, so no grid
111        let commands = generate_grid_commands(100.0, 100.0, 0.1, 0.0, 0.0, 20.0);
112        // Effective spacing = 20 * 0.1 = 2.0 < 4.0
113        assert!(commands.is_empty());
114    }
115
116    #[test]
117    fn test_grid_commands_at_spacing_threshold() {
118        // Exactly at threshold (effective_spacing = 4.0) - should render
119        // The check is `< 4.0`, so 4.0 passes and renders
120        let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 4.0);
121        assert!(!commands.is_empty()); // 4.0 is NOT < 4.0, so renders
122
123        // Just below threshold - should NOT render
124        let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 3.9);
125        assert!(commands.is_empty()); // 3.9 < 4.0, so empty
126
127        // Just above threshold - should render
128        let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 4.1);
129        assert!(!commands.is_empty());
130    }
131
132    // ========================================================================
133    // Pan Behavior
134    // ========================================================================
135
136    #[test]
137    fn test_grid_commands_pan_offsets_lines() {
138        let commands_no_pan = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 20.0);
139        let commands_with_pan = generate_grid_commands(100.0, 100.0, 1.0, 10.0, 0.0, 20.0);
140
141        // The commands should be different due to pan offset
142        assert_ne!(commands_no_pan, commands_with_pan);
143    }
144
145    #[test]
146    fn test_grid_commands_pan_wraps_with_modulo() {
147        // Pan of exactly one spacing should look the same (modulo effect)
148        let commands1 = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 20.0);
149        let commands2 = generate_grid_commands(100.0, 100.0, 1.0, 20.0, 0.0, 20.0);
150
151        assert_eq!(commands1, commands2);
152    }
153
154    #[test]
155    fn test_grid_commands_negative_pan() {
156        // Negative pan should also work (rem_euclid handles this)
157        let commands = generate_grid_commands(100.0, 100.0, 1.0, -10.0, -10.0, 20.0);
158        // Should still produce valid output
159        assert!(!commands.is_empty());
160        assert!(commands.contains("M "));
161        assert!(commands.contains(" L "));
162    }
163
164    // ========================================================================
165    // Edge Cases
166    // ========================================================================
167
168    #[test]
169    fn test_grid_commands_zero_dimensions() {
170        let commands = generate_grid_commands(0.0, 0.0, 1.0, 0.0, 0.0, 20.0);
171        // With zero dimensions, there shouldn't be many lines
172        // (loops iterate while x < 0 + spacing and y < 0 + spacing)
173        assert!(!commands.is_empty()); // Will have at least one line due to while condition
174    }
175
176    #[test]
177    fn test_grid_commands_very_small_spacing() {
178        // Spacing below threshold
179        let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 3.0);
180        // 3.0 < 4.0, so should return empty
181        assert!(commands.is_empty());
182    }
183
184    #[test]
185    fn test_grid_commands_large_canvas() {
186        let commands = generate_grid_commands(1000.0, 1000.0, 1.0, 0.0, 0.0, 50.0);
187        // Should have many lines
188        assert!(!commands.is_empty());
189        // Count approximate number of lines
190        let line_count = commands.matches("M ").count();
191        // 1000/50 = 20 vertical + 20 horizontal = ~40 lines
192        assert!(line_count >= 30);
193    }
194
195    #[test]
196    fn test_grid_commands_spacing_larger_than_canvas() {
197        let commands = generate_grid_commands(50.0, 50.0, 1.0, 0.0, 0.0, 100.0);
198        // Spacing > canvas, but should still draw first line
199        assert!(!commands.is_empty());
200    }
201
202    #[test]
203    fn test_grid_commands_non_divisible_spacing() {
204        // Canvas size not evenly divisible by spacing
205        let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 33.0);
206        assert!(!commands.is_empty());
207        // Should have lines at 0, 33, 66, 99 (approximate)
208    }
209
210    // ========================================================================
211    // Output Format Validation
212    // ========================================================================
213
214    #[test]
215    fn test_grid_commands_valid_svg_format() {
216        let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 25.0);
217
218        // Each command should follow "M x y L x y" pattern
219        for part in commands.split(' ').collect::<Vec<_>>().chunks(6) {
220            if part.len() >= 6 {
221                assert!(part[0] == "M" || part[0].parse::<f32>().is_ok());
222            }
223        }
224    }
225
226    #[test]
227    fn test_grid_commands_no_trailing_space() {
228        let commands = generate_grid_commands(100.0, 100.0, 1.0, 0.0, 0.0, 25.0);
229        assert!(!commands.ends_with(' '));
230    }
231}