use crate::{
inspector::{
editors::{
PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
PropertyEditorMessageContext, PropertyEditorTranslationContext,
},
FieldAction, InspectorError, PropertyChanged,
},
message::{MessageDirection, UiMessage},
path::{PathEditorBuilder, PathEditorMessage},
widget::WidgetBuilder,
Thickness, VerticalAlignment,
};
use std::{any::TypeId, path::PathBuf};
#[derive(Debug)]
pub struct PathPropertyEditorDefinition;
impl PropertyEditorDefinition for PathPropertyEditorDefinition {
fn value_type_id(&self) -> TypeId {
TypeId::of::<PathBuf>()
}
fn create_instance(
&self,
ctx: PropertyEditorBuildContext,
) -> Result<PropertyEditorInstance, InspectorError> {
let value = ctx.property_info.cast_value::<PathBuf>()?;
Ok(PropertyEditorInstance::simple(
PathEditorBuilder::new(
WidgetBuilder::new()
.with_margin(Thickness::top_bottom(1.0))
.with_vertical_alignment(VerticalAlignment::Center),
)
.with_path(value.clone())
.build(ctx.build_context),
))
}
fn create_message(
&self,
ctx: PropertyEditorMessageContext,
) -> Result<Option<UiMessage>, InspectorError> {
let value = ctx.property_info.cast_value::<PathBuf>()?;
Ok(Some(UiMessage::for_widget(
ctx.instance,
PathEditorMessage::Path(value.clone()),
)))
}
fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
if ctx.message.direction() == MessageDirection::FromWidget {
if let Some(PathEditorMessage::Path(value)) = ctx.message.data() {
return Some(PropertyChanged {
name: ctx.name.to_string(),
action: FieldAction::object(value.clone()),
});
}
}
None
}
}