.. index:: pair: class; EE::UI::UIValueBinding .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding: template class EE::UI::UIValueBinding ===================================== .. toctree:: :hidden: struct_EE_UI_UIValueBinding_State.rst Overview ~~~~~~~~ Move-only two-way binding between an :ref:`ObservableValue ` and a :ref:`UIWidget ` property. :ref:`More...` .. ref-code-block:: cpp :class: doxyrest-overview-code-block #include template class UIValueBinding { public: // typedefs typedef :ref:`UIValueConverter` :target:`Converter`; // structs struct :ref:`State`; // construction :target:`UIValueBinding`(); :target:`UIValueBinding`(const UIValueBinding&); :target:`UIValueBinding`(UIValueBinding&&); :ref:`UIValueBinding`(:ref:`ObservableValue`& value, :ref:`UIWidget`* widget, const :ref:`Converter`& converter = :ref:`converterDefault`(), const std::string& propertyName = "value", :ref:`Event::EventType` eventType = Event::OnValueChange); // methods static :ref:`Converter` :ref:`converterDefault`(); UIValueBinding& :target:`operator=`(const UIValueBinding&); UIValueBinding& :target:`operator=`(UIValueBinding&&); void :ref:`disconnect`(); :ref:`operator bool`() const; bool :ref:`isConnected`() const; bool :ref:`isValid`() const; :ref:`UIWidget`* :ref:`widget`() const; std::optional :ref:`value`() const; bool :ref:`setValue`(const T& value); :ref:`ObservableValue`::Connection :ref:`observeValue`(typename :ref:`ObservableValue`::Callback callback); :ref:`UIValueValidationState`* :ref:`validationState`(); const :ref:`UIValueValidationState`* :target:`validationState`() const; }; .. _details-class_e_e_1_1_u_i_1_1_u_i_value_binding: Detailed Documentation ~~~~~~~~~~~~~~~~~~~~~~ Move-only two-way binding between an :ref:`ObservableValue ` and a :ref:`UIWidget ` property. The converter maps directly between T and the widget property string. Its toValue() callback decides whether widget input may enter the model. Model-originated values are authoritative and are formatted through fromValue(). Destroying the binding disconnects both directions. Destroying either the observable or widget first is safe and does not keep that endpoint alive. Synchronization is immediate and single-threaded. The observable, widget, and binding must all be used on the widget's owning :ref:`UI ` thread. Use :ref:`UIValueBinding ` when an :ref:`ObservableValue ` belongs to a UI-independent model. The returned binding must be retained for as long as synchronization is desired. .. ref-code-block:: cpp ObservableValue userName{ "Ada" }; auto binding = bindValue( userName, textInput, UIValueConverter::converterString(), "text", Event::OnTextChanged ); userName = "Grace"; // Updates textInput without coupling the model to UIWidget. Construction ------------ .. index:: pair: function; UIValueBinding .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding_1abce872bbeb5fe11c957bcab771af3db4: .. ref-code-block:: cpp :class: doxyrest-title-code-block UIValueBinding(:ref:`ObservableValue`& value, :ref:`UIWidget`* widget, const :ref:`Converter`& converter = :ref:`converterDefault`(), const std::string& propertyName = "value", :ref:`Event::EventType` eventType = Event::OnValueChange) Starts synchronizing ``value`` with a property of ``widget``. The current model value is applied to the widget immediately. Later ``eventType`` events parse the widget property back into the model. Methods ------- .. index:: pair: function; converterDefault .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding_1aa3d45e2009ff5ed20e05f1a516a1b18d: .. ref-code-block:: cpp :class: doxyrest-title-code-block static :ref:`Converter` converterDefault() .. rubric:: Returns: The standard converter for the bound value type. .. index:: pair: function; disconnect .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding_1a8edb0875bd22b196414f4f99c1be2427: .. ref-code-block:: cpp :class: doxyrest-title-code-block void disconnect() Stops synchronization in both directions. Calling this repeatedly is safe. .. index:: pair: function; operator bool .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding_1a45608d8df5dd825ee736b6303e00a8b5: .. ref-code-block:: cpp :class: doxyrest-title-code-block operator bool() const .. rubric:: Returns: Whether both model and widget endpoints are still alive and connected. .. index:: pair: function; isConnected .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding_1ac79f405218fe76ccdf0dd9901d8c120b: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool isConnected() const .. rubric:: Returns: Whether both model and widget endpoints are still alive and connected. .. index:: pair: function; isValid .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding_1a5c4a3ac9de536d8c6c05e43007911051: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool isValid() const .. rubric:: Returns: Whether the most recent conversion or validation succeeded. .. index:: pair: function; widget .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding_1a11269fce8b94a45ddd104b9178a0cce4: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`UIWidget`* widget() const .. rubric:: Returns: The bound widget, or nullptr after disconnection or widget destruction. .. index:: pair: function; value .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding_1abcc1051617352441d0a1943d9b9a4670: .. ref-code-block:: cpp :class: doxyrest-title-code-block std::optional value() const .. rubric:: Returns: A copy of the model value, or std::nullopt after disconnection. .. index:: pair: function; setValue .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding_1a1c40875c64b94fed7aff157aeb56cdcd: .. ref-code-block:: cpp :class: doxyrest-title-code-block bool setValue(const T& value) .. rubric:: Returns: true when the connected model still exists and was assigned ``value``. .. index:: pair: function; observeValue .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding_1a2528d624cfa5c95976d86a4e79dfc44c: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`ObservableValue`::Connection observeValue(typename :ref:`ObservableValue`::Callback callback) .. rubric:: Returns: A scoped observer connection to later model changes, or an empty connection. .. index:: pair: function; validationState .. _doxid-class_e_e_1_1_u_i_1_1_u_i_value_binding_1a8b60f0f33e2786e8d180c117ec4b13a3: .. ref-code-block:: cpp :class: doxyrest-title-code-block :ref:`UIValueValidationState`* validationState() .. rubric:: Returns: Observable conversion and input-validation state.