fyrox_ui/inspector/editors/
path.rs1use crate::{
22 inspector::{
23 editors::{
24 PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
25 PropertyEditorMessageContext, PropertyEditorTranslationContext,
26 },
27 FieldKind, InspectorError, PropertyChanged,
28 },
29 message::{MessageDirection, UiMessage},
30 path::{PathEditorBuilder, PathEditorMessage},
31 widget::WidgetBuilder,
32 Thickness, VerticalAlignment,
33};
34use std::{any::TypeId, path::PathBuf};
35
36#[derive(Debug)]
37pub struct PathPropertyEditorDefinition;
38
39impl PropertyEditorDefinition for PathPropertyEditorDefinition {
40 fn value_type_id(&self) -> TypeId {
41 TypeId::of::<PathBuf>()
42 }
43
44 fn create_instance(
45 &self,
46 ctx: PropertyEditorBuildContext,
47 ) -> Result<PropertyEditorInstance, InspectorError> {
48 let value = ctx.property_info.cast_value::<PathBuf>()?;
49 Ok(PropertyEditorInstance::Simple {
50 editor: PathEditorBuilder::new(
51 WidgetBuilder::new()
52 .with_margin(Thickness::top_bottom(1.0))
53 .with_vertical_alignment(VerticalAlignment::Center),
54 )
55 .with_path(value.clone())
56 .build(ctx.build_context),
57 })
58 }
59
60 fn create_message(
61 &self,
62 ctx: PropertyEditorMessageContext,
63 ) -> Result<Option<UiMessage>, InspectorError> {
64 let value = ctx.property_info.cast_value::<PathBuf>()?;
65 Ok(Some(PathEditorMessage::path(
66 ctx.instance,
67 MessageDirection::ToWidget,
68 value.clone(),
69 )))
70 }
71
72 fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
73 if ctx.message.direction() == MessageDirection::FromWidget {
74 if let Some(PathEditorMessage::Path(value)) = ctx.message.data() {
75 return Some(PropertyChanged {
76 name: ctx.name.to_string(),
77 owner_type_id: ctx.owner_type_id,
78 value: FieldKind::object(value.clone()),
79 });
80 }
81 }
82 None
83 }
84}