flow_rs_core/auto_layout/
config.rs

1//! Auto layout configuration and strategy types
2
3// Default configuration values
4const DEFAULT_SMALL_GRAPH_THRESHOLD: usize = 10;
5const DEFAULT_TRANSITION_DURATION: f64 = 0.5;
6
7/// Strategies for automatic layout selection
8#[derive(Debug, Clone, Copy, PartialEq)]
9pub enum AutoLayoutStrategy {
10    /// Automatically detect best layout based on graph structure
11    Smart,
12    /// Prefer hierarchical layouts when possible, fallback to force-directed
13    HierarchyFirst,
14    /// Always use force-directed as primary with fallbacks
15    ForceDirectedFirst,
16    /// Use simple layouts (grid/circular) for small graphs
17    SimpleFirst,
18}
19
20/// Configuration for automatic layout management
21///
22/// This configuration controls how the AutoLayoutManager selects and applies
23/// layout algorithms based on graph characteristics.
24#[derive(Debug, Clone)]
25pub struct AutoLayoutConfig {
26    /// Strategy for automatic algorithm selection
27    pub strategy: AutoLayoutStrategy,
28    /// Maximum number of nodes to consider a graph "small" for simple layouts
29    pub small_graph_threshold: usize,
30    /// Whether to enable smooth transitions between layout changes
31    pub enable_transitions: bool,
32    /// Duration of layout transitions in seconds
33    pub transition_duration: f64,
34    /// Whether to force relayout when graph structure changes
35    pub force_relayout_on_change: bool,
36}
37
38impl Default for AutoLayoutConfig {
39    fn default() -> Self {
40        Self {
41            strategy: AutoLayoutStrategy::Smart,
42            small_graph_threshold: DEFAULT_SMALL_GRAPH_THRESHOLD,
43            enable_transitions: true,
44            transition_duration: DEFAULT_TRANSITION_DURATION,
45            force_relayout_on_change: false,
46        }
47    }
48}
49
50impl AutoLayoutConfig {
51    /// Create a new configuration builder
52    pub fn builder() -> AutoLayoutConfigBuilder {
53        AutoLayoutConfigBuilder::new()
54    }
55}
56
57/// Builder for AutoLayoutConfig
58#[derive(Debug)]
59pub struct AutoLayoutConfigBuilder {
60    config: AutoLayoutConfig,
61}
62
63impl AutoLayoutConfigBuilder {
64    /// Create a new builder with default values
65    pub fn new() -> Self {
66        Self {
67            config: AutoLayoutConfig::default(),
68        }
69    }
70
71    /// Set the layout selection strategy
72    pub fn strategy(mut self, strategy: AutoLayoutStrategy) -> Self {
73        self.config.strategy = strategy;
74        self
75    }
76
77    /// Set the small graph threshold
78    pub fn small_graph_threshold(mut self, threshold: usize) -> Self {
79        self.config.small_graph_threshold = threshold;
80        self
81    }
82
83    /// Enable or disable transitions
84    pub fn enable_transitions(mut self, enable: bool) -> Self {
85        self.config.enable_transitions = enable;
86        self
87    }
88
89    /// Set the transition duration
90    pub fn transition_duration(mut self, duration: f64) -> Self {
91        self.config.transition_duration = duration.max(0.0);
92        self
93    }
94
95    /// Set whether to force relayout on change
96    pub fn force_relayout_on_change(mut self, force: bool) -> Self {
97        self.config.force_relayout_on_change = force;
98        self
99    }
100
101    /// Build the configuration
102    pub fn build(self) -> AutoLayoutConfig {
103        self.config
104    }
105}