简化并集中化随机 NodeId 生成逻辑
This commit is contained in:
@@ -174,7 +174,7 @@ mod materialize {
|
||||
};
|
||||
use crate::ui_editor::layout::transform::Transform;
|
||||
use crate::ui_editor::state::{State, UITree};
|
||||
use crate::ui_editor::utils::{NodeId, UIDesignImageId};
|
||||
use crate::ui_editor::utils::{random_node_id, NodeId, UIDesignImageId};
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -230,12 +230,11 @@ mod materialize {
|
||||
Ok(best_index)
|
||||
}
|
||||
|
||||
fn random_node_id(occupied: &mut HashSet<NodeId>) -> Result<NodeId, String> {
|
||||
fn unique_random_node_id(occupied: &mut HashSet<NodeId>) -> NodeId {
|
||||
loop {
|
||||
let id = NodeId::new(uuid::Uuid::new_v4().simple().to_string())
|
||||
.map_err(|error| format!("生成合并容器节点 ID 失败:{error}"))?;
|
||||
let id = random_node_id();
|
||||
if occupied.insert(id.clone()) {
|
||||
return Ok(id);
|
||||
return id;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -286,7 +285,7 @@ mod materialize {
|
||||
}
|
||||
Ok(BuiltNode {
|
||||
node: LayoutNode {
|
||||
id: random_node_id(occupied_ids)?,
|
||||
id: unique_random_node_id(occupied_ids),
|
||||
layout:
|
||||
crate::ui_editor::layout::control_layout::ControlLayout::with_transform(
|
||||
original_transform,
|
||||
|
||||
@@ -222,11 +222,6 @@ fn anchor_ranges(anchor: &Anchor) -> Result<(Vector2<f32>, Vector2<f32>), String
|
||||
Ok((min, max))
|
||||
}
|
||||
|
||||
fn random_node_id() -> Result<NodeId, String> {
|
||||
NodeId::new(uuid::Uuid::new_v4().simple().to_string())
|
||||
.map_err(|error| format!("生成节点 ID 失败:{error}"))
|
||||
}
|
||||
|
||||
fn image_layout_size(image: &UIDesignImage) -> Result<Vector2<f32>, String> {
|
||||
let pixels_per_unit = image.pixels_per_unit.get();
|
||||
if !pixels_per_unit.is_finite() || pixels_per_unit <= 0.0 {
|
||||
@@ -308,7 +303,7 @@ fn convert_node(
|
||||
.map(|child| convert_node(child, image_id, image, target_rect))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
Ok(LayoutNode {
|
||||
id: random_node_id()?,
|
||||
id: random_node_id(),
|
||||
layout: ControlLayout::with_transform(transform),
|
||||
metadata: NodeMetadata {
|
||||
name: source.name.clone(),
|
||||
@@ -831,7 +826,7 @@ pub(crate) async fn recognize_ui_impl_with_provider(
|
||||
.map(|node| convert_node(node, &image_id, image, root_rect))
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
let root = LayoutNode {
|
||||
id: random_node_id()?,
|
||||
id: random_node_id(),
|
||||
layout: ControlLayout::with_transform(Transform::stretch()),
|
||||
metadata: NodeMetadata {
|
||||
name: "页面根节点".to_string(),
|
||||
|
||||
@@ -43,3 +43,10 @@ define_id!(FontAssetId);
|
||||
define_id!(SpriteAssetId);
|
||||
define_id!(UIDesignImageId);
|
||||
define_id!(NodeId);
|
||||
|
||||
const NODE_ID_LENGTH: usize = 8;
|
||||
|
||||
pub fn random_node_id() -> NodeId {
|
||||
let uuid = uuid::Uuid::new_v4().simple().to_string();
|
||||
NodeId::new(&uuid[..NODE_ID_LENGTH]).expect("generated node ID is valid")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user