BindingProtocol
public protocol BindingProtocol : ObjectProtocol
GBinding
is the representation of a binding between a property on a
GObject
instance (or source) and another property on another GObject
instance (or target).
Whenever the source property changes, the same value is applied to the target property; for instance, the following binding:
(C Language Example):
g_object_bind_property (object1, "property-a",
object2, "property-b",
G_BINDING_DEFAULT);
will cause the property named “property-b” of object2
to be updated
every time g_object_set()
or the specific accessor changes the value of
the property “property-a” of object1
.
It is possible to create a bidirectional binding between two properties
of two GObject
instances, so that if either property changes, the
other is updated as well, for instance:
(C Language Example):
g_object_bind_property (object1, "property-a",
object2, "property-b",
G_BINDING_BIDIRECTIONAL);
will keep the two properties in sync.
It is also possible to set a custom transformation function (in both directions, in case of a bidirectional binding) to apply a custom transformation from the source value to the target value before applying it; for instance, the following binding:
(C Language Example):
g_object_bind_property_full (adjustment1, "value",
adjustment2, "value",
G_BINDING_BIDIRECTIONAL,
celsius_to_fahrenheit,
fahrenheit_to_celsius,
NULL, NULL);
will keep the “value” property of the two adjustments in sync; the
celsius_to_fahrenheit
function will be called whenever the “value”
property of adjustment1
changes and will transform the current value
of the property before applying it to the “value” property of adjustment2
.
Vice versa, the fahrenheit_to_celsius
function will be called whenever
the “value” property of adjustment2
changes, and will transform the
current value of the property before applying it to the “value” property
of adjustment1
.
Note that GBinding
does not resolve cycles by itself; a cycle like
object1:propertyA -> object2:propertyB
object2:propertyB -> object3:propertyC
object3:propertyC -> object1:propertyA
might lead to an infinite loop. The loop, in this particular case,
can be avoided if the objects emit the GObject::notify
signal only
if the value has effectively been changed. A binding is implemented
using the GObject::notify
signal, so it is susceptible to all the
various ways of blocking a signal emission, like g_signal_stop_emission()
or g_signal_handler_block()
.
A binding will be severed, and the resources it allocates freed, whenever
either one of the GObject
instances it refers to are finalized, or when
the GBinding
instance loses its last reference.
Bindings for languages with garbage collection can use
g_binding_unbind()
to explicitly release a binding between the source
and target properties, instead of relying on the last reference on the
binding, source, and target instances to drop.
GBinding
is available since GObject 2.26
The BindingProtocol
protocol exposes the methods and properties of an underlying GBinding
instance.
The default implementation of these can be found in the protocol extension below.
For a concrete class that implements these methods and properties, see Binding
.
Alternatively, use BindingRef
as a lighweight, unowned
reference if you already have an instance you just want to use.
-
Untyped pointer to the underlying
GBinding
instance.Declaration
Swift
var ptr: UnsafeMutableRawPointer! { get }
-
binding_ptr
Default implementationTyped pointer to the underlying
GBinding
instance.Default Implementation
Return the stored, untyped pointer as a typed pointer to the
GBinding
instance.Declaration
Swift
var binding_ptr: UnsafeMutablePointer<GBinding>! { get }
-
Required Initialiser for types conforming to
BindingProtocol
Declaration
Swift
init(raw: UnsafeMutableRawPointer)
-
bind(property:
Extension methodto: _: flags: transformFrom: transformTo: ) Bind a
BindingPropertyName
source property to a given target object.Declaration
Swift
@discardableResult @inlinable func bind<Q, T>(property source_property: BindingPropertyName, to target: T, _ target_property: Q, flags f: BindingFlags = .default, transformFrom transform_from: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }, transformTo transform_to: @escaping GLibObject.ValueTransformer = { $0.transform(destValue: $1) }) -> BindingRef! where Q : PropertyNameProtocol, T : ObjectProtocol
Parameters
source_property
the source property to bind
target
the target object to bind to
target_property
the target property to bind to
flags
the flags to pass to the
Binding
transform_from
ValueTransformer
to use for forward transformationtransform_to
ValueTransformer
to use for backwards transformationReturn Value
binding reference or
nil
in case of an error -
get(property:
Extension method) Get the value of a Binding property
Declaration
Swift
@inlinable func get(property: BindingPropertyName) -> GLibObject.Value
Parameters
property
the property to get the value for
Return Value
the value of the named property
-
set(property:
Extension methodvalue: ) Set the value of a Binding property. Note that this will only have an effect on properties that are writable and not construct-only!
Declaration
Swift
@inlinable func set(property: BindingPropertyName, value v: GLibObject.Value)
Parameters
property
the property to get the value for
Return Value
the value of the named property
-
dupSource()
Extension methodRetrieves the
GObject
instance used as the source of the binding.A
GBinding
can outlive the sourceGObject
as the binding does not hold a strong reference to the source. If the source is destroyed before the binding then this function will returnnil
.Declaration
Swift
@inlinable func dupSource() -> GLibObject.ObjectRef!
-
dupTarget()
Extension methodRetrieves the
GObject
instance used as the target of the binding.A
GBinding
can outlive the targetGObject
as the binding does not hold a strong reference to the target. If the target is destroyed before the binding then this function will returnnil
.Declaration
Swift
@inlinable func dupTarget() -> GLibObject.ObjectRef!
-
getFlags()
Extension methodRetrieves the flags passed when constructing the
GBinding
.Declaration
Swift
@inlinable func getFlags() -> GLibObject.BindingFlags
-
getSource()
Extension methodRetrieves the
GObject
instance used as the source of the binding.A
GBinding
can outlive the sourceGObject
as the binding does not hold a strong reference to the source. If the source is destroyed before the binding then this function will returnnil
.Use
g_binding_dup_source()
if the source or binding are used from different threads as otherwise the pointer returned from this function might become invalid if the source is finalized from another thread in the meantime.get_source is deprecated: Use g_binding_dup_source() for a safer version of this function.
Declaration
Swift
@available(*, deprecated) @inlinable func getSource() -> GLibObject.ObjectRef!
-
getSourceProperty()
Extension methodRetrieves the name of the property of
GBinding:source
used as the source of the binding.Declaration
Swift
@inlinable func getSourceProperty() -> String!
-
getTarget()
Extension methodRetrieves the
GObject
instance used as the target of the binding.A
GBinding
can outlive the targetGObject
as the binding does not hold a strong reference to the target. If the target is destroyed before the binding then this function will returnnil
.Use
g_binding_dup_target()
if the target or binding are used from different threads as otherwise the pointer returned from this function might become invalid if the target is finalized from another thread in the meantime.get_target is deprecated: Use g_binding_dup_target() for a safer version of this function.
Declaration
Swift
@available(*, deprecated) @inlinable func getTarget() -> GLibObject.ObjectRef!
-
getTargetProperty()
Extension methodRetrieves the name of the property of
GBinding:target
used as the target of the binding.Declaration
Swift
@inlinable func getTargetProperty() -> String!
-
unbind()
Extension methodExplicitly releases the binding between the source and the target property expressed by
binding
.This function will release the reference that is being held on the
binding
instance if the binding is still bound; if you want to hold on to theGBinding
instance after callingg_binding_unbind()
, you will need to hold a reference to it.Note however that this function does not take ownership of
binding
, it only unrefs the reference that was initially created byg_object_bind_property()
and is owned by the binding.Declaration
Swift
@inlinable func unbind()
-
flags
Extension methodFlags to be used to control the
GBinding
Declaration
Swift
@inlinable var flags: GLibObject.BindingFlags { get }
-
source
Extension methodThe
GObject
that should be used as the source of the bindingDeclaration
Swift
@inlinable var source: GLibObject.ObjectRef! { get }
-
sourceProperty
Extension methodRetrieves the name of the property of
GBinding:source
used as the source of the binding.Declaration
Swift
@inlinable var sourceProperty: String! { get }
-
target
Extension methodThe
GObject
that should be used as the target of the bindingDeclaration
Swift
@inlinable var target: GLibObject.ObjectRef! { get }
-
targetProperty
Extension methodRetrieves the name of the property of
GBinding:target
used as the target of the binding.Declaration
Swift
@inlinable var targetProperty: String! { get }