fyrox_ui/inspector/editors/
string.rs1use crate::{
22 core::algebra::Vector2,
23 formatted_text::WrapMode,
24 inspector::{
25 editors::{
26 PropertyEditorBuildContext, PropertyEditorDefinition, PropertyEditorInstance,
27 PropertyEditorMessageContext, PropertyEditorTranslationContext,
28 },
29 FieldKind, InspectorError, PropertyChanged,
30 },
31 message::{MessageDirection, UiMessage},
32 text::TextMessage,
33 text_box::{TextBoxBuilder, TextCommitMode},
34 widget::WidgetBuilder,
35 Thickness, VerticalAlignment,
36};
37use std::any::TypeId;
38
39#[derive(Debug)]
40pub struct StringPropertyEditorDefinition;
41
42impl PropertyEditorDefinition for StringPropertyEditorDefinition {
43 fn value_type_id(&self) -> TypeId {
44 TypeId::of::<String>()
45 }
46
47 fn create_instance(
48 &self,
49 ctx: PropertyEditorBuildContext,
50 ) -> Result<PropertyEditorInstance, InspectorError> {
51 let value = ctx.property_info.cast_value::<String>()?;
52 Ok(PropertyEditorInstance::simple(
53 TextBoxBuilder::new(
54 WidgetBuilder::new()
55 .with_min_size(Vector2::new(0.0, 17.0))
56 .with_margin(Thickness::uniform(1.0)),
57 )
58 .with_wrap(WrapMode::Word)
59 .with_text_commit_mode(TextCommitMode::Changed)
60 .with_text(value)
61 .with_vertical_text_alignment(VerticalAlignment::Center)
62 .build(ctx.build_context),
63 ))
64 }
65
66 fn create_message(
67 &self,
68 ctx: PropertyEditorMessageContext,
69 ) -> Result<Option<UiMessage>, InspectorError> {
70 let value = ctx.property_info.cast_value::<String>()?;
71 Ok(Some(UiMessage::for_widget(
72 ctx.instance,
73 TextMessage::Text(value.clone()),
74 )))
75 }
76
77 fn translate_message(&self, ctx: PropertyEditorTranslationContext) -> Option<PropertyChanged> {
78 if ctx.message.direction() == MessageDirection::FromWidget {
79 if let Some(TextMessage::Text(value)) = ctx.message.data::<TextMessage>() {
80 return Some(PropertyChanged {
81 name: ctx.name.to_string(),
82 value: FieldKind::object(value.clone()),
83 });
84 }
85 }
86 None
87 }
88}