From 47d09d51ef0f109a950c11fc1f4a5e9355f0dbe6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=BE=B7=E5=AE=87?= Date: Sat, 12 Sep 2026 17:57:48 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AE=80=E5=8C=96=E5=B9=B6=E9=9B=86=E4=B8=AD?= =?UTF-8?q?=E5=8C=96=E9=9A=8F=E6=9C=BA=20NodeId=20=E7=94=9F=E6=88=90?= =?UTF-8?q?=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src-tauri/src/ui_editor/commands/merge.rs | 11 +++++------ .../src-tauri/src/ui_editor/commands/recognition.rs | 9 ++------- .../src-tauri/src/ui_editor/utils.rs | 7 +++++++ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/apps/ai-game-creator-shell/src-tauri/src/ui_editor/commands/merge.rs b/apps/ai-game-creator-shell/src-tauri/src/ui_editor/commands/merge.rs index 32648fcd6..0a32724d6 100644 --- a/apps/ai-game-creator-shell/src-tauri/src/ui_editor/commands/merge.rs +++ b/apps/ai-game-creator-shell/src-tauri/src/ui_editor/commands/merge.rs @@ -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) -> Result { + fn unique_random_node_id(occupied: &mut HashSet) -> 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, diff --git a/apps/ai-game-creator-shell/src-tauri/src/ui_editor/commands/recognition.rs b/apps/ai-game-creator-shell/src-tauri/src/ui_editor/commands/recognition.rs index f6d087ea0..d34a5d47b 100644 --- a/apps/ai-game-creator-shell/src-tauri/src/ui_editor/commands/recognition.rs +++ b/apps/ai-game-creator-shell/src-tauri/src/ui_editor/commands/recognition.rs @@ -222,11 +222,6 @@ fn anchor_ranges(anchor: &Anchor) -> Result<(Vector2, Vector2), String Ok((min, max)) } -fn random_node_id() -> Result { - NodeId::new(uuid::Uuid::new_v4().simple().to_string()) - .map_err(|error| format!("生成节点 ID 失败:{error}")) -} - fn image_layout_size(image: &UIDesignImage) -> Result, 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::, _>>()?; 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::, _>>()?; let root = LayoutNode { - id: random_node_id()?, + id: random_node_id(), layout: ControlLayout::with_transform(Transform::stretch()), metadata: NodeMetadata { name: "页面根节点".to_string(), diff --git a/apps/ai-game-creator-shell/src-tauri/src/ui_editor/utils.rs b/apps/ai-game-creator-shell/src-tauri/src/ui_editor/utils.rs index de48e841b..a088a859d 100644 --- a/apps/ai-game-creator-shell/src-tauri/src/ui_editor/utils.rs +++ b/apps/ai-game-creator-shell/src-tauri/src/ui_editor/utils.rs @@ -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") +}