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