clat_gui 0.1.3

High-performance, cross-platform Rust desktop GUI framework.
Documentation
use std::collections::HashMap;

// 组件ID类型
type ComponentId = u64;

// 布局约束结构体
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LayoutConstraints {
    pub min_width: f32,
    pub max_width: f32,
    pub min_height: f32,
    pub max_height: f32,
}

impl Default for LayoutConstraints {
    fn default() -> Self {
        Self {
            min_width: 0.0,
            max_width: f32::INFINITY,
            min_height: 0.0,
            max_height: f32::INFINITY,
        }
    }
}

// 布局结果结构体
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct LayoutResult {
    pub x: f32,
    pub y: f32,
    pub width: f32,
    pub height: f32,
}

// 布局策略特征
pub trait LayoutStrategy {
    // 计算布局
    fn calculate_layout(&self, constraints: LayoutConstraints) -> LayoutResult;
    
    // 添加子组件
    fn add_child(&mut self, child_id: ComponentId, weight: f32);
    
    // 移除子组件
    fn remove_child(&mut self, child_id: ComponentId);
    
    // 更新子组件约束
    fn update_child_constraints(&self, 
                               child_id: ComponentId, 
                               parent_constraints: LayoutConstraints) -> LayoutConstraints;
}

// 布局管理器结构体
pub struct LayoutManager {
    strategies: HashMap<ComponentId, Box<dyn LayoutStrategy>>,
    child_map: HashMap<ComponentId, Vec<ComponentId>>,
    parent_map: HashMap<ComponentId, ComponentId>,
    layouts: HashMap<ComponentId, LayoutResult>,
    next_id: ComponentId,
}

impl LayoutManager {
    // 创建新的布局管理器实例
    pub fn new() -> Self {
        Self {
            strategies: HashMap::new(),
            child_map: HashMap::new(),
            parent_map: HashMap::new(),
            layouts: HashMap::new(),
            next_id: 1,
        }
    }
    
    // 生成新的组件ID
    pub fn generate_id(&mut self) -> ComponentId {
        let id = self.next_id;
        self.next_id += 1;
        id
    }
    
    // 注册布局策略
    pub fn register_strategy(&mut self, 
                            component_id: ComponentId, 
                            strategy: Box<dyn LayoutStrategy>) {
        self.strategies.insert(component_id, strategy);
        self.child_map.entry(component_id).or_insert_with(Vec::new);
    }
    
    // 添加子组件到父组件
    pub fn add_child(&mut self, parent_id: ComponentId, child_id: ComponentId, weight: f32) {
        // 确保父组件存在
        if self.strategies.contains_key(&parent_id) {
            // 从旧父组件中移除(如果有)
            if let Some(old_parent) = self.parent_map.get(&child_id) {
                self.remove_child(*old_parent, child_id);
            }
            
            // 添加到新父组件
            self.child_map.entry(parent_id).and_modify(|children| {
                if !children.contains(&child_id) {
                    children.push(child_id);
                }
            });
            
            // 更新父子映射
            self.parent_map.insert(child_id, parent_id);
            
            // 更新父组件的布局策略
            if let Some(strategy) = self.strategies.get_mut(&parent_id) {
                strategy.add_child(child_id, weight);
            }
        }
    }
    
    // 从父组件中移除子组件
    pub fn remove_child(&mut self, parent_id: ComponentId, child_id: ComponentId) {
        // 移除父子映射
        self.parent_map.remove(&child_id);
        
        // 从父组件的子列表中移除
        if let Some(children) = self.child_map.get_mut(&parent_id) {
            if let Some(index) = children.iter().position(|&id| id == child_id) {
                children.remove(index);
            }
        }
        
        // 更新父组件的布局策略
        if let Some(strategy) = self.strategies.get_mut(&parent_id) {
            strategy.remove_child(child_id);
        }
    }
    
    // 计算指定组件的布局
    pub fn calculate_layout(&mut self, component_id: ComponentId, constraints: LayoutConstraints) {
        if let Some(strategy) = self.strategies.get(&component_id) {
            // 计算当前组件的布局
            let layout = strategy.calculate_layout(constraints);
            self.layouts.insert(component_id, layout);
            
            // 计算所有子组件的布局
            if let Some(children) = self.child_map.get(&component_id) {
                let child_constraints = LayoutConstraints {
                    min_width: 0.0,
                    max_width: layout.width,
                    min_height: 0.0,
                    max_height: layout.height,
                };
                
                for &child_id in children {
                    if let Some(child_strategy) = self.strategies.get(&child_id) {
                        let child_constraint = child_strategy.update_child_constraints(
                            child_id, 
                            child_constraints
                        );
                        self.calculate_layout(child_id, child_constraint);
                    }
                }
            }
        }
    }
    
    // 获取组件的布局结果
    pub fn get_layout(&self, component_id: ComponentId) -> Option<&LayoutResult> {
        self.layouts.get(&component_id)
    }
    
    // 获取组件的父组件
    pub fn get_parent(&self, component_id: ComponentId) -> Option<ComponentId> {
        self.parent_map.get(&component_id).copied()
    }
    
    // 获取组件的子组件列表
    pub fn get_children(&self, component_id: ComponentId) -> Option<&Vec<ComponentId>> {
        self.child_map.get(&component_id)
    }
}