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 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(UiMessage::for_widget(
66 ctx.instance,
67 PathEditorMessage::Path(value.clone()),
68 )))
69 }
70
71 fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
72 if ctx.message.direction() == MessageDirection::FromWidget {
73 if let Some(PathEditorMessage::Path(value)) = ctx.message.data() {
74 return Some(PropertyChanged {
75 name: ctx.name.to_string(),
76
77 value: FieldKind::object(value.clone()),
78 });
79 }
80 }
81 None
82 }
83}